summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java')
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java new file mode 100644 index 0000000..e5334c8 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/AbstractEvent.java
@@ -0,0 +1,122 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.gamestate.mapview.event;
7
8import com.fourisland.fourpuzzle.Direction;
9import com.fourisland.fourpuzzle.gamestate.mapview.Map;
10import com.fourisland.fourpuzzle.util.Functions;
11import java.awt.Point;
12
13/**
14 *
15 * @author hatkirby
16 */
17public abstract class AbstractEvent implements Event {
18
19 private Point location = new Point();
20 public Point getLocation()
21 {
22 return location;
23 }
24 public void setLocation(Point location)
25 {
26 this.location = location;
27 }
28 public void setLocation(int x, int y)
29 {
30 location.setLocation(x, y);
31 }
32
33 private boolean moving = false;
34 public boolean isMoving()
35 {
36 return moving;
37 }
38 public void setMoving(boolean moving)
39 {
40 this.moving = moving;
41 }
42
43 /* TODO Remove the moveDirection field. As direction itself is
44 * always the same as moveDirection when moveDirection is needed,
45 * it will do fine without having to complicated access modifiers
46 */
47
48 protected int moveTimer;
49 protected Direction moveDirection;
50 public void startMoving(Direction toMove)
51 {
52 setDirection(toMove);
53
54 if (!getParentMap().checkForCollision(this, toMove))
55 {
56 setAnimationStep(2);
57 moveTimer = 4;
58 setMoving(true);
59 moveDirection = toMove;
60 }
61 }
62
63 public void processMoving()
64 {
65 if (isMoving())
66 {
67 moveTimer--;
68 if (moveTimer == 2)
69 {
70 setAnimationStep(0);
71 } else if (moveTimer == 0)
72 {
73 setAnimationStep(1);
74 moving = false;
75
76 if (moveDirection == Direction.North)
77 {
78 setLocation(getLocation().x,getLocation().y-1);
79 } else if (moveDirection == Direction.West)
80 {
81 setLocation(getLocation().x-1,getLocation().y);
82 } else if (moveDirection == Direction.South)
83 {
84 setLocation(getLocation().x,getLocation().y+1);
85 } else if (moveDirection == Direction.East)
86 {
87 setLocation(getLocation().x+1,getLocation().y);
88 }
89 }
90 }
91 }
92
93 public boolean isOccupyingSpace(int x, int y)
94 {
95 if (getLocation().equals(new Point(x,y)))
96 {
97 return true;
98 }
99
100 if (Functions.isMovingTo(this, x, y))
101 {
102 return true;
103 }
104
105 return false;
106 }
107
108 private Map parentMap = null;
109 public Map getParentMap()
110 {
111 if (parentMap == null)
112 {
113 throw new NullPointerException();
114 }
115
116 return parentMap;
117 }
118 public void setParentMap(Map parentMap)
119 {
120 this.parentMap = parentMap;
121 }
122}