summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/LayerEvent.java
blob: b1e9bdc3c407a291113604af0afeed6c777e3fbd (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
/*
 * 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.util.ArrayList;
import com.fourisland.fourpuzzle.Direction;
import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.BlankEventGraphic;
import com.fourisland.fourpuzzle.gamestate.mapview.event.graphic.MoveableEventGraphic;
import com.fourisland.fourpuzzle.gamestate.mapview.event.precondition.Precondition;

/**
 *
 * @author hatkirby
 */
public class LayerEvent extends AbstractEvent 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)
    {
        setLocation(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;
    }
    
    public LayerEvent copy()
    {
        LayerEvent temp = new LayerEvent(getLocation().x, getLocation().y, getLabel());
        
        for (PossibleEvent pe : events)
        {
            temp.addEvent(pe.copy());
        }
        
        return temp;
    }
    
    private String label;
    public String getLabel()
    {
        return label;
    }
    
    private ArrayList<PossibleEvent> events;
    public void addEvent(PossibleEvent pe)
    {
        events.add(0, pe);
    }
    
    private PossibleEvent getPossibleEvent()
    {
        for (PossibleEvent event : events)
        {
            boolean good = true;
            for (Precondition pre : event.getPreconditions())
            {
                good = (good ? pre.match() : false);
            }
            
            if (good)
            {
                return event;
            }
        }
        
        return new PossibleEvent();
    }
    
    public void render(Graphics g)
    {
        PossibleEvent toDraw = getPossibleEvent();
        if (!(toDraw.getGraphic() instanceof BlankEventGraphic))
        {
            g.drawImage(toDraw.getGraphic().getImage(), getRenderX(), getRenderY(), null);
        }
    }
    
    public void startMoving()
    {
        Direction toMove = getPossibleEvent().getMovement().nextMovement();
        
        if (toMove != null)
        {
            startMoving(toMove);
        }
    }
    
    @Override
    public boolean startMoving(Direction toMove)
    {
        if (!(getPossibleEvent().getGraphic() instanceof MoveableEventGraphic))
        {
            return false;
        }
        
        return super.startMoving(toMove);
    }
    
    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 void setAnimationStep(int animStep)
    {
        getPossibleEvent().setAnimationStep(animStep);
    }
    
    public int getAnimationStep()
    {
        return getPossibleEvent().getAnimationStep();
    }
    
    @Override
    public void setMoving(boolean moving)
    {
        super.setMoving(moving);
        getPossibleEvent().setMoving(moving);
    }
    
}