Merge pull request #9 from cognitivegears/master
Pathfinding initial commit
This commit is contained in:
commit
70c43df686
@ -3,6 +3,7 @@ package com.droidquest.avatars;
|
|||||||
import com.droidquest.Room;
|
import com.droidquest.Room;
|
||||||
import com.droidquest.RoomDisplay;
|
import com.droidquest.RoomDisplay;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
|
import com.droidquest.pathfinder.Node;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -324,29 +325,9 @@ public class GameCursor extends Player {
|
|||||||
return level.cheatmode;
|
return level.cheatmode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Animate() {
|
|
||||||
if (automove == 1 && room == null) {
|
@Override
|
||||||
automove = 0;
|
protected void animateCharacter(int dx, int dy) {
|
||||||
}
|
|
||||||
if (automove == 1) {
|
|
||||||
int dx = autoX - x;
|
|
||||||
int dy = autoY - y;
|
|
||||||
if (dx == 0 && dy == 0) {
|
|
||||||
automove = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (dx < -28) {
|
|
||||||
dx = -28;
|
|
||||||
}
|
|
||||||
if (dx > 28) {
|
|
||||||
dx = 28;
|
|
||||||
}
|
|
||||||
if (dy < -32) {
|
|
||||||
dy = -32;
|
|
||||||
}
|
|
||||||
if (dy > 32) {
|
|
||||||
dy = 32;
|
|
||||||
}
|
|
||||||
walk = 1 - walk;
|
walk = 1 - walk;
|
||||||
if (dx == 0) {
|
if (dx == 0) {
|
||||||
if (dy < 0) {
|
if (dy < 0) {
|
||||||
@ -364,20 +345,10 @@ public class GameCursor extends Player {
|
|||||||
currentIcon = icons[6 + walk].getImage();
|
currentIcon = icons[6 + walk].getImage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (dx > 0) {
|
|
||||||
moveRight(dx);
|
|
||||||
}
|
}
|
||||||
if (dx < 0) {
|
|
||||||
moveLeft(-dx);
|
@Override
|
||||||
}
|
protected void autoMoveFull() {
|
||||||
if (dy > 0) {
|
|
||||||
moveDown(dy);
|
|
||||||
}
|
|
||||||
if (dy < 0) {
|
|
||||||
moveUp(-dy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (automove == 2) {
|
|
||||||
walk = 1 - walk;
|
walk = 1 - walk;
|
||||||
if (autoX > 0) {
|
if (autoX > 0) {
|
||||||
currentIcon = icons[6 + walk].getImage();
|
currentIcon = icons[6 + walk].getImage();
|
||||||
@ -398,7 +369,7 @@ public class GameCursor extends Player {
|
|||||||
currentIcon = icons[0 + walk].getImage();
|
currentIcon = icons[0 + walk].getImage();
|
||||||
moveUp(-autoY);
|
moveUp(-autoY);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public GenericRobot PlayerInRobot(GenericRobot robot) {
|
public GenericRobot PlayerInRobot(GenericRobot robot) {
|
||||||
|
@ -93,8 +93,8 @@ public class PaintBrush extends Player {
|
|||||||
paintMats[1] = Material.FindSimiliar(new Material(Color.green, false, false));
|
paintMats[1] = Material.FindSimiliar(new Material(Color.green, false, false));
|
||||||
Item robot = null;
|
Item robot = null;
|
||||||
|
|
||||||
for(Item item : level.items) {
|
for (Item item : level.items) {
|
||||||
if(item instanceof OrangeRobot) {
|
if (item instanceof OrangeRobot) {
|
||||||
robot = item;
|
robot = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,14 +104,14 @@ public class PaintBrush extends Player {
|
|||||||
paintMats[2] = Material.FindSimiliar(new RobotBlocker(robot, new Color(255, 128, 0)));
|
paintMats[2] = Material.FindSimiliar(new RobotBlocker(robot, new Color(255, 128, 0)));
|
||||||
|
|
||||||
for (Item item : level.items) {
|
for (Item item : level.items) {
|
||||||
if(item instanceof WhiteRobot) {
|
if (item instanceof WhiteRobot) {
|
||||||
robot = item;
|
robot = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
paintMats[3] = Material.FindSimiliar(new RobotBlocker(robot, Color.white));
|
paintMats[3] = Material.FindSimiliar(new RobotBlocker(robot, Color.white));
|
||||||
|
|
||||||
for(Item item : level.items) {
|
for (Item item : level.items) {
|
||||||
if(item instanceof BlueRobot) {
|
if (item instanceof BlueRobot) {
|
||||||
robot = item;
|
robot = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -213,6 +213,7 @@ public class PaintBrush extends Player {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void moveUp(boolean nudge) {
|
public void moveUp(boolean nudge) {
|
||||||
int dist = 32;
|
int dist = 32;
|
||||||
if (nudge) {
|
if (nudge) {
|
||||||
@ -231,6 +232,7 @@ public class PaintBrush extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void moveDown(boolean nudge) {
|
public void moveDown(boolean nudge) {
|
||||||
int dist = 32;
|
int dist = 32;
|
||||||
if (nudge) {
|
if (nudge) {
|
||||||
@ -249,6 +251,7 @@ public class PaintBrush extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void moveLeft(boolean nudge) {
|
public void moveLeft(boolean nudge) {
|
||||||
int dist = 28;
|
int dist = 28;
|
||||||
if (nudge) {
|
if (nudge) {
|
||||||
@ -267,6 +270,7 @@ public class PaintBrush extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void moveRight(boolean nudge) {
|
public void moveRight(boolean nudge) {
|
||||||
int dist = 28;
|
int dist = 28;
|
||||||
if (nudge) {
|
if (nudge) {
|
||||||
|
@ -5,6 +5,7 @@ import com.droidquest.Wire;
|
|||||||
import com.droidquest.chipstuff.Port;
|
import com.droidquest.chipstuff.Port;
|
||||||
import com.droidquest.devices.Device;
|
import com.droidquest.devices.Device;
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
|
import com.droidquest.pathfinder.Node;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -12,6 +13,7 @@ import java.awt.event.InputEvent;
|
|||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class SolderingPen extends Device implements Avatar {
|
public class SolderingPen extends Device implements Avatar {
|
||||||
private boolean hot;
|
private boolean hot;
|
||||||
@ -225,6 +227,7 @@ public class SolderingPen extends Device implements Avatar {
|
|||||||
CheckPort();
|
CheckPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void Animate() {
|
public void Animate() {
|
||||||
Room tempRoom = room;
|
Room tempRoom = room;
|
||||||
super.Animate();
|
super.Animate();
|
||||||
@ -387,50 +390,23 @@ public class SolderingPen extends Device implements Avatar {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MouseClick(MouseEvent e) {
|
@Override
|
||||||
int button = 0;
|
protected int getWidthModifier() {
|
||||||
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
|
return 2;
|
||||||
button = 1;
|
|
||||||
}
|
|
||||||
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
|
|
||||||
button = 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (button == 1) {
|
@Override
|
||||||
if (e.getClickCount() == 1) {
|
protected int getHeightModifier() {
|
||||||
autoX = e.getX() - 2;
|
return 20;
|
||||||
autoY = e.getY() - 20;
|
|
||||||
automove = 1;
|
|
||||||
}
|
|
||||||
else if (e.getClickCount() == 2) {
|
|
||||||
int dx = e.getX() - 2 - x;
|
|
||||||
int dy = e.getY() - 20 - y;
|
|
||||||
if (Math.abs(dx) > Math.abs(dy)) {
|
|
||||||
autoY = 0;
|
|
||||||
autoX = 28;
|
|
||||||
if (dx < 0) {
|
|
||||||
autoX = -28;
|
|
||||||
}
|
|
||||||
automove = 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
autoX = 0;
|
|
||||||
autoY = 32;
|
|
||||||
if (dy < 0) {
|
|
||||||
autoY = -32;
|
|
||||||
}
|
|
||||||
automove = 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (button == 3) {
|
|
||||||
KeyEvent k = new KeyEvent(e.getComponent(), e.getID(),
|
|
||||||
e.getWhen(), 0,
|
|
||||||
KeyEvent.VK_SPACE, ' ');
|
|
||||||
KeyUp(k);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setFinePositioning(MouseEvent e) {
|
||||||
|
// Fine positioning needed for solderpen
|
||||||
|
int finalX = e.getX() - getWidthModifier();
|
||||||
|
int finalY = e.getY() - getHeightModifier();
|
||||||
|
autoPath.add(new Node(finalX, finalY));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -5,6 +5,8 @@ import com.droidquest.Wire;
|
|||||||
import com.droidquest.devices.Device;
|
import com.droidquest.devices.Device;
|
||||||
import com.droidquest.levels.Level;
|
import com.droidquest.levels.Level;
|
||||||
import com.droidquest.materials.ChipTrash;
|
import com.droidquest.materials.ChipTrash;
|
||||||
|
import com.droidquest.pathfinder.Node;
|
||||||
|
import com.droidquest.pathfinder.Pathfinder;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -15,6 +17,7 @@ import java.io.IOException;
|
|||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Item implements Serializable, Cloneable {
|
public class Item implements Serializable, Cloneable {
|
||||||
public transient static Level level;
|
public transient static Level level;
|
||||||
@ -28,6 +31,7 @@ public class Item implements Serializable, Cloneable {
|
|||||||
public transient int autoX; // Destination of automovement
|
public transient int autoX; // Destination of automovement
|
||||||
public transient int autoY; // Destination of automovement
|
public transient int autoY; // Destination of automovement
|
||||||
public Room InternalRoom = null; // Room inside this item, if any.
|
public Room InternalRoom = null; // Room inside this item, if any.
|
||||||
|
public transient ArrayList<Node> autoPath = new ArrayList<Node>();
|
||||||
|
|
||||||
protected int repeating = 0; // Keyboard repeat.
|
protected int repeating = 0; // Keyboard repeat.
|
||||||
public int charge = 0; // Battery Charge of this item, if any.
|
public int charge = 0; // Battery Charge of this item, if any.
|
||||||
@ -225,6 +229,24 @@ public class Item implements Serializable, Cloneable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void findPath(int startX, int startY, int endX, int endY) {
|
||||||
|
Pathfinder pf = new Pathfinder(room);
|
||||||
|
autoPath = pf.search(startX, startY, endX, endY, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected int getWidthModifier() {
|
||||||
|
return width / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int getHeightModifier() {
|
||||||
|
return height / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setFinePositioning(MouseEvent e) {
|
||||||
|
// By default, no fine positioning
|
||||||
|
}
|
||||||
|
|
||||||
public void MouseClick(MouseEvent e) {
|
public void MouseClick(MouseEvent e) {
|
||||||
int button = 0;
|
int button = 0;
|
||||||
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
|
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
|
||||||
@ -236,15 +258,29 @@ public class Item implements Serializable, Cloneable {
|
|||||||
|
|
||||||
if (button == 1) {
|
if (button == 1) {
|
||||||
if (e.getClickCount() == 1) {
|
if (e.getClickCount() == 1) {
|
||||||
autoX = e.getX() - width / 2;
|
int endX = e.getX() / 28;
|
||||||
autoY = e.getY() - height / 2;
|
int endY = e.getY() / 32;
|
||||||
|
|
||||||
|
int startX = x / 28;
|
||||||
|
int startY = y / 32;
|
||||||
|
|
||||||
|
findPath(startX, startY, endX, endY);
|
||||||
|
|
||||||
|
if(autoPath != null && autoPath.size() > 0) {
|
||||||
|
setFinePositioning(e);
|
||||||
|
|
||||||
|
Node next = autoPath.remove(0);
|
||||||
|
|
||||||
|
autoX = next.getX();
|
||||||
|
autoY = next.getY();
|
||||||
autoX -= autoX % 2; // Even numbered pixel only!
|
autoX -= autoX % 2; // Even numbered pixel only!
|
||||||
autoY -= autoY % 2;
|
autoY -= autoY % 2;
|
||||||
automove = 1;
|
automove = 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (e.getClickCount() == 2) {
|
else if (e.getClickCount() == 2) {
|
||||||
int dx = e.getX() - width / 2 - x;
|
int dx = e.getX() - getWidthModifier() - x;
|
||||||
int dy = e.getY() - height / 2 - y;
|
int dy = e.getY() - getHeightModifier() - y;
|
||||||
if (Math.abs(dx) > Math.abs(dy)) {
|
if (Math.abs(dx) > Math.abs(dy)) {
|
||||||
autoY = 0;
|
autoY = 0;
|
||||||
autoX = 28;
|
autoX = 28;
|
||||||
@ -436,6 +472,10 @@ public class Item implements Serializable, Cloneable {
|
|||||||
moveRight(dist);
|
moveRight(dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void animateCharacter(int dx, int dy) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void Animate() {
|
public void Animate() {
|
||||||
if (automove == 1 && room == null) {
|
if (automove == 1 && room == null) {
|
||||||
automove = 0;
|
automove = 0;
|
||||||
@ -443,8 +483,21 @@ public class Item implements Serializable, Cloneable {
|
|||||||
if (automove == 1) {
|
if (automove == 1) {
|
||||||
int dx = autoX - x;
|
int dx = autoX - x;
|
||||||
int dy = autoY - y;
|
int dy = autoY - y;
|
||||||
|
|
||||||
if (dx == 0 && dy == 0) {
|
if (dx == 0 && dy == 0) {
|
||||||
|
if(autoPath.size() > 0) {
|
||||||
|
Node next = autoPath.remove(0);
|
||||||
|
autoX = next.getX();
|
||||||
|
autoY = next.getY();
|
||||||
|
autoX -= autoX % 2; // Even numbered pixel only!
|
||||||
|
autoY -= autoY % 2;
|
||||||
|
dx = autoX - x;
|
||||||
|
dy = autoY - y;
|
||||||
|
}
|
||||||
|
if(dx == 0 && dy == 0) {
|
||||||
automove = 0;
|
automove = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (dx < -28) {
|
if (dx < -28) {
|
||||||
dx = -28;
|
dx = -28;
|
||||||
@ -458,6 +511,9 @@ public class Item implements Serializable, Cloneable {
|
|||||||
if (dy > 32) {
|
if (dy > 32) {
|
||||||
dy = 32;
|
dy = 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
animateCharacter(dx, dy);
|
||||||
|
|
||||||
if (dx > 0) {
|
if (dx > 0) {
|
||||||
moveRight(dx);
|
moveRight(dx);
|
||||||
}
|
}
|
||||||
@ -472,6 +528,11 @@ public class Item implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (automove == 2) {
|
if (automove == 2) {
|
||||||
|
autoMoveFull();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void autoMoveFull() {
|
||||||
if (autoX > 0) {
|
if (autoX > 0) {
|
||||||
moveRight(autoX);
|
moveRight(autoX);
|
||||||
}
|
}
|
||||||
@ -485,7 +546,6 @@ public class Item implements Serializable, Cloneable {
|
|||||||
moveUp(-autoY);
|
moveUp(-autoY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void Decorate() {
|
public void Decorate() {
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@ import com.droidquest.devices.ContactSensor;
|
|||||||
import com.droidquest.devices.SmallChip;
|
import com.droidquest.devices.SmallChip;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.Switch;
|
||||||
|
import com.droidquest.materials.switches.Switch1;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -15,6 +15,9 @@ import com.droidquest.devices.StormShield;
|
|||||||
import com.droidquest.items.Button;
|
import com.droidquest.items.Button;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.ElevatorSwitch;
|
||||||
|
import com.droidquest.materials.switches.SwitchA;
|
||||||
|
import com.droidquest.materials.switches.SwitchB;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -10,6 +10,10 @@ import com.droidquest.decorations.Graphix;
|
|||||||
import com.droidquest.devices.SmallChip;
|
import com.droidquest.devices.SmallChip;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.Switch4A;
|
||||||
|
import com.droidquest.materials.switches.Switch4B;
|
||||||
|
import com.droidquest.materials.switches.Switch4C;
|
||||||
|
import com.droidquest.materials.switches.Switch4D;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import com.droidquest.decorations.Arrow;
|
|||||||
import com.droidquest.decorations.Graphix;
|
import com.droidquest.decorations.Graphix;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.Switch;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import com.droidquest.decorations.Graphix;
|
|||||||
import com.droidquest.devices.DirectionalSensor;
|
import com.droidquest.devices.DirectionalSensor;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.Switch;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import com.droidquest.decorations.Arrow;
|
|||||||
import com.droidquest.devices.*;
|
import com.droidquest.devices.*;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.MazeLock;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import com.droidquest.decorations.Arrow;
|
|||||||
import com.droidquest.devices.*;
|
import com.droidquest.devices.*;
|
||||||
import com.droidquest.items.*;
|
import com.droidquest.items.*;
|
||||||
import com.droidquest.materials.*;
|
import com.droidquest.materials.*;
|
||||||
|
import com.droidquest.materials.switches.MazeLock;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import com.droidquest.items.*;
|
|||||||
import com.droidquest.materials.Material;
|
import com.droidquest.materials.Material;
|
||||||
import com.droidquest.materials.PanicButton;
|
import com.droidquest.materials.PanicButton;
|
||||||
import com.droidquest.materials.Portal;
|
import com.droidquest.materials.Portal;
|
||||||
import com.droidquest.materials.Switch;
|
import com.droidquest.materials.switches.Switch;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.droidquest.materials;
|
package com.droidquest.materials;
|
||||||
|
|
||||||
import com.droidquest.RoomDisplay;
|
import com.droidquest.RoomDisplay;
|
||||||
|
import com.droidquest.avatars.PaintBrush;
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.levels.Level;
|
import com.droidquest.levels.Level;
|
||||||
|
|
||||||
@ -62,6 +63,10 @@ public class Material implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean Passable(Item item) {
|
public boolean Passable(Item item) {
|
||||||
|
// The PaintBrush can pass anything
|
||||||
|
if(item instanceof PaintBrush) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return passable;
|
return passable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.Room;
|
import com.droidquest.Room;
|
||||||
import com.droidquest.decorations.Arrow;
|
import com.droidquest.decorations.Arrow;
|
||||||
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.materials.ElevatorOutPortal;
|
import com.droidquest.materials.ElevatorOutPortal;
|
||||||
import com.droidquest.materials.Switch;
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
public class ElevatorSwitch extends Switch {
|
public class ElevatorSwitch extends Switch {
|
||||||
private int animationState = 0;
|
private int animationState = 0;
|
||||||
@ -45,7 +46,7 @@ public class ElevatorSwitch extends Switch {
|
|||||||
animationState++;
|
animationState++;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
if (ElevatorOutPortal.outRoom == level.rooms.elementAt(11)) {
|
if (ElevatorOutPortal.outRoom == Material.level.rooms.elementAt(11)) {
|
||||||
for (int a = 0; a < room.arrows.size(); a++) {
|
for (int a = 0; a < room.arrows.size(); a++) {
|
||||||
Arrow arrow = room.arrows.elementAt(a);
|
Arrow arrow = room.arrows.elementAt(a);
|
||||||
arrow.direction = Arrow.DIR_UP;
|
arrow.direction = Arrow.DIR_UP;
|
@ -1,6 +1,7 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.materials.Switch;
|
import com.droidquest.items.Item;
|
||||||
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
|
|
||||||
public class MazeLock extends Switch {
|
public class MazeLock extends Switch {
|
||||||
@ -12,15 +13,15 @@ public class MazeLock extends Switch {
|
|||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
||||||
if (paintbrush == null) {
|
if (paintbrush == null) {
|
||||||
paintbrush = level.paintbrush;
|
paintbrush = Material.level.paintbrush;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
level.paintbrush = null;
|
Material.level.paintbrush = null;
|
||||||
value = true;
|
value = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
level.paintbrush = paintbrush;
|
Material.level.paintbrush = paintbrush;
|
||||||
value = false;
|
value = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,8 @@
|
|||||||
package com.droidquest.materials;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.Room;
|
import com.droidquest.Room;
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
@ -1,7 +1,8 @@
|
|||||||
package com.droidquest.materials;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.items.Train;
|
import com.droidquest.items.Train;
|
||||||
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
public class Switch1 extends Switch {
|
public class Switch1 extends Switch {
|
||||||
private transient Train train;
|
private transient Train train;
|
||||||
@ -12,8 +13,8 @@ public class Switch1 extends Switch {
|
|||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
||||||
if (train == null) {
|
if (train == null) {
|
||||||
for (int a = 0; a < level.items.size(); a++) {
|
for (int a = 0; a < Material.level.items.size(); a++) {
|
||||||
Item t = level.items.elementAt(a);
|
Item t = Material.level.items.elementAt(a);
|
||||||
if (t.getClass().toString().endsWith("Train")) {
|
if (t.getClass().toString().endsWith("Train")) {
|
||||||
train = (Train) t;
|
train = (Train) t;
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.Room;
|
import com.droidquest.Room;
|
||||||
import com.droidquest.materials.Switch;
|
import com.droidquest.items.Item;
|
||||||
|
|
||||||
public class Switch4A extends Switch {
|
public class Switch4A extends Switch {
|
||||||
int count = 0;
|
int count = 0;
|
@ -1,7 +1,7 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.materials.Material;
|
import com.droidquest.materials.Material;
|
||||||
import com.droidquest.materials.Switch;
|
|
||||||
|
|
||||||
public class Switch4B extends Switch {
|
public class Switch4B extends Switch {
|
||||||
private transient Switch4A sw = null;
|
private transient Switch4A sw = null;
|
||||||
@ -12,8 +12,8 @@ public class Switch4B extends Switch {
|
|||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
||||||
if (sw == null) {
|
if (sw == null) {
|
||||||
for (int a = 0; a < level.materials.size(); a++) {
|
for (int a = 0; a < Material.level.materials.size(); a++) {
|
||||||
Material mat = level.materials.elementAt(a);
|
Material mat = Material.level.materials.elementAt(a);
|
||||||
if (mat instanceof Switch4A) {
|
if (mat instanceof Switch4A) {
|
||||||
sw = (Switch4A) mat;
|
sw = (Switch4A) mat;
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.materials.Material;
|
import com.droidquest.materials.Material;
|
||||||
import com.droidquest.materials.Switch;
|
|
||||||
|
|
||||||
public class Switch4C extends Switch {
|
public class Switch4C extends Switch {
|
||||||
private transient Switch4A sw = null;
|
private transient Switch4A sw = null;
|
||||||
@ -12,8 +12,8 @@ public class Switch4C extends Switch {
|
|||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
||||||
if (sw == null) {
|
if (sw == null) {
|
||||||
for (int a = 0; a < level.materials.size(); a++) {
|
for (int a = 0; a < Material.level.materials.size(); a++) {
|
||||||
Material mat = level.materials.elementAt(a);
|
Material mat = Material.level.materials.elementAt(a);
|
||||||
if (mat instanceof Switch4A) {
|
if (mat instanceof Switch4A) {
|
||||||
sw = (Switch4A) mat;
|
sw = (Switch4A) mat;
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package com.droidquest.items;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.materials.Material;
|
import com.droidquest.materials.Material;
|
||||||
import com.droidquest.materials.Switch;
|
|
||||||
|
|
||||||
public class Switch4D extends Switch {
|
public class Switch4D extends Switch {
|
||||||
private transient Switch4A sw = null;
|
private transient Switch4A sw = null;
|
||||||
@ -12,8 +12,8 @@ public class Switch4D extends Switch {
|
|||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
||||||
if (sw == null) {
|
if (sw == null) {
|
||||||
for (int a = 0; a < level.materials.size(); a++) {
|
for (int a = 0; a < Material.level.materials.size(); a++) {
|
||||||
Material mat = level.materials.elementAt(a);
|
Material mat = Material.level.materials.elementAt(a);
|
||||||
if (mat instanceof Switch4A) {
|
if (mat instanceof Switch4A) {
|
||||||
sw = (Switch4A) mat;
|
sw = (Switch4A) mat;
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.droidquest.materials;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ public class SwitchA extends Switch {
|
|||||||
// something like that.
|
// something like that.
|
||||||
|
|
||||||
public SwitchA() {
|
public SwitchA() {
|
||||||
super(Switch.ROT_LEFT);
|
super(ROT_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
@ -1,8 +1,9 @@
|
|||||||
package com.droidquest.materials;
|
package com.droidquest.materials.switches;
|
||||||
|
|
||||||
import com.droidquest.decorations.TextBox;
|
import com.droidquest.decorations.TextBox;
|
||||||
import com.droidquest.items.Item;
|
import com.droidquest.items.Item;
|
||||||
import com.droidquest.items.Sentry;
|
import com.droidquest.items.Sentry;
|
||||||
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
public class SwitchB extends Switch {
|
public class SwitchB extends Switch {
|
||||||
private transient SwitchA switchA = null;
|
private transient SwitchA switchA = null;
|
||||||
@ -10,7 +11,7 @@ public class SwitchB extends Switch {
|
|||||||
private transient Sentry sentry = null;
|
private transient Sentry sentry = null;
|
||||||
|
|
||||||
public SwitchB() {
|
public SwitchB() {
|
||||||
super(Switch.ROT_LEFT);
|
super(ROT_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TouchedByItem(Item item) {
|
public void TouchedByItem(Item item) {
|
180
src/com/droidquest/pathfinder/Node.java
Normal file
180
src/com/droidquest/pathfinder/Node.java
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
package com.droidquest.pathfinder;
|
||||||
|
|
||||||
|
import com.droidquest.materials.Material;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a Node in a path, using the A* algorithm.
|
||||||
|
*/
|
||||||
|
public class Node implements Cloneable {
|
||||||
|
private int x = -1;
|
||||||
|
private int y = -1;
|
||||||
|
private Material material = null;
|
||||||
|
|
||||||
|
private int gCost = 0;
|
||||||
|
private int hCost = 0;
|
||||||
|
private int fCost = 0;
|
||||||
|
|
||||||
|
Node parent = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new, empty Node
|
||||||
|
*/
|
||||||
|
public Node() {
|
||||||
|
this(-1, -1, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Node with given coordinates
|
||||||
|
* @param x Coordinate (of the room array, not screen location)
|
||||||
|
* @param y Coordinate (of the room array, not screen location)
|
||||||
|
*/
|
||||||
|
public Node(int x, int y) {
|
||||||
|
this(x, y, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new room Node with the given coordinates and material
|
||||||
|
* @param x Coordinate (of the room array, not screen location)
|
||||||
|
* @param y Coordinate (of the room array, not screen location)
|
||||||
|
* @param mat Material at the Node location
|
||||||
|
*/
|
||||||
|
public Node(int x, int y, Material mat) {
|
||||||
|
this(x, y, null, mat);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new room Node with a given parent Node (for pathing)
|
||||||
|
* @param x Coordinate (of the room array, not screen location)
|
||||||
|
* @param y Coordinate (of the room array, not screen location)
|
||||||
|
* @param parent current Parent node
|
||||||
|
*/
|
||||||
|
public Node(int x, int y, Node parent) {
|
||||||
|
this(x, y, parent, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new room node with a given parent Node and Material
|
||||||
|
* @param x Coordinate (of the room array, not screen location)
|
||||||
|
* @param y Coordinate (of the room array, not screen location)
|
||||||
|
* @param parent current Parent node
|
||||||
|
* @param mat Material at the Node location
|
||||||
|
*/
|
||||||
|
public Node(int x, int y, Node parent, Material mat) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.parent = parent;
|
||||||
|
this.material = mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the current room location X coordinate (room array, not screen location)
|
||||||
|
* @return X coordinate
|
||||||
|
*/
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current room location X coordinate (room array, not screen location)
|
||||||
|
* @param x coordinate
|
||||||
|
*/
|
||||||
|
public void setX(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the current room location Y coordinate (room array, not screen location)
|
||||||
|
* @return y coordinate
|
||||||
|
*/
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current room Y coordinate (room array, not screen location)
|
||||||
|
* @param y coordinate
|
||||||
|
*/
|
||||||
|
public void setY(int y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the currently set Material for the Node (or null if not set)
|
||||||
|
* @return Material
|
||||||
|
* @see com.droidquest.materials.Material
|
||||||
|
*/
|
||||||
|
public Material getMaterial() {
|
||||||
|
return material;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Material for the Node
|
||||||
|
* @param material Material
|
||||||
|
* @see com.droidquest.materials.Material
|
||||||
|
*/
|
||||||
|
public void setMaterial(Material material) {
|
||||||
|
this.material = material;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a clone of this Node
|
||||||
|
* @return Node clone (as Object)
|
||||||
|
*/
|
||||||
|
public Object clone() {
|
||||||
|
try {
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
catch(CloneNotSupportedException cnse) {
|
||||||
|
System.out.println("Clone not supported!");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the gCost (cost to the current Node from the origin)
|
||||||
|
* @return int cost
|
||||||
|
*/
|
||||||
|
public int getgCost() {
|
||||||
|
return gCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the gCost (cost to the current Node from the origin)
|
||||||
|
* @param gCost int cost
|
||||||
|
*/
|
||||||
|
public void setgCost(int gCost) {
|
||||||
|
this.gCost = gCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the hCost (heuristic cost) from the current Node to the end Node
|
||||||
|
* @return int cost
|
||||||
|
*/
|
||||||
|
public int gethCost() {
|
||||||
|
return hCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the hCost (heuristic cost) from the current Node to the end Node
|
||||||
|
* @param hCost int cost
|
||||||
|
*/
|
||||||
|
public void sethCost(int hCost) {
|
||||||
|
this.hCost = hCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the fCost (full cost) for the Node
|
||||||
|
* @return int cost
|
||||||
|
*/
|
||||||
|
public int getfCost() {
|
||||||
|
return fCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the fCost (full cost) for the Node
|
||||||
|
* @param fCost int cost
|
||||||
|
*/
|
||||||
|
public void setfCost(int fCost) {
|
||||||
|
this.fCost = fCost;
|
||||||
|
}
|
||||||
|
}
|
178
src/com/droidquest/pathfinder/Pathfinder.java
Normal file
178
src/com/droidquest/pathfinder/Pathfinder.java
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
package com.droidquest.pathfinder;
|
||||||
|
|
||||||
|
import com.droidquest.Room;
|
||||||
|
import com.droidquest.items.Item;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a path between two places on the map, using the A* algorithm
|
||||||
|
*/
|
||||||
|
public class Pathfinder {
|
||||||
|
private ArrayList<ArrayList<Node>> nodeList = new ArrayList<ArrayList<Node>>();
|
||||||
|
|
||||||
|
private ArrayList<Node> openList = new ArrayList<Node>();
|
||||||
|
|
||||||
|
private ArrayList<Node> closedList = new ArrayList<Node>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begin a search for a path. Returns an empty ArrayList if no path found.
|
||||||
|
* @param startX int starting X coordinate (not screen location)
|
||||||
|
* @param startY int starting Y coordinate (not screen location)
|
||||||
|
* @param endX int ending X coordinate (not screen location)
|
||||||
|
* @param endY int ending Y coordinate (not screen location)
|
||||||
|
* @param player Item player used to verify whether a Node is passable.
|
||||||
|
* @return ArrayList of Nodes representing the path, or empty list
|
||||||
|
*/
|
||||||
|
public ArrayList<Node> search(int startX, int startY, int endX, int endY, Item player) {
|
||||||
|
ArrayList<Node> results = new ArrayList<Node>();
|
||||||
|
if(startY < 0 || startY > nodeList.size() || startX < 0 || startX > nodeList.get(startY).size()) {
|
||||||
|
System.out.println("Starting node outside of room bounds!");
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(endY < 0 || endY > nodeList.size() || endX < 0 || endX > nodeList.get(endY).size()) {
|
||||||
|
System.out.println("Ending node outside of room bounds!");
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!nodeList.get(startY).get(startX).getMaterial().Passable(player)) {
|
||||||
|
System.out.println("Starting node isn't passable!");
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!nodeList.get(endY).get(endX).getMaterial().Passable(player)) {
|
||||||
|
System.out.println("Ending node isn't passable!");
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node endNode = nodeList.get(endY).get(endX);
|
||||||
|
|
||||||
|
// Add nodes adjacent to starting node
|
||||||
|
Node startNode = nodeList.get(startY).get(startX);
|
||||||
|
startNode.setgCost(0);
|
||||||
|
startNode.sethCost(manhattanDistance(startNode, endNode));
|
||||||
|
startNode.setfCost(startNode.gethCost());
|
||||||
|
startNode.parent = null;
|
||||||
|
openList.add(startNode);
|
||||||
|
|
||||||
|
findLowestCost(results, endNode, player);
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void findLowestCost(ArrayList<Node> results, Node endNode, Item player) {
|
||||||
|
|
||||||
|
while(openList.size() > 0) {
|
||||||
|
Node lowestCost = null;
|
||||||
|
for (Node node : openList) {
|
||||||
|
if (lowestCost == null || node.getfCost() < lowestCost.getfCost()) {
|
||||||
|
lowestCost = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
openList.remove(lowestCost);
|
||||||
|
closedList.add(lowestCost);
|
||||||
|
|
||||||
|
if (lowestCost != null && lowestCost == endNode) {
|
||||||
|
Node pathNode = lowestCost;
|
||||||
|
while (pathNode.parent != null) {
|
||||||
|
results.add(new Node(pathNode.getX() * 28, pathNode.getY() * 32));
|
||||||
|
pathNode = pathNode.parent;
|
||||||
|
}
|
||||||
|
Collections.reverse(results);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
addSurroundingNodes(lowestCost, endNode, player);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Could not find any nodes to process, no path.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void addSurroundingNodes(Node parent, Node endNode, Item player) {
|
||||||
|
if(parent == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int x = parent.getX();
|
||||||
|
int y = parent.getY();
|
||||||
|
for(int deltaY = -1; deltaY <= 1; deltaY++) {
|
||||||
|
for(int deltaX = -1; deltaX <=1; deltaX++) {
|
||||||
|
if(deltaX != 0 || deltaY != 0) {
|
||||||
|
int checkX = x + deltaX;
|
||||||
|
int checkY = y + deltaY;
|
||||||
|
if(checkY >= 0 && checkY < nodeList.size() && checkX >= 0 && checkX < nodeList.get(checkY).size() && nodeList.get(checkY).get(checkX).getMaterial().Passable(player)) {
|
||||||
|
Node currentNode = nodeList.get(checkY).get(checkX);
|
||||||
|
if(closedList.contains(currentNode)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int diagonalCost = 0;
|
||||||
|
if(deltaX != 0 && deltaY != 0) {
|
||||||
|
|
||||||
|
// Diagonal, check to make sure other squares are passable
|
||||||
|
if(!nodeList.get(y).get(checkX).getMaterial().Passable(player) || !nodeList.get(checkY).get(x).getMaterial().Passable(player)) {
|
||||||
|
diagonalCost = 500; // don't make impassible, just really expensive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(openList.contains(currentNode)) {
|
||||||
|
// Set gCost (10 for orthoginal, 14 for diagonal)
|
||||||
|
int newgCost = parent.getgCost() + ((deltaX == 0 || deltaY == 0) ? 10 : 14) + diagonalCost;
|
||||||
|
if(newgCost < currentNode.getgCost()) {
|
||||||
|
currentNode.parent = parent;
|
||||||
|
currentNode.setgCost(newgCost);
|
||||||
|
currentNode.setfCost(newgCost + manhattanDistance(currentNode, endNode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
openList.add(currentNode);
|
||||||
|
currentNode.parent = parent;
|
||||||
|
|
||||||
|
// Set gCost (10 for orthoginal, 14 for diagonal)
|
||||||
|
currentNode.setgCost(parent.getgCost() + ((deltaX == 0 || deltaY == 0) ? 10 : 14) + diagonalCost);
|
||||||
|
currentNode.sethCost(manhattanDistance(currentNode, endNode));
|
||||||
|
currentNode.setfCost(currentNode.getgCost() + currentNode.gethCost());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Pathfinder object and initialize with a Room
|
||||||
|
* @param room Room for path-finding.
|
||||||
|
* @see com.droidquest.Room
|
||||||
|
*/
|
||||||
|
public Pathfinder(Room room) {
|
||||||
|
// Generate node array for room
|
||||||
|
generate(room);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the Node list for a Room. Generally should use new Pathfinder(room) instead of this method
|
||||||
|
* @param room Room for path-finding
|
||||||
|
*/
|
||||||
|
public void generate(Room room) {
|
||||||
|
for(int y = 0; y < room.RoomArray.length;y++) {
|
||||||
|
nodeList.add(new ArrayList<Node>());
|
||||||
|
for(int x=0;x<room.RoomArray[y].length;x++) {
|
||||||
|
nodeList.get(y).add(new Node(x, y, Room.level.materialAt(x, y, room)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the Manhattan distance as the heuristic
|
||||||
|
private int manhattanDistance(Node current, Node end) {
|
||||||
|
return 10 * Math.abs(end.getX()-current.getX()) + Math.abs(end.getY()-current.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user