From 2644f5a2dc8b58d92227aec3fda718cc7d1199ae Mon Sep 17 00:00:00 2001 From: cognitivegears Date: Mon, 21 Apr 2014 22:48:39 -0500 Subject: [PATCH] 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;