Advertisement
calcpage

GWP3_Jumper.java

Apr 20th, 2012
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.55 KB | None | 0 0
  1. //Jumper.java   MrG 2012.0420
  2. import java.awt.Color;
  3. import info.gridworld.actor.Actor;
  4. import info.gridworld.grid.Location;
  5. import info.gridworld.grid.Grid;
  6.  
  7. public class Jumper extends Actor
  8. {
  9.     public Jumper()
  10.     {
  11.         setColor(Color.PURPLE);
  12.     }
  13.  
  14.     public Jumper(Color jumperColor)
  15.     {
  16.         setColor(jumperColor);
  17.     }
  18.  
  19.     public void act()
  20.     {
  21.         if(canJump())
  22.         {
  23.             jump();
  24.         }
  25.         else
  26.         {
  27.             turn();
  28.         }
  29.     }
  30.  
  31.     public void turn()
  32.     {
  33.         setDirection(getDirection() + Location.HALF_RIGHT);
  34.     }
  35.  
  36.     public void jump()
  37.     {
  38.         Grid<Actor> gr = getGrid();
  39.         if(gr == null)
  40.             return;
  41.         Location loc = getLocation();
  42.         Location next = loc.getAdjacentLocation(getDirection());
  43.         Location twoAway = next.getAdjacentLocation(getDirection());
  44.         if(gr.isValid(twoAway))
  45.         {
  46.             moveTo(twoAway);
  47.         }
  48.         else
  49.         {
  50.             removeSelfFromGrid();
  51.         }
  52.         //Flower flower = new Flower(getColor());
  53.         //flower.putSelfInGrid(gr, loc);
  54.     }
  55.  
  56.     public boolean canJump()
  57.     {
  58.         Grid<Actor> gr = getGrid();
  59.         if(gr == null)
  60.         {
  61.             return false;
  62.         }
  63.         Location loc = getLocation();
  64.         Location next = loc.getAdjacentLocation(getDirection());
  65.         if(!gr.isValid(next))
  66.         {
  67.             return false;
  68.         }
  69.         Actor neighbor = gr.get(next);
  70.         if(!((neighbor == null)||(neighbor instanceof Flower)||(neighbor instanceof Rock)))
  71.         {
  72.             return false;
  73.         }
  74.         Location twoAway = next.getAdjacentLocation(getDirection());
  75.         if(!gr.isValid(twoAway))
  76.         {
  77.             return false;
  78.         }      
  79.         neighbor = gr.get(twoAway);
  80.         return (neighbor == null)||(neighbor instanceof Flower)||(neighbor instanceof Bug)
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement