Android Open Source - LucyTheMoocher Tank






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lucythemoocher.actors;
//from ww  w . j  a  v a 2  s.c om
import com.lucythemoocher.R;
import com.lucythemoocher.physics.Cinematic;
import com.lucythemoocher.util.Direction;

public class Tank extends Monster {
  private TankState state_;
  
  public Tank(float x, float y, int direction) {
    super();
    getDrawer().initializeAnimation(R.drawable.tank);
    setCinematic(new Cinematic(0.4f));
    getCinematic().addBox(x, y, getH(), getW());
    state_ = new Move(this, direction);
  }
  
  public void update() {
    super.update();
    state_.update();
  }
  
  void changeState(TankState newState) {
    state_ = newState;
  }
  
  /**
   * X position where to create projectiles
   * @return
   */
  public float getXFire() {
    float x = getCinematic().x();
    if (state_.getDir() == Direction.LEFT) {
      x += getDrawer().getAnim().getW();
    }
    return x;
  }
  
  /**
   * Y position where to create projectiles
   * @return
   */
  public float getYFire() {
    float y = getCinematic().y();
    y += getDrawer().getAnim().getH() / 2;
    return y;
  }
}

/**
 * State of the Tank
 */
abstract class TankState {
  protected Tank context_;
  protected int dir_;
  
  public TankState(Tank context, int direction) {
    context_ = context;
    dir_ = direction;
  }
  
  public void changeState(TankState newState) {
    context_.changeState(newState);
  }
  
  public int getDir() {
    return dir_;
  }
  
  public abstract void update();
}

class Move extends TankState {

  public Move(Tank context, int direction) {
    super(context, direction);
    if (dir_ == Direction.LEFT) {
      int tab[] = {0,1,2,3};
      context_.getDrawer().setAnimation(tab, 200);
    } else {
      int tab[] = {4,5,6,7};
      context_.getDrawer().setAnimation(tab, 200);
    }
  }

  @Override
  public void update() {
    if (dir_ == Direction.LEFT) {
      context_.moveLeft();
      if ( context_.pos_.hasLeftCollision() ) {
        changeState(new Fire(context_, Direction.RIGHT));
      }
    } else {
      context_.moveRight();
      if ( context_.pos_.hasRightCollision() ) {
        changeState(new Fire(context_, Direction.LEFT));
      }
    }
  }
}

class Fire extends TankState {

  public Fire(Tank context, int direction) {
    super(context, direction);
    if (dir_ == Direction.LEFT) {
      int tab[] = {8,9,10,11};
      context_.getDrawer().setAnimation(tab, 70);
    } else {
      int tab[] = {12,13,14,15};
      context_.getDrawer().setAnimation(tab, 70);
    }
    new Projectile(context_.getXFire(), context_.getYFire(), dir_);
  }

  @Override
  public void update() {
    if ( context_.getDrawer().getAnim().cycleEnded() ) {
      changeState(new Move(context_, dir_));
    }
  }
}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java