BlueGit

timfa raytracer

add gun image but nothing else yet

timfa Tim Falken · pushed by timfa

Authored 2020-10-17 · 6 years ago

Hash
e9b2b37403015ca909a6880f70ec6496fc743c29
Parent
a062370e
5 files changed +103 −5
index.html +1 −0
@@ -7,6 +7,7 @@
7 7 <script src="js/GameWindow.js"></script>
8 8 <script src="js/Canvas.js"></script>
9 9 <script src="js/Vector.js"></script>
10 <script src="js/image.js"></script>
10 11 <script src="js/Levels.js"></script>
11 12 <script src="js/Tiles.js"></script>
12 13 <script src="js/Vector.js"></script>
@@ -7,6 +7,7 @@
7 <script src="js/GameWindow.js"></script>
7 <script src="js/GameWindow.js"></script>
8 <script src="js/Canvas.js"></script>
8 <script src="js/Canvas.js"></script>
9 <script src="js/Vector.js"></script>
9 <script src="js/Vector.js"></script>
10 <script src="js/image.js"></script>
10 <script src="js/Levels.js"></script>
11 <script src="js/Levels.js"></script>
11 <script src="js/Tiles.js"></script>
12 <script src="js/Tiles.js"></script>
12 <script src="js/Vector.js"></script>
13 <script src="js/Vector.js"></script>
js/Canvas.js +5 −0
@@ -43,5 +43,10 @@ var Canvas =
43 43
44 44 this.context.beginPath();
45 45 this.context.fillRect(xStart, yStart, xEnd, yEnd);
46 },
47
48 drawImage(img, x, y)
49 {
50 this.context.drawImage(img, x, y);
46 51 }
47 52 }
@@ -43,5 +43,10 @@ var Canvas =
43
43
44 this.context.beginPath();
44 this.context.beginPath();
45 this.context.fillRect(xStart, yStart, xEnd, yEnd);
45 this.context.fillRect(xStart, yStart, xEnd, yEnd);
46 },
47
48 drawImage(img, x, y)
49 {
50 this.context.drawImage(img, x, y);
46 }
51 }
47}
52}
js/GameWindow.js +0 −1
@@ -63,6 +63,5 @@ var GameWindow =
63 63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
64 64
65 65 Player.draw3D();
66 World.drawWorld2D();
67 66 }
68 67 }
@@ -63,6 +63,5 @@ var GameWindow =
63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
64
64
65 Player.draw3D();
65 Player.draw3D();
66 World.drawWorld2D();
67 }
66 }
68}
67}
js/Player.js +35 −4
@@ -5,6 +5,21 @@ var Player =
5 5 rotation: 0,
6 6 pitch: 0,
7 7 horizontalResolution: 200,
8 gunX:0.6,
9 gunY:0.6,
10 currentGun:0,
11 guns:3,
12
13 machineGunImg: null,
14 shotgunImg: null,
15 pulseRifleImg: null,
16
17 init: function()
18 {
19 this.machineGunImg = new image("images/machinegun.png");
20 this.shotgunImg = new image("images/shotgun.png");
21 this.pulseRifleImg = new image("images/pulserifle.png");
22 },
8 23
9 24 update: function(deltaTime)
10 25 {
@@ -142,8 +157,6 @@ var Player =
142 157 break;
143 158 }
144 159
145 Canvas.drawLine(this.x, this.y, rayHit.endX, rayHit.endY, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
146
147 160 var deltaAngle = this.rotation - rot;
148 161 rayHit.distance *= Math.cos(deltaAngle);
149 162
@@ -154,7 +167,25 @@ var Player =
154 167
155 168 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
156 169
157 Canvas.drawRect(xLine, yLine, widthLine, height);
170 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
158 171 }
172
173 var gunImg = null;
174 switch(this.currentGun)
175 {
176 case 0:
177 gunImg = this.shotgunImg;
178 break;
179 case 1:
180 gunImg = this.machineGunImg;
181 break;
182 case 2:
183 gunImg = this.pulseRifleImg;
184 break;
185 }
186
187 Canvas.drawImage(gunImg.g_image, GameWindow.width * this.gunX, GameWindow.height * this.gunY);
159 188 }
160 }
189 }
190
191 Player.init();
@@ -5,6 +5,21 @@ var Player =
5 rotation: 0,
5 rotation: 0,
6 pitch: 0,
6 pitch: 0,
7 horizontalResolution: 200,
7 horizontalResolution: 200,
8 gunX:0.6,
9 gunY:0.6,
10 currentGun:0,
11 guns:3,
12
13 machineGunImg: null,
14 shotgunImg: null,
15 pulseRifleImg: null,
16
17 init: function()
18 {
19 this.machineGunImg = new image("images/machinegun.png");
20 this.shotgunImg = new image("images/shotgun.png");
21 this.pulseRifleImg = new image("images/pulserifle.png");
22 },
8
23
9 update: function(deltaTime)
24 update: function(deltaTime)
10 {
25 {
@@ -142,8 +157,6 @@ var Player =
142 break;
157 break;
143 }
158 }
144
159
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;
160 var deltaAngle = this.rotation - rot;
148 rayHit.distance *= Math.cos(deltaAngle);
161 rayHit.distance *= Math.cos(deltaAngle);
149
162
@@ -154,7 +167,25 @@ var Player =
154
167
155 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
168 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
156
169
157 Canvas.drawRect(xLine, yLine, widthLine, height);
170 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
158 }
171 }
172
173 var gunImg = null;
174 switch(this.currentGun)
175 {
176 case 0:
177 gunImg = this.shotgunImg;
178 break;
179 case 1:
180 gunImg = this.machineGunImg;
181 break;
182 case 2:
183 gunImg = this.pulseRifleImg;
184 break;
185 }
186
187 Canvas.drawImage(gunImg.g_image, GameWindow.width * this.gunX, GameWindow.height * this.gunY);
159 }
188 }
160}
189}
190
191Player.init();
js/image.js added +62 −0
@@ -0,0 +1,62 @@
1 function image(url, subImgTotal, perRow)
2 {
3 subImgTotal = subImgTotal || 1;
4 perRow = perRow || 1;
5
6 this.subImages = subImgTotal;
7 this.subImagesPerRow = perRow;
8
9 var body = document.body;
10 body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');
11 this.g_image = document.getElementById('image');
12 document.getElementById('image').id = "loadedImage";
13
14 Object.defineProperty(this, "width",
15 {
16 get: function()
17 {
18 return this.g_image.width / this.subImagesPerRow;
19 }
20 });
21
22 Object.defineProperty(this, "height",
23 {
24 get: function()
25 {
26 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
27 }
28 });
29
30 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
31 {
32 var shouldDraw = true;
33
34 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
35 shouldDraw = false;
36
37 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38 shouldDraw = false;
39
40 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41 shouldDraw = false;
42
43 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
44 shouldDraw = false;
45
46 if(shouldDraw)
47 {
48 var context = GameWindow.canvasContext;
49 context.save();
50 var oldAlpha = context.globalAlpha;
51 context.globalAlpha = alpha;
52
53 context.translate(dx, dy);
54 context.rotate(rotation * (Math.PI/180));
55
56 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
57
58 context.restore();
59 context.globalAlpha = oldAlpha;
60 }
61 }
62 }
@@ -0,0 +1,62 @@
1function image(url, subImgTotal, perRow)
2{
3 subImgTotal = subImgTotal || 1;
4 perRow = perRow || 1;
5
6 this.subImages = subImgTotal;
7 this.subImagesPerRow = perRow;
8
9 var body = document.body;
10 body.insertAdjacentHTML('beforeend', '<img id="image" src="' + url + '" style="display:none;"></img>');
11 this.g_image = document.getElementById('image');
12 document.getElementById('image').id = "loadedImage";
13
14 Object.defineProperty(this, "width",
15 {
16 get: function()
17 {
18 return this.g_image.width / this.subImagesPerRow;
19 }
20 });
21
22 Object.defineProperty(this, "height",
23 {
24 get: function()
25 {
26 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
27 }
28 });
29
30 this.draw = function(sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
31 {
32 var shouldDraw = true;
33
34 if(dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
35 shouldDraw = false;
36
37 if(dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38 shouldDraw = false;
39
40 if(dx > GameWindow.width + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41 shouldDraw = false;
42
43 if(dy > GameWindow.height + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
44 shouldDraw = false;
45
46 if(shouldDraw)
47 {
48 var context = GameWindow.canvasContext;
49 context.save();
50 var oldAlpha = context.globalAlpha;
51 context.globalAlpha = alpha;
52
53 context.translate(dx, dy);
54 context.rotate(rotation * (Math.PI/180));
55
56 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
57
58 context.restore();
59 context.globalAlpha = oldAlpha;
60 }
61 }
62}