Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More accurate ZombieMoves #102

Merged
merged 1 commit into from May 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/com/vulcastudios/TestGame.java
Expand Up @@ -170,7 +170,7 @@ public boolean checkCollision(Steam s){
boolean playerCol = p.getBounds().intersects(s.getBounds());
if(playerCol){
p.setAlive(false);
ZombieMove move = new ZombieMove();
ZombieMove move = new ZombieMove(1);
if(p.getXPos() > s.getX())
move.addLeft();
if(p.getXPos() < s.getX())
Expand Down
2 changes: 1 addition & 1 deletion src/com/vulcastudios/actors/Player.java
Expand Up @@ -49,7 +49,7 @@ public void update(GameContainer container, StateBasedGame game, int delta){
float oldX = this.getXPos();
float oldY = this.getYPos();

ZombieMove move = new ZombieMove();
ZombieMove move = new ZombieMove(delta);
// Handle player movement

// Vertical
Expand Down
97 changes: 59 additions & 38 deletions src/com/vulcastudios/actors/Zombie.java
Expand Up @@ -19,17 +19,17 @@ public class Zombie{
private static final int HEIGHT = 20;
private float xPos = 0;
private float yPos = 0;

private float xPosStart = 0;
private float yPosStart = 0;

private Button onButton = null;
private Image image = null;

private ResourceManager rm;
private LinkedList<ZombieMove> movementMap;
private LinkedList<ZombieMove> movementMapMaster;

public Zombie(ResourceManager rm, int xPos, int yPos, LinkedList<ZombieMove> moveMap){
this.rm = rm;
this.xPos = xPos;
Expand All @@ -39,53 +39,74 @@ public Zombie(ResourceManager rm, int xPos, int yPos, LinkedList<ZombieMove> mov
this.movementMapMaster = moveMap;
this.movementMap = (LinkedList<ZombieMove>)moveMap.clone();
}

public void restartZombie(){
this.setXPos(this.xPosStart);
this.setYPos(this.yPosStart);
this.movementMap = (LinkedList<ZombieMove>)this.movementMapMaster.clone();
}

public void update(GameContainer container, StateBasedGame game, int delta){

float oldX = this.getXPos();
float oldY = this.getYPos();

if(!movementMap.isEmpty()){
ZombieMove move = movementMap.pop();

// Vertical
if(move.isUp()){
this.setYPos(getYPos() - (Player.DELTA_Y * delta));
}
if(move.isDown()){
this.setYPos(getYPos() + (Player.DELTA_Y * delta));
}
if(((TestGame)game).checkCollision(this)){
this.setYPos(oldY);
}

// Horizontal
if(move.isLeft()){
this.setXPos(getXPos() - (Player.DELTA_X * delta));
}
if(move.isRight()){
this.setXPos(getXPos() + (Player.DELTA_Y * delta));
}

if(((TestGame)game).checkCollision(this)){
this.setXPos(oldX);

int usedDelta = 0;
while(usedDelta < delta){
if(!movementMap.isEmpty()){
ZombieMove move = movementMap.pop();

int moveDelta = move.getDelta();
if(moveDelta > (delta-usedDelta)){
ZombieMove newMove = move.clone();
newMove.setDelta(moveDelta - (delta-usedDelta));
movementMap.push(newMove);
moveDelta = (delta-usedDelta);
}
usedDelta += moveDelta;

// Vertical
if(move.isUp()){
this.setYPos(getYPos() - (Player.DELTA_Y * moveDelta));
}
if(move.isDown()){
this.setYPos(getYPos() + (Player.DELTA_Y * moveDelta));
}

if(((TestGame)game).checkCollision(this)){
this.setYPos(oldY);
}

// Horizontal
if(move.isLeft()){
this.setXPos(getXPos() - (Player.DELTA_X * moveDelta));
}
if(move.isRight()){
this.setXPos(getXPos() + (Player.DELTA_Y * moveDelta));
}

if(((TestGame)game).checkCollision(this)){
this.setXPos(oldX);
}

}else{
break;
}

}





}

public void render(GameContainer container, Graphics g){
g.setColor(Color.green);
//g.drawRect(this.getXPos(), this.getYPos(), WIDTH, HEIGHT);
g.drawImage(this.getImage(), this.getXPos(), this.getYPos());
}

public float getXPos() {
return xPos;
}
Expand All @@ -101,11 +122,11 @@ public float getYPos() {
public void setYPos(float yPos) {
this.yPos = yPos;
}

public Rectangle getBounds(){
return new Rectangle(this.getXPos(), this.getYPos(), Zombie.WIDTH, Zombie.HEIGHT);
}

public void setOnButton(Button b){
if(b == null){
this.onButton = null;
Expand All @@ -117,12 +138,12 @@ public void setOnButton(Button b){
this.rm.getSound("menu_button").play();
}
}

public Image getImage(){
if(this.image == null)
this.image = this.rm.getImage("zombie").getScaledCopy(Zombie.WIDTH, Zombie.HEIGHT);
return this.image;

}

}
25 changes: 25 additions & 0 deletions src/com/vulcastudios/actors/ZombieMove.java
Expand Up @@ -5,6 +5,11 @@ public class ZombieMove {
private boolean down = false;
private boolean left = false;
private boolean right = false;
private int delta;

public ZombieMove(int delta){
this.setDelta(delta);
}


public boolean isRight() {
Expand Down Expand Up @@ -54,4 +59,24 @@ public void addLeft(){
public void noLeft(){
left = false;
}


public int getDelta() {
return delta;
}

public void setDelta(int delta) {
this.delta = delta;
}

@Override
public ZombieMove clone(){
ZombieMove clone = new ZombieMove(this.delta);
clone.down = this.down;
clone.up = this.up;
clone.left = this.left;
clone.right = this.right;
return clone;

}
}