import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * This is the Bug. * It is controlled by the user and must reach the exit by falling down platforms avoiding flames by jumping over them or missing them. * * @author Nigel Kay * @version 1.0 */ public class Bug extends Actor { private static final int RIGHT = 1; private static final int LEFT = -1; private static final int UP = 2; private static final int DOWN = 3; int direction = 0; int jumpCount = 0; int vertdirection = 0; boolean jumping = false; public Bug() { } /** * Act - This is the method which is run constantly when the environment is run. * It checks to see if the Bug is not on a platform. If it is not then it will fall. * It then checks to see if the bug has reached the end of the game and if it has, a message will display and a sound will play. The simulation will also stop. * If it has found a flame, a sound will play and the simulation will stop. * If none of the above, it checks to see if a key has been pressed and if it has, if the direction is up, it will change jumping to true. * If a key has been pressed and it is not up then it will move using the move method. */ public void act() { if(!onPlatform()) { // if the bug is not on a platform or not at the bottom then it should fall fall(); } else if(atEnd()) { Message m1 = new Message(); getWorld().addObject(m1, 8, 7); Greenfoot.playSound("completed.wav"); removeBug(); Greenfoot.stopSimulation(); } else if(foundFlame()) { Message2 m2 = new Message2(); getWorld().addObject(m2, 8, 7); Greenfoot.playSound("gameover.wav"); removeBug(); Greenfoot.stopSimulation(); } else { if(keyPressed()){ // if a key is pressed and it is the up key then jump but if not, move if(direction == UP) { jumping=true; } move(); } } } /** * onPlatform - This is the method that checks to see if the flame is on a platform. * It gets the current co-ordinates of the Bug and checks below it one space to see if the Bug is on a platform. * If there is a no block there, it returns false, if not it returns true. */ private boolean onPlatform() { World myWorld = getWorld(); int x = getX(); int y = getY(); if(myWorld.getObjectsAt(x, y+1, Block.class).isEmpty()) { // check to see if there is a block below the bug return false; } else { return true; } } /** * canMove - This is the method that checks to see if the bug can move. * It checks to see whether the bug has reached the outside borders and if it has, return false to stop it from moving. * If the direction is down and the bug is on a platform, return false. (This is also for the end of the jump) */ private boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); if ((x > myWorld.getWidth()) || y > myWorld.getHeight()) { return false; } else if (x < 0 || y < 0) { return false; } else if (direction==DOWN && onPlatform()){ // is it at end of jump and on platform return false; } else { return true; } } /** * atEnd - This method simply checks to see if the Bug has reached the co-ordinates 15, 15 and if it is, return true. (This is the exit square) */ private boolean atEnd() { int x = getX(); int y = getY(); if(x == 15 && y == 15){ return true; } else { return false; } } /** * Move - This method checks to see if the Bug is jumping and if it is, it will prepare the jump using the prepareJump method. * If then checks to see if it can move and if it can, it will get the current direction of the Bug and move it in the current direction one space. */ private void move(){ if(jumping){ prepareJump(); } if(canMove()) { switch(direction) { case DOWN : setLocation(getX(), getY() + 1); break; case RIGHT : setLocation(getX() + 1, getY()); break; case UP : setLocation(getX() + vertdirection, getY() - 1); break; case LEFT : setLocation(getX() - 1, getY()); break; } } } /** * keyPressed - This method checks to see if a key has been pressed. * It will firstly, get the key which has been pressed and check the current direction of it. If it needs rotating to change its direction, it will also set the direction. * It also sets the vertical direction so that the jump method functions correctly. */ private boolean keyPressed() { // this is the method for moving the Bug when the key is pressed String key = Greenfoot.getKey(); //check whether a key has been pressed if (key=="right") { if (direction==LEFT) { // if the key is pressed right, check if the direction is left and if it is, set the rotation to 0 setRotation(0); } direction=RIGHT; // if the direction is right, return true vertdirection=RIGHT; return true; } else if (key=="left") { if (direction==RIGHT) { setRotation(180); } direction=LEFT; vertdirection=LEFT; return true; } else if (key=="up") { if (direction==UP) { setRotation(0); } direction=UP; return true; } else if(key == null) { // this checks to see if no key has been pressed return false; } else { Greenfoot.playSound("beep.wav"); return true; } } /** * Fall - This method checks to see if the Bug can move and if it can move, it adds one to the y axis allowing the bug to move down to the next platform. */ private void fall(){ World myWorld = getWorld(); int x = getX(); int y = getY(); if (canMove()) { y++; // if the bug can move, allow it to fall on the y axis and then set its location based on its current co-orindates setLocation(x,y); } } /** * prepareJump - This method describes how the Bug will jump. * If the jumpCount is between 0 and 1, it will go up and when the jump count reaches between 3 and 4 it will come back down. * After the jump has been generated and the Bug is back down, the jumpcount is set back to 0 and jumping is set back to false. */ private void prepareJump(){ if (jumpCount >=0 && jumpCount <=1){ direction=UP; } else if (jumpCount >=3 && jumpCount <=4){ direction=DOWN; } else { vertdirection=0; } if(jumpCount==4){ jumpCount=0; vertdirection=0; } else{ jumpCount++; jumpCount=0; jumping=false; } } /** * foundFlame - This method checks to see if the Bug is in the same square as a flame and if it is, it will return true but false if not. */ public boolean foundFlame() { Actor flame = getOneObjectAtOffset(0, 0, Flame.class); if(flame != null) { return true; } else { return false; } } /** * removeBug - This method simply removes the Bug from the playing world. */ public void removeBug() { World myWorld = getWorld(); myWorld.removeObject(this); } }