BlueGit

timfa raytracer

Files

raytracer / js / Player.js

Player.js

import
timfa Tim Falken · pushed by timfa · 6 years ago
155b19f9 History
Player.js 4.9 KiB Download Raw
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}