1 | package com.medievaltech.advancewars;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.util.*;
|
---|
5 |
|
---|
6 | import com.medievaltech.advancewars.Enum.*;
|
---|
7 | import com.medievaltech.unit.*;
|
---|
8 | import com.medievaltech.gui.*;
|
---|
9 |
|
---|
10 | import android.content.Context;
|
---|
11 | import android.graphics.*;
|
---|
12 | import android.os.*;
|
---|
13 | import android.view.*;
|
---|
14 | import android.util.AttributeSet;
|
---|
15 | import android.util.Log;
|
---|
16 |
|
---|
17 | class GameView extends SurfaceView implements SurfaceHolder.Callback {
|
---|
18 |
|
---|
19 | class DrawingThread extends Thread {
|
---|
20 |
|
---|
21 | private Unit selectedUnit;
|
---|
22 |
|
---|
23 | private Paint mLinePaint, mTextPaint, mButtonPaint, mTilePaint1,
|
---|
24 | mTilePaint2, mSelectionPaint, mUnitPaint1, mUnitPaint2;
|
---|
25 |
|
---|
26 | /** Handle to the surface manager object we interact with */
|
---|
27 | private SurfaceHolder mSurfaceHolder;
|
---|
28 |
|
---|
29 | public DrawingThread(SurfaceHolder surfaceHolder, Context context, Handler handler) {
|
---|
30 | mSurfaceHolder = surfaceHolder;
|
---|
31 |
|
---|
32 | mLinePaint = new Paint();
|
---|
33 | initPaint(mLinePaint, 0, 255, 0);
|
---|
34 |
|
---|
35 | mTextPaint = new Paint();
|
---|
36 | mTextPaint.setTextSize(12);
|
---|
37 | mTextPaint.setTextAlign(Paint.Align.CENTER);
|
---|
38 | initPaint(mTextPaint, 255, 255, 255);
|
---|
39 |
|
---|
40 | mButtonPaint = new Paint();
|
---|
41 | mButtonPaint.setTextSize(20);
|
---|
42 | mButtonPaint.setTextAlign(Paint.Align.CENTER);
|
---|
43 | initPaint(mButtonPaint, 0, 0, 0);
|
---|
44 |
|
---|
45 | mTilePaint1 = new Paint();
|
---|
46 | initPaint(mTilePaint1, 0, 255, 0);
|
---|
47 |
|
---|
48 | mTilePaint2 = new Paint();
|
---|
49 | initPaint(mTilePaint2, 0, 0, 255);
|
---|
50 |
|
---|
51 | mUnitPaint1 = new Paint();
|
---|
52 | initPaint(mUnitPaint1, 255, 0, 0);
|
---|
53 |
|
---|
54 | mUnitPaint2 = new Paint();
|
---|
55 | initPaint(mUnitPaint2, 160, 160, 255);
|
---|
56 |
|
---|
57 | mSelectionPaint = new Paint();
|
---|
58 | initPaint(mSelectionPaint, 255, 127, 0);
|
---|
59 |
|
---|
60 | Player.neutralColor = new Paint();
|
---|
61 | initPaint(Player.neutralColor, 127, 127, 127);
|
---|
62 |
|
---|
63 | Static.wndMainMenu = new com.medievaltech.gui.Window(0, 0, 320, 450);
|
---|
64 | Static.wndMainMenu.addGUIObject("txtTitle", new Text("Main Menu", 100, 30, 120, 20, mTextPaint));
|
---|
65 | Static.wndMainMenu.addGUIObject("btnNewGame", new Button("New Game", 100, 90, 120, 20, mLinePaint, mButtonPaint));
|
---|
66 | Static.wndMainMenu.addGUIObject("btnLoadGame", new Button("Load Game", 100, 125, 120, 20, mLinePaint, mButtonPaint));
|
---|
67 | Static.wndMainMenu.addGUIObject("btnMapEditor", new Button("Map Editor", 100, 160, 120, 20, mLinePaint, mButtonPaint));
|
---|
68 | Static.wndMainMenu.addGUIObject("btnQuit", new Button("Quit", 100, 195, 120, 20, mLinePaint, mButtonPaint));
|
---|
69 |
|
---|
70 | Static.grassTile = new Tile(mTilePaint1, TerrainType.LAND);
|
---|
71 | Static.oceanTile = new Tile(mTilePaint2, TerrainType.SEA);
|
---|
72 |
|
---|
73 | Static.map = new Map(Static.grassTile, 6, 8, new Point(10, 25));
|
---|
74 |
|
---|
75 | boolean land = true;
|
---|
76 |
|
---|
77 | for(int x=0; x<Static.map.getWidth(); x++) {
|
---|
78 | for(int y=0; y<Static.map.getHeight(); y++) {
|
---|
79 | if(land)
|
---|
80 | Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(x, y)));
|
---|
81 | else
|
---|
82 | Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(x, y)));
|
---|
83 | land = !land;
|
---|
84 | }
|
---|
85 | land = !land;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Static.human = new Player("Human", mUnitPaint1);
|
---|
89 | Static.enemy = new Player("Comp", mUnitPaint2);
|
---|
90 |
|
---|
91 | Static.map.getTile(0, 0).addUnit(new Soldier(Static.enemy));
|
---|
92 | Static.map.getTile(2, 3).addUnit(new Soldier(Static.human));
|
---|
93 | Static.map.getTile(5, 6).addUnit(new Soldier(Static.human));
|
---|
94 |
|
---|
95 | Static.map.getTile(4, 4).addBuilding(new City());
|
---|
96 |
|
---|
97 | Static.turn = Turn.YOUR_TURN;
|
---|
98 |
|
---|
99 | Static.gameState = GameState.MAIN_MENU;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void initPaint(Paint p, int r, int g, int b) {
|
---|
103 | p.setAntiAlias(true);
|
---|
104 | p.setARGB(255, r, g, b);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Starts the game, setting parameters for the current difficulty.
|
---|
109 | */
|
---|
110 | // I don't think this gets called now. maybe we should call it in the thread constructor
|
---|
111 | public void doStart() {
|
---|
112 | synchronized (mSurfaceHolder) {
|
---|
113 | Log.i("AdvanceWars", "Player's turn starting now");
|
---|
114 | Static.gameState = GameState.MAIN_MENU;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public void run() {
|
---|
120 | while (Static.run) {
|
---|
121 | Canvas c = null;
|
---|
122 | try {
|
---|
123 | c = mSurfaceHolder.lockCanvas(null);
|
---|
124 | synchronized(mSurfaceHolder) {
|
---|
125 | doLogic();
|
---|
126 | doDraw(c);
|
---|
127 | }
|
---|
128 | } finally {
|
---|
129 | // do this in a finally so that if an exception is thrown
|
---|
130 | // during the above, we don't leave the Surface in an
|
---|
131 | // inconsistent state
|
---|
132 | if (c != null) {
|
---|
133 | mSurfaceHolder.unlockCanvasAndPost(c);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void setGameState(GameState state) {
|
---|
140 | synchronized (mSurfaceHolder) {
|
---|
141 | Static.gameState = state;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Callback invoked when the surface dimensions change. */
|
---|
146 | public void setSurfaceSize(int width, int height) {
|
---|
147 | synchronized (mSurfaceHolder) {
|
---|
148 | Log.i("AdvanceWars", "width: "+width+", height: "+height);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void doLogic() {
|
---|
153 | if(Static.turn == Turn.YOUR_TURN)
|
---|
154 | return;
|
---|
155 |
|
---|
156 | switch(Static.gameState) {
|
---|
157 | case BATTLE_MAP:
|
---|
158 | Iterator<Unit> iter = Static.enemy.getControlledUnits().iterator();
|
---|
159 | Unit cur;
|
---|
160 | int x, y;
|
---|
161 |
|
---|
162 | Log.i("AdvanceWars", "starting to move enemy units");
|
---|
163 | while(iter.hasNext()) {
|
---|
164 | cur = iter.next();
|
---|
165 | x = cur.location.x;
|
---|
166 | y = cur.location.y;
|
---|
167 |
|
---|
168 | Log.i("AdvanceWars", "moving enemy unit");
|
---|
169 |
|
---|
170 | //any unit that's in the way is removed (needs to be changed eventuallyy)
|
---|
171 | Static.map.getTile(x, y).removeUnit();
|
---|
172 | Static.map.getTile(x, y+1).addUnit(cur);
|
---|
173 | }
|
---|
174 | Log.i("AdvanceWars", "finished moving enemy units");
|
---|
175 |
|
---|
176 |
|
---|
177 | Static.turn = Turn.YOUR_TURN;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Draws the ship, fuel/speed bars, and background to the provided
|
---|
184 | * Canvas.
|
---|
185 | */
|
---|
186 | private void doDraw(Canvas canvas) {
|
---|
187 | canvas.drawColor(Color.BLACK);
|
---|
188 |
|
---|
189 | switch(Static.gameState) {
|
---|
190 | case MAIN_MENU:
|
---|
191 | Static.wndMainMenu.draw(canvas);
|
---|
192 | break;
|
---|
193 | case BATTLE_MAP:
|
---|
194 | mTextPaint.setTextSize(12);
|
---|
195 |
|
---|
196 | Static.map.draw(canvas);
|
---|
197 |
|
---|
198 | if(selectedUnit != null) {
|
---|
199 | for(Point p : selectedUnit.getMovementRange()) {
|
---|
200 | canvas.drawRect(p.x*50+10, p.y*50+25, p.x*50+50+10, p.y*50+50+25, mSelectionPaint);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | Static.map.drawBuildings(canvas);
|
---|
205 | Static.map.drawUnits(canvas);
|
---|
206 |
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Pointer to the text view to display "Paused.." etc. */
|
---|
213 | //private TextView mStatusText;
|
---|
214 |
|
---|
215 | public GameView(Context context, AttributeSet attrs) {
|
---|
216 | super(context, attrs);
|
---|
217 |
|
---|
218 | // register our interest in hearing about changes to our surface
|
---|
219 | SurfaceHolder holder = getHolder();
|
---|
220 | holder.addCallback(this);
|
---|
221 |
|
---|
222 | // create thread only; it's started in surfaceCreated()
|
---|
223 | Static.thread = new DrawingThread(holder, context, new Handler() {});
|
---|
224 |
|
---|
225 | setFocusable(true); // make sure we get key events
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override public boolean onTouchEvent(MotionEvent event) {
|
---|
229 | Log.i("AdvanceWars", "Detected touch event");
|
---|
230 |
|
---|
231 | if(event.getAction() == MotionEvent.ACTION_UP) {
|
---|
232 | Log.i("AdvanceWars", "Detected UP touch action");
|
---|
233 | switch(Static.gameState) {
|
---|
234 | case MAIN_MENU:
|
---|
235 | Log.i("AdvanceWars", "Switching to battle map");
|
---|
236 | if(Static.wndMainMenu.getGUIObject("btnNewGame").isClicked(event.getX(), event.getY())) {
|
---|
237 | Static.gameState = GameState.BATTLE_MAP;
|
---|
238 | }else if(Static.wndMainMenu.getGUIObject("btnLoadGame").isClicked(event.getX(), event.getY())) {
|
---|
239 | BufferedReader b;
|
---|
240 | try {
|
---|
241 | b = new BufferedReader(new FileReader(android.os.Environment.getExternalStorageDirectory()+"/save.txt"));
|
---|
242 |
|
---|
243 | int width = Integer.parseInt(b.readLine());
|
---|
244 | int height = Integer.parseInt(b.readLine());
|
---|
245 |
|
---|
246 | String offset = b.readLine();
|
---|
247 | Log.i("GameSave", offset);
|
---|
248 | int offsetX = Integer.parseInt(offset.substring(0, offset.indexOf("x")));
|
---|
249 | int offsetY = Integer.parseInt(offset.substring(offset.indexOf("x")+1));
|
---|
250 |
|
---|
251 | Static.map = new Map(Static.grassTile, width, height, new Point(offsetX, offsetY));
|
---|
252 |
|
---|
253 | Log.i("GameSave", "Created the map");
|
---|
254 |
|
---|
255 | for(int x=0; x<width; x++) {
|
---|
256 | String line = b.readLine();
|
---|
257 | Log.i("GameSave", line);
|
---|
258 | String[] arr = line.split(",");
|
---|
259 | for(int y=0; y<arr.length; y++) {
|
---|
260 | TerrainType type = TerrainType.values()[Integer.parseInt(arr[y])];
|
---|
261 | if(type.equals(TerrainType.LAND))
|
---|
262 | Static.map.setTile(x, y, new Tile(Static.grassTile, new Point(10, 25)));
|
---|
263 | else
|
---|
264 | Static.map.setTile(x, y, new Tile(Static.oceanTile, new Point(10, 25)));
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | while(b.ready()) {
|
---|
269 | String unit = b.readLine();
|
---|
270 | Log.i("GameSave", unit);
|
---|
271 | int x = Integer.parseInt(unit.substring(0, unit.indexOf(",")));
|
---|
272 | int y = Integer.parseInt(unit.substring(unit.indexOf(",")+1));
|
---|
273 |
|
---|
274 | Player humanPlayer = new Player("Human", Static.thread.mUnitPaint1);
|
---|
275 |
|
---|
276 | Static.map.getTile(x, y).addUnit(new Soldier(humanPlayer));
|
---|
277 | }
|
---|
278 |
|
---|
279 | b.close();
|
---|
280 | }catch(IOException ioe) {
|
---|
281 | ioe.printStackTrace();
|
---|
282 | }
|
---|
283 | Static.gameState = GameState.BATTLE_MAP;
|
---|
284 | }else if(Static.wndMainMenu.getGUIObject("btnQuit").isClicked(event.getX(), event.getY())) {
|
---|
285 | Static.game.finish();
|
---|
286 | }
|
---|
287 | break;
|
---|
288 | case BATTLE_MAP:
|
---|
289 | Log.i("AdvanceWars", "Touch event detected on battle map");
|
---|
290 |
|
---|
291 | if(event.getX() >= Static.map.offset.x && event.getY() >= Static.map.offset.y) {
|
---|
292 | int x = ((int)event.getX() - Static.map.offset.x) / 50;
|
---|
293 | int y = ((int)event.getY() - Static.map.offset.y) / 50;
|
---|
294 |
|
---|
295 | Unit target = Static.map.getTile(x, y).currentUnit;
|
---|
296 |
|
---|
297 | if(Static.thread.selectedUnit != null && Static.thread.selectedUnit.getMovementRange().contains(new Point(x, y))) {
|
---|
298 | if(target == null || target == Static.thread.selectedUnit) {
|
---|
299 | Static.map.getTile(Static.thread.selectedUnit.location.x, Static.thread.selectedUnit.location.y).removeUnit();
|
---|
300 | Static.map.getTile(x, y).addUnit(Static.thread.selectedUnit);
|
---|
301 | }else {
|
---|
302 | // the target contains another unit. If the unit is enemy-controlled, attack it
|
---|
303 | }
|
---|
304 | Static.thread.selectedUnit = null;
|
---|
305 | }else
|
---|
306 | Static.thread.selectedUnit = target;
|
---|
307 | }
|
---|
308 |
|
---|
309 | Log.i("AdvanceWars", "Touch event handling finished");
|
---|
310 |
|
---|
311 | break;
|
---|
312 | }
|
---|
313 | }else if(event.getAction() == MotionEvent.ACTION_DOWN) {
|
---|
314 |
|
---|
315 | }
|
---|
316 |
|
---|
317 | return true;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /* Callback invoked when the surface dimensions change. */
|
---|
321 | public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
---|
322 | int height) {
|
---|
323 | Static.thread.setSurfaceSize(width, height);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Callback invoked when the Surface has been created and is ready to be
|
---|
328 | * used.
|
---|
329 | */
|
---|
330 | public void surfaceCreated(SurfaceHolder holder) {
|
---|
331 | Static.run = true;
|
---|
332 | Static.thread.start();
|
---|
333 | }
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * Callback invoked when the Surface has been destroyed and must no longer
|
---|
337 | * be touched. WARNING: after this method returns, the Surface/Canvas must
|
---|
338 | * never be touched again!
|
---|
339 | */
|
---|
340 | public void surfaceDestroyed(SurfaceHolder holder) {
|
---|
341 | // we have to tell thread to shut down & wait for it to finish, or else
|
---|
342 | // it might touch the Surface after we return and explode
|
---|
343 | boolean retry = true;
|
---|
344 | Static.run = false;
|
---|
345 | while (retry) {
|
---|
346 | try {
|
---|
347 | Static.thread.join();
|
---|
348 | retry = false;
|
---|
349 | } catch (InterruptedException e) {
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|