Advertisement
Guest User

Sand Man Enemy (Greenfoot)

a guest
Jan 29th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  2.  
  3. /**
  4.  * An enemy which is stationary on the ground, but is periodicly jumping up, from the ground
  5.  * and then is going down again.
  6.  *
  7.  * @author Martin Rohwedder
  8.  * @version 1.0
  9.  */
  10. public class SandMan extends Enemy
  11. {
  12.     //Constants defining the minimum and maxiumum y position this enemy can 'jump' to.
  13.     private final int MAX_Y_POS = 695;
  14.     private final int MIN_Y_POS = 645;
  15.    
  16.     private int speed = 3;
  17.    
  18.     private boolean doJumpAnimation = false;
  19.     private boolean stretching = false;
  20.     private int timeCycle = 120;
  21.    
  22.     /**
  23.      * Act - do whatever the SandMan wants to do. This method is called whenever
  24.      * the 'Act' or 'Run' button gets pressed in the environment.
  25.      */
  26.     public void act()
  27.     {
  28.         checkForOnGoingAnimation();
  29.         doJumpAnimation();
  30.     }
  31.    
  32.     /**
  33.      * Check if the animation is in action or not. If not the time cycle will be resetted, and
  34.      * the animation can begin again.
  35.      */
  36.     private void checkForOnGoingAnimation()
  37.     {
  38.         //If not doing jump animation, then go ahead and do it.
  39.         if(!doJumpAnimation)
  40.         {
  41.             if (timeCycle <= 0)
  42.             {
  43.                 doJumpAnimation = true;
  44.                 if (doJumpAnimation)
  45.                 {
  46.                     //Reset Time cycle to between 1 and 4 seconds.
  47.                     timeCycle = Greenfoot.getRandomNumber(240) + 60;
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 //Decrement time by 1
  53.                 timeCycle--;
  54.             }
  55.         }
  56.     }
  57.    
  58.     /**
  59.      * Do the actual jump animation. If the enemy is stretching (it's at its highest), then
  60.      * make the animation, so the enemy go down, and otherwise up.
  61.      */
  62.     private void doJumpAnimation()
  63.     {
  64.         if (doJumpAnimation)
  65.         {
  66.             //If stretching, make the enemy animate downwards
  67.             if (stretching)
  68.             {
  69.                 if (getY() < MAX_Y_POS)
  70.                 {
  71.                     setLocation(getX(), getY() + speed );
  72.                 }
  73.                 else
  74.                 {
  75.                     stretching = false;
  76.                     doJumpAnimation = false;
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 if (getY() > MIN_Y_POS)
  82.                 {
  83.                     setLocation(getX(), getY() - speed );
  84.                 }
  85.                 else
  86.                 {
  87.                     stretching = true;
  88.                     doJumpAnimation = false;
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement