summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-02-07 14:11:21 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-02-07 14:11:21 -0500
commit7125747297343872ca4cc64baa2e6cc3769ab5c3 (patch)
tree848135935c34fdc7fc5770d370746039ffeb2af5
parente8e617ed1465073d59a9e5eea9f0cc6c15058d49 (diff)
downloadfourpuzzle-7125747297343872ca4cc64baa2e6cc3769ab5c3.tar.gz
fourpuzzle-7125747297343872ca4cc64baa2e6cc3769ab5c3.tar.bz2
fourpuzzle-7125747297343872ca4cc64baa2e6cc3769ab5c3.zip
Protected PossibleEvent
Package-privated most functions (except for addPrecondition() which should be used by clients) because clients shouldn't have access to them. Also implemented the Builder pattern so clients can create PossibleEvents, but not modify them, making PossibleEvent effectively immutable (to clients), though it actually isn't because it can be modified by classes within the package.
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java19
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java156
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/Precondition.java4
3 files changed, 124 insertions, 55 deletions
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java index 530af6f..e57d7f8 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java
@@ -10,6 +10,7 @@ import java.awt.Graphics;
10import java.util.ArrayList; 10import java.util.ArrayList;
11import com.fourisland.fourpuzzle.Direction; 11import com.fourisland.fourpuzzle.Direction;
12import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic; 12import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic;
13import com.fourisland.fourpuzzle.gamestate.mapview.event.precondition.Precondition;
13 14
14/** 15/**
15 * 16 *
@@ -50,24 +51,22 @@ public class LayerEvent extends AbstractEvent implements Event {
50 private ArrayList<PossibleEvent> events; 51 private ArrayList<PossibleEvent> events;
51 public void addEvent(PossibleEvent pe) 52 public void addEvent(PossibleEvent pe)
52 { 53 {
53 events.add(pe); 54 events.add(0, pe);
54 } 55 }
55 56
56 private PossibleEvent getPossibleEvent() 57 private PossibleEvent getPossibleEvent()
57 { 58 {
58 int i; 59 for (PossibleEvent event : events)
59 for (i=(events.size()-1);i>-1;i--)
60 { 60 {
61 boolean good = true; 61 boolean good = true;
62 int j; 62 for (Precondition pre : event.getPreconditions())
63 for (j=0;j<events.get(i).preconditions();j++)
64 { 63 {
65 good = (good ? events.get(i).getPrecondition(j).match() : false); 64 good = (good ? pre.match() : false);
66 } 65 }
67 66
68 if (good) 67 if (good)
69 { 68 {
70 return events.get(i); 69 return event;
71 } 70 }
72 } 71 }
73 72
@@ -79,7 +78,7 @@ public class LayerEvent extends AbstractEvent implements Event {
79 PossibleEvent toDraw = getPossibleEvent(); 78 PossibleEvent toDraw = getPossibleEvent();
80 if (!(toDraw.getGraphic() instanceof BlankEventGraphic)) 79 if (!(toDraw.getGraphic() instanceof BlankEventGraphic))
81 { 80 {
82 g.drawImage(toDraw.getImage(), getRenderX(), getRenderY(), null); 81 g.drawImage(toDraw.getGraphic().getImage(), getRenderX(), getRenderY(), null);
83 } 82 }
84 } 83 }
85 84
@@ -97,6 +96,7 @@ public class LayerEvent extends AbstractEvent implements Event {
97 { 96 {
98 return getPossibleEvent().getDirection(); 97 return getPossibleEvent().getDirection();
99 } 98 }
99
100 public void setDirection(Direction direction) 100 public void setDirection(Direction direction)
101 { 101 {
102 getPossibleEvent().setDirection(direction); 102 getPossibleEvent().setDirection(direction);
@@ -117,7 +117,8 @@ public class LayerEvent extends AbstractEvent implements Event {
117 return getPossibleEvent().getCallback(); 117 return getPossibleEvent().getCallback();
118 } 118 }
119 119
120 public void setLabel(String string) { 120 public void setLabel(String string)
121 {
121 this.label = string; 122 this.label = string;
122 } 123 }
123 124
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java index 5ba887b..9deb0b1 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java
@@ -13,10 +13,9 @@ import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraph
13import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.EventGraphic; 13import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.EventGraphic;
14import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.MovementType; 14import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.MovementType;
15import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.StayStillMovementType; 15import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.StayStillMovementType;
16import java.awt.image.BufferedImage;
17 16
18/** 17/**
19 *w 18 *
20 * @author hatkirby 19 * @author hatkirby
21 */ 20 */
22public class PossibleEvent { 21public class PossibleEvent {
@@ -27,32 +26,80 @@ public class PossibleEvent {
27 private MovementType movement; 26 private MovementType movement;
28 private EventCallTime calltime; 27 private EventCallTime calltime;
29 private EventCall callback; 28 private EventCall callback;
30 private ArrayList<Precondition> preconditions; 29 private ArrayList<Precondition> preconditions = new ArrayList<Precondition>();
31 30
32 private Direction direction; 31 private Direction direction = Direction.South;
33 private int animationStep; 32 private int animationStep = 1;
34 33
35 public PossibleEvent() 34 PossibleEvent()
36 { 35 {
37 graphic = new BlankEventGraphic(); 36 this(new Builder());
38 layer = Layer.Below;
39 animation = AnimationType.CommonWithStepping;
40 movement = new StayStillMovementType();
41 calltime = EventCallTime.PushKey;
42 callback = EventCall.getEmptyEventCall();
43 preconditions = new ArrayList<Precondition>();
44
45 direction = Direction.South;
46 animationStep = 1;
47 } 37 }
38
39 public static class Builder
40 {
41 private EventGraphic graphic = new BlankEventGraphic();
42 private Layer layer = Layer.Below;
43 private AnimationType animation = AnimationType.CommonWithoutStepping;
44 private MovementType movement = new StayStillMovementType();
45 private EventCallTime calltime = EventCallTime.PushKey;
46 private EventCall callback = EventCall.getEmptyEventCall();
47
48 public Builder graphic(EventGraphic graphic)
49 {
50 this.graphic = graphic;
51 return this;
52 }
53
54 public Builder layer(Layer layer)
55 {
56 this.layer = layer;
57 return this;
58 }
59
60 public Builder animation(AnimationType animation)
61 {
62 this.animation = animation;
63 return this;
64 }
65
66 public Builder movement(MovementType movement)
67 {
68 this.movement = movement;
69 return this;
70 }
71
72 public Builder calltime(EventCallTime calltime)
73 {
74 this.calltime = calltime;
75 return this;
76 }
77
78 public Builder callback(EventCall callback)
79 {
80 this.callback = callback;
81 return this;
82 }
48 83
49 public BufferedImage getImage() 84 public PossibleEvent build()
50 { 85 {
51 return graphic.getImage(); 86 return new PossibleEvent(this);
87 }
88 }
89
90 private PossibleEvent(Builder builder)
91 {
92 graphic = builder.graphic;
93 layer = builder.layer;
94 animation = builder.animation;
95 movement = builder.movement;
96 calltime = builder.calltime;
97 callback = builder.callback;
52 } 98 }
53 99
54 private boolean aSLC = false; 100 private boolean aSLC = false;
55 public EventGraphic getGraphic() { 101 EventGraphic getGraphic()
102 {
56 if (animation.isAlwaysStepping()) 103 if (animation.isAlwaysStepping())
57 { 104 {
58 if (aSLC) 105 if (aSLC)
@@ -72,39 +119,47 @@ public class PossibleEvent {
72 return graphic; 119 return graphic;
73 } 120 }
74 121
75 public void setGraphic(EventGraphic graphic) { 122 void setGraphic(EventGraphic graphic)
123 {
76 this.graphic = graphic; 124 this.graphic = graphic;
77 } 125 }
78 126
79 public Layer getLayer() { 127 Layer getLayer()
128 {
80 return layer; 129 return layer;
81 } 130 }
82 131
83 public void setLayer(Layer layer) { 132 void setLayer(Layer layer)
133 {
84 this.layer = layer; 134 this.layer = layer;
85 } 135 }
86 136
87 public AnimationType getAnimation() { 137 AnimationType getAnimation()
138 {
88 return animation; 139 return animation;
89 } 140 }
90 141
91 public void setAnimation(AnimationType animation) { 142 void setAnimation(AnimationType animation)
143 {
92 this.animation = animation; 144 this.animation = animation;
93 } 145 }
94 146
95 public MovementType getMovement() { 147 MovementType getMovement()
148 {
96 return movement; 149 return movement;
97 } 150 }
98 151
99 public void setMovement(MovementType movement) { 152 void setMovement(MovementType movement)
153 {
100 this.movement = movement; 154 this.movement = movement;
101 } 155 }
102 156
103 public Direction getDirection() { 157 Direction getDirection()
158 {
104 return direction; 159 return direction;
105 } 160 }
106 161
107 public void setDirection(Direction direction) 162 void setDirection(Direction direction)
108 { 163 {
109 if (animation.canTurn()) 164 if (animation.canTurn())
110 { 165 {
@@ -113,11 +168,13 @@ public class PossibleEvent {
113 } 168 }
114 } 169 }
115 170
116 public int getAnimationStep() { 171 int getAnimationStep()
172 {
117 return animationStep; 173 return animationStep;
118 } 174 }
119 175
120 public void setAnimationStep(int animationStep) { 176 void setAnimationStep(int animationStep)
177 {
121 if (animation.canStep()) 178 if (animation.canStep())
122 { 179 {
123 this.animationStep = animationStep; 180 this.animationStep = animationStep;
@@ -125,37 +182,48 @@ public class PossibleEvent {
125 } 182 }
126 } 183 }
127 184
185 /**
186 * Add a precondition to this PossibleEvent
187 *
188 * PossibleEvents are different versions of a single LayerEvent. A
189 * LayerEvent may have many PossibleEvents, or none at all. The way it
190 * determines which PossibleEvent is current is that each PossibleEvent
191 * (possibly) has a set of <b>Precondition</b>s, objects that describe
192 * certain situations. If a PossibleEvent's Preconditions are all fulfilled,
193 * it is chosen as the active one. If there are more than one PossibleEvents
194 * with completely fulfilled Preconditions (that includes having no
195 * Preconditions at all), the later one is the one chosen as current.
196 *
197 * @param precondition The Precondition to add to the list
198 */
128 public void addPrecondition(Precondition precondition) 199 public void addPrecondition(Precondition precondition)
129 { 200 {
130 preconditions.add(precondition); 201 preconditions.add(precondition);
131 } 202 }
132 203
133 public Precondition getPrecondition(int i) 204 ArrayList<Precondition> getPreconditions()
134 { 205 {
135 return preconditions.get(i); 206 return preconditions;
136 }
137
138 public int preconditions()
139 {
140 return preconditions.size();
141 } 207 }
142 208
143 public EventCall getCallback() 209 EventCall getCallback()
144 { 210 {
145 return callback; 211 return callback;
146 } 212 }
147 213
148 public void setCallback(EventCall callback) 214 void setCallback(EventCall callback)
149 { 215 {
150 this.callback = callback; 216 this.callback = callback;
151 } 217 }
152 218
153 public EventCallTime getCalltime() { 219 EventCallTime getCalltime()
220 {
154 return calltime; 221 return calltime;
155 } 222 }
156 223
157 public void setCalltime(EventCallTime calltime) { 224 void setCalltime(EventCallTime calltime)
225 {
158 this.calltime = calltime; 226 this.calltime = calltime;
159 } 227 }
160 228
161} 229} \ No newline at end of file
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/Precondition.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/Precondition.java index 46cdcd2..602bdf5 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/Precondition.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/precondition/Precondition.java
@@ -10,8 +10,8 @@ package com.fourisland.fourpuzzle.gamestate.mapview.event.precondition;
10 * of its (possibly many) <b>PossibleEvent</b>s is active. PossibleEvents can 10 * of its (possibly many) <b>PossibleEvent</b>s is active. PossibleEvents can
11 * have Preconditions attached to them and when all Preconditions are fulfilled 11 * have Preconditions attached to them and when all Preconditions are fulfilled
12 * (determined by the <code>true</code> return value from <b>match()</b>) and 12 * (determined by the <code>true</code> return value from <b>match()</b>) and
13 * there are not fulfilled PossibleEvents later in the queue, said PossibleEvent 13 * there aren't any fulfilled PossibleEvents later in the queue, said
14 * will be active. 14 * PossibleEvent will be active.
15 * 15 *
16 * @author hatkirby 16 * @author hatkirby
17 */ 17 */