Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.24 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4.  
  5. import processing.core.PApplet;
  6. import processing.event.MouseEvent;
  7.  
  8. public class CagedAlpha extends PApplet {
  9.  
  10. public static void main(String[] args) {
  11. PApplet.main("CagedAlpha");
  12. }
  13.  
  14. List<float[]> drops = new ArrayList<float[]>();
  15. List<float[]> entities = new ArrayList<float[]>();
  16. Random rand = new Random();
  17. Libraries l = new Libraries();
  18. int w = 1920 / 2;
  19. int h = 1080 / 2;
  20. int types = 10;
  21. byte[][] world = new byte[1000][100];
  22. short[][] textures = new short[types][4];
  23. int[] inventory = new int[types];
  24. float px = world.length * 25;
  25. float py = 0;
  26. float xv = 0;
  27. float yv = 0;
  28. float cameraX;
  29. float cameraY;
  30. boolean up;
  31. boolean left;
  32. boolean right;
  33. boolean space;
  34. boolean first = true;
  35. boolean touchingBlock = false;
  36. byte blockCheck = 0;
  37. byte tc = 0;
  38. byte selection = 1;
  39. boolean mining = false;
  40. int progress = 0;
  41. float health = 100;
  42. boolean checkOut = false;
  43. int mb = 0;
  44. float magnetism = 150;
  45. boolean dead = false;
  46. int burning = 0;
  47. float deathX = -1;
  48. int reload = 0;
  49. boolean won = false;
  50. boolean menus = true;
  51. int difficulty = 2;
  52.  
  53. public void settings() {
  54. size(w, h);
  55. noSmooth();
  56. }
  57.  
  58. public void setup() {
  59. // Texturing
  60. addTexture(255, 255, 255, 0);
  61. addTexture(10, 255, 50, 10);
  62. addTexture(80, 35, 0, 10);
  63. addTexture(100, 100, 100, 30);
  64. addTexture(20, 20, 20, 1000000);
  65. addTexture(50, 0, 100, 20);
  66. addTexture(10000, 0, 0, 100);
  67. addTexture(0, 170, 170, 50);
  68. addTexture(200, 200, 200, 100);
  69. addTexture(0, 1000, 0, 10);
  70. noStroke();
  71. // World Generation
  72. for (int x = 0; x < world.length; x++) {
  73. for (int y = 0; y < world[0].length; y++) {
  74. double height = l.noise((double) x / 10, 0, 1) * 20;
  75. if (height < y - 75 && y <= 83) {
  76. world[x][y] = 1;
  77. if (height > y - 76 && Math.random() < 0.05) {
  78. world[x][y - 1] = 5;
  79. }
  80. }
  81. if (height < y - 76) {
  82. world[x][y] = 2;
  83. }
  84. if (height < y - 80) {
  85. if (Math.random() < 0.98 || height > y - 85) {
  86. world[x][y] = 3;
  87. } else if (Math.random() < 0.5 || height > y - 85) {
  88. world[x][y] = 8;
  89. } else {
  90. world[x][y] = 7;
  91. }
  92. }
  93. if (world[x][y] == 0 && y > 83) {
  94. world[x][y] = 6;
  95. }
  96. /*
  97. * if (rand.nextBoolean() && y > 50) { world[x][y] = 1; }
  98. */
  99. if ((x == 0 || x == world.length - 1) || (y == 0 || y == world[0].length - 1)) {
  100. world[x][y] = 4;
  101. }
  102. }
  103. }
  104. }
  105.  
  106. public void draw() {
  107. // Main Menu
  108. if (menus) {
  109. first = true;
  110. fill(255);
  111. rectMode(CORNER);
  112. stroke(255);
  113. background(0);
  114. textSize(64);
  115. noFill();
  116. text("Difficulty: " + difficulty, 300, 200);
  117. textSize(16);
  118. text("Right & Left: Adjust difficulty (Menu Only)", 100, 250);
  119. text("WAD: Move", 100, 280);
  120. text("Left Mouse: Break block", 100, 310);
  121. text("Right Mouse: Place block", 100, 340);
  122. text("Space: Shoot (Start in menu)", 100, 370);
  123. text("Mouse Wheel: Select Block", 100, 400);
  124. textSize(32);
  125. rect(40, h - 92, w - 80, 64);
  126. if (difficulty == 0) {
  127. text("No enemies at all.", 50, h - 50);
  128. }
  129. if (difficulty == 1) {
  130. text("Pretty easy.", 50, h - 50);
  131. }
  132. if (difficulty == 2) {
  133. text("How the game is meant to be played.", 50, h - 50);
  134. }
  135. if (difficulty == 3) {
  136. text("Pretty hard.", 50, h - 50);
  137. }
  138. if (difficulty == 4) {
  139. text("Very, very hard.", 50, h - 50);
  140. }
  141. if (difficulty > 4 && difficulty < 11) {
  142. text("You won't win.", 50, h - 50);
  143. }
  144. if (difficulty > 10 && difficulty < 16) {
  145. text("What are you doing?", 50, h - 50);
  146. }
  147. if (difficulty > 15 && difficulty < 26) {
  148. text("Stop it, I can't handle this!", 50, h - 50);
  149. }
  150. if (difficulty > 25 && difficulty < 32) {
  151. text("What Have You done!?", 50, h - 50);
  152. }
  153. if (difficulty == 32) {
  154. text("I don't only feel bad for you, I feel bad for my CPU.", 50, h - 50);
  155. }
  156. textSize(100);
  157. fill(37, 75, 37);
  158. text("Caged", 325, 100);
  159. if (space) {
  160. menus = false;
  161. noStroke();
  162. }
  163.  
  164. } else {
  165. if (first) {
  166. randomTicks(10000);
  167. }
  168. randomTicks(0.1f);
  169. // Placing & Breaking
  170. int mx = (int) (mouseX + cameraX - w / 2) / 50;
  171. int my = (int) (mouseY + cameraY - h / 2) / 50;
  172. if (!dead) {
  173. if (mousePressed && mouseButton == RIGHT && inventory[selection] > 0
  174. && !check((mouseX + cameraX - w / 2), (mouseY + cameraY - h / 2))) {
  175. if (selection > 0) {
  176. world[mx][my] = selection;
  177. inventory[selection] -= 1;
  178. doTick(mx, my);
  179. doTick(mx - 1, my);
  180. doTick(mx + 1, my);
  181. doTick(mx, my - 1);
  182. doTick(mx, my + 1);
  183. }
  184. }
  185. if (mousePressed && mouseButton == LEFT
  186. && check((mouseX + cameraX - w / 2), (mouseY + cameraY - h / 2))) {
  187. if (mining) {
  188. check((mouseX + cameraX - w / 2), (mouseY + cameraY - h / 2));
  189. if (!(blockCheck == mb)) {
  190. progress = 2 * (int) ((float) textures[blockCheck][3] / Math.pow(inventory[0] + 5, 0.3));
  191. } else {
  192. progress -= 1;
  193. }
  194. if (progress <= 0) {
  195. breakBlock(mx, my);
  196. mining = false;
  197. }
  198. } else {
  199. mb = blockCheck;
  200. mining = true;
  201. progress = 2 * (int) ((float) textures[blockCheck][3] / Math.pow(inventory[0] + 5, 0.3));
  202. }
  203. } else {
  204. mining = false;
  205. }
  206. // Projectiles
  207. reload -= 1;
  208. if (space && reload <= 0 && inventory[0] > 0) {
  209. inventory[0] -= 1;
  210. reload = (int) (120 / Math.pow(inventory[0], 0.3));
  211. float tx = mouseX - w / 2;
  212. float ty = mouseY - h / 2;
  213. float dist = dist(0, 0, tx, ty) / 20;
  214. tx /= dist;
  215. ty /= dist;
  216. spawnEntity(px, py, tx, ty, 0);
  217. // health = 0;
  218. // randomTicks(1000);
  219. /*
  220. * magnetism = 9999999; for (int i = 0; i < 10; i++) { int
  221. * tx = mx + (rand.nextInt(10) - 5); int ty = my +
  222. * (rand.nextInt(10) - 5); check(tx * 50, ty * 50); if
  223. * (health > textures[blockCheck][3] / 10) { if
  224. * (breakBlock(tx, ty)) { // health -=
  225. * textures[blockCheck][3] / 10; } } }
  226. */
  227. }
  228. }
  229. background(0);
  230. rectMode(CENTER);
  231. updatePlayer();
  232. if (!dead) {
  233. cameraX = px;
  234. cameraY = py;
  235. }
  236. // Doing Entities;
  237. doDrops();
  238. doEntities();
  239. // Drawing World
  240. loadPixels();
  241. for (int i = 0; i < pixels.length; i++) {
  242. float x = i % w + cameraX - w / 2;
  243. float y = i / w + cameraY - h / 2;
  244. float px = i % w;
  245. float py = i / w;
  246. if (px < width - 100 || py < height - 50) {
  247. if (check(x, y)) {
  248. float r = textures[blockCheck][0];
  249. float g = textures[blockCheck][1];
  250. float b = textures[blockCheck][2];
  251. float h;
  252. if (dead) {
  253. h = 40 / (dist(0, 0, x - cameraX, y - cameraY));
  254. } else {
  255. h = 10000 / (l.qlength(x - cameraX, y - cameraY) + 5000);
  256. }
  257. r *= h;
  258. g *= h;
  259. b *= h;
  260. pixels[i] = color(r, g, b);
  261. } else if (checkOut) {
  262. if (y < world[0].length * 50 + 1000) {
  263. pixels[i] = color(0, 255, 255);
  264. } else {
  265. pixels[i] = color(0, 0, 255);
  266. }
  267. }
  268. } else {
  269. x -= w / 2 - 50;
  270. y -= h / 2 - 25;
  271. x = (x - cameraX) * 25 + cameraX;
  272. y = (y - cameraY) * 25 + cameraY;
  273. if (check(x, y)) {
  274. float r = textures[blockCheck][0];
  275. float g = textures[blockCheck][1];
  276. float b = textures[blockCheck][2];
  277. pixels[i] = color(r, g, b);
  278. }
  279. x -= cameraX;
  280. y -= cameraY;
  281. if (x < 25 && x > -25 && y < 50 && y > -50) {
  282. pixels[i] = color(255);
  283. }
  284. }
  285. }
  286. updatePixels();
  287. // Drawing GUI & Player
  288. if (!dead) {
  289. rectMode(CENTER);
  290. fill(255);
  291. rect(w / 2, h / 2, 50, 100);
  292. if (inventory[selection] > 0) {
  293. fill(color(textures[selection][0], textures[selection][1], textures[selection][2]));
  294. rect(25, height - 25, 50, 50);
  295. fill(255);
  296. textSize(32);
  297. text(inventory[selection], 60, height - 14);
  298. textSize(16);
  299. text(selection, 20, height - 60);
  300. }
  301. rectMode(CORNER);
  302. fill(0, 255, 0);
  303. rect(0, 0, health / 100 * width, 25);
  304. rectMode(CENTER);
  305. if (deathX > 0) {
  306. if (Math.abs(deathX - px) < 100) {
  307. deathX = -1;
  308. }
  309. fill(255);
  310. if (deathX < px) {
  311. rect(12, 37, 25, 25);
  312. } else {
  313. rect(width - 12, 37, 25, 25);
  314. }
  315. }
  316. }
  317. if (won) {
  318. health += (100 - health) / 2;
  319.  
  320. }
  321. }
  322. }
  323.  
  324. boolean breakBlock(int x, int y) {
  325. if (check(x * 50, y * 50) && (!(blockCheck == 4) || inventory[0] > 4999)) {
  326. world[x][y] = 0;
  327. if (blockCheck == 1) {
  328. blockCheck = 2;
  329. }
  330. spawnDrop(x * 50 + 25, y * 50 + 25, blockCheck, 10);
  331. if (Math.random() < 0.01 * textures[blockCheck][3]) {
  332. spawnDrop(x * 50 + 25, y * 50 + 25, 0, 10);
  333. }
  334. return true;
  335. }
  336. return false;
  337. }
  338.  
  339. void spawnDrop(float x, float y, int type, float force) {
  340. float[] buf = new float[5];
  341. buf[0] = x;
  342. buf[1] = y;
  343. buf[2] = type;
  344. buf[3] = (float) (Math.random() - 0.5) * force;
  345. buf[4] = (float) (Math.random() * -force / 2 - force / 2);
  346. drops.add(buf);
  347. }
  348.  
  349. void spawnEntity(float x, float y, float xv, float yv, float type) {
  350. float[] buf = new float[5];
  351. buf[0] = x;
  352. buf[1] = y;
  353. buf[2] = xv;
  354. buf[3] = yv;
  355. buf[4] = type;
  356. entities.add(buf);
  357. }
  358.  
  359. void killEntity(int e) {
  360. float[] buf = entities.get(e);
  361. if (buf[4] == 1) {
  362. for (int i = 0; i < 10; i++) {
  363. spawnDrop(buf[0], buf[1], 0, 10);
  364. }
  365. entities.remove(e);
  366. }
  367. }
  368.  
  369. void doEntities() {
  370. rectMode(CENTER);
  371. for (int i = 0; i < entities.size(); i++) {
  372. float[] buf = entities.get(i);
  373. buf[0] += buf[2];
  374. buf[1] += buf[3];
  375. if (buf[4] == 0) {
  376. fill(255);
  377. rect(buf[0] - cameraX + w / 2, buf[1] - cameraY + h / 2, 10, 10);
  378. for (int j = 0; j < entities.size(); j++) {
  379. float[] ob = entities.get(j);
  380. if (ob[4] == 1) {
  381. if (buf[0] - 35 < ob[0] && buf[0] + 35 > ob[0] && buf[1] - 35 < ob[1] && buf[1] + 35 > ob[1]) {
  382. entities.remove(i);
  383. if (i < j) {
  384. killEntity(j - 1);
  385. } else {
  386. killEntity(j);
  387. }
  388. j = entities.size();
  389. i--;
  390. }
  391. }
  392. }
  393. if (check(buf[0], buf[1])) {
  394. breakBlock((int) buf[0] / 50, (int) buf[1] / 50);
  395. entities.remove(i);
  396. i--;
  397. }
  398. } else if (buf[4] == 1) {
  399. boolean kill = false;
  400. check(buf[0] - 13, buf[1]);
  401. if (blockCheck == 6) {
  402. kill = true;
  403. }
  404. check(buf[0] + 13, buf[1]);
  405. if (blockCheck == 6) {
  406. kill = true;
  407. }
  408. check(buf[0], buf[1] - 13);
  409. if (blockCheck == 6) {
  410. kill = true;
  411. }
  412. check(buf[0], buf[1] + 13);
  413. if (blockCheck == 6) {
  414. kill = true;
  415. }
  416. if (kill) {
  417. killEntity(i);
  418. i--;
  419. }
  420. buf[3] += 0.1;
  421. fill(255);
  422. rect(buf[0] - cameraX + w / 2, buf[1] - cameraY + h / 2, 25, 25);
  423. if (buf[0] > px - 25 && buf[0] < px + 25 && buf[1] > py - 50 && buf[1] < py + 50 && !dead) {
  424. health -= 1;
  425. }
  426. if (check(buf[0] - 13, buf[1] + 13) || check(buf[0] + 13, buf[1] + 13)) {
  427. buf[3] = -5;
  428. }
  429. if (!dead) {
  430. if (px < buf[0]) {
  431. buf[2] -= 0.5;
  432. } else {
  433. buf[2] += 0.5;
  434. }
  435. }
  436. if (check(buf[0] - 25, buf[1]) || check(buf[0] + 25, buf[1])) {
  437. buf[2] *= -1;
  438. }
  439. buf[2] *= 0.9;
  440.  
  441. }
  442. }
  443. }
  444.  
  445. void doDrops() {
  446. rectMode(CENTER);
  447. for (int i = 0; i < drops.size(); i++) {
  448. float[] buf = drops.get(i);
  449. fill(textures[(int) buf[2]][0], textures[(int) buf[2]][1], textures[(int) buf[2]][2]);
  450. buf[0] += buf[3];
  451. buf[1] += buf[4];
  452. if (check(buf[0], buf[1]) && blockCheck != 6) {
  453. buf[0] -= buf[3];
  454. buf[1] -= buf[4];
  455. buf[3] = 0;
  456. buf[4] = 0;
  457. }
  458. buf[3] *= 0.95;
  459. buf[4] *= 0.95;
  460. buf[4] += 0.5;
  461. float d = dist(px, py, buf[0], buf[1]);
  462. if (d < magnetism) {
  463. buf[3] += (px - buf[0]) / d;
  464. buf[4] += (py - buf[1]) / d;
  465. }
  466. rect(buf[0] - cameraX + w / 2, buf[1] - cameraY + h / 2, 10, 10);
  467. float ix = buf[0];
  468. float iy = buf[1];
  469. rand.setSeed(i * 1000);
  470. if (check(ix, iy + rand.nextInt(50))) {
  471. buf[4] -= 1;
  472. }
  473. if (ix > px - 25 && ix < px + 25 && iy > py - 50 && iy < py + 50 && !dead) {
  474. inventory[(int) buf[2]] += 1;
  475. drops.remove(i);
  476. i--;
  477. }
  478. }
  479. }
  480.  
  481. void setBlock(int x, int y, int block) {
  482. if (x < world.length && x > 0 && y < world[0].length && y > 0) {
  483. world[x][y] = (byte) block;
  484. }
  485. }
  486.  
  487. void doTick(int rx, int ry) {
  488. if (!(world[rx][ry] == 0 || world[rx][ry] == 6 || world[rx][ry] == 9)) {
  489. check(rx * 50 + 25, ry * 50 - 25);
  490. if (!first && Math.random() < (double) difficulty * difficulty / 1000 && blockCheck == 0
  491. && l.qdist(rx * 50 + 25, ry * 50 + 25, px, py) > 62500) {
  492. blockCheck = 1;
  493. check(rx * 50 + 25, ry * 50 - 125);
  494. if (blockCheck == 0) {
  495. if (world[rx][ry - 1] == 0) {
  496. spawnEntity(rx * 50 + 25, ry * 50 + 25, 0, 0, 1);
  497. }
  498. }
  499. }
  500. }
  501. if (world[rx][ry] == 1) {
  502. if (world[rx - 1][ry] == 2 && world[rx - 1][ry - 1] == 0) {
  503. setBlock(rx - 1, ry, 1);
  504. }
  505. if (world[rx + 1][ry] == 2 && world[rx + 1][ry - 1] == 0) {
  506. setBlock(rx + 1, ry, 1);
  507. }
  508. if (world[rx - 1][ry + 1] == 2 && world[rx - 1][ry] == 0) {
  509. setBlock(rx - 1, ry + 1, 1);
  510. }
  511. if (world[rx + 1][ry + 1] == 2 && world[rx + 1][ry] == 0) {
  512. setBlock(rx + 1, ry + 1, 1);
  513. }
  514. if (world[rx - 1][ry - 1] == 2 && world[rx - 1][ry - 2] == 0) {
  515. setBlock(rx - 1, ry - 1, 1);
  516. }
  517. if (world[rx + 1][ry - 1] == 2 && world[rx + 1][ry - 2] == 0) {
  518. setBlock(rx + 1, ry - 1, 1);
  519. }
  520. }
  521. if (world[rx][ry] == 5) {
  522. int set = 5;
  523. if (Math.random() < 0.8) {
  524. set = 3;
  525. }
  526. world[rx][ry] = (byte) set;
  527. set = 3;
  528. if (Math.random() < 0.9) {
  529. set = 5;
  530. }
  531. int dir = rand.nextInt(5);
  532. if (dir == 0 && world[rx + 1][ry] == 0) {
  533. setBlock(rx + 1, ry, set);
  534. } else if (dir == 1 && world[rx - 1][ry] == 0) {
  535. setBlock(rx - 1, ry, set);
  536. } else if (world[rx][ry - 1] == 0) {
  537. setBlock(rx, ry - 1, set);
  538. }
  539. }
  540. if (world[rx][ry] == 6 && world[rx][ry + 1] == 0) {
  541. setBlock(rx, ry, 0);
  542. setBlock(rx, ry + 1, 6);
  543. }
  544. if (world[rx][ry] == 8) {
  545. if (world[rx - 1][ry] == 7 && world[rx + 1][ry] == 7 && world[rx][ry - 1] == 7 && world[rx][ry + 1] == 7) {
  546. world[rx][ry - 1] = 0;
  547. world[rx - 1][ry] = 0;
  548. world[rx][ry + 1] = 0;
  549. world[rx + 1][ry] = 0;
  550. world[rx][ry] = 0;
  551. for (int j = 0; j < 100; j++) {
  552. spawnDrop(rx * 50 + 25, ry * 50 + 25, 0, 20);
  553. }
  554. } else if (world[rx - 1][ry] == 1 && world[rx + 1][ry] == 1 && world[rx][ry - 1] == 1
  555. && world[rx][ry + 1] == 1) {
  556. world[rx][ry - 1] = 0;
  557. world[rx - 1][ry] = 0;
  558. world[rx][ry + 1] = 0;
  559. world[rx + 1][ry] = 0;
  560. world[rx][ry] = 0;
  561. spawnDrop(rx * 50 + 25, ry * 50 + 25, 9, 10);
  562. }
  563. }
  564. if (world[rx][ry] == 7) {
  565. for (int j = 0; j < 100; j++) {
  566. int tx = rx + rand.nextInt(10) - 5;
  567. int ty = ry + rand.nextInt(10) - 5;
  568. if (check(tx * 50, ty * 50)) {
  569. if (world[tx][ty] == 6) {
  570. setBlock(tx, ty, 7);
  571. }
  572. }
  573. }
  574. }
  575. }
  576.  
  577. void randomTicks(float per) {
  578. for (int i = 0; i < (world.length * world[0].length) * per / 100; i++) {
  579. int rx = rand.nextInt(world.length);
  580. int ry = rand.nextInt(world[0].length);
  581. doTick(rx, ry);
  582. }
  583. }
  584.  
  585. boolean check(float x, float y) {
  586. checkOut = false;
  587. x /= 50;
  588. y /= 50;
  589. if (x < world.length && x > 0 && y < world[0].length && y > 0) {
  590. if (!(world[(int) x][(int) y] == 0)) {
  591. blockCheck = world[(int) x][(int) y];
  592. return true;
  593. } else {
  594. blockCheck = 0;
  595. }
  596. } else {
  597. checkOut = true;
  598. }
  599. return false;
  600. }
  601.  
  602. void kill() {
  603. burning = 0;
  604. deathX = px;
  605. dead = true;
  606. float odds = 1 / ((float) findBlock(9).size() + 1);
  607. for (int i = 0; i < inventory.length; i++) {
  608. for (int j = 0; j < inventory[i]; j++) {
  609. if (getRandBlock(9) != null && Math.random() > odds) {
  610. int[] buf = getRandBlock(9);
  611. spawnDrop(buf[0] * 50 - 25, buf[1] * 50 - 50, i, 20);
  612. } else {
  613. spawnDrop(px, py, i, 20);
  614. }
  615. }
  616. inventory[i] = 0;
  617. }
  618. }
  619.  
  620. int[] getRandBlock(int type) {
  621. List<int[]> buf = findBlock(type);
  622. if (buf.size() > 0) {
  623. int i = rand.nextInt(buf.size());
  624. int[] b = new int[2];
  625. b[0] = buf.get(i)[0];
  626. b[1] = buf.get(i)[1];
  627. return b;
  628. } else {
  629. return null;
  630. }
  631. }
  632.  
  633. List<int[]> findBlock(int type) {
  634. List<int[]> dat = new ArrayList<int[]>();
  635. for (int x = 0; x < world.length; x++) {
  636. for (int y = 0; y < world[0].length; y++) {
  637. if (world[x][y] == type) {
  638. int[] buf = new int[2];
  639. buf[0] = x;
  640. buf[1] = y;
  641. dat.add(buf);
  642. }
  643. }
  644. }
  645. return dat;
  646. }
  647.  
  648. void updatePlayer() {
  649. if (py > world[0].length * 50 + 1000) {
  650. burning = 0;
  651. won = true;
  652. yv -= 0.3;
  653.  
  654. }
  655. if (py > world[0].length * 50 + 1100) {
  656. yv *= 0.98;
  657. menus = true;
  658. }
  659. if (dead) {
  660. health = 100;
  661. progress -= 1;
  662. if (progress == 0) {
  663. first = true;
  664. dead = false;
  665. }
  666. }
  667. health -= (float) (inventory[6] / 5) / 10;
  668. if (health <= 0 && !won) {
  669. kill();
  670. progress = 180;
  671. }
  672. if (dead) {
  673. px = 0;
  674. py = 0;
  675. }
  676. if (health < 100) {
  677. health += 0.01;
  678. }
  679. touchingBlock = false;
  680. if (first) {
  681. yv = 0;
  682. xv = 0;
  683. blockCheck = 0;
  684. int[] buf = getRandBlock(9);
  685. if (buf != null) {
  686. px = buf[0] * 50 + 25;
  687. py = buf[1] * 50 - 1;
  688. first = false;
  689. }
  690. if (first) {
  691. while (!(blockCheck == 1)) {
  692. py = 0;
  693. px = rand.nextInt(world.length) * 50;
  694. while (!check(px - 25, py + 50) && !check(px + 25, py + 50)) {
  695. py += 1;
  696. }
  697. }
  698. }
  699. py -= 50;
  700. first = false;
  701. }
  702. fill(textures[0][0], textures[0][1], textures[0][2]);
  703. yv += 0.2;
  704. xv *= 0.8;
  705. px += xv;
  706. py += yv;
  707. if (check(px - 20, py - 40) || check(px + 20, py - 40) || check(px - 20, py + 40) || check(px + 20, py + 40)) {
  708. health -= 0.1;
  709. }
  710. if (check(px - 24, py - 49) || check(px + 24, py - 49)) {
  711. touchingBlock = true;
  712. py -= yv;
  713. yv = 0;
  714. if (blockCheck == 6) {
  715. burning = 300;
  716. }
  717. }
  718. if (check(px - 24, py + 49) || check(px + 24, py + 49)) {
  719. if (yv > 15) {
  720. health -= (yv - 15) * 10;
  721. }
  722. touchingBlock = true;
  723. if (blockCheck == 6) {
  724. burning = 300;
  725. }
  726. py -= yv;
  727. if (up) {
  728. yv = -7;
  729. } else {
  730. yv = 0;
  731. }
  732. }
  733. if (check(px + 24, py - 49) || check(px + 24, py + 49) || check(px + 24, py)) {
  734. touchingBlock = true;
  735. if (blockCheck == 6) {
  736. burning = 300;
  737. }
  738. px -= xv;
  739. xv = 0;
  740. } else {
  741. if (right && !(check(px - 24, py - 24) || check(px - 24, py + 49) || check(px + 24, py))) {
  742. xv += 2;
  743. }
  744. }
  745. if (check(px - 24, py - 24) || check(px - 24, py + 49) || check(px + 24, py)) {
  746. touchingBlock = true;
  747. if (blockCheck == 6) {
  748. burning = 300;
  749. }
  750. px -= xv;
  751. xv = 0;
  752. } else {
  753. if (left && !(check(px + 24, py - 49) || check(px + 24, py + 49) || check(px + 24, py))) {
  754. xv -= 2;
  755. }
  756. }
  757. if (burning > 0) {
  758. health -= 0.2;
  759. burning--;
  760. }
  761. }
  762.  
  763. void addTexture(int r, int g, int b, int h) {
  764. textures[tc][0] = (short) r;
  765. textures[tc][1] = (short) g;
  766. textures[tc][2] = (short) b;
  767. textures[tc][3] = (short) h;
  768. tc++;
  769. }
  770.  
  771. public void mouseWheel(MouseEvent event) {
  772. byte old = selection;
  773. selection -= event.getCount();
  774. selection %= inventory.length;
  775. if (selection < 0) {
  776. selection = (byte) (inventory.length - 1);
  777. }
  778. while (inventory[selection] == 0 && !(old == selection)) {
  779. selection -= event.getCount();
  780. selection %= inventory.length;
  781. if (selection < 0) {
  782. selection = (byte) (inventory.length - 1);
  783. }
  784. }
  785. }
  786.  
  787. public void keyPressed() {
  788. if (key == 'w') {
  789. up = true;
  790. }
  791. if (key == 'a') {
  792. left = true;
  793. }
  794. if (key == 'd') {
  795. right = true;
  796. }
  797. if (key == ' ') {
  798. space = true;
  799. }
  800. if (menus) {
  801. if (keyCode == LEFT && difficulty > 0) {
  802. difficulty--;
  803. }
  804. if (keyCode == RIGHT && difficulty < 32) {
  805. difficulty++;
  806. }
  807. }
  808. }
  809.  
  810. public void keyReleased() {
  811. if (key == 'w') {
  812. up = false;
  813. }
  814. if (key == 'a') {
  815. left = false;
  816. }
  817. if (key == 'd') {
  818. right = false;
  819. }
  820. if (key == ' ') {
  821. space = false;
  822. }
  823. }
  824. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement