Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created November 13, 2012 06:54
Show Gist options
  • Select an option

  • Save chintanparikh/4064387 to your computer and use it in GitHub Desktop.

Select an option

Save chintanparikh/4064387 to your computer and use it in GitHub Desktop.
SecondEnemy
import java.awt.Point;
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* In order to help learn course concepts, I worked on this homework with Chris Lewis,
* discussed homework topics and issues with Chris Lewis
*
* A class to represent the second enemy
*
* @author Chintan Parikh
* @version 1
*/
public class SecondEnemy extends Creature
{
private static final String image_path = "images/monster2.png";
private Point playerPosition;
private ArrayList<Point> queue = new ArrayList<Point>();
private ArrayList<Point> visited = new ArrayList<Point>();
/**
* The constructor function to create a new FirstEnemy
*
* @param dungeon The dungeon instance
* @param x The x-value of the position of the enemy
* @param y The y-value of the position of the enemy
*/
public SecondEnemy(Dungeon dungeon, int x, int y)
{
super("Monster 2", dungeon, image_path, x, y, 5, 80);
}
/**
* The update method that's called every turn
*/
public void update()
{
if (queue.isEmpty() && position != playerPosition)
{
playerMove();
}
Tile newTile = dungeon.getTile(queue.get(0));
queue.remove(0);
boolean nextTile = false;
if (Tile.canStepOn(newTile))
{
dungeon.getTile(position).setOccupant(null);
newTile.setOccupant(this);
this.setPosition(newTile.getPosition());
}
else if (newTile.getOccupant() instanceof Player)
{
this.attack(newTile.getOccupant());
}
checkForDeath();
}
/**
* Called every time the player moves
*/
public void playerMove()
{
playerPosition = dungeon.getPlayer().getPosition();
generatePath(this.position);
checkForDeath();
}
/**
* Checks for death
*/
public void checkForDeath()
{
if (health <= 0)
{
dungeon.killCreature(1);
}
}
protected Point findNextTile(Point position)
{
Point next = null;
if (playerPosition.x > position.x && !visited.contains(new Point(position.x + 1, position.y)) && Tile.canStepOn(dungeon.getTile(new Point(position.x + 1, position.y))))
{
next = new Point(position.x + 1, position.y);
visited.add(next);
}
else if (playerPosition.x < position.x && !visited.contains(new Point(position.x - 1, position.y)) && Tile.canStepOn(dungeon.getTile(new Point(position.x - 1, position.y))))
{
next = new Point(position.x - 1, position.y);
visited.add(next);
}
else if (playerPosition.y > position.y && !visited.contains(new Point(position.x, position.y + 1)) && Tile.canStepOn(dungeon.getTile(new Point(position.x, position.y + 1))))
{
next = new Point(position.x, position.y + 1);
visited.add(next);
}
else if (playerPosition.y < position.y && !visited.contains(new Point(position.x, position.y - 1)) && Tile.canStepOn(dungeon.getTile(new Point(position.x, position.y - 1))))
{
next = new Point(position.x, position.y - 1);
visited.add(next);
}
else if (!visited.contains(new Point(position.x + 1, position.y)) && Tile.canStepOn(dungeon.getTile(new Point(position.x + 1, position.y))))
{
next = new Point(position.x + 1, position.y);
visited.add(next);
}
else if (!visited.contains(new Point(position.x - 1, position.y)) && Tile.canStepOn(dungeon.getTile(new Point(position.x - 1, position.y))))
{
next = new Point(position.x - 1, position.y);
visited.add(next);
}
else if (!visited.contains(new Point(position.x, position.y + 1)) && Tile.canStepOn(dungeon.getTile(new Point(position.x, position.y + 1))))
{
next = new Point(position.x, position.y + 1);
visited.add(next);
}
else if (!visited.contains(new Point(position.x, position.y - 1)) && Tile.canStepOn(dungeon.getTile(new Point(position.x, position.y- 1))))
{
next = new Point(position.x, position.y - 1);
visited.add(next);
}
else
{
next = null;
}
return next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment