To start with I decided what the AI in the game as going to do, and because this is a project that is on a very small time scale for what it is I decided that the AI will just move back and forth once it collides with a wall returning in the opposite direction. If the player hits the AI sprite then they will die forcing them to restart the level from the beginning. For this I first of all drew the AI sprite within the game and set its animation cycle with the code below:
AI Animation Code
ai = new Animation();
Image [] enemyMoveRight = {new Image ("data/nme/nmeMOVE1.png"),
new Image ("data/nme/nmeMOVE2.png")};
Image [] enemyMoveLeft = {new Image ("data/nme/nmeBACKWARDSMOVE1.png"),
new Image ("data/nme/nmeBACKWARDSMOVE2.png")};
enemyRight = new Animation(enemyMoveRight, 200, false);
enemyLeft = new Animation(enemyMoveLeft, 200, false);
enemyRight.setPingPong(true);
enemyLeft.setPingPong(true);
ai = enemyRight;
Updated Render Loop
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
BG.draw(0,0);
BlockMap.tmap.render(0, 0);
g.drawAnimation(player, x , y);
g.drawAnimation(ai, AIx, AIy);
}
After This code was inserted I then moved on to adding some movement to the AI after it had been drawn into the correct position on the map. To do this I started the AI initially moving right, constantly checking each time that it moved if it was going to collide with a wall within the map using the same collision method described earlier for the character. If the AI did collide then the movement was reversed and the AI would continue in the direction until it collided with another wall. I also made the game check each movement if the Polygon surrounding the AI collided with the Polygon surrounding the character but without the actual death code in place this only plays the death sound effect that Sam has made. The code I used to do this was :
AI Logic Code
AIPoly.setY(AIy);
if(enemyR){
AIx += floatDelta;
ai = enemyRight;
enemyRight.update(delta);
AIPoly.setX(AIx);
if(entityAICollisionWith()){
enemyR = false;
}
}
if(!enemyR){
AIx -= floatDelta;
ai = enemyLeft;
enemyLeft.update(delta);
AIPoly.setX(AIx);
if(entityAICollisionWith()){
enemyR = true;
}
}
if(playerPoly.intersects(AIPoly)){
death.play();
}
After completing the Code for the AI I moved on to the easier task of setting up all of the sound effects that I have now been given from Sam. The code that I have updated to include these tasks is below:
Sound Initialisation
Level1 = new Music("data/Level_1_Tune.ogg");
death = new Sound("data/Death_Tune.ogg");
JumpFX = new Sound("data/Jump.ogg");
Sound Code Update
if ((input.isKeyPressed(Input.KEY_UP) || input.isKeyPressed(Input.KEY_W)) && !jumping){
if(lastKeyLeft){
player = jumpLeft;
} else {
player = jumpRight;
}
gravSpeeds = -12;
jumping = true;
JumpFX.play();
}
No comments:
Post a Comment