BlueGit

timfa raytracer

Files

raytracer / js / World.js

World.js

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