BlueGit

timfa raytracer

Files

raytracer / js / Player.js

Player.js

automagically scale the gun
timfa Tim Falken · pushed by timfa · 6 years ago
1f3b58ed History
Player.js 5.7 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 rot = this.rotation - rad*(this.horizontalResolution / 2) + rad*i;
125
126 if(rot < 0)
127 rot += Math.PI * 2;
128 if(rot > Math.PI * 2)
129 rot -= Math.PI * 2;
130
131 var rayHit = World.rayCast(this.x, this.y, rot);
132
133 var color = {
134 R: rayHit.tile.Color.R,
135 G: rayHit.tile.Color.G,
136 B: rayHit.tile.Color.B
137 }
138
139 switch(rayHit.dir)
140 {
141 case "north":
142 break;
143 case "east":
144 color.R *= 0.9;
145 color.G *= 0.9;
146 color.B *= 0.9;
147 break;
148 case "west":
149 color.R *= 0.8;
150 color.G *= 0.8;
151 color.B *= 0.8;
152 break;
153 case "south":
154 color.R *= 0.7;
155 color.G *= 0.7;
156 color.B *= 0.7;
157 break;
158 }
159
160 var deltaAngle = this.rotation - rot;
161 rayHit.distance *= Math.cos(deltaAngle);
162
163 var xLine = i * (GameWindow.width / this.horizontalResolution);
164 var widthLine = (GameWindow.width / this.horizontalResolution) + 1;
165
166 var height = (World.size * GameWindow.height) / rayHit.distance;
167
168 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
169
170 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
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 var p = (GameWindow.height / 1080)
188 var w = p * gunImg.g_image.width;
189 var h = p * gunImg.g_image.height;
190
191 Canvas.drawImage(gunImg.g_image, GameWindow.width * this.gunX, GameWindow.height * this.gunY, w, h);
192 }
193}
194
195Player.init();