Advertisement
natan41014

Untitled

Jul 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.77 KB | None | 0 0
  1. package com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game;
  2.  
  3. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.*;
  4. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.annotation.*;
  5. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.entity.*;
  6. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.entity.boss.*;
  7. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.position.*;
  8.  
  9. import android.app.Activity;
  10. import android.content.*;
  11. import android.content.res.Configuration;
  12. import android.graphics.*;
  13. import android.preference.PreferenceManager;
  14. import android.util.*;
  15. import android.view.*;
  16. import java.util.*;
  17.  
  18. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.MainActivity;
  19. import com.donthavedomainyet.anddontwanttousemyemail.apps.game1.game.position.Point;
  20.  
  21. public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback
  22. {
  23. private static final String TAG = MainGamePanel.class.getSimpleName();
  24. private static final boolean debug = BuildConfig.DEBUG;
  25.  
  26. private Context context;
  27. private MainActivity main;
  28. private Explosion[] explosions;
  29. private int numberDroids;
  30. private long rounds = 0L;
  31. private Random rand;
  32. private GameThread thread;
  33. private static List<Droid> droidList;
  34. private static List<Obstacle> obstacleList;
  35. private static List<Heal> healList;
  36. private static List<Spike> corners;
  37. private static List<DamageBoost> dmgList;
  38. private static List<AdvHeal> advHealList;
  39. private static List<Shield> shieldList;
  40. private String avgFps = "";
  41. private Boolean showRange;
  42. private static Player player;
  43. private int rd = 1;
  44. private final int healChance;
  45. private boolean gameOver = false;
  46. private final int maxheals = 1;
  47. private final float droidSpeedX, droidSpeedY;
  48. private final int healthDroids;
  49. private boolean bossRound = false;
  50. private int remainingDmg = 0;
  51. private int remainingHeal = 0;
  52. private int healAmount = 0;
  53. private int baseRange = 0;
  54. private int toAddRange = 0;
  55. private int bossSize = 0;
  56. private int baseSize = 0;
  57. private int droidSize = 0;
  58. private Bitmap healBmp;
  59. private Bitmap dmgBmp;
  60. private Bitmap boss1Bmp;
  61. private Bitmap advHealBmp;
  62. private Bitmap shieldBmp;
  63. private Bitmap droidBmp;
  64. private int bossNumber = 1;
  65. private int reasonGameOver = 0;
  66. private String[] difficulties = {"easy", "medium", "hard", "extreme"};
  67. private String difficulty;
  68. private int width = 0, height = 0;
  69. private double ratio;
  70.  
  71. @Working
  72. public MainGamePanel(Context c, MainActivity main, int max_fps, int max_frameskips, float droidSpeedX, float droidSpeedY, int droids, boolean showRange, int healthDroids)
  73. {
  74. super(c);
  75. context = c;
  76. this.main = main;
  77. numberDroids = droids;
  78. SharedPreferences prefs = c.getSharedPreferences("videoPerformance", Context.MODE_PRIVATE);
  79.  
  80. SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
  81.  
  82. width = defaultPrefs.getInt("width", 540);
  83. height = defaultPrefs.getInt("height", 960);
  84. Log.d(TAG, "Width = " + width + ", height = " + height);
  85. final int orientation = prefs.getInt("orientation", 0);
  86. switch(orientation)
  87. {
  88. case Configuration.ORIENTATION_LANDSCAPE:
  89. if(width < height)
  90. {
  91. int a = width;
  92. width = height;
  93. height = a;
  94. }
  95. break;
  96. case Configuration.ORIENTATION_PORTRAIT:
  97. if(height < width)
  98. {
  99. int a = width;
  100. width = height;
  101. height = a;
  102. }
  103. break;
  104. }
  105. if(height > width)
  106. {
  107. this.ratio = height / width;
  108. Player.range = (int) (height / 4.8);
  109. baseSize = (int) (height / 32);
  110. bossSize = (int) (height / 12);
  111. toAddRange = (int) (height / 12.8);
  112. baseRange = (int) (height / 38.4);
  113. droidSize = (int) (height / 96);
  114. }
  115. else
  116. {
  117. this.ratio = width / height;
  118. Player.range = (int) (width / 4.8);
  119. baseSize = (int) (width / 32);
  120. bossSize = (int) (width / 12);
  121. toAddRange = (int) (width / 12.8);
  122. baseRange = (int) (width / 38.4);
  123. droidSize = (int) (width / 96);
  124. }
  125. healChance = prefs.getInt("healChance", 3000);
  126. this.droidSpeedX = droidSpeedX;
  127. this.droidSpeedY = droidSpeedY;
  128. this.healthDroids = healthDroids;
  129. this.width = width;
  130. this.height = height;
  131. difficulty = difficulties[PreferenceManager.getDefaultSharedPreferences(context).getInt("difficulty", 0)];
  132. SharedPreferences playerData = c.getSharedPreferences("pdata", Context.MODE_PRIVATE);
  133. final int health = playerData.getInt("health", 100);
  134. final long xp = playerData.getLong("xp", 0);
  135. player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), width / 2, height / 2, health, xp);
  136. this.showRange = showRange;
  137. Bitmap hBmp = BitmapFactory.decodeResource(getResources(), R.drawable.heal_1);
  138. healBmp = Bitmap.createScaledBitmap(hBmp, baseSize, baseSize, false);
  139. Bitmap b1Bmp = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
  140. droidBmp = Bitmap.createScaledBitmap(b1Bmp, droidSize, droidSize, false);
  141. boss1Bmp = Bitmap.createScaledBitmap(b1Bmp, bossSize, bossSize, false);
  142. Bitmap dBmp = BitmapFactory.decodeResource(getResources(), R.drawable.dmg_1);
  143. dmgBmp = Bitmap.createScaledBitmap(dBmp, baseSize, baseSize, false);
  144. Bitmap sBmp = BitmapFactory.decodeResource(getResources(), R.drawable.shield_1);
  145. shieldBmp = Bitmap.createScaledBitmap(sBmp, baseSize, baseSize, false);
  146. Matrix matrix = new Matrix();
  147. matrix.postRotate(180);
  148. advHealBmp = Bitmap.createBitmap(healBmp, 0, 0, healBmp.getWidth(), healBmp.getHeight(), matrix, true);
  149. droidList = new ArrayList<Droid>();
  150. obstacleList = new ArrayList<Obstacle>();
  151. healList = new ArrayList<Heal>();
  152. corners = new ArrayList<Spike>();
  153. dmgList = new ArrayList<DamageBoost>();
  154. advHealList = new ArrayList<AdvHeal>();
  155. shieldList = new ArrayList<Shield>();
  156. Random r = new Random();
  157. rand = new Random(r.nextInt(99999));
  158. //obstacleList.add(new Obstacle(BitmapFactory.decodeResource(getResources(), R.drawable.stone_1), r.nextInt(width / 2), r.nextInt(height / 2)));
  159. for(int i = 0; i < droids; i++)
  160. {
  161. droidList.add(new Droid(droidBmp, r.nextInt(width), r.nextInt(height)));
  162. }
  163. for(Droid droid : droidList)
  164. {
  165. droid.changeSpeed((droidSpeedX * r.nextInt(5)) / 3 + 1, (droidSpeedY * r.nextInt(5)) / 3 + 1);
  166. droid.setHealth(healthDroids);
  167. droid.setRangeAndDamage(this.baseRange, this.toAddRange);
  168. }
  169. int[] widths = {0, width, 0, width};
  170. int[] heights = {0, 0, height, height};
  171. for(int i = 0; i < 4; i++)
  172. {
  173. corners.add(new Spike(BitmapFactory.decodeResource(getResources(), R.drawable.stone_1), widths[i], heights[i]));
  174. }
  175. getHolder().addCallback(this);
  176. thread = new GameThread(getHolder(), this, max_fps, max_frameskips);
  177. setFocusable(true);
  178. thread.setRunning(true);
  179. thread.setGameRunning(true);
  180. thread.start();
  181. }
  182.  
  183. @Working
  184. public GameThread getGameThread()
  185. {
  186. return thread;
  187. }
  188.  
  189. @Working
  190. @Override
  191. public void surfaceCreated(SurfaceHolder p1)
  192. {
  193. thread.setGameRunning(true);
  194. }
  195.  
  196. @Untested
  197. @Override
  198. public void surfaceChanged(SurfaceHolder p1, int p2, int p3, int p4)
  199. {
  200. // TODO: Implement this method
  201. }
  202.  
  203. @Working
  204. @Override
  205. public void surfaceDestroyed(SurfaceHolder p1)
  206. {
  207. thread.setGameRunning(false);
  208. }
  209.  
  210. @Working
  211. public void setAvgFps(String avgFps)
  212. {
  213. this.avgFps = avgFps;
  214. }
  215.  
  216.  
  217. @Working
  218. public void render(Canvas canvas)
  219. {
  220. onDraw(canvas);
  221. }
  222.  
  223. @Working
  224. @Override
  225. protected void onDraw(Canvas canvas)
  226. {
  227. Paint paint = new Paint();
  228. float defSize = paint.getTextSize();
  229. paint.setARGB(255, 255, 255, 255);
  230. if(remainingDmg > 0) remainingDmg--;
  231. if(remainingDmg == 0) player.resetDmg();
  232. if(remainingHeal > 0)
  233. {
  234. player.healInt(healAmount);
  235. remainingHeal--;
  236. }
  237. for(Spike spike : corners)
  238. {
  239. spike.draw(canvas);
  240. }
  241. canvas.drawColor(Color.BLACK);
  242. if(!gameOver)
  243. {
  244. for(Shield shield : shieldList)
  245. {
  246. if(player.getHitbox().isCircleIntersectingWithCircle(shield.getHitbox()))
  247. {
  248. player.addShield(shield.getDurability());
  249. shieldList.remove(shield);
  250. }
  251. else shield.draw(canvas);
  252. }
  253. for(DamageBoost dmg : dmgList)
  254. {
  255. if(player.getHitbox().isCircleIntersectingWithCircle(dmg.getHitbox()))
  256. {
  257. player.getWeapon().setDamage(dmg.getDamage());
  258. remainingDmg = dmg.getDuration();
  259. dmgList.remove(dmg);
  260. }
  261. else dmg.draw(canvas);
  262. }
  263. for(AdvHeal heal : advHealList)
  264. {
  265. if(player.getHitbox().isCircleIntersectingWithCircle(heal.getHitbox()))
  266. {
  267. remainingHeal = heal.getDuration();
  268. healAmount = heal.getHealValue();
  269. advHealList.remove(heal);
  270. }
  271. else heal.draw(canvas);
  272. }
  273. for(Heal heal : healList)
  274. {
  275. if(player.getHitbox().isCircleIntersectingWithCircle(heal.getHitbox()))
  276. {
  277. player.heal(heal.getHeal());
  278. healList.remove(heal);
  279. }
  280. else heal.draw(canvas);
  281. }
  282. for(Obstacle obstacle : obstacleList)
  283. {
  284. obstacle.draw(canvas, this);
  285. }
  286. for(Droid droid : droidList)
  287. {
  288. if(droid instanceof Boss)
  289. {
  290. droid = (Boss) droid;
  291. bossRound = true;
  292. }
  293. if(droid instanceof Boss1) droid = (Boss1) droid;
  294. if(!droid.getDead())
  295. {
  296. droid.draw(canvas);
  297. if(showRange)
  298. {
  299. int colorMode = 0;
  300. for(Obstacle obstacle : obstacleList)
  301. {
  302. if(droid.getAttackArea().isPointInCircle(obstacle.getPosition())) colorMode = 1;
  303. }
  304. for(Droid droid2 : droidList)
  305. {
  306. if(droid.getAttackArea().isCircleIntersectingWithCircle(droid2.getAttackArea()))
  307. {
  308. if(!(droid2.getId() == droid.getId())) colorMode = 3;
  309. }
  310. if(droid.getAttackArea().isPointInCircle(droid2.getPosition()))
  311. {
  312. if(!(droid2.getId() == droid.getId()))
  313. {
  314. colorMode = 2;
  315. droid.attack(canvas, droid2);
  316. }
  317. }
  318. }
  319. if(droid.getAttackArea().isCircleIntersectingWithCircle(player.getAttackArea())) colorMode = 3;
  320. if(droid.getAttackArea().isPointInCircle(player.getPosition()))
  321. {
  322. colorMode = 2;
  323. droid.attack(canvas, player);
  324. }
  325. droid.drawRange(canvas, this, colorMode);
  326. }
  327. else
  328. {
  329. for(Droid droid2 : droidList)
  330. {
  331. if(droid.getAttackArea().isPointInCircle(droid2.getPosition()))
  332. {
  333. if(!(droid2.getId() == droid.getId()))
  334. {
  335. droid.attack(canvas, droid2);
  336. }
  337. }
  338. }
  339. if(droid.getAttackArea().isPointInCircle(player.getPosition())) droid.attack(canvas, player);
  340. }
  341. }
  342. else
  343. {
  344. droidList.remove(droid);
  345. }
  346. }
  347. player.draw(canvas);
  348. paint.setTextSize(defSize * 3);
  349. if(remainingDmg > 0) canvas.drawText(remainingDmg + "", player.getPosition().getX() - (player.getBitmap().getWidth() / 2), player.getPosition().getY() + (player.getBitmap().getHeight() / 2), paint);
  350. if(showRange)
  351. {
  352. int colorMode = 0;
  353. for(Obstacle obstacle : obstacleList)
  354. {
  355. if(player.getAttackArea().isPointInCircle(obstacle.getPosition())) colorMode = 1;
  356. }
  357. for(Droid droid : droidList)
  358. {
  359. if(player.getAttackArea().isCircleIntersectingWithCircle(droid.getAttackArea())) colorMode = 3;
  360. if(player.getAttackArea().isPointInCircle(droid.getPosition()))
  361. {
  362. colorMode = 2;
  363. player.attack(canvas, droid);
  364. }
  365. }
  366. player.drawRange(canvas, this, colorMode);
  367. }
  368. else
  369. {
  370. for(Droid droid : droidList)
  371. {
  372. if(player.getAttackArea().isPointInCircle(droid.getPosition())) player.attack(canvas, droid);
  373. }
  374. }
  375. displayFps(canvas, avgFps);
  376. paint.setTextSize(defSize * 2.5f);
  377. canvas.drawText("Round " + rounds, 20, 25, paint);
  378. }
  379. else
  380. {
  381. String gameOverMsg = " ";
  382. switch(reasonGameOver)
  383. {
  384. case 1:
  385. main.onLose(rounds, difficulty);
  386. break;
  387. case 2:
  388. gameOverMsg = "Carregando round" + rounds ;
  389. if(numberDroids < 30) numberDroids++;
  390. if((int)(rounds) % 30 != 0)
  391. {
  392. droidList.clear();
  393. for(int i = 0; i < numberDroids; i++)
  394. {
  395. droidList.add(new Droid(droidBmp, rand.nextInt(width), rand.nextInt(height)));
  396. }
  397. for(Droid droid : droidList)
  398. {
  399. droid.changeSpeed((droidSpeedX * rand.nextInt(5)) / 3 + 1, (droidSpeedY * rand.nextInt(5)) / 3 + 1);
  400. droid.setHealth(healthDroids);
  401. droid.setRangeAndDamage(this.baseRange, this.toAddRange);
  402. }
  403. }
  404. else
  405. {
  406. droidList.clear();
  407. droidList.add(new Boss1(boss1Bmp, width / 2, height / 2));
  408. for(Droid droid : droidList)
  409. {
  410. droid.changeSpeed((droidSpeedX * rand.nextInt(5)) / 3 + 1, (droidSpeedY * rand.nextInt(5)) / 3 + 1);
  411. droid.setRangeAndDamage(this.baseRange, this.toAddRange);
  412. droid.setHealth((50000 * bossNumber) + 100000);
  413. }
  414. bossNumber++;
  415. }
  416. gameOver = false;
  417. break;
  418. default:
  419. gameOverMsg = "erro";
  420. }
  421. /*for(Explosion e : explosions)
  422. {
  423. for(Particle p : e.getParticles())
  424. {
  425. p.draw(canvas);
  426. }
  427. }*/
  428. paint.setTextSize(defSize * 8);
  429. canvas.drawText(gameOverMsg, 200, 500, paint);
  430. }
  431. }
  432.  
  433. @Working
  434. public void drawCircle(Canvas canvas, Paint.Style style, int cx, int cy, float radius, int alpha, int red, int green, int blue)
  435. {
  436. Paint paint = new Paint();
  437. paint.setStyle(style);
  438. paint.setARGB(alpha, red, green, blue);
  439. canvas.drawCircle(cx, cy, radius, paint);
  440. }
  441.  
  442. @Working
  443. public void displayFps(Canvas canvas, String fps)
  444. {
  445. try
  446. {
  447. if(canvas != null && fps != null)
  448. {
  449. drawText(canvas, fps, 2.5f, this.getWidth() - 100, 25, 255, 255, 255, 255);
  450. }
  451. }
  452. catch(Exception e) {}
  453. }
  454.  
  455. @Working
  456. public void drawText(Canvas canvas, String text, float textSize, int x, int y, int alpha, int red, int green, int blue)
  457. {
  458. try
  459. {
  460. if(canvas != null && text != null)
  461. {
  462. Paint paint = new Paint();
  463. paint.setARGB(alpha, red, green, blue);
  464. paint.setTextSize(paint.getTextSize() * textSize);
  465. canvas.drawText(text, x, y, paint);
  466. }
  467. }
  468. catch(Exception e) {}
  469. }
  470.  
  471. @Working
  472. @Override
  473. public boolean onTouchEvent(MotionEvent event)
  474. {
  475. if(event.getAction() == MotionEvent.ACTION_DOWN)
  476. {
  477. // delegating event handling to the droid
  478.  
  479. /*int currentExplosion = 0;
  480. Explosion explosion = explosions[currentExplosion];
  481. while (explosion != null && explosion.isAlive() && currentExplosion < explosions.length - 1)
  482. {
  483. currentExplosion++;
  484. explosion = explosions[currentExplosion];
  485. }
  486. if (explosion == null || explosion.isDead())
  487. {
  488. explosion = new Explosion(100, (int)event.getX(), (int)event.getY());
  489. explosions[currentExplosion] = explosion;
  490. }*/
  491.  
  492. player.handleActionDown((int) event.getX(), (int) event.getY());
  493.  
  494. // check if in the lower part of the screen we exit
  495. if(debug)
  496. {
  497. Log.d(TAG, "Coords: {x = " + event.getX() + ", y = " + event.getY() + "}");
  498. }
  499. }
  500. if (event.getAction() == MotionEvent.ACTION_MOVE)
  501. {
  502. // the gestures
  503.  
  504. if (player.isTouched())
  505. {
  506. // the droid was picked up and is being dragged
  507. player.setX((int)event.getX());
  508. player.setY((int)event.getY());
  509. }
  510.  
  511. }
  512. if (event.getAction() == MotionEvent.ACTION_UP)
  513. {
  514. // touch was released
  515. if(player.isTouched())
  516. {
  517. player.setTouched(false);
  518. }
  519. }
  520. return true;
  521. }
  522.  
  523. public static void droidDead()
  524. {
  525. //Log.i(MainGamePanel.class.getSimpleName(), "Adding 10 currency");
  526. player.addCurrency(10);
  527. }
  528.  
  529. @Working
  530. public void update()
  531. {
  532. if(bossRound)
  533. {
  534. int y = rand.nextInt(healChance);
  535. if(y == 0) if(dmgList.size() < 1) dmgList.add(new DamageBoost(dmgBmp, rand.nextInt(width), rand.nextInt(height)));
  536. int z = rand.nextInt(healChance);
  537. if(z == 0) if(healList.size() < 1) healList.add(new Heal(healBmp, rand.nextInt(width), rand.nextInt(height)));
  538. int a = rand.nextInt(healChance);
  539. if(a == 0) if(shieldList.size() < 1) shieldList.add(new Shield(shieldBmp, rand.nextInt(width), rand.nextInt(height)));
  540. }
  541. int x = rand.nextInt(healChance);
  542. if(x == 0) if(advHealList.size() < maxheals) advHealList.add(new AdvHeal(advHealBmp, rand.nextInt(width), rand.nextInt(height)));
  543. for(Droid droid : droidList)
  544. {
  545. if(!droid.getDead())
  546. {
  547. if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth())
  548. {
  549. droid.getSpeed().toggleXDirection();
  550. }
  551.  
  552. // check collision with left wall if heading left
  553. if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && droid.getX() - droid.getBitmap().getWidth() / 2 <= 0)
  554. {
  555. droid.getSpeed().toggleXDirection();
  556. }
  557.  
  558. // check collision with bottom wall if heading down
  559. if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight())
  560. {
  561. droid.getSpeed().toggleYDirection();
  562. }
  563.  
  564. // check collision with top wall if heading up
  565. if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP && droid.getY() - droid.getBitmap().getHeight() / 2 <= 0)
  566. {
  567. droid.getSpeed().toggleYDirection();
  568. }
  569.  
  570. // Update the lone droid
  571. droid.update();
  572. }
  573. else
  574. {
  575. droidList.remove(droid);
  576. }
  577. }
  578. if(droidList.isEmpty())
  579. {
  580. main.saveCurrency(player.getCurrency());
  581. player.setCurrency(0);
  582. if(bossRound) player.heal(1);
  583. bossRound = false;
  584. rounds++;
  585. if(rounds == Long.MAX_VALUE) thread.setRunning(false);
  586. reasonGameOver = 2;
  587. gameOver = true;
  588. }
  589. if(!player.getDead())
  590. {
  591. player.update();
  592. }
  593. else
  594. {
  595. //thread.setGameRunning(false);
  596. reasonGameOver = 1;
  597. gameOver = true;
  598. player.die();
  599. }
  600. }
  601. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement