Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  6. <title>Paperoids</title>
  7. <script type="text/javascript" src="../../dist/paper-full.js"></script>
  8. <script type="text/paperscript" canvas="canvas">
  9. //
  10. //
  11. // Paperoids - an Asteroids clone for paper.js
  12. //
  13. // Sorry nerds, no enemy ships or sound just yet.
  14. //
  15. // Version: 1.2.1
  16. // Author: David Hirmes (@hirmes)
  17. // Date: 2011-08-09
  18. //
  19. // Revision History:
  20. // 1.0 - 2011-07-13 - Initial release
  21. // 1.1 - 2011-07-13 - Optimizations by Jonathan Puckey (@jonathanpuckey)
  22. // 1.2 - 2011-07-16 - Refactored with closures (@jonathanpuckey)
  23. // 1.2.1- 2011-08-09 - bug fixes
  24. //
  25. var presets = {
  26. speed: 0.2,
  27. maxRockSpeed: 4.5,
  28. rockCount: 6,
  29. lives: 3,
  30. freeShipScore: 10000,
  31. freeShipIncrement: 10000
  32. };
  33. function initialize() {
  34. Rocks.add(presets.rockCount);
  35. Score.update();
  36. Lives.initialize();
  37. }
  38. function onKeyUp(event) {
  39. if (event.key == 'space') {
  40. Ship.moveTo(Point.random() * view.size);
  41. Ship.stop();
  42. }
  43. if (event.key == 'z') {
  44. Ship.fire();
  45. }
  46. // Show stats:
  47. if (event.key == 'f') {
  48. var stats = document.getElementById('stats');
  49. if (stats) {
  50. stats.style.display = (stats.style.display == 'block')
  51. ? 'none' : 'block';
  52. }
  53. }
  54. }
  55. function onFrame() {
  56. Bullets.move();
  57. Rocks.iterateExplosions();
  58. Ship.checkCollisions();
  59. if (Key.isDown('left')) {
  60. Ship.turnLeft();
  61. }
  62. if (Key.isDown('right')) {
  63. Ship.turnRight();
  64. }
  65. if (Key.isDown('up')) {
  66. Ship.thrust();
  67. } else {
  68. Ship.coast();
  69. }
  70. Ship.move();
  71. }
  72. project.currentStyle.strokeColor = 'white';
  73. var Game = {
  74. roundDelay: false,
  75. over: function() {
  76. document.getElementById('gameover').style.display = 'block';
  77. },
  78. newRound: function() {
  79. Game.roundDelay = false;
  80. Rocks.add(presets.rockCount);
  81. },
  82. // Stats.js by Mr. Doob - https://github.com/mrdoob/stats.js
  83. };
  84. var assets = {
  85. destroyedShip: new function() {
  86. var group = new Group(
  87. new Path([-10, -8], [10, 0]),
  88. new Path([10, 0], [-10, 8]),
  89. new Path([-8, 4], [-8, -4])
  90. );
  91. group.visible = false;
  92. return group;
  93. },
  94. explosion: new function() {
  95. var explosionPath = new Path.Circle(new Point(), 1);
  96. explosionPath.fillColor = 'white';
  97. explosionPath.strokeColor = null;
  98. return new SymbolDefinition(explosionPath);
  99. }
  100. };
  101. var Ship = new function() {
  102. var path = new Path([-10, -8], [10, 0], [-10, 8], [-8, 4], [-8, -4]);
  103. path.closed = true;
  104. var thrust = new Path([-8, -4], [-14, 0], [-8, 4]);
  105. var group = new Group(path, thrust);
  106. group.position = view.bounds.center;
  107. return {
  108. item: group,
  109. angle: 0,
  110. vector: new Point({
  111. angle: 0.2,
  112. length: 1
  113. }),
  114. turnLeft: function() {
  115. group.rotate(-3);
  116. this.angle -= 3;
  117. },
  118. turnRight: function() {
  119. group.rotate(3);
  120. this.angle += 3;
  121. },
  122. thrust: function() {
  123. thrust.visible = true;
  124. this.vector += new Point({
  125. angle: this.angle,
  126. length: presets.speed
  127. });
  128. if (this.vector.length > 8) {
  129. this.vector.length = 8;
  130. }
  131. },
  132. stop: function() {
  133. this.vector.length = 0;
  134. },
  135. fire: function() {
  136. if (!this.dying)
  137. Bullets.fire(this.item.position, this.angle);
  138. },
  139. coast: function() {
  140. thrust.visible = false;
  141. this.vector *= .992;
  142. },
  143. move: function() {
  144. group.position += this.vector;
  145. keepInView(group);
  146. },
  147. moveTo: function(position) {
  148. group.position = position;
  149. keepInView(group);
  150. },
  151. destroy: function() {
  152. this.destroyedShip = assets.destroyedShip.clone();
  153. this.destroyedShip.position = this.item.position;
  154. this.destroyedShip.visible = true;
  155. this.item.visible = false;
  156. this.stop();
  157. this.item.position = view.center;
  158. this.dying = true;
  159. },
  160. destroyed: function() {
  161. this.item.visible = true;
  162. this.stop();
  163. this.item.position = view.center;
  164. this.dying = false;
  165. this.destroyedShip.visible = false;
  166. },
  167. checkCollisions: function() {
  168. var crashRock;
  169. // move rocks and do a hit-test
  170. // between bounding rect of rocks and ship
  171. for (var i = 0; i < Rocks.children.length; i++) {
  172. var rock = Rocks.children[i];
  173. rock.position += rock.vector;
  174. if (rock.bounds.intersects(this.item.bounds))
  175. crashRock = rock;
  176. keepInView(rock);
  177. }
  178. if (this.dying) {
  179. var children = this.destroyedShip.children;
  180. children[0].position.x++;
  181. children[1].position.x--;
  182. children[2].position.x--;
  183. children[2].position.y++;
  184. children[0].rotate(1);
  185. children[1].rotate(-1);
  186. children[2].rotate(1);
  187. this.destroyedShip.opacity *= 0.98;
  188. // don't update anything else if the ship is already dead.
  189. return;
  190. }
  191. // if bounding rect collision, do a line intersection test
  192. if (crashRock) {
  193. var tempRock = crashRock.symbol.definition.clone();
  194. tempRock.transform(crashRock.matrix);
  195. tempRock.remove();
  196. var intersections = this.item.firstChild.getIntersections(tempRock);
  197. if (intersections.length > 0)
  198. Lives.remove();
  199. }
  200. }
  201. };
  202. };
  203. var Bullets = new function() {
  204. var group = new Group();
  205. var children = group.children;
  206. function checkHits(bullet) {
  207. for (var r = 0; r < Rocks.children.length; r++) {
  208. var rock = Rocks.children[r];
  209. if (rock.bounds.contains(bullet.position) ) {
  210. Score.update(rock.shapeType);
  211. Rocks.explode(rock);
  212. if (rock.shapeType < Rocks.TYPE_SMALL ) {
  213. for (var j = 0; j < 2; j++) {
  214. Rocks.add(1, rock.shapeType + 4, rock.position);
  215. }
  216. }
  217. rock.remove();
  218. bullet.remove();
  219. }
  220. }
  221. }
  222. return {
  223. fire: function(position, angle) {
  224. // We can only fire 5 bullets at a time:
  225. if (children.length == 5)
  226. return;
  227. var vector = new Point({
  228. angle: angle,
  229. length: 10
  230. });
  231. var bullet = new Path.Circle({
  232. center: position + vector,
  233. radius: 0.5,
  234. parent: group,
  235. fillColor: 'white',
  236. strokeWidth: 'white',
  237. strokeWidth: 0,
  238. data: {
  239. vector: vector,
  240. timeToDie: 58
  241. }
  242. });
  243. },
  244. move: function() {
  245. for (var i = 0; i < children.length; i++) {
  246. var bullet = children[i];
  247. bullet.data.timeToDie--;
  248. if (bullet.data.timeToDie < 1) {
  249. bullet.remove();
  250. } else {
  251. bullet.position += bullet.data.vector;
  252. checkHits(bullet);
  253. keepInView(bullet);
  254. }
  255. }
  256. }
  257. };
  258. };
  259. var Rocks = new function() {
  260. var group = new Group();
  261. var shapes = [
  262. new Path(
  263. [-23, -40.5], [0, -30.5], [24, -40.5], [45, -21.5], [25, -12.5],
  264. [46, 9.5], [22, 38.5], [-10, 30.5], [-22, 40.5], [-46, 18.5],
  265. [-33, 0.5], [-44, -21.5], [-23, -40.5]),
  266. new Path(
  267. [-45, -9.5], [-12, -40.5], [24, -40.5], [46, -11.5], [45, 10.5],
  268. [24, 40.5], [0, 40.5], [0, 10.5], [-23, 38.5], [-46, 9.5], [-25, 0.5],
  269. [-45, -9.5]),
  270. new Path([-21.5, -39.5], [11.5, -39.5], [45.5, -20.5],
  271. [45.5, -8.5], [9.5, 0.5], [44.5, 21.5], [22.5, 39.5], [9.5, 31.5],
  272. [-20.5, 39.5], [-45.5, 10.5], [-45.5, -20.5], [-11.5, -21.5],
  273. [-21.5, -39.5]),
  274. new Path(
  275. [-22.5, -40.5], [-0.5, -19.5], [23.5, -39.5], [44.5, -21.5],
  276. [33.5, 0.5], [46.5, 19.5], [13.5, 40.5], [-22.5, 39.5], [-46.5, 18.5],
  277. [-46.5, -18.5], [-22.5, -40.5])
  278. ];
  279. // medium rocks
  280. for (var i = 4; i < 8; i++) {
  281. shapes[i] = shapes[i - 4].clone();
  282. shapes[i].scale(0.5);
  283. }
  284. // small rocks
  285. for (var i = 8; i < 12; i++) {
  286. shapes[i] = shapes[i - 4].clone();
  287. shapes[i].scale(0.4);
  288. }
  289. var rockSymbols = [];
  290. for (var i = 0; i < shapes.length; i++) {
  291. rockSymbols[i] = new SymbolDefinition(shapes[i]);
  292. }
  293. var explosions = new Group();
  294. return {
  295. shapes: shapes,
  296. children: group.children,
  297. make: function(type, pos) {
  298. var randomRock = type + Math.floor(4 * Math.random());
  299. var rock = rockSymbols[randomRock].place();
  300. rock.position = pos ? pos : Point.random() * view.size;
  301. rock.vector = new Point({
  302. angle: 360 * Math.random(),
  303. length: presets.maxRockSpeed * Math.random() + 0.1
  304. });
  305. rock.shapeType = type;
  306. return rock;
  307. },
  308. add: function(amount, type, position) {
  309. for (var i = 0; i < amount; i++) {
  310. var rock = this.make(type || this.TYPE_BIG, position);
  311. group.addChild(rock);
  312. }
  313. },
  314. explode: function(rock) {
  315. var boomRock = rock.symbol.definition.clone();
  316. boomRock.position = rock.position;
  317. for (var i = 0; i < boomRock.segments.length; i++) {
  318. var segmentPoint = boomRock.segments[i].point;
  319. var placed = assets.explosion.place(segmentPoint);
  320. placed.vector = (placed.position - rock.position) * 0.1;
  321. explosions.addChild(placed);
  322. }
  323. boomRock.remove();
  324. },
  325. iterateExplosions: function() {
  326. for (var i = 0; i < explosions.children.length; i++) {
  327. var explosion = explosions.children[i];
  328. explosion.vector.length *= .7;
  329. explosion.position += explosion.vector;
  330. explosion.opacity = explosion.vector.length;
  331. if (explosion.vector.length < 0.05 ) {
  332. explosion.remove();
  333. // if no more rocks, wait a second and start a new round
  334. if (this.children.length < 1 && !Game.roundDelay) {
  335. Game.roundDelay = true;
  336. presets.rockCount += 2;
  337. setTimeout(Game.newRound, 1000);
  338. }
  339. }
  340. }
  341. },
  342. TYPE_BIG: 0,
  343. TYPE_MEDIUM: 4,
  344. TYPE_SMALL: 8
  345. };
  346. };
  347. var Score = new function() {
  348. var numberGroup = new Group(
  349. new Path([0, 0], [20, 0], [20, 27], [0, 27], [0, 0]),
  350. new Path([10, 0], [10, 27]),
  351. new Path([0, 0], [20, 0], [20, 14], [0, 14], [0, 27], [20, 27]),
  352. new Path([0, 0], [20, 0], [20, 14], [0, 14], [20, 14], [20, 27], [0, 27]),
  353. new Path([0, 0], [0, 14], [20, 14], [20, 0], [20, 27]),
  354. new Path([20, 0], [0, 0], [0, 14], [20, 14], [20, 27], [0, 27]),
  355. new Path([20, 0], [0, 0], [0, 27], [20, 27], [20, 14], [0, 14]),
  356. new Path([0, 0], [20, 0], [0, 27]),
  357. new Path([0, 0], [20, 0], [20, 27], [0, 27], [0, 0], [0, 14], [20, 14]),
  358. new Path([20, 14], [0, 14], [0, 0], [20, 0], [20, 27])
  359. );
  360. numberGroup.visible = false;
  361. var scoreDisplay = new Group();
  362. var score = 0;
  363. return {
  364. update: function(type) {
  365. if (type == Rocks.TYPE_BIG) score += 20;
  366. if (type == Rocks.TYPE_MEDIUM) score += 50;
  367. if (type == Rocks.TYPE_SMALL) score += 100;
  368. if (score >= presets.freeShipScore) {
  369. Lives.add(1);
  370. presets.freeShipScore += presets.freeShipIncrement;
  371. }
  372. scoreDisplay.removeChildren();
  373. var scoreString = score + '';
  374. for (var i = 0; i < scoreString.length; i++) {
  375. var n = parseInt(scoreString[i], 10);
  376. scoreDisplay.addChild(numberGroup.children[n].clone());
  377. scoreDisplay.lastChild.position = [22 + i * 24, 22];
  378. }
  379. }
  380. };
  381. };
  382. var Lives = new function() {
  383. var currentLives;
  384. var shipPath = Ship.item.firstChild.clone();
  385. project.activeLayer.addChild(shipPath);
  386. shipPath.visible = false;
  387. Ship.visible = false;
  388. var group = new Group();
  389. return {
  390. initialize: function() {
  391. currentLives = presets.lives;
  392. this.display();
  393. },
  394. add: function() {
  395. currentLives++;
  396. this.display();
  397. },
  398. remove: function() {
  399. currentLives--;
  400. this.display();
  401. Ship.destroy();
  402. setTimeout(function() {
  403. if (currentLives == 0) {
  404. Game.over();
  405. } else {
  406. Ship.destroyed();
  407. }
  408. }, 1200);
  409. },
  410. display: function() {
  411. group.removeChildren();
  412. for (var i = 0;i < currentLives - 1; i++) {
  413. var copy = shipPath.clone();
  414. copy.rotate(-90);
  415. copy.visible = true;
  416. group.addChild(copy);
  417. copy.position = [22+ i * copy.bounds.width, 53];
  418. }
  419. }
  420. };
  421. };
  422. initialize();
  423. // Stop left and right keyboard events from propagating.
  424. function onKeyDown(event) {
  425. if (event.key == 'left' || event.key == 'right') {
  426. return false;
  427. }
  428. }
  429. function keepInView(item) {
  430. var position = item.position;
  431. var itemBounds = item.bounds;
  432. var bounds = view.bounds;
  433. if (itemBounds.left > bounds.width) {
  434. position.x = -item.bounds.width;
  435. }
  436. if (position.x < -itemBounds.width) {
  437. position.x = bounds.width;
  438. }
  439. if (itemBounds.top > view.size.height) {
  440. position.y = -itemBounds.height;
  441. }
  442. if (position.y < -itemBounds.height) {
  443. position.y = bounds.height + itemBounds.height / 2;
  444. }
  445. }
  446. </script>
  447. <style type="text/css">
  448. html,
  449. body {
  450. margin: 0;
  451. overflow: hidden;
  452. height: 100%;
  453. }
  454. canvas {
  455. width: 100%;
  456. height: 100%;
  457. }
  458. body {
  459. background: #000;
  460. font-family:Helvetica,Arial;
  461. }
  462. .footer {
  463. position:absolute;
  464. bottom:0;
  465. color:#ff0000;
  466. background-color: rgba(60,60,60,0.8);
  467. font-size:0.75em;
  468. padding:0.5em;
  469. color:#ddd;
  470. width: 100%
  471. }
  472. .footer a {
  473. color: #fff;
  474. font-weight:
  475. bold; text-decoration: none;
  476. border-bottom: 1px solid #555;
  477. }
  478. .footer b {
  479. background-color: #660000;
  480. padding-left: 0.25em;
  481. padding-right: 0.25em;
  482. }
  483. .gameover {
  484. display: none;
  485. position: absolute;
  486. left: 40%;
  487. top: 40%;
  488. color: #fff;
  489. background-color: rgba(60,60,60,0.8);
  490. padding: 32px;
  491. -moz-border-radius: 12px;
  492. -webkit-border-radius: 12px;
  493. border-radius: 12px;
  494. -moz-background-clip: padding;
  495. -webkit-background-clip: padding-box;
  496. background-clip: padding-box;
  497. }
  498. .gameover a {
  499. color: #fff;
  500. font-weight: bold;
  501. }
  502. #stats {
  503. position: absolute;
  504. left: auto !important;
  505. right: 0px;
  506. }
  507. </style>
  508. </head>
  509. <body>
  510. <canvas id="canvas" resize stats></canvas>
  511. <div id="footer" class="footer"><a href=#">Paperoids</a>. To Play: <b>&#8592;</b> and <b>&#8594;</b> to rotate. <b>&#8593;</b> for thrust. <b>Z</b> to fire. <b>F</b> to show FPS. Follow @<a href="http://twitter.com/#/hirmes">hirmes</a> for updates. Made with the amazing <a href="http://paperjs.org">Paper.js</a></div>
  512. <div id="gameover" class="gameover">Game Over. <a href="Paperoids.html">Play again</a>?</div>
  513. </body>
  514. </html>