· 8 years ago · May 28, 2018, 11:12 AM
1package com.example.gwynbleidd.verstkagame;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.graphics.Bitmap;
6import android.graphics.BitmapFactory;
7import android.graphics.Canvas;
8import android.graphics.Color;
9import android.graphics.Paint;
10import android.support.v7.app.AppCompatActivity;
11import android.view.MotionEvent;
12import android.view.View;
13import android.widget.ImageView;
14import android.widget.Toast;
15
16import com.example.gwynbleidd.verstkagame.UnitClasses.Humans.Archer;
17import com.example.gwynbleidd.verstkagame.UnitClasses.Humans.Mage;
18import com.example.gwynbleidd.verstkagame.UnitClasses.Orcs.OrcRanged;
19import com.example.gwynbleidd.verstkagame.UnitClasses.Unit;
20
21
22public class AlignmentField extends View {
23
24 Integer playerOneGold = 1000;
25 Integer playerTwoGold = 1000;
26
27
28 Resources resources;
29 Context context;
30 Integer cellWidth = 1;
31 Integer cellHeight = 1;
32 Integer indexOfCellY = 0;
33 Integer indexOfCellX = 0;
34 Integer deltaScreenX = 0;
35 Integer deltaScreenY = 0;
36 Integer indexOfCellOnClickY;
37 Integer indexOfCellOnClickX;
38 Integer width;
39 Integer height;
40 Integer[][] matrix1 = new Integer[7][9];
41 Integer[][] matrix2 = new Integer[7][9];
42 Unit[][] unitMatrix1 = new Unit[7][9];
43 Unit[][] unitMatrix2 = new Unit[7][9];
44 Bitmap redImage;
45 Bitmap blueImage;
46 Bitmap archImage;
47 Bitmap btnClassNonePressed ;
48 Bitmap btnClassPressed;
49 //Bitmap btnRaceNonePressed;
50 //Bitmap btnRacePressed;
51 ImageView imageView;
52 OurButton btnDeleteUnit;
53 OurButton btnNextPlayer;
54 OurButton btnStartGame;
55 OurButton btnPreviousPlayer;
56 OurButton btnMan;
57 OurButton btnDwarf;
58 OurButton btnElf;
59 OurButton btnOrc;
60 OurButton btnRanged;
61 OurButton btnSword;
62 OurButton btnHorse;
63 OurButton btnMage;
64
65 public long startTime1 = 0;
66 public long elapsedTime1 = 0;
67 Float dx;
68 Float dy;
69 Boolean fieldIsPressed = false;
70 Boolean flag = false;
71
72 NewGame activity;
73
74
75
76 Integer playerNumber;
77
78
79
80 public Integer getCellWidth() {
81 return cellWidth;
82 }
83
84 public void setCellWidth(Integer cellWidth) {
85 this.cellWidth = cellWidth;
86 }
87
88 public Integer getCellHeight() {
89 return cellHeight;
90 }
91
92 public void setCellHeight(Integer cellHeight) {
93 this.cellHeight = cellHeight;
94 }
95
96 public Integer getIndexOfCellY() {
97 return indexOfCellY;
98 }
99
100 public void setIndexOfCellY(Integer indexOfCellY) {
101 this.indexOfCellY = indexOfCellY;
102 }
103
104 public Integer getIndexOfCellX() {
105 return indexOfCellX;
106 }
107
108 public void setIndexOfCellX(Integer indexOfCellX) {
109 this.indexOfCellX = indexOfCellX;
110 }
111
112 public Integer getDeltaScreenX() {
113 return deltaScreenX;
114 }
115
116 public void setDeltaScreenX(Integer deltaScreenX) {
117 this.deltaScreenX = deltaScreenX;
118 }
119
120 public Integer getDeltaScreenY() {
121 return deltaScreenY;
122 }
123
124 public void setDeltaScreenY(Integer deltaScreenY) {
125 this.deltaScreenY = deltaScreenY;
126 }
127
128 Paint p;
129 Unit replaceableUnit;
130 Unit replaceableUnit2;
131
132 Timer t;
133
134 Bitmap groundImage;
135
136 Bitmap groundCellImage;
137 Bitmap orcImage;
138 Bitmap orcRangedImage;
139
140
141 Unit unitSwordMan;
142 Unit unitSwordFrog;
143 Unit unitSwordElf;
144 Unit unitSwordDwarf;
145
146 Unit unitRangedMan;
147 Unit unitRangedFrog;
148 Unit unitRangedElf;
149 Unit unitRangedDwarf;
150
151 Unit unitMageMan;
152 Unit unitMageFrog;
153 Unit unitMageElf;
154 Unit unitMageDwarf;
155
156 Unit unitHorseMan;
157 Unit unitHorseFrog;
158 Unit unitHorseElf;
159 Unit unitHorseDwarf;
160
161 Bitmap board1;
162
163
164 Paint pRed;
165 Paint pBlack;
166 Paint pYellow;
167 Paint pText;
168 Paint pPlace;
169 Paint pGreen;
170
171
172 public AlignmentField(Context context, Integer width, Integer height, NewGame activity) {
173 super(context);
174 this.context = context;
175 this.cellWidth = width / 8;
176 this.cellHeight = height / 12;
177 this.deltaScreenX = width / 8;
178 this.deltaScreenY = height / 6;
179 this.width = width;
180 this.height = height;
181 this.startTime1 = System.currentTimeMillis();
182 this.activity = activity;
183 this.playerNumber = 1;
184 this.resources = getResources();
185 //btnRaceNonePressed = BitmapFactory.decodeResource(resources, R.drawable.boxoff);
186 // btnRacePressed = BitmapFactory.decodeResource(resources, R.drawable.boxon);
187 btnClassNonePressed = BitmapFactory.decodeResource(resources, R.drawable.boxoff);
188 btnClassPressed = BitmapFactory.decodeResource(resources, R.drawable.boxon);
189 btnNextPlayer = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth,cellHeight* 2);
190 btnPreviousPlayer = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth,cellHeight * 2);
191 btnStartGame = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth,cellHeight * 2);
192 redImage = BitmapFactory.decodeResource(resources, R.drawable.mage);
193 blueImage = BitmapFactory.decodeResource(resources, R.drawable.swordman);
194 archImage = BitmapFactory.decodeResource(resources, R.drawable.archer);
195 orcImage = BitmapFactory.decodeResource(resources, R.drawable.orc_melee);
196 orcRangedImage = BitmapFactory.decodeResource(resources, R.drawable.orc_ranged);
197
198
199 groundImage = BitmapFactory.decodeResource(resources, R.drawable.ground_test);
200 groundCellImage = Bitmap.createScaledBitmap(groundImage, cellWidth, cellHeight, false);
201 // groundOutImage = Bitmap.createScaledBitmap(groundImage, cellWidth * 2, cellHeight * 2, false);
202
203 // redUnit = new Unit("red", getResources(), cellWidth, cellHeight, redImage, false,10,10,0,2,1);
204 // blueUnit = new Unit("blue", getResources(), cellWidth, cellHeight, blueImage,false,20,5,0,2,1);
205 // archer = new Archer("blue", getResources(), cellWidth, cellHeight, archImage,false,10,10,0,2,1);
206 replaceableUnit = new Unit("SwordMan", resources, cellWidth, cellHeight, blueImage, groundCellImage, false, 20, 5, 0, 2 ,1,100);
207 // unitSwordMan = new Unit("blue", resources, cellWidth, cellHeight, blueImage, groundCellImage,false,20,5,0,2,1, 100);
208 // unitMageMan = new Mage("red", resources, cellWidth, cellHeight, redImage,groundCellImage, false,10,10,0,2,1);
209 // unitRangedMan = new Archer("blue", resources, cellWidth, cellHeight, groundCellImage,false,10,10,0,2,1);
210
211
212
213
214
215 btnMan = new OurButton(btnClassNonePressed, btnClassPressed, resources, cellWidth* 2, cellHeight/ 2);
216 btnDwarf = new OurButton(btnClassNonePressed, btnClassPressed, resources, cellWidth* 2, cellHeight/ 2);
217 btnElf = new OurButton(btnClassNonePressed, btnClassPressed, resources, cellWidth* 2, cellHeight/ 2);
218 btnOrc = new OurButton(btnClassNonePressed, btnClassPressed, resources, cellWidth* 2, cellHeight/ 2);
219
220 btnMage = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth* 2,cellHeight/ 2);
221 btnSword = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth* 2,cellHeight/ 2);
222 btnHorse = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth* 2,cellHeight/ 2);
223 btnRanged = new OurButton(btnClassNonePressed, btnClassPressed ,resources,cellWidth* 2,cellHeight/ 2);
224
225
226 btnDeleteUnit = new OurButton(btnClassNonePressed, btnClassPressed, resources, cellWidth * 2, cellHeight / 2);
227
228 btnMan.isPressed = true;
229 btnSword.isPressed = true;
230
231
232
233 for (int i = 0; i < 7; i++) {
234 for (int j = 0; j < 9; j++) {
235 this.matrix1[i][j] = 0;
236 this.matrix2[i][j] = 0;
237 this.unitMatrix1[i][j] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
238 this.unitMatrix2[i][j] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
239
240 }
241 }
242
243
244
245 p = new Paint();
246 t = new Timer(this, Integer.MAX_VALUE, 100);
247 t.start();
248
249 board1 = BitmapFactory.decodeResource(resources, R.drawable.board1);
250 board1 = Bitmap.createScaledBitmap(board1, width, height, false);
251
252
253
254
255
256 pBlack = new Paint();
257 pBlack.setColor(Color.BLACK);
258 pBlack.setStrokeWidth(width/150);
259 pYellow = new Paint();
260 pYellow.setColor(Color.YELLOW);
261 pYellow.setStrokeWidth(width/150);
262 pYellow.setStyle(Paint.Style.STROKE);
263 pGreen = new Paint();
264 pGreen.setColor(Color.GREEN);
265 pGreen.setStrokeWidth(width/150);
266 pGreen.setStyle(Paint.Style.STROKE);
267 pRed = new Paint();
268 pRed.setColor(Color.RED);
269 pRed.setStrokeWidth(width/150);
270 pRed.setStyle(Paint.Style.STROKE);
271
272
273 pText = new Paint();
274 pText.setTextSize(cellHeight / 2.1f);
275
276
277
278 pPlace = new Paint();
279 pPlace.setStyle(Paint.Style.STROKE);
280 pPlace.setColor(Color.BLUE);
281 pPlace.setAlpha(127);
282
283 }
284
285
286
287 @Override
288 protected void onDraw(Canvas canvas) {
289
290 canvas.drawBitmap(board1, 0f, 0f, p);
291
292 for (int i = 0; i < 9; i++) {
293 canvas.drawLine(deltaScreenX, i*cellHeight+deltaScreenY, canvas.getWidth() - deltaScreenX, i*cellHeight + deltaScreenY, pBlack);
294 }
295
296 for (int i = 0; i < 7; i++) {
297 canvas.drawLine(i*cellWidth + deltaScreenX, deltaScreenY, i*cellWidth + deltaScreenX, canvas.getHeight() - deltaScreenY, pBlack);
298 }
299
300
301
302 if (playerNumber == 1) {
303 for (int i = 0; i < 6; i++) {
304 for (int j = 0; j < 8; j++) {
305 unitMatrix1[i][j].drawInCell(canvas, deltaScreenX + cellWidth * i * 1f, deltaScreenY + cellHeight * j * 1f);
306 }
307
308 }
309 } else {
310 for (int i = 0; i < 6; i++) {
311 for (int j = 0; j < 8; j++) {
312 unitMatrix2[i][j].drawInCell(canvas, deltaScreenX + cellWidth * i * 1f, deltaScreenY + cellHeight * j * 1f);
313 }
314
315 }
316 }
317
318
319 replaceableUnit.drawOutCell(canvas,cellWidth * 6,0);
320
321
322 btnMan.draw(canvas, deltaScreenX * 1f, 0f);
323 canvas.drawText("Man", deltaScreenX * 1.3f, deltaScreenY * 0.2f, pText);
324 btnDwarf.draw(canvas, deltaScreenX * 1f, deltaScreenY * 0.25f);
325 canvas.drawText("Dwarf", deltaScreenX * 1.3f, deltaScreenY * 0.45f, pText);
326 btnElf.draw(canvas, deltaScreenX * 1f, deltaScreenY * 0.5f);
327 canvas.drawText("Elf", deltaScreenX * 1.3f, deltaScreenY * 0.7f, pText);
328 btnOrc.draw(canvas, deltaScreenX * 1f, deltaScreenY * 0.75f);
329 canvas.drawText("Orc", deltaScreenX * 1.3f, deltaScreenY * 0.95f, pText);
330 btnSword.draw(canvas, deltaScreenX + cellWidth * 3f, 0f);
331 canvas.drawText("Sword", deltaScreenX + cellWidth * 3.25f, deltaScreenY * 0.2f, pText);
332 btnMage.draw(canvas, deltaScreenX + cellWidth * 3f, deltaScreenY * 0.25f);
333 canvas.drawText("Mage", deltaScreenX + cellWidth * 3.25f, deltaScreenY * 0.45f, pText);
334 btnHorse.draw(canvas, deltaScreenX + cellWidth * 3f, deltaScreenY * 0.5f);
335 canvas.drawText("Horse", deltaScreenX + cellWidth * 3.25f, deltaScreenY * 0.7f, pText);
336 btnRanged.draw(canvas, deltaScreenX + cellWidth * 3f, deltaScreenY * 0.75f);
337 canvas.drawText("Ranged", deltaScreenX + cellWidth * 3.25f, deltaScreenY * 0.95f, pText);
338
339
340 if (playerNumber == 1) {
341 btnNextPlayer.draw(canvas, deltaScreenX + cellWidth * 6f, deltaScreenY + cellHeight * 3f);
342 canvas.drawText(playerOneGold.toString(),cellWidth * 5f, deltaScreenY + cellHeight * 9f, pText);
343 canvas.drawText("Amount of gold:", cellWidth * 1f, deltaScreenY + cellHeight * 9f, pText);
344 }
345 else {
346 btnStartGame.draw(canvas, deltaScreenX + cellWidth * 6f, deltaScreenY + cellHeight * 3f);
347 btnPreviousPlayer.draw(canvas, 0f, deltaScreenY + cellHeight * 3f);
348 canvas.drawText(playerTwoGold.toString(), cellWidth * 5f, deltaScreenY + cellHeight * 9f, pText);
349 canvas.drawText("Amount of gold:", cellWidth * 1f, deltaScreenY + cellHeight * 9f, pText);
350 }
351
352
353
354
355
356 if (playerNumber == 1) {
357 if (fieldIsPressed) {
358 for (int i = 0; i < 6; i++) {
359 for (int j = 0; j < 8; j++) {
360 if (!unitMatrix1[indexOfCellX][indexOfCellY].isGround())
361 if (unitMatrix1[i][j].isGround()) {
362 if (Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix1[indexOfCellX][indexOfCellY].MOVE) {
363 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pGreen);
364 }
365 } else {
366 if (Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix1[indexOfCellX][indexOfCellY].RANGE) {
367 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pRed);
368 }
369 }
370 }
371 }
372
373
374 if (!unitMatrix1[indexOfCellX][indexOfCellY].isGround())
375 unitMatrix1[indexOfCellX][indexOfCellY].drawOutCell(canvas, +cellWidth * 6, height - deltaScreenY);
376 canvas.drawRect(indexOfCellX * cellWidth + deltaScreenX, indexOfCellY * cellHeight + deltaScreenY, (indexOfCellX + 1) * cellWidth + deltaScreenX, (indexOfCellY + 1) * cellHeight + deltaScreenY, pYellow);
377
378 }
379 } else {
380 if (fieldIsPressed) {
381 for (int i = 0; i < 6; i++) {
382 for (int j = 0; j < 8; j++) {
383 if (!unitMatrix2[indexOfCellX][indexOfCellY].isGround())
384 if (unitMatrix2[i][j].isGround()) {
385 if (Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix2[indexOfCellX][indexOfCellY].MOVE) {
386 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pGreen);
387 }
388 } else {
389 if (Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix2[indexOfCellX][indexOfCellY].RANGE) {
390 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pRed);
391 }
392 }
393 }
394 }
395
396 if (!unitMatrix2[indexOfCellX][indexOfCellY].isGround())
397 unitMatrix2[indexOfCellX][indexOfCellY].drawOutCell(canvas, +cellWidth * 6, height - deltaScreenY);
398 canvas.drawRect(indexOfCellX * cellWidth + deltaScreenX, indexOfCellY * cellHeight + deltaScreenY, (indexOfCellX + 1) * cellWidth + deltaScreenX, (indexOfCellY + 1) * cellHeight + deltaScreenY, pYellow);
399
400 }
401 }
402
403
404 if (replaceableUnit2 != null)
405 replaceableUnit2.drawInCell(canvas, dx, dy);
406
407
408
409 if (playerNumber == 1) {
410 if (!unitMatrix1[indexOfCellX][indexOfCellY].isGround()) {
411 btnDeleteUnit.draw(canvas, deltaScreenX + cellWidth * 2f, deltaScreenY + cellHeight * 9.25f);
412 }
413 } else {
414 if (!unitMatrix2[indexOfCellX][indexOfCellY].isGround()) {
415 btnDeleteUnit.draw(canvas, deltaScreenX + cellWidth * 2f, deltaScreenY + cellHeight * 9.25f);
416 }
417 }
418
419
420 if (playerNumber == 1) {
421 for (int i = 0; i < 6; i++) {
422 for (int j = 5; j < 8; j++) {
423 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pPlace);
424 }
425 }
426 } else {
427 for (int i = 0; i < 6; i++) {
428 for (int j = 0; j < 3; j++) {
429 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pPlace);
430 }
431 }
432 }
433
434 }
435
436
437
438
439
440 @Override
441 public boolean onTouchEvent(MotionEvent event) {
442
443
444 //При нажатии на кнопку удалениÑ, юнит заменÑетÑÑ Ð½Ð° землю
445 if (playerNumber == 1) {
446 if (!unitMatrix1[indexOfCellX][indexOfCellY].isGround()) {
447 if ((event.getX() >= deltaScreenX + cellWidth * 2f) && (event.getX() <= deltaScreenX + cellWidth * 4f) && (event.getY() >= deltaScreenY + cellHeight * 9.25f) && (event.getY() <= deltaScreenY + cellHeight * 9.75f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
448 matrix1[indexOfCellX][indexOfCellY] = 0;
449 playerOneGold += unitMatrix1[indexOfCellX][indexOfCellY].COST;
450 unitMatrix1[indexOfCellX][indexOfCellY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
451 }
452 }
453 } else {
454 if (!unitMatrix2[indexOfCellX][indexOfCellY].isGround()) {
455 if ((event.getX() >= deltaScreenX + cellWidth * 2f) && (event.getX() <= deltaScreenX + cellWidth * 4f) && (event.getY() >= deltaScreenY + cellHeight * 9.25f) && (event.getY() <= deltaScreenY + cellHeight * 9.75f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
456 matrix2[indexOfCellX][indexOfCellY] = 0;
457 playerTwoGold += unitMatrix2[indexOfCellX][indexOfCellY].COST;
458 unitMatrix2[indexOfCellX][indexOfCellY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
459 }
460 }
461 }
462
463
464
465 if ((event.getX() >= deltaScreenX) && (event.getX() <= cellWidth * 6 + deltaScreenX) && (event.getY() >= deltaScreenY) && (event.getY() <= cellHeight * 8 + deltaScreenY)) {
466 for (int i = 1; i < 9; i++) {
467 if (event.getY() > cellHeight * (i - 1) + deltaScreenY && event.getY() < cellHeight * i + deltaScreenY) {
468 this.indexOfCellY = i - 1;
469 }
470 }
471
472 for (int i = 1; i < 7; i++) {
473 if (event.getX() > cellWidth * (i - 1) + deltaScreenX && event.getX() < cellWidth * i + deltaScreenX) {
474 this.indexOfCellX = i - 1;
475 }
476 }
477 fieldIsPressed = true;
478 } else {
479 fieldIsPressed = false;
480 indexOfCellX = 6;
481 indexOfCellY = 8;
482 }
483
484
485
486
487 //Кнопки, отвечающие за раÑу юнита (тут ничего менÑть не надо)
488 if ((event.getX() >= deltaScreenX * 1f) && (event.getX() <= deltaScreenX + cellWidth * 2f) && (event.getY() >= 0f) && (event.getY() <= deltaScreenY * 0.25f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
489 btnMan.isPressed = true;
490 btnDwarf.isPressed = false;
491 btnElf.isPressed = false;
492 btnOrc.isPressed = false;
493 } else if ((event.getX() >= deltaScreenX * 1f) && (event.getX() <= deltaScreenX + cellWidth * 2f) && (event.getY() >= deltaScreenY * 0.25f) && (event.getY() <= deltaScreenY * 0.5f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
494 btnMan.isPressed = false;
495 btnDwarf.isPressed = true;
496 btnElf.isPressed = false;
497 btnOrc.isPressed = false;
498 } else if ((event.getX() >= deltaScreenX * 1f) && (event.getX() <= deltaScreenX + cellWidth * 2f) && (event.getY() >= deltaScreenY * 0.5f) && (event.getY() <= deltaScreenY * 0.75f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
499 btnMan.isPressed = false;
500 btnDwarf.isPressed = false;
501 btnElf.isPressed = true;
502 btnOrc.isPressed = false;
503 } else if ((event.getX() >= deltaScreenX * 1f) && (event.getX() <= deltaScreenX + cellWidth * 2f) && (event.getY() >= deltaScreenY * 0.75f) && (event.getY() <= deltaScreenY * 1f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
504 btnMan.isPressed = false;
505 btnDwarf.isPressed = false;
506 btnElf.isPressed = false;
507 btnOrc.isPressed = true;
508 }
509
510
511 //Кнопки, отвечающие за клаÑÑ ÑŽÐ½Ð¸Ñ‚Ð° (тут ничего менÑть не надо)
512 if ((event.getX() >= deltaScreenX + cellWidth * 3f) && (event.getX() <= deltaScreenX + cellWidth * 5f) && (event.getY() >= 0f) && (event.getY() <= deltaScreenY * 0.25f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
513 btnSword.isPressed = true;
514 btnMage.isPressed = false;
515 btnHorse.isPressed = false;
516 btnRanged.isPressed = false;
517 } else if ((event.getX() >= deltaScreenX + cellWidth * 3f) && (event.getX() <= deltaScreenX + cellWidth * 5f) && (event.getY() >= deltaScreenY * 0.25f) && (event.getY() <= deltaScreenY * 0.5f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
518 btnSword.isPressed = false;
519 btnMage.isPressed = true;
520 btnHorse.isPressed = false;
521 btnRanged.isPressed = false;
522 } else if ((event.getX() >= deltaScreenX + cellWidth * 3f) && (event.getX() <= deltaScreenX + cellWidth * 5f) && (event.getY() >= deltaScreenY * 0.5f) && (event.getY() <= deltaScreenY * 0.75f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
523 btnSword.isPressed = false;
524 btnMage.isPressed = false;
525 btnHorse.isPressed = true;
526 btnRanged.isPressed = false;
527 } else if ((event.getX() >= deltaScreenX + cellWidth * 3f) && (event.getX() <= deltaScreenX + cellWidth * 5f) && (event.getY() >= deltaScreenY * 0.75f) && (event.getY() <= deltaScreenY * 1f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
528 btnSword.isPressed = false;
529 btnMage.isPressed = false;
530 btnHorse.isPressed = false;
531 btnRanged.isPressed = true;
532 }
533
534
535 // Сюда надо вÑтавлÑть юнитов в завиÑимоÑти от того, ÐºÐ°ÐºÐ°Ñ ÐºÐ¾Ð¼Ð±Ð¸Ð½Ð°Ñ†Ð¸Ñ ÐºÐ½Ð¾Ð¿Ð¾Ðº нажата
536 if ((btnMan.isPressed) && (btnSword.isPressed) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
537 replaceableUnit = new Unit("Swordman", resources, cellWidth, cellHeight, blueImage, groundCellImage,false,20,5,0,2,1, 100);
538 } else if ((btnMan.isPressed) && (btnRanged.isPressed) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
539 replaceableUnit = new Archer("Archer", resources, cellWidth, cellHeight, BitmapFactory.decodeResource(resources, R.drawable.archer), BitmapFactory.decodeResource(resources, R.drawable.archer_model2), BitmapFactory.decodeResource(resources, R.drawable.archer_model2), groundCellImage,false,10,10,0,2,1);
540 } else if ((btnMan.isPressed) && (btnMage.isPressed) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
541 replaceableUnit = new Mage("Mage", resources, cellWidth, cellHeight, redImage,groundCellImage, false,15,10,0,2,2);
542 } else if ((btnOrc.isPressed) && (btnSword.isPressed) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
543 replaceableUnit = new Unit("Orc", resources, cellWidth, cellHeight, orcImage, groundCellImage, false, 15, 10, 0, 1, 2, 150);
544 } else if ((btnOrc.isPressed) && (btnRanged.isPressed) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
545 replaceableUnit = new OrcRanged("OrcRanged", resources, cellWidth, cellHeight, orcRangedImage, groundCellImage, false, 20, 8, 0 , 2 , 1, 200);
546 }
547
548
549
550 //ЧаÑть кода, Ð¾Ñ‚Ð²ÐµÑ‡Ð°ÑŽÑ‰Ð°Ñ Ð·Ð° Ð¿ÐµÑ€ÐµÐ½Ð¾Ñ ÑŽÐ½Ð¸Ñ‚Ð° на поле
551 if (playerNumber == 1) {
552 if ((((event.getY() < deltaScreenY) && (event.getX() > deltaScreenX + cellWidth * 5)) || (flag)) && (playerOneGold - replaceableUnit.COST >= 0)) {
553 replaceableUnit2 = replaceableUnit;
554 dx = event.getX();
555 dy = event.getY();
556 flag = true;
557 if (event.getAction() == MotionEvent.ACTION_UP) {
558 flag = false;
559 if ((fieldIsPressed) && (indexOfCellY >= 5)) {
560 if (!unitMatrix1[indexOfCellX][indexOfCellY].isGround())
561 playerOneGold += unitMatrix1[indexOfCellX][indexOfCellY].COST;
562 unitMatrix1[indexOfCellX][indexOfCellY] = replaceableUnit2;
563 playerOneGold -= replaceableUnit.COST;
564
565 //Ð’ Ñтой чаÑти заполнÑетÑÑ Ð¼Ð°ÑÑив, который потом передаÑÑ‚ÑÑ Ð² BattleField, так что при добавлении новых юнитов, надо пропиÑывать Ñюда их заполнение в маÑÑив
566 if ((btnMan.isPressed) && (btnMage.isPressed))
567 matrix1[indexOfCellX][indexOfCellY] = 1;
568 else if ((btnMan.isPressed) && (btnSword.isPressed))
569 matrix1[indexOfCellX][indexOfCellY] = 2;
570 else if ((btnMan.isPressed) && (btnRanged.isPressed))
571 matrix1[indexOfCellX][indexOfCellY] = 3;
572 else if ((btnOrc.isPressed) && (btnSword.isPressed))
573 matrix1[indexOfCellX][indexOfCellY] = 4;
574 else if ((btnOrc.isPressed) && (btnRanged.isPressed))
575 matrix1[indexOfCellX][indexOfCellY] = 5;
576 }
577 replaceableUnit2 = null;
578 }
579 }
580 } else {
581 if ((((event.getY() < deltaScreenY) && (event.getX() > deltaScreenX + cellWidth * 5)) || (flag)) && (playerTwoGold - replaceableUnit.COST >= 0)) {
582 replaceableUnit2 = replaceableUnit;
583 dx = event.getX();
584 dy = event.getY();
585 flag = true;
586 if (event.getAction() == MotionEvent.ACTION_UP) {
587 flag = false;
588 if ((fieldIsPressed) && (indexOfCellY <= 2) && (playerTwoGold >= 0)) {
589 if (!unitMatrix2[indexOfCellX][indexOfCellY].isGround())
590 playerTwoGold += unitMatrix2[indexOfCellX][indexOfCellY].COST;
591 unitMatrix2[indexOfCellX][indexOfCellY] = replaceableUnit2;
592 playerTwoGold -= replaceableUnit.COST;
593
594 //Ð’ Ñтой чаÑти заполнÑетÑÑ Ð¼Ð°ÑÑив, который потом передаÑÑ‚ÑÑ Ð² BattleField, так что при добавлении новых юнитов, надо пропиÑывать Ñюда их заполнение в маÑÑив
595 if ((btnMan.isPressed) && (btnMage.isPressed))
596 matrix2[indexOfCellX][indexOfCellY] = 1;
597 else if ((btnMan.isPressed) && (btnSword.isPressed))
598 matrix2[indexOfCellX][indexOfCellY] = 2;
599 else if ((btnMan.isPressed) && (btnRanged.isPressed))
600 matrix2[indexOfCellX][indexOfCellY] = 3;
601 else if ((btnOrc.isPressed) && (btnSword.isPressed))
602 matrix2[indexOfCellX][indexOfCellY] = 4;
603 else if ((btnOrc.isPressed) && (btnRanged.isPressed))
604 matrix2[indexOfCellX][indexOfCellY] = 5;
605 }
606 replaceableUnit2 = null;
607 }
608 }
609 }
610
611
612
613
614
615
616
617
618 if ((event.getX() >= deltaScreenX + cellWidth * 6f) && (event.getX() <= deltaScreenX + cellWidth * 7f) && (event.getY() >= deltaScreenY + cellHeight * 3f) && (event.getY() <= deltaScreenY + cellHeight * 5f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
619
620
621 if (playerNumber == 1) {
622 btnNextPlayer.isPressed = true;
623 btnStartGame.isPressed = false;
624 btnPreviousPlayer.isPressed = false;
625 playerNumber = 2;
626 } else {
627 btnStartGame.isPressed = true;
628 activity.finish();
629 t.onFinish();
630 }
631
632 }
633
634 if ((event.getX() >= 0f) && (event.getX() <= deltaScreenX) && (event.getY() >= deltaScreenY + cellHeight * 3f) && (event.getY() <= deltaScreenY + cellHeight * 5f) && (event.getAction() == MotionEvent.ACTION_DOWN)) {
635
636
637 if (playerNumber == 1) {
638
639 } else {
640 btnPreviousPlayer.isPressed = true;
641 btnNextPlayer.isPressed = false;
642 playerNumber = 1;
643 }
644
645 }
646
647
648
649 return true;
650 }
651
652
653}
654
655
656
657
658
659
660
661
662
663
664
665package com.example.gwynbleidd.verstkagame;
666
667
668import android.app.Dialog;
669import android.content.ContentValues;
670import android.database.sqlite.SQLiteDatabase;
671import android.graphics.Point;
672import android.os.Bundle;
673import android.support.v7.app.AppCompatActivity;
674import android.view.Display;
675import android.view.View;
676import android.widget.Button;
677
678import com.example.gwynbleidd.verstkagame.UnitClasses.Unit;
679
680
681public class GameActivity extends AppCompatActivity {
682
683 Integer[][] matrix1;
684 Integer[][] matrix2;
685 BattleField battleField;
686 Dialog menu;
687 Dialog saveMenu;
688 Dialog winMessage;
689
690 AppCompatActivity game;
691 Integer[][] HPMatrix = new Integer[7][9];
692 Integer[][] VALUABLEMOVEMatrix = new Integer[7][9];
693 Boolean[][] OWNERMatrix = new Boolean[7][9];
694 Boolean[][] canAttackMatrix = new Boolean[7][9];
695 String[][] nameMatrix = new String[7][9];
696
697 @Override
698 protected void onCreate(Bundle savedInstanceState) {
699 super.onCreate(savedInstanceState);
700 this.game = this;
701 getSupportActionBar().hide();
702 Display display = getWindowManager().getDefaultDisplay();
703 Point size = new Point();
704 display.getSize(size);
705 int width = size.x;
706 int height = size.y;
707 menu = new Dialog(GameActivity.this);
708 menu.setContentView(R.layout.menu_layout);
709 menu.setTitle("Меню");
710 menu.getWindow().setLayout(width / 2, height / 2);
711
712
713
714 winMessage= new Dialog(GameActivity.this);
715
716 saveMenu = new Dialog(GameActivity.this);
717 saveMenu.setContentView(R.layout.save_screen);
718 saveMenu.setTitle("Сохранение");
719 saveMenu.getWindow().setLayout(width / 2, height / 2);
720
721
722 Button btnBack = (Button) saveMenu.findViewById(R.id.btnBack);
723 btnBack.setOnClickListener(new View.OnClickListener() {
724 @Override
725 public void onClick(View view) {
726 saveMenu.cancel();
727 }
728 });
729
730 Button btnSave1 = (Button) saveMenu.findViewById(R.id.save1);
731 Button btnSave2 = (Button) saveMenu.findViewById(R.id.save2);
732 Button btnSave3 = (Button) saveMenu.findViewById(R.id.save3);
733 Button btnSave4 = (Button) saveMenu.findViewById(R.id.save4);
734 Button btnSave5 = (Button) saveMenu.findViewById(R.id.save5);
735 Button btnSave6 = (Button) saveMenu.findViewById(R.id.save6);
736
737 /* btnSave1.setOnClickListener(new View.OnClickListener() {
738 @Override
739 public void onClick(View view) {
740 SQLiteDatabase myDB = openOrCreateDatabase("save1.db", MODE_PRIVATE, null);
741 myDB.execSQL("DROP TABLE IF EXISTS save1");
742 myDB.execSQL(
743 "CREATE TABLE IF NOT EXISTS save1 (name TEXT, indexX INT, indexY INT, HP INT, canAttack INT, VALUABLEMOVE INT, OWNER INT)"
744 );
745
746 for (int i = 0; i < 6; i++) {
747 for (int j = 0; j < 8; j++) {
748 if (!battleField.unitMatrix[i][j].isGround()) {
749 ContentValues tmpRow = new ContentValues();
750 tmpRow.put("name", battleField.unitMatrix[i][j].name);
751 tmpRow.put("indexX", i);
752 tmpRow.put("indexY", j);
753 tmpRow.put("HP", battleField.unitMatrix[i][j].HP);
754 tmpRow.put("canAttack", (battleField.unitMatrix[i][j].canAttack == true ? 1 : 0));
755 tmpRow.put("VALUABLEMOVE", battleField.unitMatrix[i][j].VALUABLEMOVE);
756 tmpRow.put("OWNER", (battleField.unitMatrix[i][j].Owner == true ? 1 : 0));
757 myDB.insert("save1", null, tmpRow);
758 }
759 }
760 }
761
762 myDB.close();
763
764 }
765 });*/
766
767
768
769 Button btnSave = (Button) menu.findViewById(R.id.saveGame);
770 btnSave.setText("Сохранить игру");
771 Button btnMainMenu = (Button) menu.findViewById(R.id.mainMenu);
772 btnMainMenu.setText("В главное меню");
773
774 btnSave.setOnClickListener(new View.OnClickListener() {
775 @Override
776 public void onClick(View view) {
777 SQLiteDatabase myDB = openOrCreateDatabase("save1.db", MODE_PRIVATE, null);
778 myDB.execSQL("DROP TABLE IF EXISTS save1");
779 myDB.execSQL(
780 "CREATE TABLE IF NOT EXISTS save1 (name TEXT, indexX INT, indexY INT, HP INT, canAttack INT, VALUABLEMOVE INT, OWNER INT)"
781 );
782
783 for (int i = 0; i < 6; i++) {
784 for (int j = 0; j < 8; j++) {
785 if (!battleField.unitMatrix[i][j].isGround()) {
786 ContentValues tmpRow = new ContentValues();
787 tmpRow.put("name", battleField.unitMatrix[i][j].name);
788 tmpRow.put("indexX", i);
789 tmpRow.put("indexY", j);
790 tmpRow.put("HP", battleField.unitMatrix[i][j].HP);
791 tmpRow.put("canAttack", (battleField.unitMatrix[i][j].canAttack == true ? 1 : 0));
792 tmpRow.put("VALUABLEMOVE", battleField.unitMatrix[i][j].VALUABLEMOVE);
793 tmpRow.put("OWNER", (battleField.unitMatrix[i][j].Owner == true ? 1 : 0));
794 myDB.insert("save1", null, tmpRow);
795 }
796 }
797 }
798
799 myDB.close();
800 }
801 });
802
803 btnMainMenu.setOnClickListener(new View.OnClickListener() {
804 @Override
805 public void onClick(View view) {
806 menu.cancel();
807 game.finish();
808 }
809 });
810
811
812
813
814 matrix1 = (Integer[][]) getIntent().getExtras().getSerializable("UnitMatrixPlayerOne");
815 matrix2 = (Integer[][]) getIntent().getExtras().getSerializable("UnitMatrixPlayerTwo");
816 HPMatrix = (Integer[][]) getIntent().getExtras().getSerializable("HPMatrix");
817 VALUABLEMOVEMatrix = (Integer[][]) getIntent().getExtras().getSerializable("VALUABLEMOVEMatrix");
818 nameMatrix = (String[][]) getIntent().getExtras().getSerializable("nameMatrix");
819 canAttackMatrix = (Boolean[][]) getIntent().getExtras().getSerializable("canAttackMatrix");
820 OWNERMatrix = (Boolean[][]) getIntent().getExtras().getSerializable("OWNERMatrix");
821 battleField = new BattleField(this, width, height, matrix1, matrix2, nameMatrix, HPMatrix, canAttackMatrix, OWNERMatrix, VALUABLEMOVEMatrix, menu, winMessage);
822 setContentView(battleField);
823
824
825
826
827 winMessage.setContentView(R.layout.win_message);
828 winMessage.getWindow().setLayout(width / 2, height / 8);
829 Button btnToMainMenu = (Button) winMessage.findViewById(R.id.toMainMenu);
830 Button btnWait = (Button) winMessage.findViewById(R.id.btnWait);
831
832 btnToMainMenu.setOnClickListener(new View.OnClickListener() {
833 @Override
834 public void onClick(View view) {
835 game.finish();
836 }
837 });
838
839 btnWait.setOnClickListener(new View.OnClickListener() {
840 @Override
841 public void onClick(View view) {
842 winMessage.cancel();
843 }
844 });
845
846
847
848 }
849
850
851 @Override
852 protected void onDestroy() {
853 battleField.iconattack.recycle();
854 battleField.armor.recycle();
855 battleField.blue2Image.recycle();
856 battleField.blueImage.recycle();
857 battleField.red2Image.recycle();
858 battleField.redImage.recycle();
859 battleField.turn.recycle();
860 battleField.turn2.recycle();
861 battleField.frame.recycle();
862 battleField.board1.recycle();
863 battleField.groundCellImage.recycle();
864 battleField.groundImage.recycle();
865 battleField.btnMenuImage.recycle();
866 for (int i = 0; i < 6; i++) {
867 for (int j = 0; j < 8; j++) {
868 battleField.unitMatrix[i][j].cellImage.recycle();
869 if (battleField.unitMatrix[i][j].outImage != null)
870 battleField.unitMatrix[i][j].outImage.recycle();
871 if (battleField.unitMatrix[i][j].cellImageModel1 != null)
872 battleField.unitMatrix[i][j].cellImageModel1.recycle();
873 if (battleField.unitMatrix[i][j].cellImageModel2 != null)
874 battleField.unitMatrix[i][j].cellImageModel2.recycle();
875 if (battleField.unitMatrix[i][j].cellImageModel3 != null)
876 battleField.unitMatrix[i][j].cellImageModel3.recycle();
877 }
878 }
879 super.onDestroy();
880 }
881}
882
883
884
885
886
887
888
889
890
891
892
893
894package com.example.gwynbleidd.verstkagame.UnitClasses;
895
896
897import android.content.res.Resources;
898import android.graphics.Bitmap;
899import android.graphics.BitmapFactory;
900import android.graphics.Canvas;
901import android.graphics.Paint;
902
903import com.example.gwynbleidd.verstkagame.R;
904
905public class Unit {
906 public String name;
907 public Bitmap cellImage;
908 public Bitmap outImage;
909 public boolean ground;
910 public Integer HP;
911 public Integer MAXHP;
912 public Integer getHP() {
913 return HP;
914 }
915
916 public void setHP(Integer HP) {
917 this.HP = HP;
918 }
919
920 public Integer getDMG() {
921 return DMG;
922 }
923
924 public void setDMG(Integer DMG) {
925 this.DMG = DMG;
926 }
927
928 public Integer getSHIELD() {
929 return SHIELD;
930 }
931
932 public void setSHIELD(Integer SHIELD) {
933 this.SHIELD = SHIELD;
934 }
935
936 public Bitmap groundCellImage;
937 public Integer DMG;
938 public Integer SHIELD;
939 public Integer MOVE;
940 public Integer RANGE;
941 public Resources resources;
942 public Integer cellWidth;
943 public Integer cellHeight;
944 public Boolean Owner;
945 public Integer VALUABLEMOVE;
946 public Integer COST;
947 public Boolean canAttack;
948
949 public Integer modelInCellChangeCounter = 0;
950
951 public Bitmap cellImageModel1;
952 public Bitmap cellImageModel2;
953 public Bitmap cellImageModel3;
954
955
956 public Unit(String name, Resources resources, Integer cellWidth, Integer cellHeight, Bitmap image, Bitmap groundCellImage, boolean isground, Integer HP, Integer DMG, Integer SHIELD, Integer RANGE, Integer MOVE, Integer COST) {
957 this.name = name;
958 outImage = Bitmap.createScaledBitmap(image, cellWidth * 2, cellHeight * 2, false);
959 cellImage = Bitmap.createScaledBitmap(image, cellWidth, cellHeight, false);
960 this.ground = isground;
961 this.MAXHP = HP;
962 this.HP = MAXHP;
963 this.DMG = DMG;
964 this.MOVE = MOVE;
965 this.SHIELD = SHIELD;
966 this.RANGE = RANGE;
967 this.resources = resources;
968 this.cellHeight = cellHeight;
969 this.cellWidth = cellWidth;
970 this.VALUABLEMOVE = MOVE;
971 this.COST = COST;
972 this.groundCellImage = groundCellImage;
973 this.canAttack = true;
974
975 }
976
977 public Unit(String name, Resources resources, Integer cellWidth, Integer cellHeight, Bitmap image, Bitmap image2, Bitmap image3, Bitmap groundCellImage, boolean isground, Integer HP, Integer DMG, Integer SHIELD, Integer RANGE, Integer MOVE, Integer COST) {
978 this.name = name;
979 cellImageModel1 = Bitmap.createScaledBitmap(image, cellWidth, cellHeight, false);
980 outImage = Bitmap.createScaledBitmap(image, cellWidth * 2, cellHeight * 2, false);
981 cellImage = cellImageModel1;
982 this.ground = isground;
983 this.MAXHP = HP;
984 this.HP = MAXHP;
985 this.DMG = DMG;
986 this.MOVE = MOVE;
987 this.SHIELD = SHIELD;
988 this.RANGE = RANGE;
989 this.resources = resources;
990 this.cellHeight = cellHeight;
991 this.cellWidth = cellWidth;
992 this.VALUABLEMOVE = MOVE;
993 this.COST = COST;
994 this.groundCellImage = groundCellImage;
995 this.canAttack = true;
996 cellImageModel2 = Bitmap.createScaledBitmap(image2, cellWidth, cellHeight, false);
997 cellImageModel3 = Bitmap.createScaledBitmap(image3, cellWidth, cellHeight, false);
998
999 }
1000
1001
1002 public Unit(String name, Resources resources, Integer cellWidth, Integer cellHeight, Bitmap cellImage, boolean isground) {
1003 this.name = name;
1004 // cellImage = Bitmap.createScaledBitmap(image, cellWidth, cellHeight, false);
1005 // outImage = Bitmap.createScaledBitmap(image, cellWidth * 2, cellHeight * 2, false);
1006 this.cellImage = cellImage;
1007 this.ground = true;
1008 }
1009
1010 public Resources getResources() {
1011 return resources;
1012 }
1013
1014 public void drawInCell(Canvas canvas, Float x, Float y) {
1015 Paint p = new Paint();
1016 canvas.drawBitmap(cellImage, x, y, p);
1017 if (modelInCellChangeCounter >= 12)
1018 modelInCellChangeCounter = 0;
1019
1020 }
1021
1022 public void drawOutCell(Canvas canvas, Integer x, Integer y) {
1023 Paint p = new Paint();
1024 canvas.drawBitmap(outImage, x, y, p);
1025
1026 }
1027
1028 public boolean isGround() {
1029 return ground;
1030 }
1031
1032 public void move(Integer indexOnClickX, Integer indexOnClickY, Integer indexX, Integer indexY, Unit[][] unitMatrix, boolean ground) {
1033 if (ground) {
1034 Unit tmp = unitMatrix[indexOnClickX][indexOnClickY];
1035 unitMatrix[indexOnClickX][indexOnClickY] = this;
1036 unitMatrix[indexX][indexY] = tmp;
1037
1038
1039 }
1040
1041 }
1042 private void die(){
1043
1044
1045
1046 }
1047
1048
1049
1050
1051
1052 public void attack(Integer indexOnClickX, Integer indexOnClickY, Integer indexX, Integer indexY, Unit[][] unitMatrix, boolean ground, Canvas canvas) {
1053
1054 Bitmap tmpImage = BitmapFactory.decodeResource(resources, R.drawable.ground);
1055 if (!ground) {
1056 unitMatrix[indexOnClickX][indexOnClickY].HP -= (this.DMG - this.SHIELD);
1057 this.HP -= (unitMatrix[indexOnClickX][indexOnClickY].DMG - unitMatrix[indexOnClickX][indexOnClickY].SHIELD);
1058
1059
1060 if (this.HP <= 0 && unitMatrix[indexOnClickX][indexOnClickY].HP > 0) {
1061 if (unitMatrix[indexX][indexY].cellImage != null)
1062 unitMatrix[indexX][indexY].cellImage.recycle();
1063 if (unitMatrix[indexX][indexY].cellImageModel1 != null)
1064 unitMatrix[indexX][indexY].cellImageModel1.recycle();
1065 if (unitMatrix[indexX][indexY].cellImageModel2 != null)
1066 unitMatrix[indexX][indexY].cellImageModel2.recycle();
1067 if (unitMatrix[indexX][indexY].cellImageModel3 != null)
1068 unitMatrix[indexX][indexY].cellImageModel3.recycle();
1069 unitMatrix[indexX][indexY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
1070 }
1071
1072 if (this.HP > 0 && unitMatrix[indexOnClickX][indexOnClickY].HP <= 0) {
1073
1074
1075 if (unitMatrix[indexOnClickX][indexOnClickY].cellImage != null)
1076 unitMatrix[indexOnClickX][indexOnClickY].cellImage.recycle();
1077 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel1 != null)
1078 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel1.recycle();
1079 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel2 != null)
1080 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel2.recycle();
1081 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel3 != null)
1082 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel3.recycle();
1083 unitMatrix[indexOnClickX][indexOnClickY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
1084
1085 this.move(indexOnClickX,indexOnClickY, indexX, indexY,unitMatrix,unitMatrix[indexOnClickX][indexOnClickY].isGround());
1086 }
1087
1088 if (this.HP <= 0 && unitMatrix[indexOnClickX][indexOnClickY].HP <= 0) {
1089 if (unitMatrix[indexOnClickX][indexOnClickY].cellImage != null)
1090 unitMatrix[indexOnClickX][indexOnClickY].cellImage.recycle();
1091 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel1 != null)
1092 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel1.recycle();
1093 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel2 != null)
1094 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel2.recycle();
1095 if (unitMatrix[indexOnClickX][indexOnClickY].cellImageModel3 != null)
1096 unitMatrix[indexOnClickX][indexOnClickY].cellImageModel3.recycle();
1097 unitMatrix[indexOnClickX][indexOnClickY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
1098
1099
1100 if (unitMatrix[indexX][indexY].cellImage != null)
1101 unitMatrix[indexX][indexY].cellImage.recycle();
1102 if (unitMatrix[indexX][indexY].cellImageModel1 != null)
1103 unitMatrix[indexX][indexY].cellImageModel1.recycle();
1104 if (unitMatrix[indexX][indexY].cellImageModel2 != null)
1105 unitMatrix[indexX][indexY].cellImageModel2.recycle();
1106 if (unitMatrix[indexX][indexY].cellImageModel3 != null)
1107 unitMatrix[indexX][indexY].cellImageModel3.recycle();
1108 unitMatrix[indexX][indexY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage,true);
1109
1110 this.cellImage = Bitmap.createScaledBitmap(tmpImage, cellWidth, cellHeight, false);
1111 }
1112
1113
1114 }
1115
1116
1117
1118
1119
1120 }
1121
1122
1123
1124}
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138package com.example.gwynbleidd.verstkagame;
1139
1140
1141import android.app.Dialog;
1142import android.content.Context;
1143import android.content.res.Resources;
1144import android.graphics.Bitmap;
1145import android.graphics.BitmapFactory;
1146import android.graphics.Canvas;
1147import android.graphics.Color;
1148import android.graphics.Paint;
1149import android.view.MotionEvent;
1150import android.view.View;
1151import android.widget.ImageView;
1152import android.widget.Toast;
1153
1154import com.example.gwynbleidd.verstkagame.UnitClasses.Humans.Archer;
1155import com.example.gwynbleidd.verstkagame.UnitClasses.Humans.Mage;
1156import com.example.gwynbleidd.verstkagame.UnitClasses.Orcs.OrcRanged;
1157import com.example.gwynbleidd.verstkagame.UnitClasses.Unit;
1158
1159
1160public class BattleField extends View {
1161 Resources resources;
1162 Context context;
1163 Integer cellWidth = 1;
1164 Integer cellHeight = 1;
1165 Integer indexOfCellY = 8;
1166 Integer indexOfCellX = 6;
1167 Integer deltaScreenX = 0;
1168 Integer deltaScreenY = 0;
1169 Integer indexOfCellOnClickY;
1170 Integer indexOfCellOnClickX;
1171 Integer width;
1172 Integer height;
1173 Integer previousIndexOfCellY = 8;
1174 Integer previousIndexOfCellX = 6;
1175 Integer befpreviousIndexOfCellX = 6;
1176 Integer befpreviousIndexOfCellY = 8;
1177 Integer[][] matrix = new Integer[7][9];
1178 Unit[][] unitMatrix = new Unit[7][9];
1179 Bitmap frame;
1180 Bitmap board1;
1181 Bitmap redImage;
1182 Bitmap blueImage;
1183 //Bitmap archImage;
1184 // Bitmap arch2Image;
1185 Bitmap red2Image;
1186 Bitmap armor;
1187 Bitmap turn;
1188 Bitmap turn2;
1189 Bitmap iconattack;
1190 Bitmap btnMenuImage;
1191 ImageView imageView;
1192 OurButton btnTurn;
1193 OurButton btnMenu;
1194 Boolean TurnOfSide;
1195 Canvas canvas;
1196 public long startTime1 = 0;
1197 public long elapsedTime1 = 0;
1198 MotionEvent previousEvent;
1199 Boolean DoubleClickCheck;
1200 Integer Unitx;
1201 Integer UnitY;
1202 Boolean DrawPermissed = false;
1203 Boolean UnitChosen = false;
1204
1205
1206
1207 Bitmap groundImage;
1208
1209 Bitmap blue2Image;
1210 Bitmap groundCellImage;
1211 Bitmap orcImage;
1212 Bitmap orcRangedImage;
1213 Bitmap orc2Image;
1214 Bitmap orcRanged2Image;
1215
1216
1217 public Integer getCellWidth() {
1218 return cellWidth;
1219 }
1220 public void setCellWidth(Integer cellWidth) {
1221 this.cellWidth = cellWidth;
1222 }
1223 public Integer getCellHeight() {
1224 return cellHeight;
1225 }
1226 public void setCellHeight(Integer cellHeight) {
1227 this.cellHeight = cellHeight;
1228 }
1229 public Integer getIndexOfCellY() {
1230 return indexOfCellY;
1231 }
1232 public void setIndexOfCellY(Integer indexOfCellY) {
1233 this.indexOfCellY = indexOfCellY;
1234 }
1235 public Integer getIndexOfCellX() {
1236 return indexOfCellX;
1237 }
1238 public void setIndexOfCellX(Integer indexOfCellX) {
1239 this.indexOfCellX = indexOfCellX;
1240 }
1241 public Integer getDeltaScreenX() {
1242 return deltaScreenX;
1243 }
1244 public void setDeltaScreenX(Integer deltaScreenX) {
1245 this.deltaScreenX = deltaScreenX;
1246 }
1247 public Integer getDeltaScreenY() {
1248 return deltaScreenY;
1249 }
1250 public void setDeltaScreenY(Integer deltaScreenY) {
1251 this.deltaScreenY = deltaScreenY;
1252 }
1253
1254 boolean gameStopped = false;
1255 Paint p;
1256 Unit redUnit;
1257 Unit blueUnit;
1258 Archer archer;
1259 Paint pBlack;
1260 Paint pGreen;
1261 Paint pYellow;
1262 Paint pText;
1263 Paint pHealthBar;
1264 Paint pHealthBarBounds;
1265 Paint pRed;
1266
1267 Integer countTeamOne = 0;
1268 Integer countTeamTwo = 0;
1269
1270 Dialog menu;
1271 Dialog winMessage;
1272
1273
1274 public BattleField(Context context, Integer width, Integer height, Integer[][] matrix1, Integer[][] matrix2, String[][] nameMatrix, Integer[][] HPMatrix, Boolean[][] canAttackMatrix, Boolean[][] OWNERmatrix, Integer[][] VALUABLEMOVEMatrix, Dialog menu, Dialog winMessage) {
1275 super(context);
1276 this.context = context;
1277 this.menu = menu;
1278 this.winMessage = winMessage;
1279 this.cellWidth = width / 8;
1280 this.cellHeight = height / 12;
1281 this.deltaScreenX = width / 8;
1282 this.deltaScreenY = height / 6;
1283 this.width = width;
1284 this.height = height;
1285 this.resources = getResources();
1286 this.startTime1 = System.currentTimeMillis();
1287 turn = BitmapFactory.decodeResource(resources, R.drawable.turn);
1288 turn2 = BitmapFactory.decodeResource(resources, R.drawable.turn2);
1289 btnTurn = new OurButton(turn, turn2, resources, cellWidth * 2, cellHeight / 2);
1290 board1 = BitmapFactory.decodeResource(resources, R.drawable.board1);
1291
1292
1293 btnMenuImage = BitmapFactory.decodeResource(resources, R.drawable.gear);
1294
1295 btnMenu = new OurButton(btnMenuImage, btnMenuImage, resources , width / 17, height / 33);
1296
1297
1298 frame = BitmapFactory.decodeResource(resources, R.drawable.cutramka);
1299 frame = Bitmap.createScaledBitmap(frame, cellWidth, cellHeight, false);
1300 redImage = BitmapFactory.decodeResource(resources, R.drawable.mage);
1301 blueImage = BitmapFactory.decodeResource(resources, R.drawable.swordman);
1302 //archImage = BitmapFactory.decodeResource(resources, R.drawable.archer);
1303 blue2Image = BitmapFactory.decodeResource(resources, R.drawable.swordman2);
1304 //arch2Image = BitmapFactory.decodeResource(resources, R.drawable.archer2);
1305
1306 orcImage = BitmapFactory.decodeResource(resources, R.drawable.orc_melee);
1307 orcRangedImage = BitmapFactory.decodeResource(resources, R.drawable.orc_ranged);
1308 orc2Image = BitmapFactory.decodeResource(resources, R.drawable.orc2_melee);
1309 orcRanged2Image = BitmapFactory.decodeResource(resources, R.drawable.orc2_ranged);
1310
1311
1312 groundImage = BitmapFactory.decodeResource(resources, R.drawable.ground_test);
1313
1314 groundCellImage = Bitmap.createScaledBitmap(groundImage, cellWidth, cellHeight, false);
1315
1316
1317 red2Image = BitmapFactory.decodeResource(resources, R.drawable.mage2);
1318
1319
1320 armor = BitmapFactory.decodeResource(resources, R.drawable.armor);
1321 iconattack = BitmapFactory.decodeResource(resources, R.drawable.iconattack);
1322 // redUnit = new Unit("red", resources, cellWidth, cellHeight, redImage, groundCellImage, false, 10, 10, 0, 2, 1, 100);
1323 // blueUnit = new Unit("blue", resources, cellWidth, cellHeight, blueImage, groundCellImage, false, 20, 5, 0, 2, 1, 100);
1324 // archer = new Archer("blue", resources, cellWidth, cellHeight, groundCellImage, false, 10, 10, 0, 2, 1);
1325 DoubleClickCheck = false;
1326 TurnOfSide = true;
1327
1328
1329
1330
1331 /* for (int i = 0; i < 6; i++) {
1332
1333 this.matrix[i][0] = 1;
1334 this.unitMatrix[i][0] = new Archer("blarcher", getResources(),cellWidth, cellHeight, archImage, false,10,5,0,2,1);
1335
1336 }
1337 for (int i = 0; i < 6; i++) {
1338
1339 this.matrix[i][1] = 1;
1340 this.unitMatrix[i][1] = new Unit("blue", getResources(),cellWidth, cellHeight, blueImage, false,25,5,0,1,2);
1341
1342 }
1343 for (int i = 0; i < 6; i++) {
1344
1345 this.matrix[i][6] = 1;
1346 this.unitMatrix[i][6] = new Unit("red", getResources(),cellWidth, cellHeight, redImage, false,15,10,0,1,2);
1347
1348 }
1349 for (int i = 0; i < 6; i++) {
1350
1351 this.matrix[i][7] = 1;
1352 this.unitMatrix[i][7] = new Archer("rarcher", getResources(),cellWidth, cellHeight, archImage, false,10,5,0,2,1);
1353
1354 } */
1355
1356 if (VALUABLEMOVEMatrix == null || nameMatrix == null || OWNERmatrix == null || HPMatrix == null || canAttackMatrix == null) {
1357
1358 //РаÑÑтановка второго игрока
1359 for (int i = 0; i < 7; i++) {
1360 for (int j = 0; j < 4; j++) {
1361 if (matrix2[i][j] == 0) {
1362 this.unitMatrix[i][j] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage, true);
1363 } else if (matrix2[i][j] == 1) {
1364 this.unitMatrix[i][j] = new Mage("Mage", resources, cellWidth, cellHeight, red2Image, groundCellImage, false, 15, 10, 0, 2, 2);
1365 this.unitMatrix[i][j].Owner = false;
1366 } else if (matrix2[i][j] == 2) {
1367 this.unitMatrix[i][j] = new Unit("Swordman", resources, cellWidth, cellHeight, blue2Image, groundCellImage, false, 25, 5, 0, 1, 2, 100);
1368 this.unitMatrix[i][j].Owner = false;
1369 } else if (matrix2[i][j] == 3) {
1370 this.unitMatrix[i][j] = new Archer("Archer", resources, cellWidth, cellHeight, BitmapFactory.decodeResource(resources, R.drawable.archer2), BitmapFactory.decodeResource(resources, R.drawable.archer2_model2), BitmapFactory.decodeResource(resources, R.drawable.archer2_model3), groundCellImage, false, 20, 5, 0, 2, 2);
1371 this.unitMatrix[i][j].Owner = false;
1372 } else if (matrix2[i][j] == 4) {
1373 this.unitMatrix[i][j] = new Unit("Orc", resources, cellWidth, cellHeight, orc2Image, groundCellImage, false, 20, 10, 0, 1, 2, 150);
1374 this.unitMatrix[i][j].Owner = false;
1375 } else if (matrix2[i][j] == 5) {
1376 this.unitMatrix[i][j] = new OrcRanged("OrcRanged", resources, cellWidth, cellHeight, orcRanged2Image, groundCellImage, false, 20, 10, 0 , 2 , 1, 200);
1377 this.unitMatrix[i][j].Owner = false;
1378 }
1379 }
1380 }
1381
1382 //РаÑÑтановка первого игрока
1383 for (int i = 0; i < 7; i++) {
1384 for (int j = 4; j < 9; j++) {
1385 if (matrix1[i][j] == 0) {
1386 this.unitMatrix[i][j] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage, true);
1387 } else if (matrix1[i][j] == 1) {
1388 this.unitMatrix[i][j] = new Mage("Mage", resources, cellWidth, cellHeight, redImage, groundCellImage, false, 15, 10, 0, 2, 2);
1389 this.unitMatrix[i][j].Owner = true;
1390 } else if (matrix1[i][j] == 2) {
1391 this.unitMatrix[i][j] = new Unit("Swordman", resources, cellWidth, cellHeight, blueImage, groundCellImage, false, 25, 5, 0, 1, 2, 100);
1392 this.unitMatrix[i][j].Owner = true;
1393 } else if (matrix1[i][j] == 3) {
1394 this.unitMatrix[i][j] = new Archer("Archer", resources, cellWidth, cellHeight, BitmapFactory.decodeResource(resources, R.drawable.archer), BitmapFactory.decodeResource(resources, R.drawable.archer_model2), BitmapFactory.decodeResource(resources, R.drawable.archer_model3), groundCellImage, false, 20, 5, 0, 2, 2);
1395 this.unitMatrix[i][j].Owner = true;
1396 } else if (matrix1[i][j] == 4) {
1397 this.unitMatrix[i][j] = new Unit("Orc", resources, cellWidth, cellHeight, orcImage, groundCellImage, false, 20, 10, 0, 1, 2, 150);
1398 this.unitMatrix[i][j].Owner = true;
1399 } else if (matrix1[i][j] == 5) {
1400 this.unitMatrix[i][j] = new OrcRanged("OrcRanged", resources, cellWidth, cellHeight, orcRangedImage, groundCellImage, false, 20, 10, 0 , 2 , 1, 200);
1401 this.unitMatrix[i][j].Owner = true;
1402 }
1403 }
1404 }
1405
1406 } else {
1407
1408 for (int i = 0; i < 7; i++) {
1409 for (int j = 0; j < 9; j++) {
1410 if (nameMatrix[i][j].equals("Archer")) {
1411 if (OWNERmatrix[i][j])
1412 this.unitMatrix[i][j] = new Archer("Archer", resources, cellWidth, cellHeight, BitmapFactory.decodeResource(resources, R.drawable.archer), BitmapFactory.decodeResource(resources, R.drawable.archer_model2), BitmapFactory.decodeResource(resources, R.drawable.archer_model3), groundCellImage, false, 20, 5, 0, 2, 2);
1413 else
1414 this.unitMatrix[i][j] = new Archer("Archer", resources, cellWidth, cellHeight, BitmapFactory.decodeResource(resources, R.drawable.archer2), BitmapFactory.decodeResource(resources, R.drawable.archer2_model2), BitmapFactory.decodeResource(resources, R.drawable.archer2_model3), groundCellImage, false, 20, 5, 0, 2, 2);
1415 this.unitMatrix[i][j].HP = HPMatrix[i][j];
1416 this.unitMatrix[i][j].canAttack = canAttackMatrix[i][j];
1417 this.unitMatrix[i][j].Owner = OWNERmatrix[i][j];
1418 this.unitMatrix[i][j].VALUABLEMOVE = VALUABLEMOVEMatrix[i][j];
1419 } else if (nameMatrix[i][j].equals("Swordman")) {
1420 if (OWNERmatrix[i][j])
1421 this.unitMatrix[i][j] = new Unit("Swordman", resources, cellWidth, cellHeight, blueImage, groundCellImage, false, 25, 5, 0, 1, 2, 100);
1422 else
1423 this.unitMatrix[i][j] = new Unit("Swordman", resources, cellWidth, cellHeight, blue2Image, groundCellImage, false, 25, 5, 0, 1, 2, 100);
1424 this.unitMatrix[i][j].HP = HPMatrix[i][j];
1425 this.unitMatrix[i][j].canAttack = canAttackMatrix[i][j];
1426 this.unitMatrix[i][j].Owner = OWNERmatrix[i][j];
1427 this.unitMatrix[i][j].VALUABLEMOVE = VALUABLEMOVEMatrix[i][j];
1428 } else if (nameMatrix[i][j].equals("Mage")) {
1429 if (OWNERmatrix[i][j])
1430 this.unitMatrix[i][j] = new Mage("Mage", resources, cellWidth, cellHeight, redImage, groundCellImage, false, 15, 10, 0, 2, 2);
1431 else
1432 this.unitMatrix[i][j] = new Mage("Mage", resources, cellWidth, cellHeight, red2Image, groundCellImage, false, 15, 10, 0, 2, 2);
1433 this.unitMatrix[i][j].HP = HPMatrix[i][j];
1434 this.unitMatrix[i][j].canAttack = canAttackMatrix[i][j];
1435 this.unitMatrix[i][j].Owner = OWNERmatrix[i][j];
1436 this.unitMatrix[i][j].VALUABLEMOVE = VALUABLEMOVEMatrix[i][j];
1437 } else if (nameMatrix[i][j].equals("Orc")) {
1438 if (OWNERmatrix[i][j])
1439 this.unitMatrix[i][j] = new Unit("Orc", resources, cellWidth, cellHeight, orcImage, groundCellImage, false, 20, 10, 0, 1, 2, 150);
1440 else
1441 this.unitMatrix[i][j] = new Unit("Orc", resources, cellWidth, cellHeight, orc2Image, groundCellImage, false, 20, 10, 0, 1, 2, 150);
1442 this.unitMatrix[i][j].HP = HPMatrix[i][j];
1443 this.unitMatrix[i][j].canAttack = canAttackMatrix[i][j];
1444 this.unitMatrix[i][j].Owner = OWNERmatrix[i][j];
1445 this.unitMatrix[i][j].VALUABLEMOVE = VALUABLEMOVEMatrix[i][j];
1446 } else if (nameMatrix[i][j].equals("OrcRanged")) {
1447 if (OWNERmatrix[i][j])
1448 this.unitMatrix[i][j] = new OrcRanged("OrcRanged", resources, cellWidth, cellHeight, orcRangedImage, groundCellImage, false, 20, 10, 0 , 2 , 1, 200);
1449 else
1450 this.unitMatrix[i][j] = new OrcRanged("OrcRanged", resources, cellWidth, cellHeight, orcRanged2Image, groundCellImage, false, 20, 10, 0 , 2 , 1, 200);
1451 this.unitMatrix[i][j].HP = HPMatrix[i][j];
1452 this.unitMatrix[i][j].canAttack = canAttackMatrix[i][j];
1453 this.unitMatrix[i][j].Owner = OWNERmatrix[i][j];
1454 this.unitMatrix[i][j].VALUABLEMOVE = VALUABLEMOVEMatrix[i][j];
1455 } else
1456 this.unitMatrix[i][j] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage, true);
1457 }
1458 }
1459
1460 }
1461
1462
1463 p = new Paint();
1464 Timer t = new Timer(this, Integer.MAX_VALUE, 100);
1465 t.start();
1466
1467
1468
1469
1470
1471 pBlack = new Paint();
1472 pBlack.setColor(Color.BLACK);
1473 pBlack.setStrokeWidth(width / 150);
1474 pYellow = new Paint();
1475 pYellow.setColor(Color.YELLOW);
1476 pYellow.setStrokeWidth(width / 150);
1477 pYellow.setStyle(Paint.Style.STROKE);
1478 pGreen = new Paint();
1479 pGreen.setColor(Color.GREEN);
1480 pGreen.setStrokeWidth(width / 150);
1481 pGreen.setStyle(Paint.Style.STROKE);
1482 pRed = new Paint();
1483 pRed.setColor(Color.RED);
1484 pRed.setStrokeWidth(width / 150);
1485 pRed.setStyle(Paint.Style.STROKE);
1486 pHealthBarBounds = new Paint();
1487 pHealthBarBounds.setColor(Color.BLACK);
1488 pHealthBarBounds.setStyle(Paint.Style.STROKE);
1489 pHealthBarBounds.setStrokeWidth(width / 170);
1490 pHealthBar = new Paint();
1491 pHealthBar.setColor(Color.RED);
1492 pHealthBar.setStyle(Paint.Style.FILL);
1493 pHealthBar.setStrokeWidth(width / 200);
1494 pText = new Paint();
1495 pText.setTextSize(cellHeight * 0.3f);
1496
1497 board1 = Bitmap.createScaledBitmap(board1, width, height, false);
1498 armor = Bitmap.createScaledBitmap(armor, cellWidth * 2 / 3, cellHeight * 2 / 3, false);
1499 iconattack = Bitmap.createScaledBitmap(iconattack, cellWidth * 2 / 3, cellHeight / 3, false);
1500
1501
1502 }
1503
1504
1505 @Override
1506 protected void onDraw(Canvas canvas) {
1507 canvas.drawBitmap(board1, 0f, 0f, p);
1508 btnMenu.draw(canvas, width * 16 / 17f, 0f);
1509 this.canvas = canvas;
1510 for (int i = 0; i < 9; i++) {
1511 canvas.drawLine(deltaScreenX, i * cellHeight + deltaScreenY, canvas.getWidth() - deltaScreenX, i * cellHeight + deltaScreenY, pBlack);
1512 }
1513
1514 for (int i = 0; i < 7; i++) {
1515 canvas.drawLine(i * cellWidth + deltaScreenX, deltaScreenY, i * cellWidth + deltaScreenX, canvas.getHeight() - deltaScreenY, pBlack);
1516 }
1517
1518
1519 for (int i = 0; i < 6; i++) {
1520 for (int j = 0; j < 8; j++) {
1521 unitMatrix[i][j].drawInCell(canvas, deltaScreenX + cellWidth * i * 1f, deltaScreenY + cellHeight * j * 1f);
1522 /* if (!unitMatrix[i][j].isGround()){
1523 if(!unitMatrix[i][j].Owner){
1524 canvas.drawBitmap(frame,deltaScreenX + cellWidth*i * 1f, deltaScreenY + cellHeight*j * 1f,p);
1525 }
1526 }*/
1527 }
1528
1529 }
1530
1531
1532
1533
1534 if (!unitMatrix[indexOfCellX][indexOfCellY].isGround()) {
1535 unitMatrix[indexOfCellX][indexOfCellY].drawOutCell(canvas, +cellWidth * 6, height - deltaScreenY);
1536 canvas.drawRect(cellWidth * 4.25f, cellHeight * 10.1f, cellWidth * 5.75f, cellHeight * 10.4f, pHealthBarBounds);
1537 canvas.drawRect(cellWidth * 4.25f, cellHeight * 10.1f, cellWidth * 5.75f - (1 - (float) unitMatrix[indexOfCellX][indexOfCellY].HP / (float) unitMatrix[indexOfCellX][indexOfCellY].MAXHP) * 1.5f * cellWidth, cellHeight * 10.4f, pHealthBar);
1538 canvas.drawText("Health Points : ", cellWidth * 1.5f, cellHeight * 10.35f, pText);
1539 canvas.drawBitmap(armor, cellWidth * 4.6f, cellHeight * 10.65f, p);
1540 canvas.drawBitmap(iconattack, cellWidth * 4.6f, cellHeight * 11.6f, p);
1541 canvas.drawText(unitMatrix[indexOfCellX][indexOfCellY].SHIELD.toString(), cellWidth * 4.85f, cellHeight * 11.1f, pText);
1542 canvas.drawText(unitMatrix[indexOfCellX][indexOfCellY].DMG.toString(), cellWidth * 4.85f, cellHeight * 11.85f, pText);
1543 if (unitMatrix[indexOfCellX][indexOfCellY].HP / 10 > 0)
1544 canvas.drawText(unitMatrix[indexOfCellX][indexOfCellY].HP.toString() + " / " + unitMatrix[indexOfCellX][indexOfCellY].MAXHP.toString(), cellWidth * 4.5f, cellHeight * 10.35f, pText);
1545 else
1546 canvas.drawText(" " + unitMatrix[indexOfCellX][indexOfCellY].HP.toString() + " / " + unitMatrix[indexOfCellX][indexOfCellY].MAXHP.toString(), cellWidth * 4.5f, cellHeight * 10.35f, pText);
1547
1548
1549
1550 }
1551
1552 btnTurn.draw(canvas, cellWidth * 3f, cellHeight * 1f);
1553
1554
1555 if (DrawPermissed) {
1556 if (TurnOfSide.equals(unitMatrix[indexOfCellX][indexOfCellY].Owner)) {
1557
1558
1559 for (int i = 0; i < 6; i++) {
1560 for (int j = 0; j < 8; j++) {
1561 if (!unitMatrix[indexOfCellX][indexOfCellY].isGround())
1562 if (unitMatrix[i][j].isGround()) {
1563 if (Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix[indexOfCellX][indexOfCellY].VALUABLEMOVE) {
1564 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pGreen);
1565 }
1566 } else {
1567 if ((Math.abs(indexOfCellX - i) + Math.abs(indexOfCellY - j) <= unitMatrix[indexOfCellX][indexOfCellY].RANGE) && (unitMatrix[indexOfCellX][indexOfCellY].Owner != unitMatrix[i][j].Owner) && unitMatrix[indexOfCellX][indexOfCellY].canAttack) {
1568 canvas.drawRect(i * cellWidth + deltaScreenX, j * cellHeight + deltaScreenY, (i + 1) * cellWidth + deltaScreenX, (j + 1) * cellHeight + deltaScreenY, pRed);
1569 }
1570 }
1571 }
1572 }
1573 }
1574 canvas.drawRect(indexOfCellX * cellWidth + deltaScreenX, indexOfCellY * cellHeight + deltaScreenY, (indexOfCellX + 1) * cellWidth + deltaScreenX, (indexOfCellY + 1) * cellHeight + deltaScreenY, pYellow);
1575 }
1576 }
1577
1578
1579 boolean firstTouch = false;
1580 long time = 0;
1581
1582 public boolean isDoubleClick(MotionEvent event) {
1583
1584 if (event.getAction() == event.ACTION_DOWN) {
1585 if (firstTouch && (System.currentTimeMillis() - time) <= 300 && (System.currentTimeMillis() - time) > 50) {
1586 //do stuff here for double tap
1587
1588 return true;
1589
1590 } else {
1591
1592
1593 firstTouch = true;
1594 time = System.currentTimeMillis();
1595 }
1596 }
1597 return false;
1598 }
1599
1600
1601 @Override
1602 public boolean onTouchEvent(MotionEvent event) {
1603
1604 if (event.getX() >= (width * 16 / 17) && event.getY() <= height / 33) {
1605 btnMenu.isPressed = true;
1606 menu.show();
1607 }
1608
1609
1610 if (event.getX() > cellWidth * 3f && event.getY() >=cellHeight * 1f && event.getX() <= cellWidth * 3f + cellWidth*2 && event.getY() <=cellHeight * 1f + cellHeight/2 ){
1611 btnTurn.isPressed = !btnTurn.isPressed;
1612 for (int i = 0; i < 6; i++) {
1613 for (int j = 0; j < 8; j++) {
1614
1615 if (!unitMatrix[i][j].isGround()){
1616 if (unitMatrix[i][j].Owner.equals(TurnOfSide)){
1617 unitMatrix[i][j].VALUABLEMOVE = unitMatrix[i][j].MOVE;
1618 unitMatrix[i][j].canAttack = true;
1619 }
1620
1621 }
1622 }
1623
1624 }
1625 TurnOfSide = !TurnOfSide;
1626 }
1627
1628
1629 if ((event.getX() >= deltaScreenX) && (event.getX() <= cellWidth * 6 + deltaScreenX) && (event.getY() >= deltaScreenY) && (event.getY() <= cellHeight * 8 + deltaScreenY)) {
1630 DrawPermissed = true;
1631 for (int i = 1; i < 9; i++) {
1632 if (event.getY() > cellHeight * (i - 1) + deltaScreenY && event.getY() < cellHeight * i + deltaScreenY) {
1633 this.indexOfCellY = i - 1;
1634 }
1635 }
1636
1637 for (int i = 1; i < 7; i++) {
1638 if (event.getX() > cellWidth * (i - 1) + deltaScreenX && event.getX() < cellWidth * i + deltaScreenX) {
1639 this.indexOfCellX = i - 1;
1640 }
1641 }
1642
1643
1644
1645 if (!unitMatrix[indexOfCellX][indexOfCellY].isGround() && !isDoubleClick(event)) {
1646
1647
1648 }
1649 } else {
1650 indexOfCellX = 6;
1651 indexOfCellY = 8;
1652 DrawPermissed = false;
1653 }
1654
1655
1656 if ((event.getX() >= deltaScreenX) && (event.getX() <= cellWidth * 6 + deltaScreenX) && (event.getY() >= deltaScreenY) && (event.getY() <= cellHeight * 8 + deltaScreenY) && isDoubleClick(event)) {
1657 DrawPermissed = true;
1658
1659 for (int i = 1; i < 9; i++) {
1660 if (event.getY() > cellHeight * (i - 1) + deltaScreenY && event.getY() < cellHeight * i + deltaScreenY) {
1661 this.indexOfCellOnClickY = i - 1;
1662 }
1663 }
1664
1665 for (int i = 1; i < 7; i++) {
1666 if (event.getX() > cellWidth * (i - 1) + deltaScreenX && event.getX() < cellWidth * i + deltaScreenX) {
1667 this.indexOfCellOnClickX = i - 1;
1668 }
1669
1670 }
1671
1672
1673 //Передвижение и атака
1674 if(!unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].ground && unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].Owner.equals(TurnOfSide) ) {
1675 if ( unitMatrix[indexOfCellOnClickX][indexOfCellOnClickY].ground &&(Math.abs(indexOfCellOnClickX - befpreviousIndexOfCellX) + Math.abs(indexOfCellOnClickY - befpreviousIndexOfCellY)) <= unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].VALUABLEMOVE ){
1676 unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].VALUABLEMOVE -= Math.abs(indexOfCellOnClickX - befpreviousIndexOfCellX) + Math.abs(indexOfCellOnClickY - befpreviousIndexOfCellY);
1677 unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].move(indexOfCellOnClickX, indexOfCellOnClickY, befpreviousIndexOfCellX, befpreviousIndexOfCellY, unitMatrix, unitMatrix[indexOfCellOnClickX][indexOfCellOnClickY].isGround());
1678 indexOfCellX = indexOfCellOnClickX;
1679 indexOfCellY = indexOfCellOnClickY;
1680
1681 } else if ((Math.abs(indexOfCellOnClickX - befpreviousIndexOfCellX) + Math.abs(indexOfCellOnClickY - befpreviousIndexOfCellY)) <= unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].RANGE && !(unitMatrix[indexOfCellOnClickX][indexOfCellOnClickY].Owner.equals(unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].Owner)) && unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].canAttack) {
1682 unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].canAttack = false;
1683 unitMatrix[befpreviousIndexOfCellX][befpreviousIndexOfCellY].attack(indexOfCellOnClickX, indexOfCellOnClickY, befpreviousIndexOfCellX, befpreviousIndexOfCellY, unitMatrix, unitMatrix[indexOfCellOnClickX][indexOfCellOnClickY].isGround(), canvas);
1684
1685 }
1686
1687 }
1688 }
1689
1690
1691 befpreviousIndexOfCellX = previousIndexOfCellX;
1692 befpreviousIndexOfCellY = previousIndexOfCellY;
1693 previousIndexOfCellY = indexOfCellY;
1694 previousIndexOfCellX = indexOfCellX;
1695
1696
1697 for (int i = 0; i < 6; i++) {
1698 for (int j = 0; j < 8; j++) {
1699 if (unitMatrix[i][j].Owner != null) {
1700 if (unitMatrix[i][j].Owner)
1701 countTeamOne++;
1702 else
1703 countTeamTwo++;
1704 }
1705 }
1706 }
1707
1708 if (((countTeamOne == 0) || (countTeamTwo == 0)) && !gameStopped) {
1709 winMessage.setTitle("Победа игрока: " + (countTeamTwo == 0 ? "Синий" : "КраÑный"));
1710 gameStopped = true;
1711 winMessage.show();
1712 }
1713
1714 if (!gameStopped) {
1715 countTeamOne = 0;
1716 countTeamTwo = 0;
1717 }
1718
1719 return false;
1720 }
1721
1722}
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738package com.example.gwynbleidd.verstkagame;
1739
1740import android.content.Intent;
1741import android.database.Cursor;
1742import android.database.sqlite.SQLiteDatabase;
1743import android.graphics.Color;
1744import android.support.v7.app.AppCompatActivity;
1745import android.os.Bundle;
1746import android.view.View;
1747import android.widget.Button;
1748import android.widget.LinearLayout;
1749
1750public class MainActivity extends AppCompatActivity {
1751
1752
1753 Button newGame;
1754 Button loadGame;
1755 Button settings;
1756
1757 Integer[][] HPMatrix = new Integer[7][9];
1758 Integer[][] VALUABLEMOVEMatrix = new Integer[7][9];
1759 Boolean[][] OWNERMatrix = new Boolean[7][9];
1760 Boolean[][] canAttackMatrix = new Boolean[7][9];
1761 String[][] nameMatrix = new String[7][9];
1762 LinearLayout layout;
1763
1764 @Override
1765 protected void onCreate(Bundle savedInstanceState) {
1766 super.onCreate(savedInstanceState);
1767 setContentView(R.layout.activity_main);
1768 getSupportActionBar().hide();
1769
1770 newGame = (Button) findViewById(R.id.newGame);
1771 loadGame = (Button) findViewById(R.id.loadGame);
1772 settings = (Button) findViewById(R.id.settings);
1773 layout = (LinearLayout) findViewById(R.id.layout);
1774 newGame.setOnClickListener(new View.OnClickListener() {
1775 @Override
1776 public void onClick(View v) {
1777 Intent intent = new Intent(getApplicationContext(), NewGame.class);
1778 startActivity(intent);
1779 }
1780 });
1781 loadGame.setOnClickListener(new View.OnClickListener() {
1782 @Override
1783 public void onClick(View v) {
1784 // Intent intent = new Intent(getApplicationContext(), LoadGame.class);
1785 // startActivity(intent);
1786
1787 for (int i = 0; i < 7; i++) {
1788 for (int j = 0; j < 9; j++) {
1789 HPMatrix[i][j] = 1;
1790 VALUABLEMOVEMatrix[i][j] = 0;
1791 OWNERMatrix[i][j] = false;
1792 canAttackMatrix[i][j] = false;
1793 nameMatrix[i][j] = "";
1794 }
1795 }
1796
1797 SQLiteDatabase myDB = openOrCreateDatabase("save1.db", MODE_PRIVATE, null);
1798 Cursor myCursor = myDB.rawQuery("select name, indexX, indexY, HP, canAttack, VALUABLEMOVE, OWNER from save1", null);
1799
1800 while(myCursor.moveToNext()) {
1801 String name = myCursor.getString(0);
1802 Integer indexX = myCursor.getInt(1);
1803 Integer indexY = myCursor.getInt(2);
1804 Integer HP = myCursor.getInt(3);
1805 boolean canAttack = (myCursor.getInt(4) == 1);
1806 Integer VALUABLEMOVE = myCursor.getInt(5);
1807 boolean OWNER = (myCursor.getInt(6) == 1);
1808
1809 nameMatrix[indexX][indexY] = name;
1810 HPMatrix[indexX][indexY] = HP;
1811 canAttackMatrix[indexX][indexY] = canAttack;
1812 VALUABLEMOVEMatrix[indexX][indexY] = VALUABLEMOVE;
1813 OWNERMatrix[indexX][indexY] = OWNER;
1814
1815 }
1816
1817 myCursor.close();
1818 myDB.close();
1819 Intent intent = new Intent(getApplicationContext(), GameActivity.class);
1820 intent.putExtra("HPMatrix", HPMatrix);
1821 intent.putExtra("VALUABLEMOVEMatrix", VALUABLEMOVEMatrix);
1822 intent.putExtra("canAttackMatrix", canAttackMatrix);
1823 intent.putExtra("OWNERMatrix", OWNERMatrix);
1824 intent.putExtra("nameMatrix", nameMatrix);
1825 startActivity(intent);
1826 }
1827 });
1828
1829 }
1830
1831 @Override
1832 protected void onPause() {
1833 super.onPause();
1834 }
1835
1836 @Override
1837 protected void onResume() {
1838 super.onResume();
1839 }
1840
1841 @Override
1842 protected void onStart() {
1843 super.onStart();
1844 }
1845}
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857package com.example.gwynbleidd.verstkagame.UnitClasses.Orcs;
1858
1859import android.content.res.Resources;
1860import android.graphics.Bitmap;
1861import android.graphics.BitmapFactory;
1862import android.graphics.Canvas;
1863
1864import com.example.gwynbleidd.verstkagame.R;
1865import com.example.gwynbleidd.verstkagame.UnitClasses.Humans.Archer;
1866import com.example.gwynbleidd.verstkagame.UnitClasses.Unit;
1867
1868
1869public class OrcRanged extends Unit {
1870 public OrcRanged(String name, Resources resources, Integer cellWidth, Integer cellHeight, Bitmap image, Bitmap groundCellImage, boolean isground, Integer HP, Integer DMG, Integer SHIELD, Integer RANGE, Integer MOVE, Integer COST) {
1871 super(name, resources, cellWidth, cellHeight, image, groundCellImage, isground, HP, DMG, SHIELD, RANGE, MOVE, COST);
1872 }
1873
1874
1875
1876
1877 @Override
1878 public void attack(Integer indexOnClickX, Integer indexOnClickY, Integer indexX, Integer indexY, Unit[][] unitMatrix, boolean ground, Canvas canvas) {
1879 if (!ground) {
1880 unitMatrix[indexOnClickX][indexOnClickY].HP -= (this.DMG - this.SHIELD);
1881
1882
1883
1884
1885 if (this.HP > 0 && unitMatrix[indexOnClickX][indexOnClickY].HP <= 0) {
1886
1887 unitMatrix[indexOnClickX][indexOnClickY] = new Unit("ground", resources, cellWidth, cellHeight, groundCellImage, true);
1888
1889 }
1890
1891
1892 }
1893 }
1894}