summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-01-17 10:36:37 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-01-17 10:36:37 -0500
commit69b495c392bffe96dab97306a42466edd4b2474e (patch)
tree2adccc01f4027bcb76bbefee03bb6a9622ce3e00 /src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
parent4c768483aecd687529b9abb48ed42950fabc886f (diff)
downloadfourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.tar.gz
fourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.tar.bz2
fourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.zip
Imported sources
Diffstat (limited to 'src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java')
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java226
1 files changed, 226 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java new file mode 100644 index 0000000..0e5ec44 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java
@@ -0,0 +1,226 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.gamestate.mapview;
7
8import com.fourisland.fourpuzzle.Audio;
9import com.fourisland.fourpuzzle.gamestate.*;
10import com.fourisland.fourpuzzle.Direction;
11import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent;
12import com.fourisland.fourpuzzle.Game;
13import com.fourisland.fourpuzzle.Layer;
14import com.fourisland.fourpuzzle.PuzzleApplication;
15import com.fourisland.fourpuzzle.gamestate.mapview.event.EventCallTime;
16import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
17import java.awt.Graphics2D;
18import java.awt.event.KeyEvent;
19import java.util.ArrayList;
20
21/**
22 *
23 * @author hatkirby
24 */
25public class MapViewGameState implements GameState {
26
27 boolean debugWalkthrough = false;
28 boolean processInput = true;
29 Map currentMap;
30
31 public MapViewGameState(String map, int x, int y) throws Exception
32 {
33 //currentMap = ObjectLoader.getMap(map);
34 setCurrentMap(map);
35 Game.getSaveFile().getHero().setLocation(x, y);
36 }
37
38 public void initalize() throws Exception
39 {
40 //if (!currentMap.getMusic().equals(""))
41 {
42 // Audio.playMusic(currentMap.getMusic());
43 }
44 }
45
46 public void deinitalize() throws Exception
47 {
48 //if (!currentMap.getMusic().equals(""))
49 {
50 Audio.stopMusic();
51 }
52 }
53
54 public void processInput() throws Exception
55 {
56 if (processInput)
57 {
58 HeroEvent hero = Game.getSaveFile().getHero();
59
60 if (Game.getKey().isControlDown() && !debugWalkthrough)
61 {
62 if (PuzzleApplication.INSTANCE.getContext().getResourceMap().getBoolean("debugMode"))
63 {
64 debugWalkthrough = true;
65 }
66 } else {
67 debugWalkthrough = false;
68 }
69
70 if (!hero.isMoving())
71 {
72 Direction toMove = null;
73 Boolean letsMove = false;
74
75 switch (Game.getKey().getKeyCode())
76 {
77 case KeyEvent.VK_UP:
78 toMove = Direction.North;
79 letsMove = true;
80 break;
81 case KeyEvent.VK_RIGHT:
82 toMove = Direction.East;
83 letsMove = true;
84 break;
85 case KeyEvent.VK_DOWN:
86 toMove = Direction.South;
87 letsMove = true;
88 break;
89 case KeyEvent.VK_LEFT:
90 toMove = Direction.West;
91 letsMove = true;
92 break;
93 }
94
95 if (letsMove)
96 {
97 if (debugWalkthrough || (!currentMap.checkForCollision(hero.getLocation().x, hero.getLocation().y, toMove)))
98 {
99 hero.startMoving(toMove);
100 } else {
101 hero.setDirection(toMove);
102 }
103 }
104
105 if ((Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) || (Game.getKey().getKeyCode() == KeyEvent.VK_SPACE))
106 {
107 int i=0;
108 for (i=0;i<currentMap.getEvents().size();i++)
109 {
110 LayerEvent ev = currentMap.getEvents().get(i);
111
112 if (ev.getCalltime() == EventCallTime.PushKey)
113 {
114 if ((hero.getDirection() == Direction.North) && (ev.getLocation().x == hero.getLocation().x) && (ev.getLocation().y == (hero.getLocation().y - 1)))
115 {
116 ev.setDirection(Direction.South);
117 ev.getCallback().run();
118 } else if ((hero.getDirection() == Direction.West) && (ev.getLocation().x == (hero.getLocation().x - 1)) && (ev.getLocation().y == hero.getLocation().y))
119 {
120 ev.setDirection(Direction.East);
121 ev.getCallback().run();
122 } else if ((hero.getDirection() == Direction.South) && (ev.getLocation().x == hero.getLocation().x) && (ev.getLocation().y == (hero.getLocation().y + 1)))
123 {
124 ev.setDirection(Direction.North);
125 ev.getCallback().run();
126 } else if ((hero.getDirection() == Direction.East) && (ev.getLocation().x == (hero.getLocation().x + 1)) && (ev.getLocation().y == hero.getLocation().y))
127 {
128 ev.setDirection(Direction.West);
129 ev.getCallback().run();
130 }
131 }
132 }
133
134 Game.getKey().setKeyCode(KeyEvent.VK_UNDEFINED);
135 }
136 }
137 }
138 }
139
140 public void doGameCycle() throws Exception
141 {
142 HeroEvent hero = Game.getSaveFile().getHero();
143 if (hero.isMoving())
144 {
145 hero.processMoving();
146 }
147
148 int i=0;
149 for (i=0;i<currentMap.getEvents().size();i++)
150 {
151 if (!currentMap.getEvents().get(i).isMoving())
152 {
153 currentMap.getEvents().get(i).startMoving(currentMap);
154 } else {
155 currentMap.getEvents().get(i).processMoving();
156 }
157 }
158 }
159
160 public void render(Graphics2D g) throws Exception
161 {
162 ChipSet chipSet = ChipSet.getChipSet(currentMap.getChipSet());
163 int i,x,y;
164 for (i=0;i<currentMap.getMapData().size();i++)
165 {
166 for (y=0;y<currentMap.getSize().height;y++)
167 {
168 for (x=0;x<currentMap.getSize().width;x++)
169 {
170 int tile = currentMap.getMapData().get(i).get(x+(y*currentMap.getSize().width));
171 if (chipSet.getChipSetData().get(tile).getLayer() != Layer.Above)
172 {
173 g.drawImage(chipSet.getImage(tile), x*16, y*16, null);
174 }
175 }
176 }
177 }
178/*
179 MapViewer mv = new MapViewer(currentMap, new com.alienfactory.javamappy.viewer.render.J2SE14Renderer(currentMap), Game.WIDTH, Game.HEIGHT);
180 mv.setBlockX(Game.getSaveFile().getHero().getLocation().x);
181 mv.setBlockY(Game.getSaveFile().getHero().getLocation().y);
182 mv.setPixelX((4 - Game.getSaveFile().getHero().getMoveTimer()) * 4);
183 mv.draw(g, true);*/
184 Game.getSaveFile().getHero().render(g);
185
186 ArrayList<LayerEvent> events = currentMap.getEvents();
187 for (i=0;i<events.size();i++)
188 {
189 events.get(i).render(g);
190 }
191
192 for (i=0;i<currentMap.getMapData().size();i++)
193 {
194 for (y=0;y<currentMap.getSize().height;y++)
195 {
196 for (x=0;x<currentMap.getSize().width;x++)
197 {
198 int tile = currentMap.getMapData().get(i).get(x+(y*currentMap.getSize().width));
199 if (chipSet.getChipSetData().get(tile).getLayer() == Layer.Above)
200 {
201 g.drawImage(chipSet.getImage(tile), x*16, y*16, null);
202 }
203 }
204 }
205 }
206 }
207
208 public void initCurrentMap(String mapName) throws Exception
209 {
210 Class mapClass = Class.forName(PuzzleApplication.INSTANCE.getGamePackage() + ".gamedata.map." + mapName);
211 Object mapObject = mapClass.newInstance();
212 Map map = (Map) mapObject;
213 map.initalize();
214 currentMap = map;
215 }
216 public void setCurrentMap(String mapName) throws Exception
217 {
218 Game.getSaveFile().setCurrentMap(mapName);
219 initCurrentMap(mapName);
220 }
221 public Map getCurrentMap()
222 {
223 return currentMap;
224 }
225
226}