summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/Map.java
blob: 33a85168a18f65bab9186d3e0cf0a7b2419b10b3 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.gamestate.mapview;

import com.fourisland.fourpuzzle.*;
import com.fourisland.fourpuzzle.gamestate.mapview.event.Event;
import com.fourisland.fourpuzzle.gamestate.mapview.event.EventList;
import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent;
import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author hatkirby
 */
public class Map {
    
    /**
     * Creates a new Map
     * 
     * @param width The width of the Map in tiles
     * @param height The height of the Map in tiles
     * @param chipSet The name of the ChipSet to use
     * @param music The name of the Music file to play in the background
     */
    public Map(int width, int height, String chipSet, String music)
    {
        setSize(new Dimension(width, height));
        setChipSet(chipSet);
        setMusic(music);
    }
    
    /**
     * Creates a new Map
     * 
     * @param width The width of the Map in tiles
     * @param height The height of the Map in tiles
     * @param chipSet The name of the ChipSet to use
     * @param musicType The non-Specified Music mode to use
     * 
     * @throws IllegalArgumentException if MapMusicType.Specified is passed in
     * musicType. This constructor is for MapMusicType.NoMusic or
     * MapMusicType.NoChange. If you wish to specify a Music file, use the other
     * constructor (int, int, String, String)
     */
    public Map(int width, int height, String chipSet, MapMusicType musicType)
    {
        if (musicType == MapMusicType.Specified)
        {
            throw new IllegalArgumentException("MapMusicType.Specified is not a valid value for musicType. If you wish to specify a music type, use the other constructor (int, int, String, String)");
        }
        
        setSize(new Dimension(width, height));
        setChipSet(chipSet);
        setMusicType(musicType);
    }
    
    public Map copy()
    {
        Map temp;
        if (getMusicType() == MapMusicType.Specified)
        {
            temp = new Map(getSize().width, getSize().height, getChipSet(), getMusic());
        } else {
            temp = new Map(getSize().width, getSize().height, getChipSet(), getMusicType());
        }
        
        temp.mapData = new Vector<HashMap<Integer,Integer>>(getMapData());
        temp.events = getEvents().copy(this);
        
        return temp;
    }
    
    private Dimension size;
    public Dimension getSize()
    {
        return size;
    }
    private void setSize(Dimension size)
    {
        if ((size.width < 20) || (size.height < 15))
        {
            this.size = new Dimension(20,15);
        } else {
            this.size = size;
        }
    }
    
    private EventList events = new EventList(this);
    public EventList getEvents()
    {
        return events;
    }
    public LayerEvent getEvent(String event)
    {
        for (LayerEvent ev : events)
        {
            if (ev.getLabel().equals(event))
            {
                return ev;
            }
        }
        
        return null;
    }
    
    public boolean checkForCollision(Event ev, Direction toMove)
    {
        int x = ev.getLocation().x;
        int y = ev.getLocation().y;
        
        if ((toMove == Direction.North) && (y == 0))
        {
            return true;
        } else if ((toMove == Direction.West) && (x == 0))
        {
            return true;
        } else if ((toMove == Direction.South) && (y == (size.height - 1)))
        {
            return true;
        } else if ((toMove == Direction.East) && (x == (size.width - 1)))
        {
            return true;
        }
        
        if ((ev instanceof HeroEvent) && (((MapViewGameState) Game.getGameState()).debugWalkthrough))
        {
            return false;
        }
        
        if ((toMove == Direction.North) && (checkForEventCollision(x, y-1)))
        {
            return true;
        }
        
        if ((toMove == Direction.West) && (checkForEventCollision(x-1, y)))
        {
            return true;
        }
        
        if ((toMove == Direction.South) && (checkForEventCollision(x, y+1)))
        {
            return true;
        }
        
        if ((toMove == Direction.East) && (checkForEventCollision(x+1, y)))
        {
            return true;
        }
        
        ChipSet cSI = ChipSet.getChipSet(chipSet);
        HashMap<Integer,ChipSetData> cSID = cSI.getChipSetData();
        for (HashMap<Integer,Integer> mapArea : getMapData())
        {
            if ((toMove == Direction.North) && (!cSID.get(mapArea.get(x+((y-1)*getSize().width))).isEnableSouth()))
            {
                return true;
            } else if ((toMove == Direction.West) && (!cSID.get(mapArea.get(x-1+(y*getSize().width))).isEnableEast()))
            {
                return true;
            } else if ((toMove == Direction.South) && (!cSID.get(mapArea.get(x+((y+1)*getSize().width))).isEnableNorth()))
            {
                return true;
            } else if ((toMove == Direction.East) && (!cSID.get(mapArea.get(x+1+(y*getSize().width))).isEnableWest()))
            {
                return true;
            }

            if ((toMove == Direction.North) && (!cSID.get(mapArea.get(x+(y*getSize().width))).isEnableNorth()))
            {
                return true;
            } else if ((toMove == Direction.West) && (!cSID.get(mapArea.get(x+(y*getSize().width))).isEnableWest()))
            {
                return true;
            } else if ((toMove == Direction.South) && (!cSID.get(mapArea.get(x+(y*getSize().width))).isEnableSouth()))
            {
                return true;
            } else if ((toMove == Direction.East) && (!cSID.get(mapArea.get(x+(y*getSize().width))).isEnableEast()))
            {
                return true;
            }    
        }
        
        return false;
    }
    
    private boolean checkForEventCollision(int x, int y)
    {
        for (LayerEvent ev : events)
        {
            if (ev.getLayer() == Layer.Middle)
            {
                if (ev.isOccupyingSpace(x, y))
                {
                    return true;
                }
            }
        }

        if (Game.getHeroEvent().isOccupyingSpace(x, y))
        {
            return true;
        }
       
        return false;
    }

    private String chipSet;
    public String getChipSet() {
        return chipSet;
    }
    public void setChipSet(String chipSet) {
        this.chipSet = chipSet;
    }

    private List<HashMap<Integer,Integer>> mapData = new Vector<HashMap<Integer,Integer>>();
    public List<HashMap<Integer, Integer>> getMapData() {
        return mapData;
    }
    
    private String music;
    public String getMusic()
    {
        return music;
    }

    private void setMusic(String music)
    {
        this.music = music;
        this.musicType = MapMusicType.Specified;
    }
    
    private MapMusicType musicType = MapMusicType.NoChange;
    public MapMusicType getMusicType()
    {
        return musicType;
    }
    
    private void setMusicType(MapMusicType musicType)
    {
        this.musicType = musicType;
    }
    
    BufferedImage lowerLayer = null;
    public BufferedImage renderLower()
    {
        if (lowerLayer == null)
        {
            lowerLayer = Display.createCanvas(size.width*16, size.height*16);
            Graphics2D g = lowerLayer.createGraphics();
            ChipSet chipSetObj = ChipSet.getChipSet(chipSet);
            int i,x,y;
            for (i=0;i<mapData.size();i++)
            {
                for (y=0;y<size.height;y++)
                {
                    for (x=0;x<size.width;x++)
                    {
                        int tile = mapData.get(i).get(x+(y*size.width));
                        if (chipSetObj.getChipSetData().get(tile).getLayer() != Layer.Above)
                        {
                            g.drawImage(chipSetObj.getImage(tile), x*16, y*16, null);
                        }
                    }
                }
            }
        }
        
        return lowerLayer;
        
    }
    
    BufferedImage upperLayer = null;
    public BufferedImage renderUpper()
    {
        if (upperLayer == null)
        {
            upperLayer = Display.createCanvas(size.width*16, size.height*16);
            Graphics2D g = upperLayer.createGraphics();
            ChipSet chipSetObj = ChipSet.getChipSet(chipSet);
            int i,x,y;
            for (i=0;i<mapData.size();i++)
            {
                for (y=0;y<size.height;y++)
                {
                    for (x=0;x<size.width;x++)
                    {
                        int tile = mapData.get(i).get(x+(y*size.width));
                        if (chipSetObj.getChipSetData().get(tile).getLayer() == Layer.Above)
                        {
                            g.drawImage(chipSetObj.getImage(tile), x*16, y*16, null);
                        }
                    }
                }
            }
        }
        
        return upperLayer;
    }
    
}