-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv5.html
More file actions
211 lines (188 loc) · 4.4 KB
/
v5.html
File metadata and controls
211 lines (188 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<div id="fps" class="outlined" style="position:absolute;z-index:1;font-size:50px;background-color:black;color:rgb(0,255,0);font-family:consolas">FPS: 60</div>
<div id="hud" class="outlined" style="position:absolute;bottom:0px;z-index:1;font-size:50px;background-color:black;color:rgb(0,255,0);font-family:consolas">FPS: 60</div>
<canvas id="c"style="position:absolute;z-index:0"></canvas>
<script>
let lastTime = performance.now();
let frameCount = 0;
let fpsDisplay = document.getElementById('fps');
function updateFPS() {
const currentTime = performance.now();
frameCount++;
// Calculate FPS every second
if (currentTime - lastTime >= 1000) {
const fps = frameCount;
fpsDisplay.textContent = `FPS: ${fps}`;
frameCount = 0;
lastTime = currentTime;
}
requestAnimationFrame(updateFPS);
}
// Start the FPS counter
updateFPS();
</script>
<script>
max=(a,b)=>{
return Math.max(a,b)
}
min=(a,b)=>{
return Math.min(a,b)
}
floor=(x)=>{
return Math.floor(x)
}
pi=()=>{
return Math.PI
}
rnd=()=>{
return Math.random()
}
sin=(a)=>{
return Math.sin(a)
}
cos=(a)=>{
return Math.cos(a)
}
clamp=(min1,val1,max1)=>{
return min(max(val1,min1),max1)
}
clampColor=(val1)=>{
return clamp(0,val1,255)
}
rndColor=()=>{
return clampColor(rnd()*255)
}
m=[]
onmousedown=onmouseup=(e)=>{
m[e.button=e.type="mousedown"]
if(
document.pointerLockElement===c
||
document.mozPointerLockElement===c
){
//DO NOTHING
}else{
document.getElementById("c").requestPointerLock()
}
}
onmousemove=(event)=>{
if(
document.pointerLockElement===c
||
document.mozPointerLockElement===c
){
player.rot.x+=0.005 * event.movementX
player.rot.y-=0.005 * event.movementY
player.rot.y=clamp(-1.6,player.rot.y,1.6)
}
}
k=[]
onkeydown=onkeyup=(e)=>{k[e.keyCode]=e.type=="keydown"}
player={
pos:{x:0,y:2,z:0},
rot:{x:0,y:0}
}
map=[]
mapSize=10
for(x=0;x<mapSize;x++){
map[x]=[]
for(y=0;y<mapSize;y++){
map[x][y]=[]
for(z=0;z<mapSize;z++){
map[x][y][z]=1
}
}
}
placeFree=(x,y,z)=>{
x=floor(x)
y=floor(y)
z=floor(z)
if (x < 0 || y < 0 || z < 0 ||
x >= map.length ||
y >= map[0].length ||
z >= map[0][0].length) {
return true; // fuera del mapa
}
return map[x][y][z]!=1
}
shotRay = (x0,y0,z0,angleX,angleY) => {
dist = -0.5;
while(dist < 10){
sinX = sin(angleX);
cosX = cos(angleX);
sinY = sin(angleY);
cosY = cos(angleY);
dx1 = sinX * cosY;
dy1 = sinY;
dz1 = cosX * cosY;
dx0 = floor(dist*dx1 + x0);
dy0 = floor(dist*dy1 + y0);
dz0 = floor(dist*dz1 + z0);
// Aquí la condición correcta:
if (!placeFree(dx0,dy0,dz0)) {
return dist; // chocó con un bloque
}
dist += 0.1;
}
return 10; // nada encontrado
}
render=()=>{
requestAnimationFrame(render)
c.width=innerWidth
c.height=innerHeight
ctx.fillStyle = "rgb(0,127,255)"
ctx.fillRect(0, 0, innerWidth, innerHeight)
PxSize=32
for (x = 0; x < innerWidth; x += PxSize) {
for (y = 0; y < innerHeight; y += PxSize) {
currDist=shotRay(
player.pos.x,
player.pos.y,
player.pos.z,
player.rot.x+x/1000-innerWidth/2000,
player.rot.y-y/1000-innerHeight/2000
)
prox=2*currDist**-1
ctx.globalAlpha = prox
ctx.fillStyle = "rgb(0,255,0)"
ctx.fillRect(x, y, PxSize, PxSize)
}
}
hud.innerHTML=
"x:"+floor(player.pos.x)+
" y:"+floor(player.pos.y)+
" z:"+floor(player.pos.z)+
" RotX:"+floor(player.rot.x*1000/16)+
" RotY:"+floor(-player.rot.y*1000/16)+"%"
walkSpeed=0.1
camCos=Math.cos(player.rot.x)
camSin=Math.sin(player.rot.x)
if(k[65]){//A
player.pos.x -= walkSpeed * camCos;
player.pos.z += walkSpeed * camSin;
}
if(k[68]){//D
player.pos.x += walkSpeed * camCos;
player.pos.z -= walkSpeed * camSin;
}
if(k[87]){//W
player.pos.x += walkSpeed * camSin;
player.pos.z += walkSpeed * camCos;
}
if(k[83]){//S
player.pos.x -= walkSpeed * camSin;
player.pos.z -= walkSpeed * camCos;
}
if(k[32]){
player.pos.y += walkSpeed
}
if(k[81]){
player.pos.y -= walkSpeed
}
}
init=()=>{
document.body.style.margin=0
ctx=c.getContext("2d")
render()
}
onload=()=>init()
</script>