BlueGit

timfa raytracer

import

timfa Tim Falken · pushed by timfa

Authored 2020-10-17 · 6 years ago

Hash
155b19f9c02ecb0121d8e537199cbd260795bee5
Parent
6a8df796
18 files changed +885 −0
images/bullet.gif added Binary file +0 −0

The contents of this binary file are not shown.

images/enemy1.png added Binary file +0 −0

The contents of this binary file are not shown.

images/enemy2.png added Binary file +0 −0

The contents of this binary file are not shown.

images/enemy3.png added Binary file +0 −0

The contents of this binary file are not shown.

images/ship.png added Binary file +0 −0

The contents of this binary file are not shown.

images/star.png added Binary file +0 −0

The contents of this binary file are not shown.

images/timerPlaceholder.png added Binary file +0 −0

The contents of this binary file are not shown.

index.html added +24 −0
@@ -0,0 +1,24 @@
1 <html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="js/GameWindow.js"></script>
8 <script src="js/Canvas.js"></script>
9 <script src="js/Vector.js"></script>
10 <script src="js/Levels.js"></script>
11 <script src="js/Tiles.js"></script>
12 <script src="js/Vector.js"></script>
13 <script src="js/GameInput.js"></script>
14 <script src="js/World.js"></script>
15 <script src="js/Player.js"></script>
16
17 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
18
19 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
20 Arrow keys to move, space to shoot!
21 </div>
22 </body>
23
24 </html>
@@ -0,0 +1,24 @@
1<html>
2
3 <head>
4 </head>
5
6 <body>
7 <script src="js/GameWindow.js"></script>
8 <script src="js/Canvas.js"></script>
9 <script src="js/Vector.js"></script>
10 <script src="js/Levels.js"></script>
11 <script src="js/Tiles.js"></script>
12 <script src="js/Vector.js"></script>
13 <script src="js/GameInput.js"></script>
14 <script src="js/World.js"></script>
15 <script src="js/Player.js"></script>
16
17 <script src="main.js"></script> <!-- Our own script, which will interact with the engine to make a game! -->
18
19 <div style="position: absolute; top: 90%;"> <!-- tiny block of text containing download link, you can delete these as I'm guessing you won't need them. -->
20 Arrow keys to move, space to shoot!
21 </div>
22 </body>
23
24</html>
js/Canvas.js added +47 −0
@@ -0,0 +1,47 @@
1 // Wrapper for some canvas drawing functions
2
3 var Canvas =
4 {
5 get canvas()
6 {
7 return GameWindow.canvas;
8 },
9
10 get context()
11 {
12 return GameWindow.canvasContext;
13 },
14
15 drawText: function(text, x, y, size, font, color)
16 {
17 this.context.font = font? size + 'px ' + font : size + 'px Arial';
18
19 this.context.fillStyle = color;
20 this.context.strokeStyle = color;
21 this.context.textAlign = "center";
22 this.context.textBaseline="middle";
23
24 this.context.fillText(text, x, y);
25 },
26
27 drawLine(xStart, yStart, xEnd, yEnd, color)
28 {
29 this.context.fillStyle = color;
30 this.context.strokeStyle = color;
31
32 this.context.beginPath();
33 this.context.moveTo(xStart, yStart);
34 this.context.lineTo(xEnd, yEnd);
35
36 this.context.stroke();
37 },
38
39 drawRect(xStart, yStart, xEnd, yEnd, color)
40 {
41 this.context.fillStyle = color;
42 this.context.strokeStyle = color;
43
44 this.context.beginPath();
45 this.context.fillRect(xStart, yStart, xEnd, yEnd);
46 }
47 }
@@ -0,0 +1,47 @@
1// Wrapper for some canvas drawing functions
2
3var Canvas =
4{
5 get canvas()
6 {
7 return GameWindow.canvas;
8 },
9
10 get context()
11 {
12 return GameWindow.canvasContext;
13 },
14
15 drawText: function(text, x, y, size, font, color)
16 {
17 this.context.font = font? size + 'px ' + font : size + 'px Arial';
18
19 this.context.fillStyle = color;
20 this.context.strokeStyle = color;
21 this.context.textAlign = "center";
22 this.context.textBaseline="middle";
23
24 this.context.fillText(text, x, y);
25 },
26
27 drawLine(xStart, yStart, xEnd, yEnd, color)
28 {
29 this.context.fillStyle = color;
30 this.context.strokeStyle = color;
31
32 this.context.beginPath();
33 this.context.moveTo(xStart, yStart);
34 this.context.lineTo(xEnd, yEnd);
35
36 this.context.stroke();
37 },
38
39 drawRect(xStart, yStart, xEnd, yEnd, color)
40 {
41 this.context.fillStyle = color;
42 this.context.strokeStyle = color;
43
44 this.context.beginPath();
45 this.context.fillRect(xStart, yStart, xEnd, yEnd);
46 }
47}
js/GameInput.js added +186 −0
@@ -0,0 +1,186 @@
1 var GameInput =
2 {
3 g_keyStatus: [],
4 g_pKeyStatus: [],
5
6 g_pressKeyStatus: [],
7 g_ppressKeyStatus: [],
8
9 g_pressBlacklist: [],
10
11 g_mouseDown: false,
12 g_mouseDownThisFrame: false,
13
14 g_altMouseDown: false,
15 g_altMouseDownThisFrame: false,
16
17 g_middleMouseDown: false,
18 g_middleMouseDownThisFrame: false,
19
20 trueMousePosition: new Vector(0, 0),
21 mousePosition: new Vector(0, 0),
22 g_prevMousePosition: new Vector(0, 0),
23 mouseDelta: new Vector(0, 0),
24
25 MouseButtons: {
26 Left: 0,
27 Middle: 1,
28 Right: 2
29 },
30
31 g_init: function()
32 {
33 document.onkeydown = GameInput.g_setKeyStatus;
34 document.onkeyup = GameInput.g_setKeyStatus;
35
36 document.oncontextmenu = function(){return false;}
37
38 document.onmousedown = function(e)
39 {
40 switch(e.button)
41 {
42 case GameInput.MouseButtons.Left: GameInput.g_mouseDownThisFrame = true; break;
43 case GameInput.MouseButtons.Middle: GameInput.g_middleMouseDownThisFrame = true; break;
44 case GameInput.MouseButtons.Right: GameInput.g_altMouseDownThisFrame = true; break;
45 }
46 }
47
48 document.onmouseup = function(e)
49 {
50 switch(e.button)
51 {
52 case GameInput.MouseButtons.Left:
53 GameInput.g_mouseDownThisFrame = false;
54 GameInput.g_mouseDown = false;
55 break;
56 case GameInput.MouseButtons.Middle:
57 GameInput.g_middleMouseDownThisFrame = false;
58 GameInput.g_middleMouseDown = false;
59 break;
60 case GameInput.MouseButtons.Right:
61 GameInput.g_altMouseDownThisFrame = false;
62 GameInput.g_altMouseDown = false;
63 break;
64 }
65 }
66
67 document.onmousemove = function(e)
68 {
69 GameInput.trueMousePosition.x = e.x + GameWindow.x;
70 GameInput.trueMousePosition.y = e.y + GameWindow.y;
71
72 Player.pitch += e.movementY * 0.01;
73 Player.rotation += e.movementX * 0.001;
74
75 if(Player.pitch > Math.PI)
76 Player.pitch = Math.PI;
77
78
79 if(Player.pitch < -Math.PI)
80 Player.pitch = -Math.PI;
81 }
82
83 var el = document.getElementsByTagName("canvas")[0];
84 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
85 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
86 el.addEventListener("touchcancel", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
87 el.addEventListener("touchmove", function(){return false}, false);
88 },
89
90 g_setKeyStatus: function(e)
91 {
92 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
93
94 if(!GameInput.g_pressBlacklist[e.which])
95 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
96
97 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
98 },
99
100 g_updateKeys: function()
101 {
102 GameInput.mousePosition = GameInput.trueMousePosition;
103
104 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
105 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
106 GameInput.g_pressKeyStatus = [];
107
108 GameInput.mouseDelta.x = GameInput.trueMousePosition.x - GameInput.g_prevMousePosition.x;
109 GameInput.mouseDelta.y = GameInput.trueMousePosition.y - GameInput.g_prevMousePosition.y;
110
111 GameInput.g_prevMousePosition.x = GameInput.trueMousePosition.x;
112 GameInput.g_prevMousePosition.y = GameInput.trueMousePosition.y;
113
114 if (GameInput.g_mouseDown && GameInput.g_mouseDownThisFrame)
115 {
116 GameInput.g_mouseDownThisFrame = false;
117 }
118
119 if(GameInput.g_mouseDownThisFrame && !GameInput.g_mouseDown)
120 {
121 GameInput.g_mouseDown = true;
122 }
123
124 if (GameInput.g_altMouseDown && GameInput.g_altMouseDownThisFrame)
125 {
126 GameInput.g_altMouseDownThisFrame = false;
127 }
128
129 if(GameInput.g_altMouseDownThisFrame && !GameInput.g_altMouseDown)
130 {
131 GameInput.g_altMouseDown = true;
132 }
133
134 if (GameInput.g_middleMouseDown && GameInput.g_middleMouseDownThisFrame)
135 {
136 GameInput.g_middleMouseDownThisFrame = false;
137 }
138
139 if(GameInput.g_middleMouseDownThisFrame && !GameInput.g_middleMouseDown)
140 {
141 GameInput.g_midleMouseDown = true;
142 }
143 },
144
145
146 isPressed: function(keyCode)
147 {
148 if(GameInput.g_ppressKeyStatus[keyCode] == null)
149 return false;
150 else
151 return GameInput.g_ppressKeyStatus[keyCode];
152 },
153
154
155 isHeld: function(keyCode)
156 {
157 if(GameInput.g_pKeyStatus[keyCode] == null)
158 return false;
159 else
160 return GameInput.g_pKeyStatus[keyCode];
161 },
162
163 mousePressed: function(btn)
164 {
165 switch(btn)
166 {
167 case GameInput.MouseButtons.Left: return GameInput.g_mouseDownThisFrame;
168 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDownThisFrame;
169 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDownThisFrame;
170 }
171
172 return GameInput.g_mouseDownThisFrame;
173 },
174
175 mouseHeld: function(btn)
176 {
177 switch(btn)
178 {
179 case GameInput.MouseButtons.Left: return GameInput.g_mouseDown;
180 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDown;
181 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDown;
182 }
183
184 return GameInput.g_mouseDown;
185 }
186 }//
@@ -0,0 +1,186 @@
1var GameInput =
2{
3 g_keyStatus: [],
4 g_pKeyStatus: [],
5
6 g_pressKeyStatus: [],
7 g_ppressKeyStatus: [],
8
9 g_pressBlacklist: [],
10
11 g_mouseDown: false,
12 g_mouseDownThisFrame: false,
13
14 g_altMouseDown: false,
15 g_altMouseDownThisFrame: false,
16
17 g_middleMouseDown: false,
18 g_middleMouseDownThisFrame: false,
19
20 trueMousePosition: new Vector(0, 0),
21 mousePosition: new Vector(0, 0),
22 g_prevMousePosition: new Vector(0, 0),
23 mouseDelta: new Vector(0, 0),
24
25 MouseButtons: {
26 Left: 0,
27 Middle: 1,
28 Right: 2
29 },
30
31 g_init: function()
32 {
33 document.onkeydown = GameInput.g_setKeyStatus;
34 document.onkeyup = GameInput.g_setKeyStatus;
35
36 document.oncontextmenu = function(){return false;}
37
38 document.onmousedown = function(e)
39 {
40 switch(e.button)
41 {
42 case GameInput.MouseButtons.Left: GameInput.g_mouseDownThisFrame = true; break;
43 case GameInput.MouseButtons.Middle: GameInput.g_middleMouseDownThisFrame = true; break;
44 case GameInput.MouseButtons.Right: GameInput.g_altMouseDownThisFrame = true; break;
45 }
46 }
47
48 document.onmouseup = function(e)
49 {
50 switch(e.button)
51 {
52 case GameInput.MouseButtons.Left:
53 GameInput.g_mouseDownThisFrame = false;
54 GameInput.g_mouseDown = false;
55 break;
56 case GameInput.MouseButtons.Middle:
57 GameInput.g_middleMouseDownThisFrame = false;
58 GameInput.g_middleMouseDown = false;
59 break;
60 case GameInput.MouseButtons.Right:
61 GameInput.g_altMouseDownThisFrame = false;
62 GameInput.g_altMouseDown = false;
63 break;
64 }
65 }
66
67 document.onmousemove = function(e)
68 {
69 GameInput.trueMousePosition.x = e.x + GameWindow.x;
70 GameInput.trueMousePosition.y = e.y + GameWindow.y;
71
72 Player.pitch += e.movementY * 0.01;
73 Player.rotation += e.movementX * 0.001;
74
75 if(Player.pitch > Math.PI)
76 Player.pitch = Math.PI;
77
78
79 if(Player.pitch < -Math.PI)
80 Player.pitch = -Math.PI;
81 }
82
83 var el = document.getElementsByTagName("canvas")[0];
84 el.addEventListener("touchstart", function(){GameInput.g_mouseDownThisFrame = true;}, false);
85 el.addEventListener("touchend", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
86 el.addEventListener("touchcancel", function(){GameInput.g_mouseDownThisFrame = false;GameInput.g_mouseDown = false;}, false);
87 el.addEventListener("touchmove", function(){return false}, false);
88 },
89
90 g_setKeyStatus: function(e)
91 {
92 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
93
94 if(!GameInput.g_pressBlacklist[e.which])
95 GameInput.g_pressKeyStatus[e.which] = (e.type == "keydown");
96
97 GameInput.g_pressBlacklist[e.which] = (e.type == "keydown"); //only allow single presses if key has been released once
98 },
99
100 g_updateKeys: function()
101 {
102 GameInput.mousePosition = GameInput.trueMousePosition;
103
104 GameInput.g_pKeyStatus = GameInput.g_keyStatus;
105 GameInput.g_ppressKeyStatus = GameInput.g_pressKeyStatus;
106 GameInput.g_pressKeyStatus = [];
107
108 GameInput.mouseDelta.x = GameInput.trueMousePosition.x - GameInput.g_prevMousePosition.x;
109 GameInput.mouseDelta.y = GameInput.trueMousePosition.y - GameInput.g_prevMousePosition.y;
110
111 GameInput.g_prevMousePosition.x = GameInput.trueMousePosition.x;
112 GameInput.g_prevMousePosition.y = GameInput.trueMousePosition.y;
113
114 if (GameInput.g_mouseDown && GameInput.g_mouseDownThisFrame)
115 {
116 GameInput.g_mouseDownThisFrame = false;
117 }
118
119 if(GameInput.g_mouseDownThisFrame && !GameInput.g_mouseDown)
120 {
121 GameInput.g_mouseDown = true;
122 }
123
124 if (GameInput.g_altMouseDown && GameInput.g_altMouseDownThisFrame)
125 {
126 GameInput.g_altMouseDownThisFrame = false;
127 }
128
129 if(GameInput.g_altMouseDownThisFrame && !GameInput.g_altMouseDown)
130 {
131 GameInput.g_altMouseDown = true;
132 }
133
134 if (GameInput.g_middleMouseDown && GameInput.g_middleMouseDownThisFrame)
135 {
136 GameInput.g_middleMouseDownThisFrame = false;
137 }
138
139 if(GameInput.g_middleMouseDownThisFrame && !GameInput.g_middleMouseDown)
140 {
141 GameInput.g_midleMouseDown = true;
142 }
143 },
144
145
146 isPressed: function(keyCode)
147 {
148 if(GameInput.g_ppressKeyStatus[keyCode] == null)
149 return false;
150 else
151 return GameInput.g_ppressKeyStatus[keyCode];
152 },
153
154
155 isHeld: function(keyCode)
156 {
157 if(GameInput.g_pKeyStatus[keyCode] == null)
158 return false;
159 else
160 return GameInput.g_pKeyStatus[keyCode];
161 },
162
163 mousePressed: function(btn)
164 {
165 switch(btn)
166 {
167 case GameInput.MouseButtons.Left: return GameInput.g_mouseDownThisFrame;
168 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDownThisFrame;
169 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDownThisFrame;
170 }
171
172 return GameInput.g_mouseDownThisFrame;
173 },
174
175 mouseHeld: function(btn)
176 {
177 switch(btn)
178 {
179 case GameInput.MouseButtons.Left: return GameInput.g_mouseDown;
180 case GameInput.MouseButtons.Middle: return GameInput.g_middleMouseDown;
181 case GameInput.MouseButtons.Right: return GameInput.g_altMouseDown;
182 }
183
184 return GameInput.g_mouseDown;
185 }
186}//
js/GameWindow.js added +68 −0
@@ -0,0 +1,68 @@
1 var GameWindow =
2 {
3 width: 0,
4
5 height: 0,
6
7 g_width: 0,
8 g_height: 0,
9
10 fullscreen: false,
11
12 backgroundColor:
13 {
14 r: 0,
15 g: 0,
16 b: 0
17 },
18
19 windowElement: null,
20 elementID: "GameBuilder_Window",
21
22 init: function(x, y, width, height)
23 {
24 GameWindow.x = x;
25 GameWindow.y = y;
26
27 GameWindow.width = width;
28 GameWindow.height = height;
29
30 GameWindow.g_width = width;
31 GameWindow.g_height = height;
32
33 var body = document.body;
34 body.insertAdjacentHTML('beforeend', '<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
35
36 //$("body").append('<div id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; width: ' + GameWindow.width + 'px; height: ' + GameWindow.height + 'px; overflow: hidden; border: 1px solid black;"></div>');
37 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
38
39 GameWindow.canvas = document.getElementById(GameWindow.elementID);
40
41 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
42
43 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
44 },
45
46 redraw: function()
47 {
48 if(GameWindow.fullscreen)
49 {
50 GameWindow.width = window.innerWidth - 2;
51 GameWindow.height = window.innerHeight - 2;
52 }
53 else
54 {
55 GameWindow.width = GameWindow.g_width;
56 GameWindow.height = GameWindow.g_height;
57 }
58
59 GameWindow.canvas.width = GameWindow.width;
60 GameWindow.canvas.height = GameWindow.height;
61
62 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
64
65 Player.draw3D();
66 World.drawWorld2D();
67 }
68 }
@@ -0,0 +1,68 @@
1var GameWindow =
2{
3 width: 0,
4
5 height: 0,
6
7 g_width: 0,
8 g_height: 0,
9
10 fullscreen: false,
11
12 backgroundColor:
13 {
14 r: 0,
15 g: 0,
16 b: 0
17 },
18
19 windowElement: null,
20 elementID: "GameBuilder_Window",
21
22 init: function(x, y, width, height)
23 {
24 GameWindow.x = x;
25 GameWindow.y = y;
26
27 GameWindow.width = width;
28 GameWindow.height = height;
29
30 GameWindow.g_width = width;
31 GameWindow.g_height = height;
32
33 var body = document.body;
34 body.insertAdjacentHTML('beforeend', '<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
35
36 //$("body").append('<div id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; width: ' + GameWindow.width + 'px; height: ' + GameWindow.height + 'px; overflow: hidden; border: 1px solid black;"></div>');
37 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
38
39 GameWindow.canvas = document.getElementById(GameWindow.elementID);
40
41 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
42
43 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
44 },
45
46 redraw: function()
47 {
48 if(GameWindow.fullscreen)
49 {
50 GameWindow.width = window.innerWidth - 2;
51 GameWindow.height = window.innerHeight - 2;
52 }
53 else
54 {
55 GameWindow.width = GameWindow.g_width;
56 GameWindow.height = GameWindow.g_height;
57 }
58
59 GameWindow.canvas.width = GameWindow.width;
60 GameWindow.canvas.height = GameWindow.height;
61
62 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
64
65 Player.draw3D();
66 World.drawWorld2D();
67 }
68}
js/Levels.js added +23 −0
@@ -0,0 +1,23 @@
1 var Levels =
2 [
3 {
4 name: "Level 1",
5 width: 10,
6 height: 10,
7 spawnX: 1,
8 spawnY: 1,
9 tiles:
10 [
11 1,1,1,1,1,1,1,1,1,1,
12 1,0,0,0,0,0,0,0,0,1,
13 1,0,1,1,0,0,3,3,0,1,
14 1,0,0,1,0,0,0,0,0,1,
15 1,0,0,0,0,1,0,1,0,1,
16 1,0,0,0,0,1,0,1,0,1,
17 1,0,0,0,0,1,2,1,0,1,
18 1,0,1,0,0,1,3,1,0,1,
19 1,0,0,0,0,0,0,0,0,1,
20 1,1,1,1,1,1,1,1,1,1,
21 ]
22 }
23 ]
@@ -0,0 +1,23 @@
1var Levels =
2[
3 {
4 name: "Level 1",
5 width: 10,
6 height: 10,
7 spawnX: 1,
8 spawnY: 1,
9 tiles:
10 [
11 1,1,1,1,1,1,1,1,1,1,
12 1,0,0,0,0,0,0,0,0,1,
13 1,0,1,1,0,0,3,3,0,1,
14 1,0,0,1,0,0,0,0,0,1,
15 1,0,0,0,0,1,0,1,0,1,
16 1,0,0,0,0,1,0,1,0,1,
17 1,0,0,0,0,1,2,1,0,1,
18 1,0,1,0,0,1,3,1,0,1,
19 1,0,0,0,0,0,0,0,0,1,
20 1,1,1,1,1,1,1,1,1,1,
21 ]
22 }
23]
js/Player.js added +160 −0
@@ -0,0 +1,160 @@
1 var Player =
2 {
3 x: 0,
4 y: 0,
5 rotation: 0,
6 pitch: 0,
7 horizontalResolution: 200,
8
9 update: function(deltaTime)
10 {
11 if(GameInput.isHeld(37) || GameInput.isHeld(65)) // left arrow
12 {
13 var rot = this.rotation - Math.PI/2;
14 if(rot < 0)
15 rot += Math.PI*2;
16 if(rot > Math.PI*2)
17 rot -= Math.PI*2
18
19 var forward = Vector.fromAngle(rot);
20
21 if(World.rayCast(this.x, this.y, rot).distance > 5)
22 {
23 this.x += forward.x * deltaTime * 60;
24 this.y += forward.y * deltaTime * 60;
25 }
26 }
27
28 if(GameInput.isHeld(39) || GameInput.isHeld(68)) // right arrow
29 {
30 var rot = this.rotation + Math.PI/2;
31 if(rot < 0)
32 rot += Math.PI*2;
33 if(rot > Math.PI*2)
34 rot -= Math.PI*2
35
36 var forward = Vector.fromAngle(rot);
37
38 if(World.rayCast(this.x, this.y, rot).distance > 5)
39 {
40 this.x += forward.x * deltaTime * 60;
41 this.y += forward.y * deltaTime * 60;
42 }
43 }
44
45 //this.pitch += //((GameInput.mousePosition.y - (GameWindow.height / 2)) / (GameWindow.height/2)) * Math.PI;
46
47 if(this.rotation > Math.PI * 2)
48 this.rotation -= Math.PI * 2;
49
50 if(this.rotation < 0)
51 this.rotation += Math.PI * 2;
52
53 if(GameInput.isHeld(38) || GameInput.isHeld(87)) // up arrow
54 {
55 var forward = Vector.fromAngle(this.rotation);
56
57 if(World.rayCast(this.x, this.y, this.rotation).distance > 5)
58 {
59 this.x += forward.x * deltaTime * 60;
60 this.y += forward.y * deltaTime * 60;
61 }
62 }
63
64 if(GameInput.isHeld(40) || GameInput.isHeld(83)) // down arrow
65 {
66 var forward = Vector.fromAngle(this.rotation);
67
68 var rot = this.rotation + Math.PI;
69 if(rot < 0)
70 rot += Math.PI*2;
71 if(rot > Math.PI*2)
72 rot -= Math.PI*2
73
74 if(World.rayCast(this.x, this.y, rot).distance > 5)
75 {
76 this.x -= forward.x * deltaTime * 40;
77 this.y -= forward.y * deltaTime * 40;
78 }
79 }
80
81 if(GameInput.isHeld(188)) // < arrow
82 {
83 this.horizontalResolution-= this.horizontalResolution * 0.1 * deltaTime;
84 if(this.horizontalResolution < 30)
85 this.horizontalResolution = 30;
86 }
87 if(GameInput.isHeld(190)) // > arrow
88 {
89 this.horizontalResolution+= this.horizontalResolution * 0.1 * deltaTime;
90 if(this.horizontalResolution > GameWindow.width)
91 this.horizontalResolution = GameWindow.width;
92 }
93 },
94
95 draw: function()
96 {
97 var forward = Vector.fromAngle(this.rotation).stretch(25);
98
99 Canvas.drawRect(this.x - 10, this.y - 10, 20, 20, 0xff0000);
100 Canvas.drawLine(this.x, this.y, this.x + forward.x, this.y + forward.y, 0xff00ff);
101 },
102
103 draw3D: function()
104 {
105 var rad = Math.PI / (this.horizontalResolution*2);
106
107 for(var i = 0; i < this.horizontalResolution; i++)
108 {
109 var rot = this.rotation - rad*(this.horizontalResolution / 2) + rad*i;
110
111 if(rot < 0)
112 rot += Math.PI * 2;
113 if(rot > Math.PI * 2)
114 rot -= Math.PI * 2;
115
116 var rayHit = World.rayCast(this.x, this.y, rot);
117
118 var color = {
119 R: rayHit.tile.Color.R,
120 G: rayHit.tile.Color.G,
121 B: rayHit.tile.Color.B
122 }
123
124 switch(rayHit.dir)
125 {
126 case "north":
127 break;
128 case "east":
129 color.R *= 0.9;
130 color.G *= 0.9;
131 color.B *= 0.9;
132 break;
133 case "west":
134 color.R *= 0.8;
135 color.G *= 0.8;
136 color.B *= 0.8;
137 break;
138 case "south":
139 color.R *= 0.7;
140 color.G *= 0.7;
141 color.B *= 0.7;
142 break;
143 }
144
145 Canvas.drawLine(this.x, this.y, rayHit.endX, rayHit.endY, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
146
147 var deltaAngle = this.rotation - rot;
148 rayHit.distance *= Math.cos(deltaAngle);
149
150 var xLine = i * (GameWindow.width / this.horizontalResolution);
151 var widthLine = (GameWindow.width / this.horizontalResolution) + 1;
152
153 var height = (World.size * GameWindow.height) / rayHit.distance;
154
155 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
156
157 Canvas.drawRect(xLine, yLine, widthLine, height);
158 }
159 }
160 }
@@ -0,0 +1,160 @@
1var Player =
2{
3 x: 0,
4 y: 0,
5 rotation: 0,
6 pitch: 0,
7 horizontalResolution: 200,
8
9 update: function(deltaTime)
10 {
11 if(GameInput.isHeld(37) || GameInput.isHeld(65)) // left arrow
12 {
13 var rot = this.rotation - Math.PI/2;
14 if(rot < 0)
15 rot += Math.PI*2;
16 if(rot > Math.PI*2)
17 rot -= Math.PI*2
18
19 var forward = Vector.fromAngle(rot);
20
21 if(World.rayCast(this.x, this.y, rot).distance > 5)
22 {
23 this.x += forward.x * deltaTime * 60;
24 this.y += forward.y * deltaTime * 60;
25 }
26 }
27
28 if(GameInput.isHeld(39) || GameInput.isHeld(68)) // right arrow
29 {
30 var rot = this.rotation + Math.PI/2;
31 if(rot < 0)
32 rot += Math.PI*2;
33 if(rot > Math.PI*2)
34 rot -= Math.PI*2
35
36 var forward = Vector.fromAngle(rot);
37
38 if(World.rayCast(this.x, this.y, rot).distance > 5)
39 {
40 this.x += forward.x * deltaTime * 60;
41 this.y += forward.y * deltaTime * 60;
42 }
43 }
44
45 //this.pitch += //((GameInput.mousePosition.y - (GameWindow.height / 2)) / (GameWindow.height/2)) * Math.PI;
46
47 if(this.rotation > Math.PI * 2)
48 this.rotation -= Math.PI * 2;
49
50 if(this.rotation < 0)
51 this.rotation += Math.PI * 2;
52
53 if(GameInput.isHeld(38) || GameInput.isHeld(87)) // up arrow
54 {
55 var forward = Vector.fromAngle(this.rotation);
56
57 if(World.rayCast(this.x, this.y, this.rotation).distance > 5)
58 {
59 this.x += forward.x * deltaTime * 60;
60 this.y += forward.y * deltaTime * 60;
61 }
62 }
63
64 if(GameInput.isHeld(40) || GameInput.isHeld(83)) // down arrow
65 {
66 var forward = Vector.fromAngle(this.rotation);
67
68 var rot = this.rotation + Math.PI;
69 if(rot < 0)
70 rot += Math.PI*2;
71 if(rot > Math.PI*2)
72 rot -= Math.PI*2
73
74 if(World.rayCast(this.x, this.y, rot).distance > 5)
75 {
76 this.x -= forward.x * deltaTime * 40;
77 this.y -= forward.y * deltaTime * 40;
78 }
79 }
80
81 if(GameInput.isHeld(188)) // < arrow
82 {
83 this.horizontalResolution-= this.horizontalResolution * 0.1 * deltaTime;
84 if(this.horizontalResolution < 30)
85 this.horizontalResolution = 30;
86 }
87 if(GameInput.isHeld(190)) // > arrow
88 {
89 this.horizontalResolution+= this.horizontalResolution * 0.1 * deltaTime;
90 if(this.horizontalResolution > GameWindow.width)
91 this.horizontalResolution = GameWindow.width;
92 }
93 },
94
95 draw: function()
96 {
97 var forward = Vector.fromAngle(this.rotation).stretch(25);
98
99 Canvas.drawRect(this.x - 10, this.y - 10, 20, 20, 0xff0000);
100 Canvas.drawLine(this.x, this.y, this.x + forward.x, this.y + forward.y, 0xff00ff);
101 },
102
103 draw3D: function()
104 {
105 var rad = Math.PI / (this.horizontalResolution*2);
106
107 for(var i = 0; i < this.horizontalResolution; i++)
108 {
109 var rot = this.rotation - rad*(this.horizontalResolution / 2) + rad*i;
110
111 if(rot < 0)
112 rot += Math.PI * 2;
113 if(rot > Math.PI * 2)
114 rot -= Math.PI * 2;
115
116 var rayHit = World.rayCast(this.x, this.y, rot);
117
118 var color = {
119 R: rayHit.tile.Color.R,
120 G: rayHit.tile.Color.G,
121 B: rayHit.tile.Color.B
122 }
123
124 switch(rayHit.dir)
125 {
126 case "north":
127 break;
128 case "east":
129 color.R *= 0.9;
130 color.G *= 0.9;
131 color.B *= 0.9;
132 break;
133 case "west":
134 color.R *= 0.8;
135 color.G *= 0.8;
136 color.B *= 0.8;
137 break;
138 case "south":
139 color.R *= 0.7;
140 color.G *= 0.7;
141 color.B *= 0.7;
142 break;
143 }
144
145 Canvas.drawLine(this.x, this.y, rayHit.endX, rayHit.endY, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
146
147 var deltaAngle = this.rotation - rot;
148 rayHit.distance *= Math.cos(deltaAngle);
149
150 var xLine = i * (GameWindow.width / this.horizontalResolution);
151 var widthLine = (GameWindow.width / this.horizontalResolution) + 1;
152
153 var height = (World.size * GameWindow.height) / rayHit.distance;
154
155 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
156
157 Canvas.drawRect(xLine, yLine, widthLine, height);
158 }
159 }
160}
js/Tiles.js added +51 −0
@@ -0,0 +1,51 @@
1 var Tiles =
2 {
3 Nothing:
4 {
5 ID: 0,
6 Color: {
7 R: 255,
8 G: 255,
9 B: 255
10 },
11 Solid: false
12 },
13 Stone:
14 {
15 ID: 1,
16 Color: {
17 R: 155,
18 G: 155,
19 B: 155
20 },
21 Solid: true
22 },
23 Finish:
24 {
25 ID: 2,
26 Color: {
27 R: 255,
28 G: 255,
29 B: 0
30 },
31 Solid: false
32 },
33 Wood:
34 {
35 ID: 3,
36 Color: {
37 R: 168,
38 G: 102,
39 B: 50
40 },
41 Solid: true
42 },
43 findByID(id)
44 {
45 for (const [key, value] of Object.entries(Tiles))
46 {
47 if(value.ID == id)
48 return value;
49 }
50 }
51 }
@@ -0,0 +1,51 @@
1var Tiles =
2{
3 Nothing:
4 {
5 ID: 0,
6 Color: {
7 R: 255,
8 G: 255,
9 B: 255
10 },
11 Solid: false
12 },
13 Stone:
14 {
15 ID: 1,
16 Color: {
17 R: 155,
18 G: 155,
19 B: 155
20 },
21 Solid: true
22 },
23 Finish:
24 {
25 ID: 2,
26 Color: {
27 R: 255,
28 G: 255,
29 B: 0
30 },
31 Solid: false
32 },
33 Wood:
34 {
35 ID: 3,
36 Color: {
37 R: 168,
38 G: 102,
39 B: 50
40 },
41 Solid: true
42 },
43 findByID(id)
44 {
45 for (const [key, value] of Object.entries(Tiles))
46 {
47 if(value.ID == id)
48 return value;
49 }
50 }
51}
js/Vector.js added +130 −0
@@ -0,0 +1,130 @@
1 function Vector(x, y)
2 {
3
4 this.x = x;
5
6 this.y = y;
7
8
9 this.length = function()
10 {
11 return Math.sqrt((this.x * this.x) + (this.y * this.y));
12 }
13
14
15 this.toAngle = function()
16 {
17 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
18 }
19
20 this.normalize = function()
21 {
22 var newVector = this.getCopy();
23
24 if(newVector.length() != 0)
25 {
26 var length = newVector.length();
27 newVector.x /= length;
28 newVector.y /= length;
29 }
30
31 return newVector;
32 }
33
34
35 this.lerpTo = function(otherVector, t)
36 {
37 var delta = otherVector.subtract(this);
38
39 return this.add(delta.stretch(t));
40 }
41
42
43 this.distanceTo = function(otherVector)
44 {
45 var dX = this.x - otherVector.x;
46 var dY = this.y - otherVector.y;
47
48 return new Vector(dX, dY).length();
49 }
50
51
52 this.add = function(otherVector)
53 {
54 var newVector = this.getCopy();
55
56 newVector.x += otherVector.x;
57 newVector.y += otherVector.y;
58
59 return newVector;
60 }
61
62
63 this.subtract = function(otherVector)
64 {
65 var newVector = this.getCopy();
66
67 newVector.x -= otherVector.x;
68 newVector.y -= otherVector.y;
69
70 return newVector;
71 }
72
73
74 this.multiply = function(otherVector)
75 {
76 var newVector = this.getCopy();
77
78 newVector.x *= otherVector.x;
79 newVector.y *= otherVector.y;
80
81 return newVector;
82 }
83
84
85 this.divide = function(otherVector)
86 {
87 var newVector = this.getCopy();
88
89 newVector.x /= otherVector.x;
90 newVector.y /= otherVector.y;
91
92 return newVector;
93 }
94
95
96 this.stretch = function(length)
97 {
98 var newVector = this.getCopy();
99
100 newVector.x *= length;
101 newVector.y *= length;
102
103 return newVector;
104 }
105
106 this.deltaAngle = function(otherVector)
107 {
108 var a = this.toAngle() * (Math.PI / 180);
109 var b = otherVector.toAngle() * (Math.PI / 180);
110
111 return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI)
112 }
113
114 this.cross = function(otherVector)
115 {
116 return this.x * otherVector.y - this.y * otherVector.x;
117 }
118
119
120 this.getCopy = function()
121 {
122 return new Vector(this.x, this.y);
123 }
124 }
125
126 Vector.fromAngle = function(radians)
127 {
128 radians += Math.PI / 2;
129 return new Vector(Math.sin(radians), -Math.cos(radians));
130 }
@@ -0,0 +1,130 @@
1function Vector(x, y)
2{
3
4 this.x = x;
5
6 this.y = y;
7
8
9 this.length = function()
10 {
11 return Math.sqrt((this.x * this.x) + (this.y * this.y));
12 }
13
14
15 this.toAngle = function()
16 {
17 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
18 }
19
20 this.normalize = function()
21 {
22 var newVector = this.getCopy();
23
24 if(newVector.length() != 0)
25 {
26 var length = newVector.length();
27 newVector.x /= length;
28 newVector.y /= length;
29 }
30
31 return newVector;
32 }
33
34
35 this.lerpTo = function(otherVector, t)
36 {
37 var delta = otherVector.subtract(this);
38
39 return this.add(delta.stretch(t));
40 }
41
42
43 this.distanceTo = function(otherVector)
44 {
45 var dX = this.x - otherVector.x;
46 var dY = this.y - otherVector.y;
47
48 return new Vector(dX, dY).length();
49 }
50
51
52 this.add = function(otherVector)
53 {
54 var newVector = this.getCopy();
55
56 newVector.x += otherVector.x;
57 newVector.y += otherVector.y;
58
59 return newVector;
60 }
61
62
63 this.subtract = function(otherVector)
64 {
65 var newVector = this.getCopy();
66
67 newVector.x -= otherVector.x;
68 newVector.y -= otherVector.y;
69
70 return newVector;
71 }
72
73
74 this.multiply = function(otherVector)
75 {
76 var newVector = this.getCopy();
77
78 newVector.x *= otherVector.x;
79 newVector.y *= otherVector.y;
80
81 return newVector;
82 }
83
84
85 this.divide = function(otherVector)
86 {
87 var newVector = this.getCopy();
88
89 newVector.x /= otherVector.x;
90 newVector.y /= otherVector.y;
91
92 return newVector;
93 }
94
95
96 this.stretch = function(length)
97 {
98 var newVector = this.getCopy();
99
100 newVector.x *= length;
101 newVector.y *= length;
102
103 return newVector;
104 }
105
106 this.deltaAngle = function(otherVector)
107 {
108 var a = this.toAngle() * (Math.PI / 180);
109 var b = otherVector.toAngle() * (Math.PI / 180);
110
111 return Math.atan2(Math.sin(a - b), Math.cos(a - b)) * (180 / Math.PI)
112 }
113
114 this.cross = function(otherVector)
115 {
116 return this.x * otherVector.y - this.y * otherVector.x;
117 }
118
119
120 this.getCopy = function()
121 {
122 return new Vector(this.x, this.y);
123 }
124}
125
126Vector.fromAngle = function(radians)
127{
128 radians += Math.PI / 2;
129 return new Vector(Math.sin(radians), -Math.cos(radians));
130}
js/World.js added +159 −0
@@ -0,0 +1,159 @@
1 var World =
2 {
3 size: 32,
4
5 level: null,
6
7 loadLevel: function(index)
8 {
9 this.level = Levels[index];
10
11 Player.x = this.level.spawnX * this.size + this.size/2;
12 Player.y = this.level.spawnY * this.size + this.size/2;
13 },
14
15 drawWorld2D: function()
16 {
17 for(var x = 0; x < this.level.width; x++)
18 {
19 for(var y = 0; y < this.level.height; y++)
20 {
21 cell = this.level.tiles[x + (y * this.level.width)];
22
23 var color = Tiles.findByID(cell).Color;
24 Canvas.drawRect(x * this.size, y * this.size, this.size, this.size, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
25 }
26 }
27
28 Player.draw();
29 },
30
31 rayCast: function(x, y, radian)
32 {
33 var maxDist = (this.level.width + this.level.height);
34 var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit;
35
36 var hDir = "north";
37
38 distDone = 0;
39
40 //horizontal
41 var aTan = -1/Math.tan(radian);
42 if(radian > Math.PI)
43 {
44 hrayY = (Math.floor(y / this.size) * this.size) - 0.01;
45 hrayX = (y - hrayY) * aTan + x;
46 hoffY = -this.size;
47 hoffX = -hoffY*aTan;
48 }
49 else if(radian < Math.PI)
50 {
51 hrayY = (Math.floor(y / this.size) * this.size) + this.size;
52 hrayX = (y - hrayY) * aTan + x;
53 hoffY = this.size;
54 hoffX = -hoffY*aTan;
55 hDir = "south";
56 }
57 else
58 {
59 hrayX = x;
60 hrayY = y;
61 distDone = maxDist;
62 }
63
64 while (distDone < maxDist)
65 {
66 hmapX = Math.floor(hrayX / this.size);
67 hmapY = Math.floor(hrayY / this.size);
68 var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width];
69
70 htileHit = Tiles.findByID(htileHitId);
71
72 if(htileHit && htileHit.Solid)
73 {
74 distDone = maxDist;
75 }
76 else
77 {
78 hrayX += hoffX;
79 hrayY += hoffY;
80 distDone += 1;
81 }
82 }
83
84 var hResult =
85 {
86 tile: htileHit,
87 endX: hrayX,
88 endY: hrayY,
89 dir: hDir
90 }
91
92 //vertical
93 var rayX, rayY, offX, offY, distDone, mapX, mapY, tileHit;
94 var vDir = "east";
95 var nTan = -Math.tan(radian);
96 distDone = 0;
97
98 if(radian > Math.PI / 2 && radian < Math.PI * 1.5)
99 {
100 rayX = (Math.floor(x / this.size) * this.size) - 0.01;
101 rayY = (x - rayX) * nTan + y;
102 offX = -this.size;
103 offY = -offX*nTan;
104 }
105 else if(radian < Math.PI / 2 || radian > Math.PI * 1.5)
106 {
107 rayX = (Math.floor(x / this.size) * this.size) + this.size;
108 rayY = (x - rayX) * nTan + y;
109 offX = this.size;
110 offY = -offX*nTan;
111 vDir = "west";
112 }
113 else
114 {
115 rayX = x;
116 rayY = y;
117 distDone = maxDist;
118 }
119
120 while (distDone < maxDist)
121 {
122 mapX = Math.floor(rayX / this.size);
123 mapY = Math.floor(rayY / this.size);
124 var tileHitId = this.level.tiles[mapX + mapY * this.level.width];
125
126 tileHit = Tiles.findByID(tileHitId);
127
128 if(tileHit && tileHit.Solid)
129 {
130 distDone = maxDist;
131 }
132 else
133 {
134 rayX += offX;
135 rayY += offY;
136 distDone += 1;
137 }
138 }
139
140 var vResult =
141 {
142 tile: tileHit,
143 endX: rayX,
144 endY: rayY,
145 dir: vDir
146 }
147
148 var vectorH = new Vector(hResult.endX - x, hResult.endY - y);
149 var vectorV = new Vector(vResult.endX - x, vResult.endY - y);
150
151 hResult.distance = vectorH.length();
152 vResult.distance = vectorV.length();
153
154 if(vectorH.length() < vectorV.length())
155 return hResult;
156 else
157 return vResult;
158 }
159 }
@@ -0,0 +1,159 @@
1var World =
2{
3 size: 32,
4
5 level: null,
6
7 loadLevel: function(index)
8 {
9 this.level = Levels[index];
10
11 Player.x = this.level.spawnX * this.size + this.size/2;
12 Player.y = this.level.spawnY * this.size + this.size/2;
13 },
14
15 drawWorld2D: function()
16 {
17 for(var x = 0; x < this.level.width; x++)
18 {
19 for(var y = 0; y < this.level.height; y++)
20 {
21 cell = this.level.tiles[x + (y * this.level.width)];
22
23 var color = Tiles.findByID(cell).Color;
24 Canvas.drawRect(x * this.size, y * this.size, this.size, this.size, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
25 }
26 }
27
28 Player.draw();
29 },
30
31 rayCast: function(x, y, radian)
32 {
33 var maxDist = (this.level.width + this.level.height);
34 var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit;
35
36 var hDir = "north";
37
38 distDone = 0;
39
40 //horizontal
41 var aTan = -1/Math.tan(radian);
42 if(radian > Math.PI)
43 {
44 hrayY = (Math.floor(y / this.size) * this.size) - 0.01;
45 hrayX = (y - hrayY) * aTan + x;
46 hoffY = -this.size;
47 hoffX = -hoffY*aTan;
48 }
49 else if(radian < Math.PI)
50 {
51 hrayY = (Math.floor(y / this.size) * this.size) + this.size;
52 hrayX = (y - hrayY) * aTan + x;
53 hoffY = this.size;
54 hoffX = -hoffY*aTan;
55 hDir = "south";
56 }
57 else
58 {
59 hrayX = x;
60 hrayY = y;
61 distDone = maxDist;
62 }
63
64 while (distDone < maxDist)
65 {
66 hmapX = Math.floor(hrayX / this.size);
67 hmapY = Math.floor(hrayY / this.size);
68 var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width];
69
70 htileHit = Tiles.findByID(htileHitId);
71
72 if(htileHit && htileHit.Solid)
73 {
74 distDone = maxDist;
75 }
76 else
77 {
78 hrayX += hoffX;
79 hrayY += hoffY;
80 distDone += 1;
81 }
82 }
83
84 var hResult =
85 {
86 tile: htileHit,
87 endX: hrayX,
88 endY: hrayY,
89 dir: hDir
90 }
91
92 //vertical
93 var rayX, rayY, offX, offY, distDone, mapX, mapY, tileHit;
94 var vDir = "east";
95 var nTan = -Math.tan(radian);
96 distDone = 0;
97
98 if(radian > Math.PI / 2 && radian < Math.PI * 1.5)
99 {
100 rayX = (Math.floor(x / this.size) * this.size) - 0.01;
101 rayY = (x - rayX) * nTan + y;
102 offX = -this.size;
103 offY = -offX*nTan;
104 }
105 else if(radian < Math.PI / 2 || radian > Math.PI * 1.5)
106 {
107 rayX = (Math.floor(x / this.size) * this.size) + this.size;
108 rayY = (x - rayX) * nTan + y;
109 offX = this.size;
110 offY = -offX*nTan;
111 vDir = "west";
112 }
113 else
114 {
115 rayX = x;
116 rayY = y;
117 distDone = maxDist;
118 }
119
120 while (distDone < maxDist)
121 {
122 mapX = Math.floor(rayX / this.size);
123 mapY = Math.floor(rayY / this.size);
124 var tileHitId = this.level.tiles[mapX + mapY * this.level.width];
125
126 tileHit = Tiles.findByID(tileHitId);
127
128 if(tileHit && tileHit.Solid)
129 {
130 distDone = maxDist;
131 }
132 else
133 {
134 rayX += offX;
135 rayY += offY;
136 distDone += 1;
137 }
138 }
139
140 var vResult =
141 {
142 tile: tileHit,
143 endX: rayX,
144 endY: rayY,
145 dir: vDir
146 }
147
148 var vectorH = new Vector(hResult.endX - x, hResult.endY - y);
149 var vectorV = new Vector(vResult.endX - x, vResult.endY - y);
150
151 hResult.distance = vectorH.length();
152 vResult.distance = vectorV.length();
153
154 if(vectorH.length() < vectorV.length())
155 return hResult;
156 else
157 return vResult;
158 }
159}
main.js added +37 −0
@@ -0,0 +1,37 @@
1 window.onload = function()
2 {
3 console.log("Loading Everything");
4 GameWindow.init(0, 0, window.innerWidth - 2, window.innerHeight - 2);
5 GameWindow.fullscreen = true;
6
7 GameInput.g_init();
8
9 Canvas.canvas.requestPointerLock = Canvas.canvas.requestPointerLock ||
10 element.mozRequestPointerLock ||
11 element.webkitRequestPointerLock;
12 // Ask the browser to lock the pointer
13
14 Canvas.canvas.onclick = function() {
15 Canvas.canvas.requestPointerLock();
16 }
17
18 World.loadLevel(0);
19
20 setInterval(function()
21 {
22 var stepTime = Date.now();
23 var stepTimeCurr = (stepTime - stepPrevious) * 0.001;
24
25 if(isNaN(stepTimeCurr))
26 stepTimeCurr = 0;
27
28 GameInput.g_updateKeys();
29 Player.update(stepTimeCurr);
30
31 GameWindow.redraw();
32
33 this.stepPrevious = stepTime;
34 }, 20);
35 }
36
37 var stepPrevious = 0;
@@ -0,0 +1,37 @@
1window.onload = function()
2{
3 console.log("Loading Everything");
4 GameWindow.init(0, 0, window.innerWidth - 2, window.innerHeight - 2);
5 GameWindow.fullscreen = true;
6
7 GameInput.g_init();
8
9 Canvas.canvas.requestPointerLock = Canvas.canvas.requestPointerLock ||
10 element.mozRequestPointerLock ||
11 element.webkitRequestPointerLock;
12 // Ask the browser to lock the pointer
13
14 Canvas.canvas.onclick = function() {
15 Canvas.canvas.requestPointerLock();
16 }
17
18 World.loadLevel(0);
19
20 setInterval(function()
21 {
22 var stepTime = Date.now();
23 var stepTimeCurr = (stepTime - stepPrevious) * 0.001;
24
25 if(isNaN(stepTimeCurr))
26 stepTimeCurr = 0;
27
28 GameInput.g_updateKeys();
29 Player.update(stepTimeCurr);
30
31 GameWindow.redraw();
32
33 this.stepPrevious = stepTime;
34 }, 20);
35}
36
37var stepPrevious = 0;
sounds/shoot.mp3 added Binary file +0 −0

The contents of this binary file are not shown.