From 7125747297343872ca4cc64baa2e6cc3769ab5c3 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 7 Feb 2009 14:11:21 -0500 Subject: 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. --- .../gamestate/mapview/event/LayerEvent.java | 19 +-- .../gamestate/mapview/event/PossibleEvent.java | 156 +++++++++++++++------ .../mapview/event/precondition/Precondition.java | 4 +- 3 files changed, 124 insertions(+), 55 deletions(-) (limited to 'src/com/fourisland') 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; import java.util.ArrayList; import com.fourisland.fourpuzzle.Direction; import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic; +import com.fourisland.fourpuzzle.gamestate.mapview.event.precondition.Precondition; /** * @@ -50,24 +51,22 @@ public class LayerEvent extends AbstractEvent implements Event { private ArrayList events; public void addEvent(PossibleEvent pe) { - events.add(pe); + events.add(0, pe); } private PossibleEvent getPossibleEvent() { - int i; - for (i=(events.size()-1);i>-1;i--) + for (PossibleEvent event : events) { boolean good = true; - int j; - for (j=0;j preconditions; + private ArrayList preconditions = new ArrayList(); - private Direction direction; - private int animationStep; + private Direction direction = Direction.South; + private int animationStep = 1; - public PossibleEvent() - { - graphic = new BlankEventGraphic(); - layer = Layer.Below; - animation = AnimationType.CommonWithStepping; - movement = new StayStillMovementType(); - calltime = EventCallTime.PushKey; - callback = EventCall.getEmptyEventCall(); - preconditions = new ArrayList(); - - direction = Direction.South; - animationStep = 1; + PossibleEvent() + { + this(new Builder()); } + + public static class Builder + { + private EventGraphic graphic = new BlankEventGraphic(); + private Layer layer = Layer.Below; + private AnimationType animation = AnimationType.CommonWithoutStepping; + private MovementType movement = new StayStillMovementType(); + private EventCallTime calltime = EventCallTime.PushKey; + private EventCall callback = EventCall.getEmptyEventCall(); + + public Builder graphic(EventGraphic graphic) + { + this.graphic = graphic; + return this; + } + + public Builder layer(Layer layer) + { + this.layer = layer; + return this; + } + + public Builder animation(AnimationType animation) + { + this.animation = animation; + return this; + } + + public Builder movement(MovementType movement) + { + this.movement = movement; + return this; + } + + public Builder calltime(EventCallTime calltime) + { + this.calltime = calltime; + return this; + } + + public Builder callback(EventCall callback) + { + this.callback = callback; + return this; + } - public BufferedImage getImage() - { - return graphic.getImage(); + public PossibleEvent build() + { + return new PossibleEvent(this); + } + } + + private PossibleEvent(Builder builder) + { + graphic = builder.graphic; + layer = builder.layer; + animation = builder.animation; + movement = builder.movement; + calltime = builder.calltime; + callback = builder.callback; } private boolean aSLC = false; - public EventGraphic getGraphic() { + EventGraphic getGraphic() + { if (animation.isAlwaysStepping()) { if (aSLC) @@ -72,39 +119,47 @@ public class PossibleEvent { return graphic; } - public void setGraphic(EventGraphic graphic) { + void setGraphic(EventGraphic graphic) + { this.graphic = graphic; } - public Layer getLayer() { + Layer getLayer() + { return layer; } - public void setLayer(Layer layer) { + void setLayer(Layer layer) + { this.layer = layer; } - public AnimationType getAnimation() { + AnimationType getAnimation() + { return animation; } - public void setAnimation(AnimationType animation) { + void setAnimation(AnimationType animation) + { this.animation = animation; } - public MovementType getMovement() { + MovementType getMovement() + { return movement; } - public void setMovement(MovementType movement) { + void setMovement(MovementType movement) + { this.movement = movement; } - public Direction getDirection() { + Direction getDirection() + { return direction; } - public void setDirection(Direction direction) + void setDirection(Direction direction) { if (animation.canTurn()) { @@ -113,11 +168,13 @@ public class PossibleEvent { } } - public int getAnimationStep() { + int getAnimationStep() + { return animationStep; } - public void setAnimationStep(int animationStep) { + void setAnimationStep(int animationStep) + { if (animation.canStep()) { this.animationStep = animationStep; @@ -125,37 +182,48 @@ public class PossibleEvent { } } + /** + * Add a precondition to this PossibleEvent + * + * PossibleEvents are different versions of a single LayerEvent. A + * LayerEvent may have many PossibleEvents, or none at all. The way it + * determines which PossibleEvent is current is that each PossibleEvent + * (possibly) has a set of Preconditions, objects that describe + * certain situations. If a PossibleEvent's Preconditions are all fulfilled, + * it is chosen as the active one. If there are more than one PossibleEvents + * with completely fulfilled Preconditions (that includes having no + * Preconditions at all), the later one is the one chosen as current. + * + * @param precondition The Precondition to add to the list + */ public void addPrecondition(Precondition precondition) { preconditions.add(precondition); } - public Precondition getPrecondition(int i) + ArrayList getPreconditions() { - return preconditions.get(i); - } - - public int preconditions() - { - return preconditions.size(); + return preconditions; } - public EventCall getCallback() + EventCall getCallback() { return callback; } - public void setCallback(EventCall callback) + void setCallback(EventCall callback) { this.callback = callback; } - public EventCallTime getCalltime() { + EventCallTime getCalltime() + { return calltime; } - public void setCalltime(EventCallTime calltime) { + void setCalltime(EventCallTime calltime) + { this.calltime = calltime; } -} +} \ 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; * of its (possibly many) PossibleEvents is active. PossibleEvents can * have Preconditions attached to them and when all Preconditions are fulfilled * (determined by the true return value from match()) and - * there are not fulfilled PossibleEvents later in the queue, said PossibleEvent - * will be active. + * there aren't any fulfilled PossibleEvents later in the queue, said + * PossibleEvent will be active. * * @author hatkirby */ -- cgit 1.4.1