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;
//fromwww.java2s.comimport com.lucythemoocher.R;
import com.lucythemoocher.physics.Cinematic;
import com.lucythemoocher.util.Direction;
publicclass 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);
}
publicvoid update() {
super.update();
state_.update();
}
void changeState(TankState newState) {
state_ = newState;
}
/**
* X position where to create projectiles
* @return
*/publicfloat getXFire() {
float x = getCinematic().x();
if (state_.getDir() == Direction.LEFT) {
x += getDrawer().getAnim().getW();
}
return x;
}
/**
* Y position where to create projectiles
* @return
*/publicfloat getYFire() {
float y = getCinematic().y();
y += getDrawer().getAnim().getH() / 2;
return y;
}
}
/**
* State of the Tank
*/abstractclass TankState {
protected Tank context_;
protectedint dir_;
public TankState(Tank context, int direction) {
context_ = context;
dir_ = direction;
}
publicvoid changeState(TankState newState) {
context_.changeState(newState);
}
publicint getDir() {
return dir_;
}
publicabstractvoid 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
publicvoid 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
publicvoid update() {
if ( context_.getDrawer().getAnim().cycleEnded() ) {
changeState(new Move(context_, dir_));
}
}
}