Guest User

Untitled

a guest
Oct 21st, 2019
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************
  2. * lasers.js *
  3. *************
  4. *
  5. * Time to unleash the killer lasers! Each laser will kill you
  6. * unless you have the appropriate color. Too bad you can't
  7. * see which color corresponds to which laser!
  8. */
  9.  
  10. function getRandomInt(min, max) {
  11. return Math.floor(Math.random() * (max - min + 1)) + min;
  12. }
  13.  
  14. function startLevel(map) {
  15. map.placePlayer(0, 0);
  16. map.placeObject(map.getWidth()-1, map.getHeight()-1, 'exit');
  17. var player = map.getPlayer();
  18.  
  19. for (var i = 0; i < 25; i++) {
  20. var colors = ['red', 'yellow', 'teal'];
  21.  
  22. var startX = getRandomInt(0, 600);
  23. var startY = getRandomInt(0, 500);
  24. var angle = getRandomInt(0, 360);
  25. var length = getRandomInt(200, 300);
  26. var color = colors[i % 3];
  27. createLaser(startX, startY, angle, length, color);
  28. }
  29.  
  30. function createLaser(centerX, centerY, angleInDegrees, length, color) {
  31. var angleInRadians = angleInDegrees * Math.PI / 180;
  32.  
  33. var x1 = centerX - Math.cos(angleInRadians) * length / 2;
  34. var y1 = centerY + Math.sin(angleInRadians) * length / 2;
  35. var x2 = centerX + Math.cos(angleInRadians) * length / 2;
  36. var y2 = centerY - Math.sin(angleInRadians) * length / 2;
  37.  
  38. // map.createLine() creates a line with an effect when
  39. // the player moves over it, but doesn't display it
  40. map.createLine([x1, y1], [x2, y2], function (player) {
  41. if (player.getColor() != color) {
  42. player.killedBy('a ' + color + ' laser');
  43. }
  44. });
  45.  
  46. // using canvas to draw the line
  47. var ctx = map.getCanvasContext();
  48. ctx.beginPath();
  49. ctx.strokeStyle = color;
  50. ctx.lineWidth = 5;
  51. ctx.moveTo(x1, y1);
  52. ctx.lineTo(x2, y2);
  53. ctx.stroke();
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. player.setPhoneCallback(()=>player.setColor(['red', 'yellow', 'teal']
  62. [getRandomInt(0,2)]))
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69. function validateLevel(map) {
  70. map.validateExactlyXManyObjects(1, 'exit');
  71. map.validateAtLeastXLines(25);
  72. }
RAW Paste Data