From a8e68af6dbb714c50bbe71cae2c5c62f3f601028 Mon Sep 17 00:00:00 2001 From: cognitivegears Date: Mon, 21 Apr 2014 21:44:36 -0500 Subject: [PATCH 1/4] Initial version of pathfinding --- src/com/droidquest/avatars/GameCursor.java | 111 ++++------- src/com/droidquest/avatars/PaintBrush.java | 29 ++- src/com/droidquest/avatars/SolderingPen.java | 67 +++---- src/com/droidquest/items/Item.java | 81 ++++++-- src/com/droidquest/materials/Material.java | 5 + src/com/droidquest/pathfinder/Node.java | 180 ++++++++++++++++++ src/com/droidquest/pathfinder/Pathfinder.java | 178 +++++++++++++++++ 7 files changed, 524 insertions(+), 127 deletions(-) create mode 100644 src/com/droidquest/pathfinder/Node.java create mode 100644 src/com/droidquest/pathfinder/Pathfinder.java diff --git a/src/com/droidquest/avatars/GameCursor.java b/src/com/droidquest/avatars/GameCursor.java index 4ee9fa3..54d3568 100644 --- a/src/com/droidquest/avatars/GameCursor.java +++ b/src/com/droidquest/avatars/GameCursor.java @@ -3,6 +3,7 @@ package com.droidquest.avatars; import com.droidquest.Room; import com.droidquest.RoomDisplay; import com.droidquest.items.*; +import com.droidquest.pathfinder.Node; import javax.swing.*; import java.awt.*; @@ -324,81 +325,51 @@ public class GameCursor extends Player { return level.cheatmode; } - public void Animate() { - if (automove == 1 && room == null) { - automove = 0; - } - 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; - if (dx == 0) { - if (dy < 0) { - currentIcon = icons[0 + walk].getImage(); - } - else { - currentIcon = icons[2 + walk].getImage(); - } + + @Override + protected void animateCharacter(int dx, int dy) { + walk = 1 - walk; + if (dx == 0) { + if (dy < 0) { + currentIcon = icons[0 + walk].getImage(); } else { - if (dx < 0) { - currentIcon = icons[4 + walk].getImage(); - } - else { - currentIcon = icons[6 + walk].getImage(); - } - } - if (dx > 0) { - moveRight(dx); - } - if (dx < 0) { - moveLeft(-dx); - } - if (dy > 0) { - moveDown(dy); - } - if (dy < 0) { - moveUp(-dy); - } - } - if (automove == 2) { - walk = 1 - walk; - if (autoX > 0) { - currentIcon = icons[6 + walk].getImage(); - moveRight(autoX); - } - - if (autoX < 0) { - currentIcon = icons[4 + walk].getImage(); - moveLeft(-autoX); - } - - if (autoY > 0) { currentIcon = icons[2 + walk].getImage(); - moveDown(autoY); - } - - if (autoY < 0) { - currentIcon = icons[0 + walk].getImage(); - moveUp(-autoY); } } + else { + if (dx < 0) { + currentIcon = icons[4 + walk].getImage(); + } + else { + currentIcon = icons[6 + walk].getImage(); + } + } + } + + @Override + protected void autoMoveFull() { + walk = 1 - walk; + if (autoX > 0) { + currentIcon = icons[6 + walk].getImage(); + moveRight(autoX); + } + + if (autoX < 0) { + currentIcon = icons[4 + walk].getImage(); + moveLeft(-autoX); + } + + if (autoY > 0) { + currentIcon = icons[2 + walk].getImage(); + moveDown(autoY); + } + + if (autoY < 0) { + currentIcon = icons[0 + walk].getImage(); + moveUp(-autoY); + } + } public GenericRobot PlayerInRobot(GenericRobot robot) { diff --git a/src/com/droidquest/avatars/PaintBrush.java b/src/com/droidquest/avatars/PaintBrush.java index 16b11c6..6dd7322 100644 --- a/src/com/droidquest/avatars/PaintBrush.java +++ b/src/com/droidquest/avatars/PaintBrush.java @@ -93,8 +93,8 @@ public class PaintBrush extends Player { paintMats[1] = Material.FindSimiliar(new Material(Color.green, false, false)); Item robot = null; - for(Item item : level.items) { - if(item instanceof OrangeRobot) { + for (Item item : level.items) { + if (item instanceof OrangeRobot) { robot = item; } } @@ -104,14 +104,14 @@ public class PaintBrush extends Player { paintMats[2] = Material.FindSimiliar(new RobotBlocker(robot, new Color(255, 128, 0))); for (Item item : level.items) { - if(item instanceof WhiteRobot) { + if (item instanceof WhiteRobot) { robot = item; } } paintMats[3] = Material.FindSimiliar(new RobotBlocker(robot, Color.white)); - for(Item item : level.items) { - if(item instanceof BlueRobot) { + for (Item item : level.items) { + if (item instanceof BlueRobot) { robot = item; } } @@ -210,9 +210,10 @@ public class PaintBrush extends Player { @Override protected boolean handleRepeatSpace() { - return false; + return false; } + @Override public void moveUp(boolean nudge) { int dist = 32; if (nudge) { @@ -231,6 +232,7 @@ public class PaintBrush extends Player { } } + @Override public void moveDown(boolean nudge) { int dist = 32; if (nudge) { @@ -249,6 +251,20 @@ public class PaintBrush extends Player { } } +// @Override +// protected void findPath(int startX, int startY, int endX, int endY) { +// // The paintbrush can go anywhere +//// autoPath = new ArrayList(); +//// autoPath.add(new Node(endX * 28, endY * 32)); +// autoX = endX * 28; +// autoY = endY * 28; +// autoX -= autoX % 2; // Even numbered pixel only! +// autoY -= autoY % 2; +// automove = 1; +// +// } + + @Override public void moveLeft(boolean nudge) { int dist = 28; if (nudge) { @@ -267,6 +283,7 @@ public class PaintBrush extends Player { } } + @Override public void moveRight(boolean nudge) { int dist = 28; if (nudge) { diff --git a/src/com/droidquest/avatars/SolderingPen.java b/src/com/droidquest/avatars/SolderingPen.java index a82d041..f8c1ded 100644 --- a/src/com/droidquest/avatars/SolderingPen.java +++ b/src/com/droidquest/avatars/SolderingPen.java @@ -225,6 +225,7 @@ public class SolderingPen extends Device implements Avatar { CheckPort(); } + @Override public void Animate() { Room tempRoom = room; super.Animate(); @@ -297,16 +298,16 @@ public class SolderingPen extends Device implements Avatar { public boolean KeyUp(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_C && handleGameCursor()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_R && handleRadio()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_P && handlePaintbrush()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_SLASH && handleHelp()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { if (carriedBy == null) { @@ -340,13 +341,13 @@ public class SolderingPen extends Device implements Avatar { WirePort(); } else if (e.getKeyCode() == KeyEvent.VK_F && handleFlipDevice()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_E && handleEnterRoom()) { - return false; + return false; } else if (e.getKeyCode() == KeyEvent.VK_X && handleExitRoom()) { - return false; + return false; } return false; } @@ -592,32 +593,32 @@ public class SolderingPen extends Device implements Avatar { @Override public boolean handleFlipDevice() { if (hot) { - if (ports[0].myWire != null) // If SP is wired - { - // Flip wire attached to SP - Port tempPort = ports[0].myWire.fromPort; - ports[0].myWire.fromPort = ports[0].myWire.toPort; - ports[0].myWire.toPort = tempPort; - } - else if (ports[0].myWire == null) // If SP is not wired - { - // Flip wire attached to CurrentPort - if (currentPort.myWire != null) { - Port tempPort = currentPort.myWire.fromPort; - currentPort.myWire.fromPort = currentPort.myWire.toPort; - currentPort.myWire.toPort = tempPort; - } - } - } - else { - if (ports[0].myWire != null) // If SP is wired - { - // Flip wire attached to SP - Port tempPort = ports[0].myWire.fromPort; - ports[0].myWire.fromPort = ports[0].myWire.toPort; - ports[0].myWire.toPort = tempPort; - } - } + if (ports[0].myWire != null) // If SP is wired + { + // Flip wire attached to SP + Port tempPort = ports[0].myWire.fromPort; + ports[0].myWire.fromPort = ports[0].myWire.toPort; + ports[0].myWire.toPort = tempPort; + } + else if (ports[0].myWire == null) // If SP is not wired + { + // Flip wire attached to CurrentPort + if (currentPort.myWire != null) { + Port tempPort = currentPort.myWire.fromPort; + currentPort.myWire.fromPort = currentPort.myWire.toPort; + currentPort.myWire.toPort = tempPort; + } + } + } + else { + if (ports[0].myWire != null) // If SP is wired + { + // Flip wire attached to SP + Port tempPort = ports[0].myWire.fromPort; + ports[0].myWire.fromPort = ports[0].myWire.toPort; + ports[0].myWire.toPort = tempPort; + } + } return true; } } diff --git a/src/com/droidquest/items/Item.java b/src/com/droidquest/items/Item.java index 1eee5af..ce54d4a 100644 --- a/src/com/droidquest/items/Item.java +++ b/src/com/droidquest/items/Item.java @@ -5,6 +5,8 @@ import com.droidquest.Wire; import com.droidquest.devices.Device; import com.droidquest.levels.Level; import com.droidquest.materials.ChipTrash; +import com.droidquest.pathfinder.Node; +import com.droidquest.pathfinder.Pathfinder; import javax.swing.*; import java.awt.*; @@ -15,6 +17,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.ArrayList; public class Item implements Serializable, Cloneable { 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 autoY; // Destination of automovement public Room InternalRoom = null; // Room inside this item, if any. + public transient ArrayList autoPath = new ArrayList(); protected int repeating = 0; // Keyboard repeat. public int charge = 0; // Battery Charge of this item, if any. @@ -225,6 +229,11 @@ public class Item implements Serializable, Cloneable { 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); + } + public void MouseClick(MouseEvent e) { int button = 0; if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { @@ -236,11 +245,23 @@ public class Item implements Serializable, Cloneable { if (button == 1) { if (e.getClickCount() == 1) { - autoX = e.getX() - width / 2; - autoY = e.getY() - height / 2; - autoX -= autoX % 2; // Even numbered pixel only! - autoY -= autoY % 2; - automove = 1; + int endX = e.getX() / 28; + int endY = e.getY() / 32; + + int startX = x / 28; + int startY = y / 32; + + findPath(startX, startY, endX, endY); + + if(autoPath != null && autoPath.size() > 0) { + Node next = autoPath.remove(0); + + autoX = next.getX(); + autoY = next.getY(); + autoX -= autoX % 2; // Even numbered pixel only! + autoY -= autoY % 2; + automove = 1; + } } else if (e.getClickCount() == 2) { int dx = e.getX() - width / 2 - x; @@ -436,6 +457,10 @@ public class Item implements Serializable, Cloneable { moveRight(dist); } + protected void animateCharacter(int dx, int dy) { + + } + public void Animate() { if (automove == 1 && room == null) { automove = 0; @@ -443,8 +468,21 @@ public class Item implements Serializable, Cloneable { if (automove == 1) { int dx = autoX - x; int dy = autoY - y; + if (dx == 0 && dy == 0) { - automove = 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; + return; + } } if (dx < -28) { dx = -28; @@ -458,6 +496,9 @@ public class Item implements Serializable, Cloneable { if (dy > 32) { dy = 32; } + + animateCharacter(dx, dy); + if (dx > 0) { moveRight(dx); } @@ -472,18 +513,22 @@ public class Item implements Serializable, Cloneable { } } if (automove == 2) { - if (autoX > 0) { - moveRight(autoX); - } - if (autoX < 0) { - moveLeft(-autoX); - } - if (autoY > 0) { - moveDown(autoY); - } - if (autoY < 0) { - moveUp(-autoY); - } + autoMoveFull(); + } + } + + protected void autoMoveFull() { + if (autoX > 0) { + moveRight(autoX); + } + if (autoX < 0) { + moveLeft(-autoX); + } + if (autoY > 0) { + moveDown(autoY); + } + if (autoY < 0) { + moveUp(-autoY); } } diff --git a/src/com/droidquest/materials/Material.java b/src/com/droidquest/materials/Material.java index 00266ef..3f4a55a 100644 --- a/src/com/droidquest/materials/Material.java +++ b/src/com/droidquest/materials/Material.java @@ -1,6 +1,7 @@ package com.droidquest.materials; import com.droidquest.RoomDisplay; +import com.droidquest.avatars.PaintBrush; import com.droidquest.items.Item; import com.droidquest.levels.Level; @@ -62,6 +63,10 @@ public class Material implements Serializable, Cloneable { } public boolean Passable(Item item) { + // The PaintBrush can pass anything + if(item instanceof PaintBrush) { + return true; + } return passable; } diff --git a/src/com/droidquest/pathfinder/Node.java b/src/com/droidquest/pathfinder/Node.java new file mode 100644 index 0000000..c305ef6 --- /dev/null +++ b/src/com/droidquest/pathfinder/Node.java @@ -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; + } +} diff --git a/src/com/droidquest/pathfinder/Pathfinder.java b/src/com/droidquest/pathfinder/Pathfinder.java new file mode 100644 index 0000000..1f621ce --- /dev/null +++ b/src/com/droidquest/pathfinder/Pathfinder.java @@ -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> nodeList = new ArrayList>(); + + private ArrayList openList = new ArrayList(); + + private ArrayList closedList = new ArrayList(); + + /** + * 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 search(int startX, int startY, int endX, int endY, Item player) { + ArrayList results = new ArrayList(); + 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 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()); + for(int x=0;x Date: Mon, 21 Apr 2014 22:48:39 -0500 Subject: [PATCH 2/4] Initial version of pathfinding - fixed SolderPen --- src/com/droidquest/avatars/SolderingPen.java | 57 ++++++-------------- src/com/droidquest/items/Item.java | 19 ++++++- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/com/droidquest/avatars/SolderingPen.java b/src/com/droidquest/avatars/SolderingPen.java index f8c1ded..6d9b286 100644 --- a/src/com/droidquest/avatars/SolderingPen.java +++ b/src/com/droidquest/avatars/SolderingPen.java @@ -5,6 +5,7 @@ import com.droidquest.Wire; import com.droidquest.chipstuff.Port; import com.droidquest.devices.Device; import com.droidquest.items.Item; +import com.droidquest.pathfinder.Node; import javax.swing.*; import java.awt.*; @@ -12,6 +13,7 @@ import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; +import java.util.ArrayList; public class SolderingPen extends Device implements Avatar { private boolean hot; @@ -388,50 +390,23 @@ public class SolderingPen extends Device implements Avatar { return false; } - public void MouseClick(MouseEvent e) { - int button = 0; - if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { - button = 1; - } - if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { - button = 3; - } + @Override + protected int getWidthModifier() { + return 2; + } - if (button == 1) { - if (e.getClickCount() == 1) { - autoX = e.getX() - 2; - 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; - } - } - } + @Override + protected int getHeightModifier() { + return 20; + } - 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 diff --git a/src/com/droidquest/items/Item.java b/src/com/droidquest/items/Item.java index ce54d4a..c9ed8af 100644 --- a/src/com/droidquest/items/Item.java +++ b/src/com/droidquest/items/Item.java @@ -234,6 +234,19 @@ public class Item implements Serializable, Cloneable { 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) { int button = 0; if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { @@ -254,6 +267,8 @@ public class Item implements Serializable, Cloneable { findPath(startX, startY, endX, endY); if(autoPath != null && autoPath.size() > 0) { + setFinePositioning(e); + Node next = autoPath.remove(0); autoX = next.getX(); @@ -264,8 +279,8 @@ public class Item implements Serializable, Cloneable { } } else if (e.getClickCount() == 2) { - int dx = e.getX() - width / 2 - x; - int dy = e.getY() - height / 2 - y; + int dx = e.getX() - getWidthModifier() - x; + int dy = e.getY() - getHeightModifier() - y; if (Math.abs(dx) > Math.abs(dy)) { autoY = 0; autoX = 28; From 97bc20059cbbcbafaa8b3316ed51077b578c59c0 Mon Sep 17 00:00:00 2001 From: cognitivegears Date: Mon, 21 Apr 2014 22:52:43 -0500 Subject: [PATCH 3/4] Removed commented code --- src/com/droidquest/avatars/PaintBrush.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/com/droidquest/avatars/PaintBrush.java b/src/com/droidquest/avatars/PaintBrush.java index 6dd7322..f78325f 100644 --- a/src/com/droidquest/avatars/PaintBrush.java +++ b/src/com/droidquest/avatars/PaintBrush.java @@ -251,19 +251,6 @@ public class PaintBrush extends Player { } } -// @Override -// protected void findPath(int startX, int startY, int endX, int endY) { -// // The paintbrush can go anywhere -//// autoPath = new ArrayList(); -//// autoPath.add(new Node(endX * 28, endY * 32)); -// autoX = endX * 28; -// autoY = endY * 28; -// autoX -= autoX % 2; // Even numbered pixel only! -// autoY -= autoY % 2; -// automove = 1; -// -// } - @Override public void moveLeft(boolean nudge) { int dist = 28; From e3c76fa9d353dc5d52e6b321af22592186959f42 Mon Sep 17 00:00:00 2001 From: cognitivegears Date: Mon, 21 Apr 2014 23:21:21 -0500 Subject: [PATCH 4/4] Moved switches to their own package inside materials --- src/com/droidquest/levels/RO2.java | 2 ++ src/com/droidquest/levels/RO3.java | 3 +++ src/com/droidquest/levels/RO4.java | 4 ++++ src/com/droidquest/levels/RO5.java | 1 + src/com/droidquest/levels/RO6.java | 1 + src/com/droidquest/levels/ROLab.java | 1 + src/com/droidquest/levels/ROTut3.java | 1 + src/com/droidquest/levels/ROTutF.java | 2 +- .../{items => materials/switches}/ElevatorSwitch.java | 7 ++++--- .../{items => materials/switches}/MazeLock.java | 11 ++++++----- .../droidquest/materials/{ => switches}/Switch.java | 3 ++- .../droidquest/materials/{ => switches}/Switch1.java | 7 ++++--- .../{items => materials/switches}/Switch4A.java | 4 ++-- .../{items => materials/switches}/Switch4B.java | 8 ++++---- .../{items => materials/switches}/Switch4C.java | 8 ++++---- .../{items => materials/switches}/Switch4D.java | 8 ++++---- .../droidquest/materials/{ => switches}/SwitchA.java | 4 ++-- .../droidquest/materials/{ => switches}/SwitchB.java | 5 +++-- 18 files changed, 49 insertions(+), 31 deletions(-) rename src/com/droidquest/{items => materials/switches}/ElevatorSwitch.java (91%) rename src/com/droidquest/{items => materials/switches}/MazeLock.java (57%) rename src/com/droidquest/materials/{ => switches}/Switch.java (98%) rename src/com/droidquest/materials/{ => switches}/Switch1.java (74%) rename src/com/droidquest/{items => materials/switches}/Switch4A.java (98%) rename src/com/droidquest/{items => materials/switches}/Switch4B.java (70%) rename src/com/droidquest/{items => materials/switches}/Switch4C.java (69%) rename src/com/droidquest/{items => materials/switches}/Switch4D.java (70%) rename src/com/droidquest/materials/{ => switches}/SwitchA.java (89%) rename src/com/droidquest/materials/{ => switches}/SwitchB.java (93%) diff --git a/src/com/droidquest/levels/RO2.java b/src/com/droidquest/levels/RO2.java index 398d43b..b31c8cb 100644 --- a/src/com/droidquest/levels/RO2.java +++ b/src/com/droidquest/levels/RO2.java @@ -12,6 +12,8 @@ import com.droidquest.devices.ContactSensor; import com.droidquest.devices.SmallChip; import com.droidquest.items.*; import com.droidquest.materials.*; +import com.droidquest.materials.switches.Switch; +import com.droidquest.materials.switches.Switch1; import java.awt.*; diff --git a/src/com/droidquest/levels/RO3.java b/src/com/droidquest/levels/RO3.java index 4dc581c..f695ecf 100644 --- a/src/com/droidquest/levels/RO3.java +++ b/src/com/droidquest/levels/RO3.java @@ -15,6 +15,9 @@ import com.droidquest.devices.StormShield; import com.droidquest.items.Button; import com.droidquest.items.*; 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.*; diff --git a/src/com/droidquest/levels/RO4.java b/src/com/droidquest/levels/RO4.java index 5549fe8..f3baf7e 100644 --- a/src/com/droidquest/levels/RO4.java +++ b/src/com/droidquest/levels/RO4.java @@ -10,6 +10,10 @@ import com.droidquest.decorations.Graphix; import com.droidquest.devices.SmallChip; import com.droidquest.items.*; 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.*; diff --git a/src/com/droidquest/levels/RO5.java b/src/com/droidquest/levels/RO5.java index 64f10a4..5a2510d 100644 --- a/src/com/droidquest/levels/RO5.java +++ b/src/com/droidquest/levels/RO5.java @@ -10,6 +10,7 @@ import com.droidquest.decorations.Arrow; import com.droidquest.decorations.Graphix; import com.droidquest.items.*; import com.droidquest.materials.*; +import com.droidquest.materials.switches.Switch; import java.awt.*; diff --git a/src/com/droidquest/levels/RO6.java b/src/com/droidquest/levels/RO6.java index a10c45d..cc299b6 100644 --- a/src/com/droidquest/levels/RO6.java +++ b/src/com/droidquest/levels/RO6.java @@ -11,6 +11,7 @@ import com.droidquest.decorations.Graphix; import com.droidquest.devices.DirectionalSensor; import com.droidquest.items.*; import com.droidquest.materials.*; +import com.droidquest.materials.switches.Switch; import java.awt.*; diff --git a/src/com/droidquest/levels/ROLab.java b/src/com/droidquest/levels/ROLab.java index 8b55468..4f4e3e3 100644 --- a/src/com/droidquest/levels/ROLab.java +++ b/src/com/droidquest/levels/ROLab.java @@ -8,6 +8,7 @@ import com.droidquest.decorations.Arrow; import com.droidquest.devices.*; import com.droidquest.items.*; import com.droidquest.materials.*; +import com.droidquest.materials.switches.MazeLock; import java.awt.*; diff --git a/src/com/droidquest/levels/ROTut3.java b/src/com/droidquest/levels/ROTut3.java index 69bee15..9573c8b 100644 --- a/src/com/droidquest/levels/ROTut3.java +++ b/src/com/droidquest/levels/ROTut3.java @@ -9,6 +9,7 @@ import com.droidquest.decorations.Arrow; import com.droidquest.devices.*; import com.droidquest.items.*; import com.droidquest.materials.*; +import com.droidquest.materials.switches.MazeLock; import java.awt.*; diff --git a/src/com/droidquest/levels/ROTutF.java b/src/com/droidquest/levels/ROTutF.java index bc3812b..f398636 100644 --- a/src/com/droidquest/levels/ROTutF.java +++ b/src/com/droidquest/levels/ROTutF.java @@ -11,7 +11,7 @@ import com.droidquest.items.*; import com.droidquest.materials.Material; import com.droidquest.materials.PanicButton; import com.droidquest.materials.Portal; -import com.droidquest.materials.Switch; +import com.droidquest.materials.switches.Switch; import java.awt.*; diff --git a/src/com/droidquest/items/ElevatorSwitch.java b/src/com/droidquest/materials/switches/ElevatorSwitch.java similarity index 91% rename from src/com/droidquest/items/ElevatorSwitch.java rename to src/com/droidquest/materials/switches/ElevatorSwitch.java index 2ca5219..b96325a 100644 --- a/src/com/droidquest/items/ElevatorSwitch.java +++ b/src/com/droidquest/materials/switches/ElevatorSwitch.java @@ -1,9 +1,10 @@ -package com.droidquest.items; +package com.droidquest.materials.switches; import com.droidquest.Room; import com.droidquest.decorations.Arrow; +import com.droidquest.items.Item; import com.droidquest.materials.ElevatorOutPortal; -import com.droidquest.materials.Switch; +import com.droidquest.materials.Material; public class ElevatorSwitch extends Switch { private int animationState = 0; @@ -45,7 +46,7 @@ public class ElevatorSwitch extends Switch { animationState++; break; 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++) { Arrow arrow = room.arrows.elementAt(a); arrow.direction = Arrow.DIR_UP; diff --git a/src/com/droidquest/items/MazeLock.java b/src/com/droidquest/materials/switches/MazeLock.java similarity index 57% rename from src/com/droidquest/items/MazeLock.java rename to src/com/droidquest/materials/switches/MazeLock.java index 0f98f79..c47d922 100644 --- a/src/com/droidquest/items/MazeLock.java +++ b/src/com/droidquest/materials/switches/MazeLock.java @@ -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 { @@ -12,15 +13,15 @@ public class MazeLock extends Switch { public void TouchedByItem(Item item) { if (paintbrush == null) { - paintbrush = level.paintbrush; + paintbrush = Material.level.paintbrush; } if (!value) { - level.paintbrush = null; + Material.level.paintbrush = null; value = true; } else { - level.paintbrush = paintbrush; + Material.level.paintbrush = paintbrush; value = false; } } diff --git a/src/com/droidquest/materials/Switch.java b/src/com/droidquest/materials/switches/Switch.java similarity index 98% rename from src/com/droidquest/materials/Switch.java rename to src/com/droidquest/materials/switches/Switch.java index 50a53e3..8c647dc 100644 --- a/src/com/droidquest/materials/Switch.java +++ b/src/com/droidquest/materials/switches/Switch.java @@ -1,7 +1,8 @@ -package com.droidquest.materials; +package com.droidquest.materials.switches; import com.droidquest.Room; import com.droidquest.items.Item; +import com.droidquest.materials.Material; import javax.swing.*; import java.awt.*; diff --git a/src/com/droidquest/materials/Switch1.java b/src/com/droidquest/materials/switches/Switch1.java similarity index 74% rename from src/com/droidquest/materials/Switch1.java rename to src/com/droidquest/materials/switches/Switch1.java index abb1e0b..7807e29 100644 --- a/src/com/droidquest/materials/Switch1.java +++ b/src/com/droidquest/materials/switches/Switch1.java @@ -1,7 +1,8 @@ -package com.droidquest.materials; +package com.droidquest.materials.switches; import com.droidquest.items.Item; import com.droidquest.items.Train; +import com.droidquest.materials.Material; public class Switch1 extends Switch { private transient Train train; @@ -12,8 +13,8 @@ public class Switch1 extends Switch { public void TouchedByItem(Item item) { if (train == null) { - for (int a = 0; a < level.items.size(); a++) { - Item t = level.items.elementAt(a); + for (int a = 0; a < Material.level.items.size(); a++) { + Item t = Material.level.items.elementAt(a); if (t.getClass().toString().endsWith("Train")) { train = (Train) t; } diff --git a/src/com/droidquest/items/Switch4A.java b/src/com/droidquest/materials/switches/Switch4A.java similarity index 98% rename from src/com/droidquest/items/Switch4A.java rename to src/com/droidquest/materials/switches/Switch4A.java index 686c35d..81d400b 100644 --- a/src/com/droidquest/items/Switch4A.java +++ b/src/com/droidquest/materials/switches/Switch4A.java @@ -1,7 +1,7 @@ -package com.droidquest.items; +package com.droidquest.materials.switches; import com.droidquest.Room; -import com.droidquest.materials.Switch; +import com.droidquest.items.Item; public class Switch4A extends Switch { int count = 0; diff --git a/src/com/droidquest/items/Switch4B.java b/src/com/droidquest/materials/switches/Switch4B.java similarity index 70% rename from src/com/droidquest/items/Switch4B.java rename to src/com/droidquest/materials/switches/Switch4B.java index 5b9e2e0..f8a6acf 100644 --- a/src/com/droidquest/items/Switch4B.java +++ b/src/com/droidquest/materials/switches/Switch4B.java @@ -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.Switch; public class Switch4B extends Switch { private transient Switch4A sw = null; @@ -12,8 +12,8 @@ public class Switch4B extends Switch { public void TouchedByItem(Item item) { if (sw == null) { - for (int a = 0; a < level.materials.size(); a++) { - Material mat = level.materials.elementAt(a); + for (int a = 0; a < Material.level.materials.size(); a++) { + Material mat = Material.level.materials.elementAt(a); if (mat instanceof Switch4A) { sw = (Switch4A) mat; } diff --git a/src/com/droidquest/items/Switch4C.java b/src/com/droidquest/materials/switches/Switch4C.java similarity index 69% rename from src/com/droidquest/items/Switch4C.java rename to src/com/droidquest/materials/switches/Switch4C.java index 4c0d222..b2efb6b 100644 --- a/src/com/droidquest/items/Switch4C.java +++ b/src/com/droidquest/materials/switches/Switch4C.java @@ -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.Switch; public class Switch4C extends Switch { private transient Switch4A sw = null; @@ -12,8 +12,8 @@ public class Switch4C extends Switch { public void TouchedByItem(Item item) { if (sw == null) { - for (int a = 0; a < level.materials.size(); a++) { - Material mat = level.materials.elementAt(a); + for (int a = 0; a < Material.level.materials.size(); a++) { + Material mat = Material.level.materials.elementAt(a); if (mat instanceof Switch4A) { sw = (Switch4A) mat; } diff --git a/src/com/droidquest/items/Switch4D.java b/src/com/droidquest/materials/switches/Switch4D.java similarity index 70% rename from src/com/droidquest/items/Switch4D.java rename to src/com/droidquest/materials/switches/Switch4D.java index 4d64f40..4f60bf4 100644 --- a/src/com/droidquest/items/Switch4D.java +++ b/src/com/droidquest/materials/switches/Switch4D.java @@ -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.Switch; public class Switch4D extends Switch { private transient Switch4A sw = null; @@ -12,8 +12,8 @@ public class Switch4D extends Switch { public void TouchedByItem(Item item) { if (sw == null) { - for (int a = 0; a < level.materials.size(); a++) { - Material mat = level.materials.elementAt(a); + for (int a = 0; a < Material.level.materials.size(); a++) { + Material mat = Material.level.materials.elementAt(a); if (mat instanceof Switch4A) { sw = (Switch4A) mat; } diff --git a/src/com/droidquest/materials/SwitchA.java b/src/com/droidquest/materials/switches/SwitchA.java similarity index 89% rename from src/com/droidquest/materials/SwitchA.java rename to src/com/droidquest/materials/switches/SwitchA.java index 99243ea..7c8fddd 100644 --- a/src/com/droidquest/materials/SwitchA.java +++ b/src/com/droidquest/materials/switches/SwitchA.java @@ -1,4 +1,4 @@ -package com.droidquest.materials; +package com.droidquest.materials.switches; import com.droidquest.items.Item; @@ -11,7 +11,7 @@ public class SwitchA extends Switch { // something like that. public SwitchA() { - super(Switch.ROT_LEFT); + super(ROT_LEFT); } public void TouchedByItem(Item item) { diff --git a/src/com/droidquest/materials/SwitchB.java b/src/com/droidquest/materials/switches/SwitchB.java similarity index 93% rename from src/com/droidquest/materials/SwitchB.java rename to src/com/droidquest/materials/switches/SwitchB.java index 9529b81..28cc052 100644 --- a/src/com/droidquest/materials/SwitchB.java +++ b/src/com/droidquest/materials/switches/SwitchB.java @@ -1,8 +1,9 @@ -package com.droidquest.materials; +package com.droidquest.materials.switches; import com.droidquest.decorations.TextBox; import com.droidquest.items.Item; import com.droidquest.items.Sentry; +import com.droidquest.materials.Material; public class SwitchB extends Switch { private transient SwitchA switchA = null; @@ -10,7 +11,7 @@ public class SwitchB extends Switch { private transient Sentry sentry = null; public SwitchB() { - super(Switch.ROT_LEFT); + super(ROT_LEFT); } public void TouchedByItem(Item item) {