summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/PossibleEvent.java
blob: f8934f44d9afd70b4728a40774b7451f9eca66d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.gamestate.mapview.event;

import com.fourisland.fourpuzzle.Layer;
import com.fourisland.fourpuzzle.gamestate.mapview.event.precondition.Precondition;
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.graphic.EventGraphic;
import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.MovementType;
import com.fourisland.fourpuzzle.gamestate.mapview.event.movement.StayStillMovementType;

/**
 *
 * @author hatkirby
 */
public class PossibleEvent {

    private EventGraphic graphic;
    private Layer layer;
    private AnimationType animation;
    private MovementType movement;
    private EventCallTime calltime;
    private EventCall callback;
    
    private ArrayList<Precondition> preconditions = new ArrayList<Precondition>();
    private Direction direction = Direction.South;
    private int 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 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;
    }
    
    public PossibleEvent copy()
    {
        PossibleEvent temp =  new Builder()
                .graphic(graphic)
                .layer(layer)
                .animation(animation)
                .movement(movement)
                .calltime(calltime)
                .callback(callback)
                .build();
        
        temp.getPreconditions().addAll(preconditions);
        
        return temp;
    }

    public EventGraphic getGraphic()
    {
        animation.tick(this);
        
        return graphic;
    }

    public Layer getLayer()
    {
        return layer;
    }

    public AnimationType getAnimation()
    {
        return animation;
    }

    public MovementType getMovement()
    {
        return movement;
    }

    private boolean moving = false;
    void setMoving(boolean moving)
    {
        this.moving = moving;
    }
    
    Direction getDirection()
    {
        return direction;
    }

    void setDirection(Direction direction)
    {
        if (animation.canTurn() && !moving)
        {
            this.direction = direction;
            graphic.setDirection(direction);
        }
    }

    int getAnimationStep()
    {
        return animationStep;
    }

    void setAnimationStep(int animationStep)
    {
        if (animation.canStep())
        {
            this.animationStep = animationStep;
            graphic.setAnimationStep(animationStep);
        }
    }
    
    /**
     * Add a precondition to this PossibleEvent
     * 
     * <p>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 <b>Precondition</b>s, 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.</p>
     * 
     * @param precondition The Precondition to add to the list
     */
    public void addPrecondition(Precondition precondition)
    {
        preconditions.add(precondition);
    }
    
    public ArrayList<Precondition> getPreconditions()
    {
        return preconditions;
    }

    public EventCall getCallback()
    {
        return callback;
    }

    public EventCallTime getCalltime()
    {
        return calltime;
    }
    
}