Advertisement
Guest User

cc

a guest
Sep 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>cc</title>
  5. <style type="">
  6. body, html, head {
  7. padding: 0;
  8. margin: 0;
  9. }
  10. canvas {
  11. position: absolute;
  12. top: 0;
  13. left: 0;
  14. right: 0;
  15. bottom: 0;
  16. }
  17.  
  18. </style>
  19. </head>
  20. <body>
  21. <canvas></canvas>
  22. <script type="">
  23. var random = Math.random, sin = Math.sin, cos = Math.cos, atan2 = Math.atan2, sqrt = Math.sqrt, round = Math.round, PI = Math.PI, pow = Math.pow;
  24. Array.prototype.random = function () {
  25. return this[round(random()*(this.length-1))];
  26. }
  27. String.prototype.random = function () {
  28. return this[round(random()*(this.length-1))];
  29. }
  30. function randomString(n) {
  31. return Array.from({length: n||16}, function (x, i) {
  32. return 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'.random();
  33. });
  34. }
  35.  
  36. var canvas = document.querySelector('canvas');
  37. var ctx = canvas.getContext('2d');
  38. var W, H;
  39. function onResize() {
  40. canvas.width = W = window.innerWidth;
  41. canvas.height = H = window.innerHeight;
  42. }
  43. window.addEventListener('resize', onResize);
  44. onResize();
  45.  
  46. var MOUSE = [W/2, H/2];
  47. var INPUT = {};
  48. window.addEventListener('keydown', function (e) {
  49. INPUT[e.code.toUpperCase()] = true;
  50. });
  51. window.addEventListener('keyup', function (e) {
  52. INPUT[e.code.toUpperCase()] = false;
  53. });
  54. window.addEventListener('mousemove', function (e) {
  55. MOUSE[0] = e.clientX;
  56. MOUSE[1] = e.clientY;
  57. });
  58. window.addEventListener('mousedown', function (e) {
  59. INPUT[(['LEFT', 'MIDDLE', 'RIGHT'])[e.button]] = true;
  60. });
  61. window.addEventListener('mouseup', function (e) {
  62. INPUT[(['LEFT', 'MIDDLE', 'RIGHT'])[e.button]] = false;
  63. });
  64.  
  65. var bullets = [];
  66. var types = {
  67. 'player': {
  68. color: [200, 50, 200],
  69. tick: function (c, nearest) {
  70. var sp = (!!INPUT.KEYW-0) - (!!INPUT.KEYS-0);
  71.  
  72. c.dir = atan2(MOUSE[0] - c.x, MOUSE[1] - c.y);
  73. c.x += sin(c.dir)*sp*2;
  74. c.y += cos(c.dir)*sp*2;
  75.  
  76. if ((INPUT.LEFT || INPUT.SPACE) && Date.now()-c.l > 150) {
  77. bullets.push({
  78. from: c.id,
  79. x: c.x + sin(c.dir)*15,
  80. y: c.y + cos(c.dir)*15,
  81. dir: c.dir,
  82. damage: 0.1,
  83. speed: 2
  84. });
  85. c.l = Date.now();
  86. }
  87. }
  88. },
  89. 'pacifist': {
  90. color: [50, 200, 50],
  91. vdist: 400,
  92. tick: function (c, nearest, nbull) {
  93. if (nbull && nbull.dist < 200) {
  94. c.dir = atan2(c.x - nbull.x, c.y - nbull.y);
  95. c.x += sin(c.dir)*(2-nbull.dist/200);
  96. c.y += cos(c.dir)*(2-nbull.dist/200);
  97. } else if (nearest && nearest.type != 'pacifist') {
  98. c.dir = atan2(c.x - nearest.x, c.y - nearest.y);
  99. c.x += sin(c.dir)*(2-nearest.dist/200);
  100. c.y += cos(c.dir)*(2-nearest.dist/200);
  101. }
  102. }
  103. },
  104. 'angry': {
  105. color: [200, 50, 50],
  106. vdist: 150,
  107. tick: function (c, nearest, nbull) {
  108. if (nbull && nbull.dist < 200) {
  109. c.dir = atan2(c.x - nbull.x, c.y - nbull.y);
  110. c.x += sin(c.dir)*(2-nbull.dist/200);
  111. c.y += cos(c.dir)*(2-nbull.dist/200);
  112. } else if (nearest) {
  113. c.dir = atan2(nearest.x - c.x, nearest.y - c.y);
  114. c.x += sin(c.dir)*2;
  115. c.y += cos(c.dir)*2;
  116.  
  117. if (Date.now()-c.l > 150) {
  118. bullets.push({
  119. from: c.id,
  120. x: c.x + sin(c.dir)*15,
  121. y: c.y + cos(c.dir)*15,
  122. dir: c.dir,
  123. damage: 0.1
  124. });
  125. c.l = Date.now();
  126. }
  127. }
  128.  
  129. }
  130. },
  131. 'big': {
  132. color: [200, 200, 50],
  133. vdist: 500,
  134. tick: function (c, nearest) {
  135. if (nearest) {
  136. c.dir = atan2(nearest.x - c.x, nearest.y - c.y);
  137. c.x += sin(c.dir)*2;
  138. c.y += cos(c.dir)*2;
  139.  
  140. if (Date.now()-c.l > 1000) {
  141. bullets.push({
  142. from: c.id,
  143. x: c.x + sin(c.dir)*15,
  144. y: c.y + cos(c.dir)*15,
  145. dir: c.dir,
  146. damage: 0.5,
  147. speed: 0.75
  148. });
  149. c.l = Date.now();
  150. }
  151. }
  152.  
  153. }
  154. },
  155. 'baam': {
  156. color: [50, 200, 200],
  157. vdist: 500,
  158. tick: function (c, nearest) {
  159. if (nearest) {
  160. c.dir = atan2(nearest.x - c.x, nearest.y - c.y);
  161. c.x += sin(c.dir)*2;
  162. c.y += cos(c.dir)*2;
  163.  
  164. if (Date.now()-c.l > 1000) {
  165. for (var i = 0; i < 10; ++i) {
  166. bullets.push({
  167. from: c.id,
  168. x: c.x + sin(c.dir)*15,
  169. y: c.y + cos(c.dir)*15,
  170. dir: c.dir+((5-i)/5*(PI/180*15)),
  171. damage: 0.025,
  172. speed: 1.5
  173. });
  174. }
  175. c.l = Date.now();
  176. }
  177. }
  178.  
  179. }
  180. }
  181. }
  182. var creatures = Array.from({length: 70}, newCreature);
  183. function newCreature(x, i) {
  184. // if (i == 0) {
  185. // return {
  186. // type: 'player',
  187. // x: W/2,
  188. // y: H/2,
  189. // dir: random()*PI*2,
  190. // health: 1,
  191. // l: Date.now(),
  192. // id: randomString()
  193. // }
  194. // } else
  195. return {
  196. type: Object.keys(types).filter(function(t){return t!='player'}).random(),
  197. x: random()*(W-30)+15,
  198. y: random()*(H-30)+15,
  199. dir: random()*PI*2,
  200. health: 1,
  201. l: Date.now(),
  202. id: randomString(),
  203. im: 1
  204. }
  205. }
  206.  
  207. var lastTime = Date.now();
  208. function render() {
  209. var P = 60 / (1000/(Date.now()-lastTime));
  210. lastTime = Date.now();
  211. ctx.strokeStyle = 'black';
  212. ctx.fillStyle = 'white';
  213. ctx.fillRect(0, 0, W, H);
  214. ctx.strokeRect(2, 0, W-2, H-1);
  215.  
  216. for (var i = 0; i < creatures.length; ++i) {
  217. var C = creatures[i];
  218. var cc = types[C.type].color;
  219. var sc = Array.from(cc, function (x) {return x-50<0?0:x-50;});
  220. var oc = 'rgba('+cc.join()+','+(1-C.im)+')';
  221. var os = 'rgba('+sc.join()+','+(1-C.im)+')';
  222.  
  223. ctx.lineWidth = 1;
  224. ctx.fillStyle = oc;
  225. ctx.strokeStyle = os;
  226. ctx.beginPath();
  227. ctx.moveTo(C.x, C.y);
  228. ctx.arc(C.x, C.y, 15, 0, PI*2*C.health);
  229. ctx.lineTo(C.x, C.y);
  230. ctx.fill();
  231. ctx.beginPath();
  232. ctx.arc(C.x, C.y, 15, 0, PI*2);
  233. ctx.stroke();
  234. ctx.lineWidth = 3;
  235. ctx.beginPath();
  236. ctx.moveTo(C.x, C.y);
  237. ctx.lineTo(C.x + sin(C.dir)*15, C.y + cos(C.dir)*15);
  238. ctx.stroke();
  239.  
  240. if (C.im > 0) {
  241. ctx.strokeStyle = 'rgba('+sc.join()+','+C.im+')';
  242. ctx.lineWidth = 1;
  243. ctx.beginPath();
  244. ctx.arc(C.x, C.y, 15 + (1-C.im)*20, 0, PI*2);
  245. ctx.stroke();
  246. }
  247. }
  248.  
  249. ctx.fillStyle = '#D84315';
  250. for (var i = 0; i < bullets.length; ++i) {
  251. var b = bullets[i];
  252. ctx.beginPath();
  253. ctx.arc(b.x, b.y, 20*b.damage, 0, PI*2);
  254. ctx.fill();
  255. }
  256.  
  257. // tick
  258. for (var u = 0; u < bullets.length; ++u) {
  259. var B = bullets[u];
  260. var k = false;
  261. for (var c = 0; c < creatures.length; ++c) {
  262. var C = creatures[c];
  263. if (B.from == C.id || C.im != 0) continue;
  264. var dist = sqrt(pow(C.x - B.x, 2) + pow(C.y - B.y, 2));
  265. if (dist <= 18) {
  266. C.health -= B.damage;
  267. bullets.splice(u, 1);
  268. k = true;
  269. u--;
  270. if (C.health <= 0) {
  271. creatures.splice(c, 1);
  272. creatures.push(newCreature());
  273. }
  274. break;
  275. }
  276. }
  277. if (!k) {
  278. B.x += sin(B.dir)*4*(B.speed||1);
  279. B.y += cos(B.dir)*4*(B.speed||1);
  280. if (B.x < 0 || B.x > W || B.y < 0 || B.y > H) {
  281. bullets.splice(u, 1);
  282. }
  283. }
  284. }
  285. for (var c = 0; c < creatures.length; ++c) {
  286. var C = creatures[c];
  287. var f = types[C.type].tick;
  288. var vdist = types[C.type].vdist || 150;
  289. if (typeof f !== 'function') {
  290. continue;
  291. }
  292. var nearest = null, nval = -1;
  293. for (var h = 0; h < creatures.length; ++h) {
  294. if (c == h) continue;
  295. var dist = sqrt(pow(C.x - creatures[h].x, 2) + pow(C.y - creatures[h].y, 2));
  296. if (dist > vdist) continue;
  297. if (dist < nval || nval == -1) {
  298. nearest = Object.assign(creatures[h], {dist: dist});
  299. nval = dist;
  300. }
  301. }
  302. var nbull = null;
  303. nval = -1;
  304. for (var h = 0; h < bullets.length; ++h) {
  305. if (bullets[h].from == C.id) continue;
  306. var dist = sqrt(pow(C.x - bullets[h].x, 2) + pow(C.y - bullets[h].y, 2));
  307. if (dist > vdist) continue;
  308. if (dist < nval || nval == -1) {
  309. nbull = Object.assign(bullets[h], {dist: dist});
  310. nval = dist;
  311. }
  312. }
  313. var wasX = C.x;
  314. var wasY = C.y;
  315. f(C, nearest, nbull);
  316. if (C.x < 15 || C.x > W-15)
  317. C.x = wasX;
  318. if (C.y < 15 || C.y > H-15)
  319. C.y = wasY;
  320.  
  321. if (C.im > 0) {
  322. C.im -= 0.01*P;
  323. if (C.im < 0)
  324. C.im = 0;
  325. }
  326. }
  327.  
  328. requestAnimationFrame(render);
  329. }
  330. render();
  331. </script>
  332. </body>
  333. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement