Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  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. map.getPlayer().setPhoneCallback(function(){
  61.  
  62. if(player.getColor()=='teal'){
  63. player.setColor('red');
  64. }
  65. else if(player.getColor()=='red'){
  66. player.setColor('yellow');
  67. }
  68. else if(player.getColor()=='yellow'){
  69. player.setColor('teal');
  70. }
  71. else {
  72. player.setColor('teal');
  73. }
  74.  
  75. });
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
  83. function validateLevel(map) {
  84. map.validateExactlyXManyObjects(1, 'exit');
  85. map.validateAtLeastXLines(25);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement