Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Jumper.java MrG 2012.0420
- import java.awt.Color;
- import info.gridworld.actor.Actor;
- import info.gridworld.grid.Location;
- import info.gridworld.grid.Grid;
- public class Jumper extends Actor
- {
- public Jumper()
- {
- setColor(Color.PURPLE);
- }
- public Jumper(Color jumperColor)
- {
- setColor(jumperColor);
- }
- public void act()
- {
- if(canJump())
- {
- jump();
- }
- else
- {
- turn();
- }
- }
- public void turn()
- {
- setDirection(getDirection() + Location.HALF_RIGHT);
- }
- public void jump()
- {
- Grid<Actor> gr = getGrid();
- if(gr == null)
- return;
- Location loc = getLocation();
- Location next = loc.getAdjacentLocation(getDirection());
- Location twoAway = next.getAdjacentLocation(getDirection());
- if(gr.isValid(twoAway))
- {
- moveTo(twoAway);
- }
- else
- {
- removeSelfFromGrid();
- }
- //Flower flower = new Flower(getColor());
- //flower.putSelfInGrid(gr, loc);
- }
- public boolean canJump()
- {
- Grid<Actor> gr = getGrid();
- if(gr == null)
- {
- return false;
- }
- Location loc = getLocation();
- Location next = loc.getAdjacentLocation(getDirection());
- if(!gr.isValid(next))
- {
- return false;
- }
- Actor neighbor = gr.get(next);
- if(!((neighbor == null)||(neighbor instanceof Flower)||(neighbor instanceof Rock)))
- {
- return false;
- }
- Location twoAway = next.getAdjacentLocation(getDirection());
- if(!gr.isValid(twoAway))
- {
- return false;
- }
- neighbor = gr.get(twoAway);
- return (neighbor == null)||(neighbor instanceof Flower)||(neighbor instanceof Bug)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement