Advertisement
Guest User

Classes

a guest
Dec 5th, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. class Cell {
  2. int x;
  3. int y;
  4.  
  5. boolean isPath = false;
  6.  
  7. Tower occupant = null;
  8.  
  9. void buildOn(Tower t) {
  10. if (buildable()) {
  11. occupant = t;
  12. AllTowers.add(occupant);
  13. }
  14. }
  15.  
  16. boolean buildable() {
  17. if (occupant == null && !isPath)return true;
  18. else return false;
  19. }
  20.  
  21. void outlineMe() {
  22. noFill();
  23. if (buildable())stroke(#00FF00);
  24. else stroke(#FF0000);
  25. rect(x * cellSize, y * cellSize, cellSize, cellSize);
  26. }
  27.  
  28. Cell(int _x, int _y) {
  29. x = _x;
  30. y = _y;
  31. }
  32. }
  33.  
  34. class Tower {
  35. int cellX;
  36. int cellY;
  37.  
  38.  
  39. int spriteIndex;
  40. int tint;
  41.  
  42. void drawMe() {
  43. image(TowerSprites[spriteIndex], cellX * cellSize, cellY * cellSize, cellSize, cellSize);
  44. }
  45.  
  46. Tower(int x, int y, int sprite) {
  47. cellX = x;
  48. cellY = y;
  49. spriteIndex = sprite;
  50. }
  51. }
  52.  
  53. class Vector {
  54. int x;
  55. int y;
  56.  
  57. Vector(int _x, int _y) {
  58. x = _x;
  59. y = _y;
  60. }
  61. }
  62.  
  63. class CreepSprite {
  64. PImage[] up = new PImage[2];
  65. PImage[] down = new PImage[2];
  66. PImage[] left = new PImage[2];
  67. PImage[] right = new PImage[2];
  68.  
  69. PImage getImage(Direction dir, int anim){
  70. if(dir == Direction.up) return up[anim];
  71. else if(dir == Direction.down) return down[anim];
  72. else if(dir == Direction.left) return left[anim];
  73. else return right[anim];
  74. }
  75.  
  76. CreepSprite(String name) {
  77. String path = "creeps/" + name + "/" + name + "_";
  78. up[0] = loadImage(path + "u0.png");
  79. up[1] = loadImage(path + "u1.png");
  80. down[0] = loadImage(path + "d0.png");
  81. down[1] = loadImage(path + "d1.png");
  82. left[0] = loadImage(path + "l0.png");
  83. left[1] = loadImage(path + "l1.png");
  84. right[0] = loadImage(path + "r0.png");
  85. right[1] = loadImage(path + "r1.png");
  86. }
  87. }
  88.  
  89. class Creep{
  90. int posX;
  91. int posY;
  92. int onLane = 0;
  93. Direction dir;
  94.  
  95. int speed = 1;
  96. int health = 5;
  97.  
  98. int spriteIndex;
  99. int anim = 0;
  100.  
  101. int animTimer = 0;
  102. int animDelay = 5;
  103.  
  104. void move(){
  105. PathStatus status = Level.checkPos(new Vector(posX, posY), onLane);
  106. if(status == PathStatus.finished){
  107. leak(this);//leak!!
  108. return;
  109. } else if (status == PathStatus.next){
  110. onLane ++;
  111. dir = Level.getDir(onLane);
  112. }
  113.  
  114. if(dir == Direction.up) posY -= speed;
  115. else if(dir == Direction.down) posY += speed;
  116. else if(dir == Direction.left) posX -= speed;
  117. else posX += speed;
  118.  
  119. image(CreepSprites[spriteIndex].getImage(dir, anim), posX, posY, cellSize, cellSize);
  120.  
  121. animTimer++;
  122. if(animTimer > animDelay){
  123. animTimer = 0;
  124. if(anim == 0) anim = 1;
  125. else anim = 0;
  126. }
  127.  
  128. }
  129.  
  130. Creep(int sprite){
  131. spriteIndex = sprite;
  132. Vector p = Level.getSpawn();
  133. posX = p.x;
  134. posY = p.y;
  135. dir = Level.getDir(0);
  136. }
  137. }
  138.  
  139. class Path {
  140. Vector[] Pathway;
  141. Lane[] allLanes;
  142. Vector spawnPos;
  143.  
  144. Vector getSpawn(){
  145. return spawnPos;
  146. }
  147.  
  148. Direction getDir(int i){
  149. if(i < allLanes.length) return allLanes[i].dir;
  150. else return allLanes[allLanes.length - 1].dir;
  151. }
  152.  
  153. PathStatus checkPos(Vector v, int lane){
  154. if(lane > allLanes.length - 1) return PathStatus.finished; //done with path
  155.  
  156. boolean check = allLanes[lane].checkPos(v);
  157. if(check) return PathStatus.next; // turn
  158. else return PathStatus.stay; // keep going
  159. }
  160.  
  161. class Lane{
  162. int endPos;
  163. Direction dir;
  164.  
  165. boolean checkPos(Vector v){
  166. if(dir == Direction.up){
  167. if(endPos > v.y) return true;
  168. else return false;
  169. } else if(dir == Direction.down){
  170. if(endPos < v.y) return true;
  171. else return false;
  172. } else if(dir == Direction.left){
  173. if(endPos > v.x) return true;
  174. else return false;
  175. } else {
  176. if(endPos < v.x) return true;
  177. else return false;
  178. }
  179. }
  180.  
  181. Lane(Direction _dir, Vector cell){
  182. dir = _dir;
  183. if(_dir == Direction.up || _dir == Direction.down) endPos = round(cellSize * cell.y);
  184. else endPos = round(cellSize * cell.x);
  185. }
  186. }
  187.  
  188. Path(Vector[] v) {
  189. Pathway = v;
  190. Direction[] directions = new Direction[v.length - 1];
  191. spawnPos = new Vector(round(v[0].x * cellSize), round(v[0].y * cellSize));
  192.  
  193. for (int i = 0; i < v.length - 1; i++) {
  194. Grid[v[i].x][v[i].y].isPath = true;
  195.  
  196. int difference = v[i].x - v[i + 1].x;
  197. if (difference != 0) {//using x
  198. for (int z = 0; z < abs(difference); z++) {
  199. if (difference < 0){
  200. Grid[v[i].x + z][v[i].y].isPath = true;//path goes right
  201. directions[i] = Direction.right;
  202. }
  203. else{
  204. Grid[v[i].x - z][v[i].y].isPath = true;//path goes left
  205. directions[i] = Direction.left;
  206. }
  207. }
  208. }
  209. else { //using y
  210. difference = v[i].y - v[i + 1].y;
  211.  
  212. for (int z = 0; z < abs(difference); z++) {
  213. if (difference < 0){
  214. Grid[v[i].x][v[i].y + z].isPath = true; //path goes down
  215. directions[i] = Direction.down;
  216. }
  217. else {
  218. Grid[v[i].x][v[i].y - z].isPath = true;// path goes up
  219. directions[i] = Direction.up;
  220. }
  221. }
  222. }
  223. }
  224.  
  225. if (v.length > 0)Grid[v[v.length - 1].x][v[v.length - 1].y].isPath = true;
  226.  
  227. allLanes = new Lane[directions.length];
  228. for(int i = 0; i < allLanes.length; i++) allLanes[i] = new Lane(directions[i], v[i + 1]);
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement