summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java
blob: 4f8b51c2f00ae875e91744ca4100a3403d8823bd (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * 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 java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import com.fourisland.fourpuzzle.Direction;
import com.fourisland.fourpuzzle.gamestate.mapview.Map;
import com.fourisland.fourpuzzle.util.Functions;

/**
 *
 * @author hatkirby
 */
public class LayerEvent implements Event {
    
    /** Create a new Event instance
     * 
     * @param x The horizontal location of the Event on the Map
     * @param y The vertical location of the Event on the Map
     */
    public LayerEvent(int x, int y)
    {
        location = new Point(x,y);
        events = new ArrayList<PossibleEvent>();
        label = "Unlabelled";
    }
    
    /** Create a new Event instance
     * 
     * @param x The horizontal location of the Event on the Map
     * @param y The vertical location of the Event on the Map
     * @param label An identifying label for the Event
     */
    public LayerEvent(int x, int y, String label)
    {
        this(x,y);
        this.label = label;
    }
    
    private String label;
    public String getLabel()
    {
        return label;
    }
    
    private Point location;
    public Point getLocation()
    {
        return location;
    }
    public void setLocation(Point location)
    {
        this.location = location;
    }
    public void setLocation(int x, int y)
    {
        location.setLocation(x, y);
    }
    
    private ArrayList<PossibleEvent> events;
    public void addEvent(PossibleEvent pe)
    {
        events.add(pe);
    }
    
    private PossibleEvent getPossibleEvent()
    {
        int i;
        for (i=(events.size()-1);i>-1;i--)
        {
            boolean good = true;
            int j;
            for (j=0;j<events.get(i).preconditions();j++)
            {
                good = (good ? events.get(i).getPrecondition(j).match() : false);
            }
            
            if (good)
            {
                return events.get(i);
            }
        }
        
        return new PossibleEvent();
    }
    
    public void render(Graphics g)
    {
        int x = (location.x * 16) - 4;
        int y = (location.y * 16) - 16;
        
        if (moving)
        {
            if (moveDirection == Direction.North)
            {
                y -= (4 - moveTimer) * 4;
            } else if (moveDirection == Direction.West)
            {
                x -= (4 - moveTimer) * 4;
            } else if (moveDirection == Direction.South)
            {
                y += (4 - moveTimer) * 4;
            } else if (moveDirection == Direction.East)
            {
                x += (4 - moveTimer) * 4;
            }
        }
        
        PossibleEvent toDraw = getPossibleEvent();
        if (!toDraw.getGraphic().equals("blank"))
        {
            g.drawImage(toDraw.getImage(), x, y, null);
        }
    }
    
    
    private boolean moving = false;
    public boolean isMoving()
    {
        return moving;
    }
    public void setMoving(boolean moving)
    {
        this.moving = moving;
    }
    
    private int moveTimer;
    private Direction moveDirection;
    public void startMoving(Map map)
    {
        Direction toMove = getPossibleEvent().getMovement().startMoving();
        
        if (toMove != null)
        {
            if (!map.checkForCollision(getLocation().x, getLocation().y, toMove))
            {
                startMoving(toMove);
            }
        }
    }
    public void startMoving(Direction toMove)
    {
        getPossibleEvent().setDirection(toMove);
        getPossibleEvent().setAnimationStep(2);
        moveTimer = 4;
        moving = true;
        moveDirection = toMove;
    }
    public void processMoving()
    {
        if (moving)
        {
            moveTimer--;
            if (moveTimer == 2)
            {
                getPossibleEvent().setAnimationStep(0);
            } else if (moveTimer == 0)
            {
                getPossibleEvent().setAnimationStep(1);
                moving = false;
                
                if (moveDirection == Direction.North)
                {
                    setLocation(getLocation().x,getLocation().y-1);
                } else if (moveDirection == Direction.West)
                {
                    setLocation(getLocation().x-1,getLocation().y);
                } else if (moveDirection == Direction.South)
                {
                    setLocation(getLocation().x,getLocation().y+1);
                } else if (moveDirection == Direction.East)
                {
                    setLocation(getLocation().x+1,getLocation().y);
                }
            }
        }
    }
    
    public Direction getDirection()
    {
        return getPossibleEvent().getDirection();
    }
    public void setDirection(Direction direction)
    {
        getPossibleEvent().setDirection(direction);
    }

    public Layer getLayer()
    {
        return getPossibleEvent().getLayer();
    }
    
    public EventCallTime getCalltime()
    {
        return getPossibleEvent().getCalltime();
    }
    
    public EventCall getCallback()
    {
        return getPossibleEvent().getCallback();
    }

    public void setLabel(String string) {
        this.label = string;
    }
    
    public boolean isOccupyingSpace(int x, int y)
    {
        if (getLocation().equals(new Point(x,y)))
        {
            return true;
        }
        
        if (Functions.isMovingTo(this, x, y))
        {
            return true;
        }
        
        return false;
    }
    
}