Guest User

Untitled

a guest
Aug 16th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.51 KB | None | 0 0
  1. package com.bacteria.app;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.SharedPreferences;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Canvas;
  11. import android.graphics.Color;
  12. import android.graphics.Paint;
  13. import android.graphics.PixelFormat;
  14. import android.graphics.Rect;
  15. import android.graphics.Typeface;
  16. import android.graphics.drawable.Drawable;
  17. import android.media.MediaPlayer;
  18. import android.os.AsyncTask;
  19. import android.os.Bundle;
  20. import android.util.DisplayMetrics;
  21. import android.util.Log;
  22. import android.view.MotionEvent;
  23. import android.view.SurfaceHolder;
  24. import android.view.SurfaceView;
  25. import android.view.View;
  26. import android.view.View.OnTouchListener;
  27. import android.view.Window;
  28. import android.view.WindowManager;
  29. import android.widget.EditText;
  30. import android.widget.LinearLayout;
  31. import android.widget.Toast;
  32.  
  33. import com.google.android.gms.ads.AdRequest;
  34. import com.google.android.gms.ads.AdView;
  35.  
  36. import java.io.BufferedReader;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.io.InputStreamReader;
  40. import java.util.ArrayList;
  41. import java.util.Random;
  42. import java.util.concurrent.CopyOnWriteArrayList;
  43.  
  44. class myVector2d{
  45. public float x;
  46. public float y;
  47. myVector2d(float _x , float _y){
  48. x = _x;
  49. y = _y;
  50. }
  51. public myVector2d norm(){
  52. Double d = Math.sqrt(x*x+y*y);
  53. if(d != 0){
  54. x /= d;
  55. y /= d;
  56. }
  57. return this;
  58. }
  59. public double dist(){
  60. return Math.sqrt(x*x+y*y);
  61. }
  62. public void rotation(float angle){
  63. float lx = x;
  64. float ly = y;
  65. x = (float) (x*Math.cos(angle) - ly * Math.sin(angle));
  66. y = (float) (lx*Math.sin(angle) + ly * Math.cos(angle));
  67. }
  68. }
  69.  
  70.  
  71.  
  72. enum GameMod { ACTIVE_GAME, PAUSE, MAP_MENU, LEVEL_MENU, BETWEEN_LEVEL, LOAD_GAME }
  73.  
  74. public class FullscreenActivity extends Activity implements OnTouchListener {
  75.  
  76. public Bitmap scaleToWidth(Bitmap b , int width){
  77. return Bitmap.createScaledBitmap(b , width, width*b.getHeight()/b.getWidth() , true);
  78. }
  79.  
  80. public Bitmap bitmapLoadRaw(int raw){
  81. return BitmapFactory.decodeStream(getResources().openRawResource(raw));
  82. }
  83. public float distBetween(float x1, float y1, float x2, float y2){
  84. return (float) Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  85. }
  86.  
  87. GameMod gameMod = GameMod.LOAD_GAME;
  88.  
  89. AdView adView;
  90.  
  91. Typeface fontFace;
  92.  
  93. Bitmap fonImgB , buttonNextLevel , fingerImg;
  94. Bitmap[][] iconLevel = new Bitmap[4][2];
  95. Bitmap[][] virusImg = new Bitmap[9][16];
  96. Bitmap[][] GameMap = new Bitmap[3][2];
  97. Bitmap[][][] bactImg = new Bitmap[9][2][3];
  98. Bitmap borderImg;
  99. int[] bactRadius = new int[3];
  100. int[][] bactColor = {{100,255,150} , {255,100,100} , {100,150,255} ,
  101. {255,140,200} , {255,255,0} , {255,255,255} , {0,0,0} , {0,0,0} , {0,0,0}};
  102. public int gameHardLevel = 100;
  103. public int gameLevel = 0;
  104. public int mapLevel = 0;
  105. public int score = 0;
  106. public int countAtacBact = 100;
  107. public int timeToAtacBact = 7000;
  108. public float timeCorrect = 0;
  109. public int numberOfAttackBact = 0;
  110. int timeSurvival = 0;
  111. public int playerIndexBact = 0;
  112. public int xxxReplaceIt = 8;
  113. public int WHmin;
  114. public float overWidth; // use for different DPI;
  115. public float mouseX, mouseY;
  116. public boolean canDrawLine;
  117. public boolean needInit = false;
  118. public boolean survival = false;
  119. public boolean playerWin = true;
  120. public boolean playerLose = true;
  121. public int timeFromInit;
  122. DisplayMetrics metrics = new DisplayMetrics();
  123. public int heightView;
  124. Random rn = new Random();
  125.  
  126. EditText etText;
  127. SharedPreferences sPref;
  128.  
  129. long startTimeLevel, finishTimeLevel, startTimeInit;
  130.  
  131. DrawView dv;
  132.  
  133. //CopyOnWriteArrayList<Integer> passageTime = new CopyOnWriteArrayList<Integer>();
  134. public int[] passageTime = new int [30];
  135. CopyOnWriteArrayList<Bacteria> bact = new CopyOnWriteArrayList<Bacteria>();
  136. CopyOnWriteArrayList<Virus> virus = new CopyOnWriteArrayList<Virus>();
  137. CopyOnWriteArrayList<GameLevel> gameLevels = new CopyOnWriteArrayList<GameLevel>();
  138. CopyOnWriteArrayList<myButton> myButtons = new CopyOnWriteArrayList<myButton>();
  139. CopyOnWriteArrayList<floatingNumber> floatingNumbers= new CopyOnWriteArrayList<floatingNumber>();
  140. CopyOnWriteArrayList<BactАttack> bactАttacks= new CopyOnWriteArrayList<BactАttack>();
  141.  
  142. class BactАttack{
  143. //public int time;
  144. public String all;
  145. }
  146.  
  147. class floatingNumber{
  148. public int number,team;
  149. public float life,x,y;
  150. }
  151.  
  152. class myButton{
  153. public int x1 = 14;
  154. public int y1 = 14;
  155. public int x2 = 430;
  156. public int y2 = 62;
  157. public Bitmap just;
  158. public Bitmap active;
  159. public int nWidth, nHeight;
  160. public int timePress , x , y;
  161. public int dx,dy;
  162. public int maxTimePress = 500;
  163. public boolean pressed = false;
  164. public boolean click = false;
  165. public boolean show = true;
  166. public String text = "";
  167. public void draw(Canvas canvas , Paint p){
  168. if(maxTimePress < 16 || true){
  169. if(timePress > 0){
  170. canvas.drawBitmap(active, x + dx, y + dy , p);
  171. }else{
  172. canvas.drawBitmap(just,x,y,p);
  173. }
  174. }else {
  175. p.setAlpha(255);
  176. canvas.drawBitmap(just, x, y, p);
  177. if (255 * timePress / maxTimePress > 0) {
  178. p.setAlpha(255 * timePress / maxTimePress);
  179. canvas.drawBitmap(active, x + dx, y + dy, p);
  180. }
  181. p.setAlpha(255);
  182. }
  183.  
  184. }
  185. int width(){
  186. return just.getWidth();
  187. }
  188. int height(){
  189. return just.getHeight();
  190. }
  191.  
  192. void render(){
  193. Paint p = new Paint();
  194. Bitmap tmp = Bitmap.createBitmap(nWidth , nHeight, Bitmap.Config.ARGB_8888);
  195. Canvas canvas = new Canvas(tmp);
  196. Rect rect = new Rect(0,0,x1,y1);
  197.  
  198.  
  199. int RW = nWidth - width();
  200. int RH = nHeight - height();
  201.  
  202.  
  203. canvas.drawBitmap(just, rect , rect ,p);
  204.  
  205. canvas.drawBitmap(just, new Rect(x1,0,x2,y1) , new Rect(x1,0,x2+RW , y1) ,p);
  206.  
  207. canvas.drawBitmap(just, new Rect(x2,0,width(),y1) , new Rect(x2 + RW,0, nWidth , y1) ,p);
  208.  
  209. canvas.drawBitmap(just, new Rect(0, y1, x1 ,y2) , new Rect(0, y1, x1 , y2 + RH) ,p);
  210.  
  211. canvas.drawBitmap(just, new Rect(x1, y1, x2 ,y2) , new Rect(x1,y1, x2 + RW , y2 + RH) ,p);
  212.  
  213. canvas.drawBitmap(just, new Rect(x2, y1, width() ,y2) , new Rect(x2 + RW ,y1, nWidth , y2 + RH) ,p);
  214.  
  215. canvas.drawBitmap(just, new Rect(0, y2, x1 ,height()) , new Rect(0, y2 + RH, x1 , nHeight) ,p);
  216.  
  217. canvas.drawBitmap(just, new Rect(x1, y2, x2, height()) , new Rect(x1, y2 + RH, x2 + RW , nHeight) ,p);
  218.  
  219. canvas.drawBitmap(just, new Rect(x2, y2, width(), height()) , new Rect(x2 + RW, y2 + RH, nWidth , nHeight) ,p);
  220.  
  221. p.setTypeface(fontFace);
  222. p.setFlags(Paint.ANTI_ALIAS_FLAG);
  223. p.setTextAlign(Paint.Align.CENTER);
  224. p.setTextSize(WHmin/25);
  225. canvas.drawText(text , nWidth/2 , nHeight/2 , p);
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. Bitmap tmp2 = Bitmap.createBitmap((int) (nWidth*1.1), (int) (nHeight*1.1), Bitmap.Config.ARGB_8888);
  233. canvas = new Canvas(tmp2);
  234. rect = new Rect(0,0,x1,y1);
  235.  
  236.  
  237.  
  238. RW = (int) (nWidth + nHeight*0.1 - width());
  239. RH = (int) (nHeight*1.1 - height());
  240.  
  241. dx = (int) (-nHeight*0.05);
  242. dy = (int) (-nHeight*0.05);
  243.  
  244. nWidth += nHeight*0.1;
  245. nHeight *= 1.1;
  246.  
  247.  
  248.  
  249.  
  250. canvas.drawBitmap(just, rect , rect ,p);
  251.  
  252. canvas.drawBitmap(just, new Rect(x1,0,x2,y1) , new Rect(x1,0,x2+RW , y1) ,p);
  253.  
  254. canvas.drawBitmap(just, new Rect(x2,0,width(),y1) , new Rect(x2 + RW,0, nWidth , y1) ,p);
  255.  
  256. canvas.drawBitmap(just, new Rect(0, y1, x1 ,y2) , new Rect(0, y1, x1 , y2 + RH) ,p);
  257.  
  258. canvas.drawBitmap(just, new Rect(x1, y1, x2 ,y2) , new Rect(x1,y1, x2 + RW , y2 + RH) ,p);
  259.  
  260. canvas.drawBitmap(just, new Rect(x2, y1, width() ,y2) , new Rect(x2 + RW ,y1, nWidth , y2 + RH) ,p);
  261.  
  262. canvas.drawBitmap(just, new Rect(0, y2, x1 ,height()) , new Rect(0, y2 + RH, x1 , nHeight) ,p);
  263.  
  264. canvas.drawBitmap(just, new Rect(x1, y2, x2, height()) , new Rect(x1, y2 + RH, x2 + RW , nHeight) ,p);
  265.  
  266. canvas.drawBitmap(just, new Rect(x2, y2, width(), height()) , new Rect(x2 + RW, y2 + RH, nWidth , nHeight) ,p);
  267.  
  268. p.setTypeface(fontFace);
  269. p.setFlags(Paint.ANTI_ALIAS_FLAG);
  270. p.setTextAlign(Paint.Align.CENTER);
  271. p.setTextSize(WHmin/25);
  272. canvas.drawText(text , nWidth/2 , nHeight/2 , p);
  273.  
  274. just = tmp;
  275. active = tmp2;
  276.  
  277. }
  278. }
  279.  
  280. class LoadStruct{
  281. public float x,y;
  282. public int paintTypeRadius , team;
  283. }
  284.  
  285. class GameLevel{
  286. ArrayList<LoadStruct> one = new ArrayList<LoadStruct>();
  287. int[] times = new int[2];
  288. }
  289.  
  290. class Virus{
  291. public float x,y,dx,dy,vx,vy;
  292. public int team,target,parent;
  293. public float drawBorder = 0;
  294. public boolean borderGrow;
  295. public void move(float TL){
  296. myVector2d vec = new myVector2d(dx,dy);
  297.  
  298. Bacteria b = bact.get(target);
  299.  
  300. if(vec.dist() > 1){
  301. vec.norm();
  302. }
  303.  
  304. myVector2d vec2 = new myVector2d(b.x - x, b.y - y).norm();
  305. vec.x += vec2.x*0.7;
  306. vec.y += vec2.y*0.7;
  307.  
  308. vec.norm();
  309. float angle = (float) ((rn.nextInt(100)-50)/40.0);
  310. //Log.e("",Float.toString(angle));
  311. vec.rotation(angle);
  312.  
  313. vx += vec.x*0.4;
  314. vy += vec.y*0.4;
  315.  
  316.  
  317.  
  318. //x += (vx+(rn.nextInt(100)-50)/50.0)*overWidth*1.5*TL;
  319. //y += (vy+(rn.nextInt(100)-50)/50.0)*overWidth*1.5*TL;
  320.  
  321. x += vx*overWidth*TL;
  322. y += vy*overWidth*TL;
  323.  
  324. //x+= vec.x;
  325. //y+= vec.y;
  326. vx *= 0.95;
  327. vy *= 0.95;
  328.  
  329. dx = dy = 0;
  330. }
  331. }
  332.  
  333.  
  334.  
  335. class Bacteria{
  336. public float x;
  337. public float y;
  338. public float dx;
  339. public float dy;
  340. public int radius;
  341. public int paintTypeRadius;
  342. public int team;
  343. public boolean select;
  344. public boolean tmpSelect;
  345. public float count;
  346. public float maxCount;
  347. public float drawBorder = 0;
  348. public float lastNotif = 0;
  349.  
  350. public void move(float TL){
  351. drawBorder -= 1;
  352. lastNotif += 16.7*TL;
  353. x += dx*overWidth*TL;
  354. y += dy*overWidth*TL;
  355. dx *= 0.95;
  356. dy *= 0.95;
  357. if(team != xxxReplaceIt) {
  358. count += TL * 0.1 * Math.cos(Math.min(3.1415, 3.1415 / 2 * count / maxCount));
  359. }
  360. }
  361.  
  362. public void fire(int target , int parent){
  363. float tmpCount = bact.get(parent).count/2;
  364. for(int i=0; i<tmpCount; i++){
  365. Virus tmp = new Virus();
  366. tmp.drawBorder = 10 + rn.nextInt(200);
  367. tmp.dx = tmp.dy = 0;
  368. tmp.x = x + rn.nextInt(30) -15;
  369. tmp.y = y + rn.nextInt(30) -15;
  370. tmp.team = team;
  371. tmp.target = target;
  372. tmp.parent = parent;
  373. virus.add(tmp);
  374. count -= 1;
  375. }
  376. myVector2d vec = new myVector2d(bact.get(target).x - bact.get(parent).x ,bact.get(target).y - bact.get(parent).y ).norm();
  377. dx -= vec.x*count*0.05 + vec.x;
  378. dy -= vec.y*count*0.05 + vec.y;
  379. }
  380. }
  381.  
  382. public void DrawMapMenu(Canvas canvas, Paint p){
  383. canvas.drawBitmap(fonImgB, 0, 0, p);
  384. for(int i=0; i < myButtons.size() && i < 3; i++) {
  385. myButton b = myButtons.get(i);
  386. b.draw(canvas,p);
  387. if(i != 0) {
  388. if (b.click) {
  389. gameMod = GameMod.LEVEL_MENU;
  390. survival = false;
  391. mapLevel = i;
  392. b.click = false;
  393. needInit = true;
  394. }
  395. }else{
  396. if (b.click) {
  397. gameMod = GameMod.ACTIVE_GAME;
  398. gameLevel = 30;
  399. survival = true;
  400. b.click = false;
  401. needInit = true;
  402. }
  403. }
  404. }
  405. }
  406.  
  407. public void DrawBetweenLevel(Canvas canvas, Paint p){
  408. int time = (int) (System.currentTimeMillis() - startTimeInit);
  409. canvas.drawBitmap(fonImgB, 0, 0, p);
  410.  
  411.  
  412. for (int i = 0; i < bact.size(); i++) {
  413. Bacteria b = bact.get(i);
  414. int s = 0;
  415.  
  416. Bitmap tmpBitmap = bactImg[b.team][s][b.paintTypeRadius];
  417. canvas.drawBitmap(tmpBitmap, b.x - tmpBitmap.getWidth() / 2, b.y - tmpBitmap.getWidth() / 2, p);
  418. }
  419.  
  420. for(int i = 0; i < virus.size(); i++){
  421. Virus l = virus.get(i);
  422. canvas.drawBitmap(virusImg[l.team][0] , l.x - 6 , l.y - 6 , p);
  423.  
  424. }
  425.  
  426.  
  427. p.setAlpha(Math.min(255 , time/3));
  428. Log.e("", Integer.toString(p.getAlpha()));
  429.  
  430.  
  431.  
  432. canvas.drawBitmap(borderImg,0,0,p);
  433.  
  434. canvas.drawText("Your result " + Long.toString((finishTimeLevel - startTimeLevel)/1000) + ","
  435. + Long.toString((finishTimeLevel - startTimeLevel) % 1000) + "s" , (int)(0.5*metrics.widthPixels) , (float) (0.16*metrics.heightPixels), p);
  436.  
  437. canvas.drawText("Green level - " + Integer.toString(gameLevels.get(gameLevel).times[0]) + " Blue level - " + Integer.toString(gameLevels.get(gameLevel).times[1]) , (int)(0.5*metrics.widthPixels) , (float) (0.20*metrics.heightPixels), p);
  438.  
  439.  
  440. for (myButton b : myButtons) {
  441. b.show = true;
  442. b.draw(canvas, p);
  443. if (b.click) {
  444. b.click = false;
  445. gameMod = GameMod.ACTIVE_GAME;
  446. if(survival){
  447. numberOfAttackBact = 0;
  448. timeToAtacBact = 7000;
  449. }
  450. needInit = true;
  451. if(playerWin) {
  452. gameLevel++;
  453. }
  454. }
  455. }
  456.  
  457. if(Math.min(255 , time/4) < 255) {
  458. p.setAlpha(255);
  459. dv.drawThread.update = true;
  460. }
  461. }
  462.  
  463. public void DrawPause(Canvas canvas, Paint p){
  464. int time = timeFromInit;
  465. canvas.drawBitmap(fonImgB, 0, 0, p);
  466.  
  467. for (int i = 0; i < bact.size(); i++) {
  468. Bacteria b = bact.get(i);
  469. int s = 0;
  470. if (b.select || b.tmpSelect) {
  471. s = 1;
  472. }
  473. Bitmap tmpBitmap = bactImg[b.team][s][b.paintTypeRadius];
  474. if (b.drawBorder > 0) {
  475. p.setColor(Color.argb((int) b.drawBorder * 3, bactColor[b.team][0], bactColor[b.team][1], bactColor[b.team][2]));
  476. canvas.drawCircle(b.x, b.y, bactImg[b.team][1][b.paintTypeRadius].getWidth() / 2 + (float) (5 + b.drawBorder * 0.3), p);
  477. p.setColor(Color.BLACK);
  478. }
  479.  
  480. canvas.drawBitmap(tmpBitmap, b.x - tmpBitmap.getWidth() / 2, b.y - tmpBitmap.getWidth() / 2, p);
  481. }
  482.  
  483. for(int i = 0; i < virus.size(); i++){
  484. Virus l = virus.get(i);
  485. canvas.drawBitmap(virusImg[l.team][0] , l.x - 6 , l.y - 6 , p);
  486.  
  487. }
  488.  
  489.  
  490.  
  491. p.setAlpha(Math.min(255 , time/3));
  492. Log.e("", Integer.toString(p.getAlpha()) + " " + Long.toString(timeFromInit) + " " + Long.toString(startTimeInit));
  493. canvas.drawBitmap(borderImg,0,0,p);
  494.  
  495.  
  496. for(int i = 0; i < myButtons.size(); i++) {
  497. myButton b = myButtons.get(i);
  498. b.show = true;
  499. b.draw(canvas,p);
  500. if(b.click){
  501.  
  502. switch (i){
  503. case 0:
  504. gameMod = GameMod.ACTIVE_GAME;
  505. break;
  506. case 1:
  507. gameMod = GameMod.ACTIVE_GAME;
  508. needInit = true;
  509. break;
  510. case 2:
  511. gameMod = GameMod.MAP_MENU;
  512. needInit = true;
  513. break;
  514. }
  515. b.click = false;
  516. }
  517. }
  518.  
  519. if(Math.min(255 , time/4) < 255) {
  520. p.setAlpha(255);
  521. dv.drawThread.update = true;
  522. }
  523. }
  524.  
  525. public void DrawActiveGame(Canvas canvas, Paint p , float timeLag, int width , int height){
  526. canvas.drawBitmap(fonImgB, 0, 0, p);
  527.  
  528. playerWin = true;
  529. playerLose = true;
  530. for(Bacteria b : bact){
  531. if(b.team != playerIndexBact){
  532. playerWin = false;
  533. }else{
  534. playerLose = false;
  535. }
  536. }
  537.  
  538. for(Virus l : virus){
  539. if(l.team != playerIndexBact){
  540. playerWin = false;
  541. }else{
  542. playerLose = false;
  543. }
  544. }
  545.  
  546.  
  547. for (int i = 0; i < bact.size(); i++) {
  548. Bacteria b = bact.get(i);
  549. int s = 0;
  550. if (b.select || b.tmpSelect) {
  551. s = 1;
  552. }
  553. Bitmap tmpBitmap = bactImg[b.team][s][b.paintTypeRadius];
  554. if (b.drawBorder > 0) {
  555. p.setColor(Color.argb((int)b.drawBorder * 3, bactColor[b.team][0], bactColor[b.team][1], bactColor[b.team][2]));
  556. canvas.drawCircle(b.x, b.y, bactImg[b.team][1][b.paintTypeRadius].getWidth() / 2 + (float) (5 + b.drawBorder * 0.3), p);
  557. p.setColor(Color.BLACK);
  558. }
  559.  
  560. canvas.drawBitmap(tmpBitmap, b.x - tmpBitmap.getWidth() / 2, b.y - tmpBitmap.getWidth() / 2, p);
  561. canvas.drawText(Integer.toString((int) b.count), b.x, b.y + WHmin/75 , p);
  562.  
  563. float dist;
  564. float minDist = 9999999;
  565. int curKey = -1;
  566.  
  567.  
  568. for (int j = 0; j < bact.size(); j++) {
  569. if(i == j) continue;
  570. Bacteria bj = bact.get(j);
  571. dist = distBetween(b.x, b.y, bj.x, bj.y) - (b.radius + bj.radius);
  572. if (dist < minDist) {
  573. minDist = dist;
  574. curKey = j;
  575. }
  576.  
  577. timeCorrect += 16*timeLag;
  578. while(timeCorrect > 0) {
  579. timeCorrect -= 16;
  580. if (b.team != playerIndexBact && bj.team != b.team && b.team != xxxReplaceIt) {
  581. if (rn.nextInt(1000) < gameHardLevel &&
  582. dist / overWidth < rn.nextInt(1000) * rn.nextInt(1000) / 1000 &&
  583. b.count / b.maxCount * 10000 > 1000 + rn.nextInt(100) * rn.nextInt(100) &&
  584. b.count - bj.count > 10 - (rn.nextInt(20) * rn.nextInt(20) / 20)
  585. ) {
  586. b.fire(j, i);
  587. }
  588. }
  589. }
  590. }
  591.  
  592.  
  593. if (minDist < 0) {
  594. myVector2d vec = new myVector2d(bact.get(curKey).x - b.x, bact.get(curKey).y - b.y).norm();
  595.  
  596. b.dx -= vec.x;
  597. b.dy -= vec.y;
  598.  
  599. bact.get(curKey).dx += vec.x;
  600. bact.get(curKey).dy += vec.y;
  601. }
  602.  
  603.  
  604. if (b.x + b.radius > width) b.dx -= 0.5;
  605. if (b.y + b.radius > height) b.dy -= 0.5;
  606. if (b.x - b.radius < 0) b.dx += 0.5;
  607. if (b.y - b.radius < 0) b.dy += 0.5;
  608.  
  609. b.move(timeLag);
  610.  
  611. for (int j = virus.size() - 1; j >= 0; j--) {
  612. Virus l = virus.get(j);
  613. dist = distBetween(l.x, l.y, b.x ,b.y);
  614. if (dist < b.radius) {
  615. if (i == l.target && dist < b.radius) {
  616.  
  617. if (b.team == l.team) {
  618. b.count++;
  619. } else {
  620. b.count--;
  621. if(b.lastNotif > 32){
  622. b.lastNotif = 0;
  623. floatingNumber tmp = new floatingNumber();
  624. tmp.x = (int)b.x+(rn.nextInt(300)-150)*overWidth;
  625. tmp.y = (int)b.y+(rn.nextInt(300)-150)*overWidth*0;
  626. tmp.team = l.team;
  627. tmp.life = 1500;
  628. tmp.number = (int)b.count+1;
  629. floatingNumbers.add(tmp);
  630. }
  631. if(b.count < 1) { // capture
  632. b.count = 0;
  633. b.team = l.team;
  634. b.select = b.tmpSelect = false;
  635. b.drawBorder = 60;
  636. }
  637. }
  638.  
  639. myVector2d vec = new myVector2d(b.x - l.x, b.y - l.y).norm();
  640.  
  641. b.dx += vec.x * 0.01 * (b.radius-dist); // important
  642. b.dy += vec.y * 0.01 * (b.radius-dist);
  643.  
  644. virus.remove(j);
  645.  
  646. } else if (!(l.parent == i || l.target == i)) {
  647. myVector2d vec = new myVector2d(b.x - l.x, b.y - l.y).norm();
  648. float g = (b.radius + 20 - dist) / 30;
  649. l.dx += -vec.x * g;
  650. l.dy += -vec.y * g;
  651. }
  652. }
  653. }
  654. }
  655.  
  656. for(int i = floatingNumbers.size()-1; i >= 0; i--){
  657. floatingNumber n = floatingNumbers.get(i);
  658. p.setColor(Color.rgb(bactColor[n.team][0] , bactColor[n.team][1] , bactColor[n.team][2]));
  659. p.setAlpha((int) (255*n.life/1500));
  660. canvas.drawText(Integer.toString(n.number) , n.x , n.y , p);
  661. n.life -= 16*timeLag;
  662. n.y -= WHmin/360.0*timeLag;
  663. if(n.life < 0) floatingNumbers.remove(i);
  664. }
  665. p.setAlpha(255);
  666.  
  667. p.setStrokeWidth((float) (bactRadius[0] * 0.1));
  668. p.setColor(Color.WHITE);
  669. if (canDrawLine) {
  670. for (Bacteria b : bact) {
  671. if (b.select) canvas.drawLine(mouseX, mouseY, b.x, b.y, p);
  672. }
  673. }
  674. p.setColor(Color.BLACK);
  675.  
  676. for(int i = 0; i < virus.size(); i++){
  677. Virus l = virus.get(i);
  678. l.move(timeLag);
  679. if(l.drawBorder < 20){l.borderGrow = true;}
  680. if(l.drawBorder > 234){l.borderGrow = false;}
  681. if(l.borderGrow){
  682. l.drawBorder += rn.nextInt(20)*timeLag;
  683. }else {
  684. l.drawBorder -= rn.nextInt(20)*timeLag;
  685. }
  686. l.drawBorder = Math.min(255,Math.max(0,l.drawBorder));
  687.  
  688.  
  689.  
  690. canvas.drawBitmap(virusImg[l.team][(int)l.drawBorder/16],l.x-virusImg[0][0].getWidth()/2, l.y-virusImg[0][0].getWidth()/2 , p);
  691.  
  692. }
  693.  
  694. if(gameLevel == 1){
  695. float t = System.currentTimeMillis() - startTimeLevel;
  696. int x = (int) (bact.get(0).x + (bact.get(1).x - bact.get(0).x)*( 1 + Math.sin(t*0.001))/2.0 - WHmin/5*256/600.0);
  697. int y = (int) (bact.get(0).y + (bact.get(1).y - bact.get(0).y)*( 1 + Math.sin(t*0.001))/2.0);
  698. canvas.drawBitmap(fingerImg,x , y,p);
  699.  
  700.  
  701. /*floatingNumber tmp = new floatingNumber();
  702. tmp.x = (float) (x + WHmin/5*256/600.0) + rn.nextInt(50)-25;
  703. tmp.y = y + rn.nextInt(50)-25 ;
  704. tmp.team = rn.nextInt(3);
  705. tmp.life = 1500;
  706. tmp.number = 0;
  707. floatingNumbers.add(tmp);*/
  708.  
  709.  
  710. }
  711.  
  712. if(survival) {
  713.  
  714. timeToAtacBact -= 16 * timeLag;
  715. timeSurvival += 16 * timeLag;
  716. canvas.drawText(Integer.toString(timeToAtacBact / 1000), 200, 200, p);
  717. if (timeToAtacBact < 0) {
  718.  
  719. BactАttack attack;
  720. if(bactАttacks.size() > numberOfAttackBact) {
  721.  
  722. attack = bactАttacks.get(numberOfAttackBact);
  723. numberOfAttackBact++;
  724.  
  725.  
  726. String[] AAA = attack.all.split(",");
  727.  
  728. timeToAtacBact = Integer.parseInt(AAA[0]);
  729. for(int j=1; j < AAA.length; j++) {
  730. String[] CCC = AAA[j].split("/");
  731.  
  732. int x = (int) (metrics.widthPixels*(-0.2) + metrics.widthPixels*(Integer.parseInt(CCC[0]) % 2)*1.4);
  733. int y = (int) (metrics.heightPixels*(-0.2) + metrics.heightPixels*(Integer.parseInt(CCC[0]) / 2)*1.4);
  734. for (int i = 0; i < Integer.parseInt(CCC[1]); i++) {
  735. Virus tmp = new Virus();
  736. tmp.dx = tmp.dy = 0;
  737. tmp.x = x;
  738. tmp.y = y;
  739. tmp.team = Integer.parseInt(CCC[0]);
  740. tmp.target = Integer.parseInt(CCC[2]);
  741. tmp.parent = 0;
  742. virus.add(tmp);
  743. }
  744. }
  745. }
  746. }
  747. if (playerLose) {
  748. gameMod = GameMod.BETWEEN_LEVEL;
  749. needInit = true;
  750. }
  751. }else { // not survival
  752.  
  753. if (playerLose || playerWin) {
  754. finishTimeLevel = System.currentTimeMillis();
  755. gameMod = GameMod.BETWEEN_LEVEL;
  756.  
  757. /*if (passageTime.size() <= gameLevel) {
  758. passageTime[gameLevel] = ((int) ((finishTimeLevel - startTimeLevel) / 1000));
  759. } else {
  760. int time = passageTime.get(gameLevel);
  761. if (time > (int) ((finishTimeLevel - startTimeLevel) / 1000)) {
  762. passageTime.set(gameLevel, (int) ((finishTimeLevel - startTimeLevel) / 1000));
  763. }
  764. }*/
  765.  
  766. if(passageTime[gameLevel] > 0){ // it means your once play this level
  767. if(passageTime[gameLevel] > (int) ((finishTimeLevel - startTimeLevel) / 1000)){
  768. passageTime[gameLevel] = (int) ((finishTimeLevel - startTimeLevel) / 1000);
  769. }
  770. }else{
  771. passageTime[gameLevel] = (int) ((finishTimeLevel - startTimeLevel) / 1000);
  772. }
  773.  
  774. sPref = getPreferences(MODE_PRIVATE);
  775. SharedPreferences.Editor ed = sPref.edit();
  776.  
  777. String str = "";
  778. for (int i = 0; i < 30; i++) {
  779. str += Integer.toString(passageTime[i]) + " ";
  780. }
  781.  
  782. ed.putString("passage_time_1", str);
  783. ed.commit();
  784. needInit = true;
  785.  
  786. }
  787. }
  788. }
  789.  
  790. public void myInit(){
  791. startTimeInit = System.currentTimeMillis();
  792. switch (gameMod){
  793. case LEVEL_MENU:
  794. swithcAdv(true);
  795. myButtons.clear();
  796. for(int ii = 0; ii < 15; ii++){
  797. int i = ii;
  798. if(mapLevel == 2){
  799. i = ii + 15;
  800. }
  801. myButton b = new myButton();
  802. b.maxTimePress = 1;
  803.  
  804. b.dx = (int) (-WHmin/4*0.05);
  805. b.dy = (int) (-WHmin/4*0.05);
  806.  
  807. if(passageTime[i] > 0){
  808.  
  809. if(passageTime[i] < gameLevels.get(i).times[0]){
  810. b.just = iconLevel[0][0];
  811. b.active = iconLevel[0][1];
  812. }else if(passageTime[i] < gameLevels.get(i).times[1]){
  813. b.just = iconLevel[1][0];
  814. b.active = iconLevel[1][1];
  815. }else{
  816. b.just = iconLevel[2][0];
  817. b.active = iconLevel[2][1];
  818. }
  819. }else{
  820. b.just = iconLevel[3][0];
  821. b.active = iconLevel[3][1];
  822. }
  823.  
  824. b.timePress = 0;
  825. int width = metrics.widthPixels;
  826. int height = heightView;
  827. int margin = (int) (width*0.2 - b.width())/2;
  828. b.x = margin + (int) (width * 0.2 * (ii % 5) );
  829. b.y = margin + (int) ((ii / 5) * 0.5 * (height-margin*2-b.height()) );
  830.  
  831. myButtons.add(b);
  832. }
  833. break;
  834. case MAP_MENU:
  835. swithcAdv(true);
  836. myButtons.clear();
  837. for(int i = 0; i < 3; i++){
  838. myButton b = new myButton();
  839. b.maxTimePress = 1;
  840. b.just = GameMap[i][0];
  841. b.active = GameMap[i][1];
  842.  
  843. switch (i){
  844. case 0:
  845. b.x = 0;
  846. b.y = 0;
  847. break;
  848. case 1:
  849. b.x = 0;
  850. b.y = GameMap[0][0].getHeight();
  851. break;
  852. case 2:
  853. b.x = metrics.widthPixels - GameMap[1][0].getWidth();
  854. b.y = GameMap[0][0].getHeight();
  855. break;
  856. }
  857.  
  858. b.timePress = 0;
  859. myButtons.add(b);
  860. }
  861.  
  862. break;
  863. case ACTIVE_GAME:
  864. Log.e("" , Integer.toString(gameLevel));
  865.  
  866. swithcAdv(false);
  867. bact.clear();
  868. virus.clear();
  869. myButtons.clear();
  870. for(int i = 0; i < gameLevels.get(gameLevel).one.size(); i++){
  871. Bacteria newbact = new Bacteria();
  872. newbact.x = gameLevels.get(gameLevel).one.get(i).x;
  873. newbact.y = gameLevels.get(gameLevel).one.get(i).y;
  874. newbact.team = gameLevels.get(gameLevel).one.get(i).team;
  875. newbact.paintTypeRadius = gameLevels.get(gameLevel).one.get(i).paintTypeRadius;
  876. newbact.radius = bactRadius[newbact.paintTypeRadius];
  877. newbact.maxCount = 16 + newbact.paintTypeRadius*16;
  878. bact.add(newbact);
  879. }
  880.  
  881.  
  882. startTimeLevel = System.currentTimeMillis();
  883. break;
  884. case BETWEEN_LEVEL:
  885. swithcAdv(true);
  886. myButtons.clear();
  887. myButton b = new myButton();
  888. b.just = buttonNextLevel;
  889. b.active = buttonNextLevel;
  890. b.nWidth = (int) (metrics.widthPixels*0.6);
  891. b.nHeight = (int) (metrics.widthPixels*0.6*0.2);
  892. if(playerWin) {
  893. b.text = "Next level";
  894. }else{
  895. b.text = "Try again";
  896. }
  897. b.render();
  898. b.click = false;b.show = false;
  899.  
  900. b.x = (int) (metrics.widthPixels*0.20);
  901. b.y = (int) (metrics.heightPixels - b.just.getHeight()*2.5);
  902.  
  903.  
  904. b.timePress = 0;
  905. myButtons.add(b);
  906. break;
  907. case PAUSE:
  908. myButtons.clear();
  909. for(int i = 0; i < 3; i++){
  910. b = new myButton();
  911. b.just = buttonNextLevel;
  912. b.active = buttonNextLevel;
  913. b.click = false;b.show = false;
  914. b.nWidth = (int) (metrics.widthPixels*0.6);
  915. b.nHeight = (int) (metrics.widthPixels*0.6*0.2);
  916.  
  917. switch (i){
  918. case 0:
  919. b.text = "Continues";
  920. break;
  921. case 1:
  922. b.text = "Restart";
  923. break;
  924. case 2:
  925. b.text = "Go to menu";
  926. }
  927.  
  928. b.render();
  929.  
  930. b.x = (int) (metrics.widthPixels*0.20);
  931. //b.y = (int) (metrics.heightPixels - b.just.getHeight()*2.5 - b.just.getHeight()*1.5*i);
  932. b.y = (int) (metrics.widthPixels*60/800 + b.just.getHeight()*1.3*i);
  933. b.timePress = 0;
  934. myButtons.add(b);
  935. }
  936. }
  937. if(dv != null){
  938. dv.drawThread.update = true;
  939. }
  940. }
  941.  
  942. public void swithcAdv(boolean show){
  943. if(adView != null) {
  944. if (!show) {
  945. runOnUiThread(new Runnable() {
  946. @Override
  947. public void run() {
  948. adView.pause();
  949. adView.setVisibility(View.GONE);
  950. adView.setEnabled(false);
  951. }
  952. });
  953. } else {
  954. runOnUiThread(new Runnable() {
  955. @Override
  956. public void run() {
  957. adView.setVisibility(View.VISIBLE);
  958. adView.setEnabled(true);
  959. }
  960. });
  961. }
  962. }
  963. }
  964.  
  965. public void pause(){
  966.  
  967. switch (gameMod){
  968. case ACTIVE_GAME:
  969. gameMod = GameMod.PAUSE;
  970. break;
  971. case LEVEL_MENU:
  972. gameMod = GameMod.MAP_MENU;
  973. break;
  974. }
  975. needInit = true;
  976. }
  977.  
  978. @Override
  979. public void onBackPressed() {
  980. if(gameMod == GameMod.MAP_MENU){
  981. AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
  982. alertDialog.setTitle("EXIT?");
  983.  
  984. alertDialog.setMessage("Do you really want to quit?");
  985.  
  986. alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  987. public void onClick(DialogInterface dialog,int which) {
  988. finish();
  989. }
  990. });
  991.  
  992. alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
  993. public void onClick(DialogInterface dialog, int which) {
  994. dialog.cancel();
  995. }
  996. });
  997.  
  998. alertDialog.show();
  999. }
  1000. pause(); }
  1001.  
  1002. public boolean onTouch(View v, MotionEvent event) {
  1003.  
  1004. float x = event.getX();
  1005. float y = event.getY();
  1006. boolean oneSelect , bactClick;
  1007.  
  1008. mouseX = x;
  1009. mouseY = y;
  1010.  
  1011. if( dv != null) dv.drawThread.update(); // maybe it's don`t need
  1012.  
  1013. switch (event.getAction()) {
  1014. case MotionEvent.ACTION_DOWN:
  1015. for(myButton b : myButtons){
  1016. if (!b.show) continue;
  1017. if (mouseX > b.x && mouseX < b.x + b.width() && mouseY > b.y && mouseY < b.y + b.height()) {
  1018. if(Color.alpha(b.just.getPixel((int)mouseX - b.x, (int)mouseY - b.y)) > 100 ) b.pressed = true;
  1019. }
  1020. }
  1021.  
  1022. canDrawLine = true;
  1023. bactClick = false;
  1024.  
  1025. for( int i = 0; i < bact.size(); i++){
  1026. Bacteria b = bact.get(i);
  1027. if( (b.x - x)*(b.x - x) + (b.y - y)*(b.y - y) < b.radius*b.radius ){
  1028. bactClick = true;
  1029. if(b.select || b.team != playerIndexBact) {
  1030. for (int j = 0; j < bact.size(); j++) {
  1031. if (bact.get(j).select && bact.get(j).team == playerIndexBact && i!=j) {
  1032. bact.get(j).fire(i, j);
  1033. b.tmpSelect = b.select = false;
  1034. bact.get(j).select = bact.get(j).tmpSelect = false;
  1035. }
  1036. }
  1037. }else if(b.team == playerIndexBact) { b.select = true;}
  1038. }
  1039. }
  1040.  
  1041. if(!bactClick){
  1042. for (Bacteria b : bact) {
  1043. b.select = false;
  1044. }
  1045. }
  1046. break;
  1047. case MotionEvent.ACTION_MOVE:
  1048. oneSelect = false;
  1049. for (Bacteria b : bact) {
  1050. if (b.select) {
  1051. oneSelect = true;
  1052. break;
  1053. }
  1054. }
  1055.  
  1056. for (Bacteria b : bact) {
  1057. b.tmpSelect = false;
  1058. if ((b.x - x) * (b.x - x) + (b.y - y) * (b.y - y) < b.radius * b.radius) {
  1059. if (b.team == playerIndexBact) {
  1060. b.select = true;
  1061. b.drawBorder = Math.max(b.drawBorder , 30);
  1062. } else if (oneSelect){
  1063. b.tmpSelect = true;
  1064. }
  1065. }
  1066. }
  1067.  
  1068. break;
  1069. case MotionEvent.ACTION_UP:
  1070. case MotionEvent.ACTION_CANCEL:
  1071. for(myButton b : myButtons){
  1072. if (mouseX > b.x && mouseX < b.x + b.width() && mouseY > b.y && mouseY < b.y + b.height()) {
  1073. if(b.pressed && b.show) {
  1074. if (Color.alpha(b.just.getPixel((int) mouseX - b.x, (int) mouseY - b.y)) > 100){
  1075. b.click = true;
  1076. }
  1077. }
  1078. }
  1079. b.pressed = false;
  1080. }
  1081.  
  1082. canDrawLine = false;
  1083. for( int i = 0; i < bact.size(); i++){
  1084. Bacteria b = bact.get(i);
  1085. if( (b.x - x)*(b.x - x) + (b.y - y)*(b.y - y) < b.radius*b.radius ){
  1086. if(b.team != playerIndexBact){
  1087. for( int j = 0; j < bact.size(); j++){
  1088. if(bact.get(j).select && bact.get(j).team == playerIndexBact){
  1089. bact.get(j).fire(i,j);
  1090. bact.get(j).select = bact.get(j).tmpSelect = false;
  1091. b.tmpSelect = b.select = false;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. }
  1097.  
  1098. break;
  1099. }
  1100. return true;
  1101. }
  1102.  
  1103. @Override
  1104. protected void onCreate(Bundle savedInstanceState) {
  1105.  
  1106. Log.e("","start Game");
  1107.  
  1108. requestWindowFeature(Window.FEATURE_NO_TITLE);
  1109. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  1110.  
  1111. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  1112. super.onCreate(savedInstanceState);
  1113.  
  1114. WHmin = Math.min(metrics.widthPixels, metrics.heightPixels);
  1115. overWidth = (float) (WHmin / 1920.0);
  1116.  
  1117. bactRadius[0] = (int) (WHmin / 11.32);
  1118. bactRadius[1] = WHmin / 7;
  1119. bactRadius[2] = WHmin / 5;
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.level)));
  1127. String line;
  1128.  
  1129. GameLevel tmp = new GameLevel();
  1130. boolean readTime = true;
  1131.  
  1132. try {
  1133. while ((line = reader.readLine()) != null) {
  1134. if (line.length() < 5) {
  1135. gameLevels.add(tmp);
  1136. tmp = new GameLevel();
  1137. readTime = true;
  1138. } else {
  1139. String[] number = line.split(" ");
  1140. if(readTime){
  1141. tmp.times[0] = Integer.parseInt(number[0]);
  1142. tmp.times[1] = Integer.parseInt(number[1]);
  1143. readTime = false;
  1144. }else {
  1145. LoadStruct tmpStruct = new LoadStruct();
  1146. tmpStruct.x = Integer.parseInt(number[0]) * metrics.widthPixels / 1920;
  1147. tmpStruct.y = Integer.parseInt(number[1]) * metrics.heightPixels / 1080;
  1148. tmpStruct.team = Integer.parseInt(number[2]);
  1149. tmpStruct.paintTypeRadius = Integer.parseInt(number[3]);
  1150.  
  1151. tmp.one.add(tmpStruct);
  1152. }
  1153. }
  1154. }
  1155. } catch (IOException e) {}
  1156.  
  1157. gameLevels.add(tmp);
  1158.  
  1159. String ddd = "";
  1160.  
  1161. for(int i = 0; i < 20; i++){
  1162. int a = 10000 + rn.nextInt(2000) - i*400;
  1163. int r = 2+rn.nextInt(5);
  1164. ddd+=Integer.toString(a) + ",";
  1165. for(int j = 0; j < r; j++) {
  1166. int b = 1+rn.nextInt(3);
  1167. int c = 25 + rn.nextInt(25) + i;
  1168. int d = (b-1)*3+rn.nextInt(3);
  1169. ddd += Integer.toString(b)+"/" + Integer.toString(c) + "/" + Integer.toString(d);
  1170. if(j != r-1){
  1171. ddd += ",";
  1172. }
  1173. }
  1174.  
  1175. if(i != 19){
  1176. ddd += "_";
  1177. }
  1178. }
  1179.  
  1180. Log.e("" , ddd);
  1181.  
  1182. String bbb = "10000,2/30/1,3/30/2_8000,2/30/1,3/30/2_10000,2/30/1,3/30/2_10000,4/40/1,1/30/2";
  1183. bbb+= "_10000,5/60/1,3/30/2_8000,2/30/1,3/30/2_10000,2/30/1,3/30/2_10000,4/40/1,1/30/2";
  1184.  
  1185. bbb = ddd;
  1186.  
  1187. String[] BBB = bbb.split("_");
  1188.  
  1189. for(int i = 0; i < BBB.length; i++){
  1190. //String[] DDD = BBB[i].split(",");
  1191. BactАttack bactАttack = new BactАttack();
  1192. //bactАttack.time = Integer.parseInt(DDD[0]);
  1193. bactАttack.all = BBB[i];
  1194. bactАttacks.add(bactАttack);
  1195. }
  1196.  
  1197.  
  1198.  
  1199.  
  1200. sPref = getPreferences(MODE_PRIVATE);
  1201. String passage_time_1 = sPref.getString("passage_time_1", "");
  1202.  
  1203. for(int i = 0; i < 30; i++) {
  1204. passageTime[i] = -1;
  1205. }
  1206.  
  1207. if(passage_time_1.length() > 0) {
  1208. String[] number = passage_time_1.split(" ");
  1209. for (int i = 0; i < number.length; i++) {
  1210. passageTime[i] = Integer.parseInt(number[i]);
  1211. }
  1212.  
  1213. }
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222. dv = new DrawView(this);
  1223. dv.setOnTouchListener(this);
  1224.  
  1225. setContentView(R.layout.activity_fullscreen);
  1226.  
  1227. LinearLayout layout = (LinearLayout) this.findViewById(R.id.mainLayout);
  1228. layout.addView(dv);
  1229. adView = (AdView)this.findViewById(R.id.adView);
  1230. AdRequest adRequest = new AdRequest.Builder().addTestDevice("1FC018C81DA00D37BA952F5798664BED").build();
  1231.  
  1232. adView.loadAd(adRequest); // ITS LOAD MY CPU
  1233. }
  1234.  
  1235. class DrawView extends SurfaceView implements SurfaceHolder.Callback {
  1236. Paint p;
  1237. private DrawThread drawThread;
  1238. String loadText = "Load";
  1239. long timeout = System.currentTimeMillis();
  1240. long curTime;
  1241.  
  1242.  
  1243. public DrawView(Context context) {
  1244.  
  1245. super(context);
  1246. getHolder().addCallback(this);
  1247. getHolder().setFormat(PixelFormat.RGBA_8888);
  1248.  
  1249. Log.e("","its must be only one");
  1250.  
  1251.  
  1252.  
  1253. class WorkingClass implements Runnable{
  1254. @Override
  1255. public void run() {
  1256.  
  1257. fingerImg = scaleToWidth(bitmapLoadRaw(R.raw.finger),WHmin/5);
  1258.  
  1259. iconLevel[0][0] = scaleToWidth(bitmapLoadRaw(R.raw.icongreen) , WHmin/4);
  1260. iconLevel[1][0] = scaleToWidth(bitmapLoadRaw(R.raw.icon) , WHmin/4);
  1261. iconLevel[2][0] = scaleToWidth(bitmapLoadRaw(R.raw.iconred) , WHmin/4);
  1262. iconLevel[3][0] = scaleToWidth(bitmapLoadRaw(R.raw.icondis) , WHmin/4);
  1263.  
  1264. iconLevel[0][1] = scaleToWidth(bitmapLoadRaw(R.raw.icongreen) , (int) (WHmin/4*1.1));
  1265. iconLevel[1][1] = scaleToWidth(bitmapLoadRaw(R.raw.icon) , (int) (WHmin/4*1.1));
  1266. iconLevel[2][1] = scaleToWidth(bitmapLoadRaw(R.raw.iconred) , (int) (WHmin/4*1.1));
  1267. iconLevel[3][1] = scaleToWidth(bitmapLoadRaw(R.raw.icondis) , (int) (WHmin/4*1.1));
  1268. fonImgB = Bitmap.createScaledBitmap(bitmapLoadRaw(R.raw.fon3), metrics.widthPixels ,metrics.heightPixels,true);
  1269. buttonNextLevel = bitmapLoadRaw(R.raw.buttonnext);
  1270.  
  1271.  
  1272.  
  1273. borderImg = scaleToWidth(bitmapLoadRaw(R.raw.between),metrics.widthPixels);
  1274.  
  1275. GameMap[1][0] = scaleToWidth(bitmapLoadRaw(R.raw.easy) , (int) (metrics.widthPixels*0.94*0.5));
  1276. GameMap[1][1] = scaleToWidth(bitmapLoadRaw(R.raw.easywithout) , (int) (metrics.widthPixels*0.94*0.5));
  1277.  
  1278. GameMap[2][0] = scaleToWidth(bitmapLoadRaw(R.raw.hard) , (int) (metrics.widthPixels*0.94*0.5));
  1279. GameMap[2][1] = scaleToWidth(bitmapLoadRaw(R.raw.hardwithout) , (int) (metrics.widthPixels*0.94*0.5));
  1280.  
  1281. GameMap[0][0] = scaleToWidth(bitmapLoadRaw(R.raw.survival) , metrics.widthPixels);
  1282. GameMap[0][1] = scaleToWidth(bitmapLoadRaw(R.raw.survival) , metrics.widthPixels);
  1283.  
  1284. Paint pp = new Paint();
  1285. pp.setFilterBitmap(true);
  1286. pp.setFlags(Paint.ANTI_ALIAS_FLAG);
  1287.  
  1288. Bitmap bm = bitmapLoadRaw(R.raw.bact);
  1289.  
  1290. for( int j = 2; j != -1; j--){
  1291. Bitmap bmS = Bitmap.createScaledBitmap(bm, bactRadius[j]*2, bactRadius[j]*2, true);
  1292. for( int i = 0 ; i < 9; i++){
  1293. if(i > 5 && i != 8)
  1294. continue;
  1295.  
  1296. int r,g,b;
  1297. r = bactColor[i][0];
  1298. g = bactColor[i][1];
  1299. b = bactColor[i][2];
  1300.  
  1301. if(j == 2) {
  1302. bactImg[i][0][j] = Bitmap.createBitmap(bactRadius[j] * 2, bactRadius[j] * 2, Bitmap.Config.ARGB_8888);
  1303.  
  1304.  
  1305. pp.setStyle(Paint.Style.FILL);
  1306. Canvas canvasB = new Canvas(bactImg[i][0][j]);
  1307. canvasB.drawBitmap(bmS, 0, 0, pp);
  1308. pp.setColor(Color.argb(120, r, g, b));
  1309. canvasB.drawCircle(bactRadius[j], bactRadius[j], (float) (bactRadius[j] - bactRadius[j] * 0.04), pp);
  1310. }else{
  1311. bactImg[i][0][j] = Bitmap.createScaledBitmap(bactImg[i][0][2], bactRadius[j]*2, bactRadius[j]*2, true);
  1312. }
  1313.  
  1314. Canvas canvasB;
  1315. bactImg[i][1][j] = Bitmap.createBitmap((int)(bactRadius[j]*2.4),(int)(bactRadius[j]*2.4),
  1316. Bitmap.Config.ARGB_8888);
  1317. canvasB = new Canvas((bactImg[i][1][j]));
  1318. pp.setColor(Color.BLACK);
  1319. canvasB.drawBitmap(bmS , (int)(bactRadius[j]*0.2) , (int)(bactRadius[j]*0.2) , pp);
  1320. pp.setColor(Color.argb(200 , r,g,b));
  1321. canvasB.drawCircle((int)(bactRadius[j]*1.2) , (int)(bactRadius[j]*1.2) , bactRadius[j]-1 , pp);
  1322. pp.setStyle(Paint.Style.STROKE);
  1323. pp.setStrokeWidth((float)(bactRadius[j]*0.1));
  1324. pp.setColor(Color.WHITE);
  1325.  
  1326. canvasB.drawCircle((int)(bactRadius[j]*1.2) , (int)(bactRadius[j]*1.2) ,
  1327. bactRadius[j]+(float)(bactRadius[j]*0.1) ,pp);
  1328. }
  1329. }
  1330.  
  1331. pp.setStyle(Paint.Style.FILL);
  1332. pp.setAlpha(255);
  1333.  
  1334. for(int i = 0; i < 9; i++){
  1335. int r,g,b;
  1336. r = bactColor[i][0];
  1337. g = bactColor[i][1];
  1338. b = bactColor[i][2];
  1339.  
  1340. float radius = (float) (WHmin/80.0);
  1341.  
  1342. for(int jj = 0; jj < 16; jj++){
  1343. virusImg[i][jj] = Bitmap.createBitmap((int)(radius*4) , (int) (radius*4), Bitmap.Config.ARGB_8888);
  1344. Canvas canvasB = new Canvas(virusImg[i][jj]);
  1345.  
  1346. float j = (float) (8*( 1 - Math.cos(jj*Math.PI/15)));
  1347. pp.setColor(Color.argb(255 , r,g,b));
  1348. canvasB.drawCircle(radius*2, radius*2 , (float) (radius*0.66), pp);
  1349.  
  1350. pp.setColor(Color.argb((int) (j*15), r, g, b));
  1351. canvasB.drawCircle(radius*2, radius*2, radius*2/16*j, pp);
  1352.  
  1353.  
  1354. }
  1355. }
  1356.  
  1357. p.setTextSize(WHmin/25);
  1358. p.setColor(Color.BLACK);
  1359. p.setTextAlign(Paint.Align.CENTER);
  1360. gameMod = GameMod.MAP_MENU;
  1361. myInit();
  1362. }
  1363. }
  1364.  
  1365.  
  1366.  
  1367.  
  1368. fontFace = Typeface.createFromAsset(getAssets(), "fonts/komix.ttf");
  1369. p = new Paint();
  1370. p.setColor(Color.WHITE);
  1371. p.setTypeface(fontFace);
  1372. p.setFlags(Paint.ANTI_ALIAS_FLAG);
  1373. p.setTextSize(WHmin/25*4);
  1374. p.setFilterBitmap(true);
  1375.  
  1376. WorkingClass workingClass = new WorkingClass();
  1377. Thread thread = new Thread(workingClass);
  1378. thread.start();
  1379.  
  1380.  
  1381.  
  1382. //myInit();
  1383.  
  1384. }
  1385.  
  1386.  
  1387. @Override
  1388. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
  1389.  
  1390. @Override
  1391. public void surfaceCreated(SurfaceHolder holder) {
  1392. drawThread = new DrawThread(getHolder());
  1393. drawThread.setRunning(true);
  1394. drawThread.start();
  1395. }
  1396.  
  1397. @Override
  1398. public void surfaceDestroyed(SurfaceHolder holder) {
  1399. boolean retry = true;
  1400. drawThread.setRunning(false);
  1401. while (retry) {
  1402. try {
  1403. drawThread.join();
  1404. retry = false;
  1405. } catch (InterruptedException e) {
  1406. }
  1407. }
  1408. }
  1409.  
  1410. class DrawThread extends Thread {
  1411.  
  1412. public boolean running = false;
  1413. public boolean update = true;
  1414. private SurfaceHolder surfaceHolder;
  1415.  
  1416. public DrawThread(SurfaceHolder surfaceHolder) {
  1417. this.surfaceHolder = surfaceHolder;
  1418. }
  1419.  
  1420. public void setRunning(boolean running) {
  1421. this.running = running;
  1422. }
  1423. public void update() {this.update = true;}
  1424.  
  1425. @Override
  1426. public void run() {
  1427. Canvas canvas;
  1428. while (running) {
  1429. //Log.e("update" , Integer.toString((int) update));
  1430. curTime = System.currentTimeMillis() - timeout;
  1431. timeout = System.currentTimeMillis();
  1432. timeFromInit = (int) (System.currentTimeMillis() - startTimeInit);
  1433.  
  1434. heightView = dv.getHeight();
  1435.  
  1436.  
  1437. if (update) {
  1438. canvas = null;
  1439. try {
  1440. canvas = surfaceHolder.lockCanvas(null);
  1441. /*if(canvas.isHardwareAccelerated()){
  1442. bact.get(1).x += 10; // KIIIIIIIIIIIIIILLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
  1443. }*/
  1444. if (canvas == null) continue;
  1445.  
  1446. float timeLag = curTime/16;
  1447. timeLag = Math.min(3,(float)Math.max(0.3, timeLag));
  1448.  
  1449.  
  1450.  
  1451. switch (gameMod){
  1452. case MAP_MENU:
  1453.  
  1454. update = false;
  1455. DrawMapMenu(canvas,p);
  1456. break;
  1457.  
  1458.  
  1459. case LEVEL_MENU:
  1460. update = false;
  1461. canvas.drawBitmap(fonImgB, 0, 0, p);
  1462. for (int ii = 0; ii < myButtons.size(); ii++) {
  1463. myButton b = myButtons.get(ii);
  1464. int i = ii + mapLevel*15 - 15;
  1465. if(b.click){
  1466. b.click = false;
  1467. if(i == 0 || i == 15 || passageTime[i-1] > 0 ) {
  1468. gameMod = GameMod.ACTIVE_GAME;
  1469. gameLevel = i;
  1470. needInit = true;
  1471. }else{
  1472. runOnUiThread(new Runnable() {
  1473. @Override
  1474. public void run() {
  1475. Toast toast = Toast.makeText(getApplicationContext(),
  1476. "This level is closed.", Toast.LENGTH_SHORT);
  1477. toast.show();
  1478. }
  1479. });
  1480.  
  1481.  
  1482. }
  1483. }
  1484. b.draw(canvas,p);
  1485. canvas.drawText(Integer.toString(i + 1), b.x+b.width()/2,
  1486. b.y+b.height()/2 + WHmin/75, p);
  1487. }
  1488. break;
  1489. case BETWEEN_LEVEL:
  1490. update = false;
  1491. DrawBetweenLevel(canvas, p);
  1492. break;
  1493. case PAUSE:
  1494. update = false;
  1495. DrawPause(canvas, p);
  1496. break;
  1497. case ACTIVE_GAME:
  1498. update = true;
  1499. DrawActiveGame(canvas,p,timeLag,getWidth(),getHeight());
  1500. break;
  1501. case LOAD_GAME:
  1502. update = true;
  1503. canvas.drawText(loadText,WHmin*0.04f,heightView/2,p);
  1504. loadText += ".";
  1505. break;
  1506. }
  1507.  
  1508. for(myButton b : myButtons){
  1509. int last = b.timePress;
  1510. if(b.pressed) {
  1511. b.timePress = (int) Math.min(b.timePress+curTime , b.maxTimePress);
  1512. }else{
  1513. b.timePress = (int) Math.max(b.timePress-curTime , 0);
  1514. }
  1515. if(b.timePress != last) update = true;
  1516. }
  1517.  
  1518. canvas.drawText(Long.toString(curTime) , 100 , 100 , p);
  1519.  
  1520.  
  1521. } finally {
  1522. if (canvas != null) {
  1523. surfaceHolder.unlockCanvasAndPost(canvas);
  1524. }
  1525. }
  1526. }else{
  1527. try {
  1528. Thread.sleep(16);
  1529. } catch (InterruptedException e) {
  1530. e.printStackTrace();
  1531. }
  1532. }
  1533. if(needInit){
  1534. needInit = false;
  1535. myInit();
  1536. }
  1537. }
  1538. }
  1539. }
  1540. }
  1541. }
Advertisement
Add Comment
Please, Sign In to add comment