• API
• FAQ
• Tools
• Archive
daily pastebin goal
46%
SHARE
TWEET

# Untitled

a guest Feb 21st, 2019 68 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.     player.setPhoneCallback(function () {
60.         var color = player.getColor();
61.         switch(color) {
62.             case 'red':
63.                 player.setColor('yellow');
64.                 break;
65.             case 'yellow':
66.                 player.setColor('teal');
67.                 break;
68.             case 'teal':
69.                 player.setColor('red');
70.                 break;
71.             default:
72.                 player.setColor('yellow');
73.                 break;
74.         }
75.     });
76.
77.
78.
79.
80.
81.
82. }
83.
84. function validateLevel(map) {
85.     map.validateExactlyXManyObjects(1, 'exit');
86.     map.validateAtLeastXLines(25);
87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top