BlueGit

timfa raytracer

Files

raytracer / js / Player.js

Player.js

texture
timfa Tim Falken · pushed by timfa · 5 years ago
d4c309d6 History
Player.js 6.5 KiB Download Raw
1var Player =
2{
3 x: 0,
4 y: 0,
5 rotation: 0,
6 pitch: 0,
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 },
23
24 update: function(deltaTime)
25 {
26 if(GameInput.isHeld(37) || GameInput.isHeld(65)) // left arrow
27 {
28 var rot = this.rotation - Math.PI/2;
29 if(rot < 0)
30 rot += Math.PI*2;
31 if(rot > Math.PI*2)
32 rot -= Math.PI*2
33
34 var forward = Vector.fromAngle(rot);
35
36 if(World.rayCast(this.x, this.y, rot).distance > 5)
37 {
38 this.x += forward.x * deltaTime * 60;
39 this.y += forward.y * deltaTime * 60;
40 }
41 }
42
43 if(GameInput.isHeld(39) || GameInput.isHeld(68)) // right arrow
44 {
45 var rot = this.rotation + Math.PI/2;
46 if(rot < 0)
47 rot += Math.PI*2;
48 if(rot > Math.PI*2)
49 rot -= Math.PI*2
50
51 var forward = Vector.fromAngle(rot);
52
53 if(World.rayCast(this.x, this.y, rot).distance > 5)
54 {
55 this.x += forward.x * deltaTime * 60;
56 this.y += forward.y * deltaTime * 60;
57 }
58 }
59
60 //this.pitch += //((GameInput.mousePosition.y - (GameWindow.height / 2)) / (GameWindow.height/2)) * Math.PI;
61
62 if(this.rotation > Math.PI * 2)
63 this.rotation -= Math.PI * 2;
64
65 if(this.rotation < 0)
66 this.rotation += Math.PI * 2;
67
68 if(GameInput.isHeld(38) || GameInput.isHeld(87)) // up arrow
69 {
70 var forward = Vector.fromAngle(this.rotation);
71
72 if(World.rayCast(this.x, this.y, this.rotation).distance > 5)
73 {
74 this.x += forward.x * deltaTime * 60;
75 this.y += forward.y * deltaTime * 60;
76 }
77 }
78
79 if(GameInput.isHeld(40) || GameInput.isHeld(83)) // down arrow
80 {
81 var forward = Vector.fromAngle(this.rotation);
82
83 var rot = this.rotation + Math.PI;
84 if(rot < 0)
85 rot += Math.PI*2;
86 if(rot > Math.PI*2)
87 rot -= Math.PI*2
88
89 if(World.rayCast(this.x, this.y, rot).distance > 5)
90 {
91 this.x -= forward.x * deltaTime * 40;
92 this.y -= forward.y * deltaTime * 40;
93 }
94 }
95
96 if(GameInput.isHeld(188)) // < arrow
97 {
98 this.horizontalResolution-= this.horizontalResolution * 0.1 * deltaTime;
99 if(this.horizontalResolution < 30)
100 this.horizontalResolution = 30;
101 }
102 if(GameInput.isHeld(190)) // > arrow
103 {
104 this.horizontalResolution+= this.horizontalResolution * 0.1 * deltaTime;
105 if(this.horizontalResolution > GameWindow.width)
106 this.horizontalResolution = GameWindow.width;
107 }
108 },
109
110 draw: function()
111 {
112 var forward = Vector.fromAngle(this.rotation).stretch(25);
113
114 Canvas.drawRect(this.x - 10, this.y - 10, 20, 20, 0xff0000);
115 Canvas.drawLine(this.x, this.y, this.x + forward.x, this.y + forward.y, 0xff00ff);
116 },
117
118 draw3D: function()
119 {
120 var rad = Math.PI / (this.horizontalResolution*2);
121
122 for(var i = 0; i < this.horizontalResolution; i++)
123 {
124 var offsetP = -0.5 + (i / this.horizontalResolution);
125 var offset = offsetP * this.horizontalResolution;
126
127 var rotationalOffset = offset * rad;
128
129 var rot = this.rotation + rotationalOffset;
130
131 if(rot < 0)
132 rot += Math.PI * 2;
133 if(rot > Math.PI * 2)
134 rot -= Math.PI * 2;
135
136 var rayHit = World.rayCast(this.x, this.y, rot);
137
138 var color = {
139 R: rayHit.tile.Color.R,
140 G: rayHit.tile.Color.G,
141 B: rayHit.tile.Color.B
142 }
143
144 var light = 1;
145
146 switch(rayHit.dir)
147 {
148 case "north":
149 break;
150 case "east":
151 color.R *= 0.9;
152 color.G *= 0.9;
153 color.B *= 0.9;
154 light = 0.9;
155 break;
156 case "west":
157 color.R *= 0.8;
158 color.G *= 0.8;
159 color.B *= 0.8;
160 light = 0.8;
161 break;
162 case "south":
163 color.R *= 0.7;
164 color.G *= 0.7;
165 color.B *= 0.7;
166 light = 0.7;
167 break;
168 }
169
170 var deltaAngle = this.rotation - rot;
171 rayHit.distance *= Math.cos(deltaAngle);
172
173 var xLine = i * (GameWindow.width / this.horizontalResolution);
174 var widthLine = (GameWindow.width / this.horizontalResolution);
175
176 var height = (World.size * GameWindow.height) / rayHit.distance;
177
178 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
179 var imgyLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
180
181 if(rayHit.tile.Image)
182 {
183 var subImg = Math.min(rayHit.tile.Image.g_image.width - widthLine, Math.max(1, rayHit.tileX * rayHit.tile.Image.g_image.width));
184 rayHit.tile.Image.draw(subImg, 0, widthLine, rayHit.tile.Image.height, xLine, imgyLine + height/2, widthLine, height, 0, light);
185 }
186 else
187 {
188 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
189 }
190 }
191
192 var gunImg = null;
193 switch(this.currentGun)
194 {
195 case 0:
196 gunImg = this.shotgunImg;
197 break;
198 case 1:
199 gunImg = this.machineGunImg;
200 break;
201 case 2:
202 gunImg = this.pulseRifleImg;
203 break;
204 }
205
206 var p = (GameWindow.height / 1080)
207 var w = p * gunImg.g_image.width;
208 var h = p * gunImg.g_image.height;
209
210 Canvas.drawImage(gunImg.g_image, GameWindow.width * this.gunX, GameWindow.height * this.gunY, w, h);
211 }
212}
213
214Player.init();