Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package com.ru.tgra.shapes;
  2.  
  3.  
  4.  
  5. /**
  6. * Created by Svanur on 12.10.2017.
  7. */
  8. public class Enemy {
  9. private float enemyLocX;
  10. private float enemyLocZ;
  11. private float destinationX;
  12. private float destinationZ;
  13. private boolean moving;
  14. private int direction;
  15.  
  16. private final float SPEED = 3.f;
  17. private final int MIN = 1;
  18. private final int MAX = 4;
  19.  
  20. Vector3D velocity;
  21. int [][] maze;
  22.  
  23. Shader shader;
  24.  
  25. public Enemy(float enemyLocX, float enemyLocZ, int[][] maze)
  26. {
  27. this.enemyLocX = enemyLocX;
  28. this.enemyLocZ = enemyLocZ;
  29. this.velocity = new Vector3D(0,0,0);
  30. shader = new Shader();
  31. this.maze = maze;
  32. this.moving = false;
  33. this.direction = 0;
  34. }
  35.  
  36. public void drawEnemy()
  37. {
  38. if(!moving)
  39. {
  40. direction = whereToMove();
  41. }
  42. move(direction);
  43. //System.out.println(enemyLocX + " " + enemyLocZ);
  44.  
  45. shader.setMaterialDiffuse(1, 0, 0, 1);
  46. ModelMatrix.main.loadIdentityMatrix();
  47. ModelMatrix.main.addTranslation(enemyLocX, 1, enemyLocZ);
  48. shader.setModelMatrix(ModelMatrix.main.getMatrix());
  49. BoxGraphic.drawSolidCube();
  50. }
  51. private boolean hasLeft(int x)
  52. {
  53. return (x & 2) != 0;
  54. }
  55.  
  56. private boolean hasRight(int x)
  57. {
  58. return (x & 1) != 0;
  59. }
  60.  
  61. private boolean hasUp(int x)
  62. {
  63. return (x & 8) != 0;
  64. }
  65.  
  66. private boolean hasDown(int x)
  67. {
  68. return (x & 4) != 0;
  69. }
  70. private int random() {return (int )(Math.random() * MAX + MIN);}
  71.  
  72. private int whereToMove()
  73. {
  74. Boolean moveFound = false;
  75. int random = random();
  76.  
  77. int x = Math.round(enemyLocX - 2.5f) / 5;
  78. int z = Math.round(enemyLocZ + 2.5f) / 5;
  79.  
  80. if(z < 0)
  81. {
  82. z = -z;
  83. }
  84. if(x < 0)
  85. {
  86. x = -x;
  87. }
  88.  
  89. System.out.println(x + " " + z);
  90. System.out.println(enemyLocX + " " + enemyLocZ);
  91.  
  92. int cell = maze[z][x];
  93.  
  94. System.out.println(cell);
  95.  
  96. while(!moveFound)
  97. {
  98. if(random == 1 && !hasUp(cell))
  99. {
  100. moving = true;
  101. destinationX = enemyLocX;
  102. destinationZ = enemyLocZ - 5f;
  103. break;
  104. }
  105. if(random == 2 && !hasRight(cell))
  106. {
  107. moving = true;
  108. destinationX = enemyLocX + 5f;
  109. destinationZ = enemyLocZ;
  110. break;
  111. }
  112. if(random == 3 && !hasDown(cell))
  113. {
  114. moving = true;
  115. destinationX = enemyLocX;
  116. destinationZ = enemyLocZ + 5f;
  117. break;
  118. }
  119. if(random == 4 && !hasLeft(cell))
  120. {
  121. moving = true;
  122. destinationX = enemyLocX - 5f;
  123. destinationZ = enemyLocZ;
  124. break;
  125. }
  126. random = random();
  127. }
  128. return random;
  129. }
  130. private void move(int dir)
  131. {
  132. if((Math.round(enemyLocX * 10.0) / 10.0) == destinationX && (Math.round(enemyLocZ * 10.0) / 10.0) == destinationZ)
  133. {
  134. moving = false;
  135. direction = 0;
  136. enemyLocX = destinationX;
  137. enemyLocZ = destinationZ;
  138. return;
  139. }
  140. if(dir == 1)
  141. {
  142. enemyLocZ -= 0.01f;
  143. }
  144. if(dir == 2)
  145. {
  146. enemyLocX += 0.01f;
  147. }
  148. if(dir == 3)
  149. {
  150. enemyLocZ += 0.01f;
  151. }
  152. if(dir == 4)
  153. {
  154. enemyLocX -= 0.01f;
  155. }
  156. }
  157.  
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement