new life 2.0
23
.editorconfig
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.py]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[{*.html,*.css,*.json}]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[humans.txt]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[*.js]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
21
LICENSE
@ -1,7 +1,22 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
Copyright 2022 Paul Saikko
|
Copyright 2022 Paul Saikko
|
||||||
|
Copyright 2022 Alexander Popov
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
IN THE SOFTWARE.
|
||||||
|
11
README.md
@ -1,8 +1,5 @@
|
|||||||
# raycast
|
# JSRay
|
||||||
A small raycaster graphics engine in javascript with html 5 canvas.
|
A Small Raycaster Graphics Engine in JavaScript with HTML5 `<canvas>`.
|
||||||
|
|
||||||
Code mostly follows [this C++ raycaster tutorial](http://lodev.org/cgtutor/raycasting.html)
|
## Original author
|
||||||
|
Paul Saikko - [Github](https://github.com/psaikko/raycast)
|
||||||
Live at [github.io](http://psaikko.github.io/raycast/)
|
|
||||||
|
|
||||||
![screenshot](https://raw.githubusercontent.com/psaikko/raycast/master/screenshot.png)
|
|
||||||
|
21
game.js
@ -1,21 +0,0 @@
|
|||||||
var raycast = raycast || {};
|
|
||||||
|
|
||||||
window.requestAnimFrame = (function(){
|
|
||||||
return window.requestAnimationFrame ||
|
|
||||||
window.webkitRequestAnimationFrame ||
|
|
||||||
window.mozRequestAnimationFrame ||
|
|
||||||
window.oRequestAnimationFrame ||
|
|
||||||
window.msRequestAnimationFrame ||
|
|
||||||
function(callback, element){
|
|
||||||
window.setTimeout(callback, 1000 / 60);
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
function start() {
|
|
||||||
document.onkeyup = raycast.keyhandler.onKeyup;
|
|
||||||
document.onkeydown = raycast.keyhandler.onKeydown;
|
|
||||||
var textureFiles = ["img/brick.png", "img/ground.png", "img/sky.png"];
|
|
||||||
raycast.texture.initiateLoad(textureFiles, raycast.engine.start);
|
|
||||||
};
|
|
||||||
|
|
||||||
window.onload = start;
|
|
17
index.html
@ -1,17 +0,0 @@
|
|||||||
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body bgcolor="999999">
|
|
||||||
<div id="canvasdiv">
|
|
||||||
<canvas id="viewport" width="800" height="600"></canvas>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="keyhandler.js"></script>
|
|
||||||
<script src="texture.js"></script>
|
|
||||||
<script src="raycast.js"></script>
|
|
||||||
<script src="game.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
1
requirements-dev.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
editorconfig-checker==2.4.0
|
BIN
screenshot.png
Before Width: | Height: | Size: 157 KiB |
21
src/game.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
var raycast = raycast || {};
|
||||||
|
|
||||||
|
window.requestAnimFrame = (function(){
|
||||||
|
return window.requestAnimationFrame ||
|
||||||
|
window.webkitRequestAnimationFrame ||
|
||||||
|
window.mozRequestAnimationFrame ||
|
||||||
|
window.oRequestAnimationFrame ||
|
||||||
|
window.msRequestAnimationFrame ||
|
||||||
|
function(callback, element){
|
||||||
|
window.setTimeout(callback, 1000 / 60);
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
function start() {
|
||||||
|
document.onkeyup = raycast.keyhandler.onKeyup;
|
||||||
|
document.onkeydown = raycast.keyhandler.onKeydown;
|
||||||
|
var textureFiles = ["img/brick.png", "img/ground.png", "img/sky.png"];
|
||||||
|
raycast.texture.initiateLoad(textureFiles, raycast.engine.start);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.onload = start;
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 6.6 KiB After Width: | Height: | Size: 6.6 KiB |
Before Width: | Height: | Size: 215 B After Width: | Height: | Size: 215 B |
18
src/index.html
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>JSRay</title>
|
||||||
|
</head>
|
||||||
|
<body bgcolor="999999">
|
||||||
|
<div id="canvasdiv">
|
||||||
|
<canvas id="viewport" width="800" height="600"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="keyhandler.js"></script>
|
||||||
|
<script src="texture.js"></script>
|
||||||
|
<script src="raycast.js"></script>
|
||||||
|
<script src="game.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -18,19 +18,19 @@ raycast.keyhandler = (function () {
|
|||||||
var lastState = new Array();
|
var lastState = new Array();
|
||||||
|
|
||||||
for (var i = 0; i < 255; i++) {
|
for (var i = 0; i < 255; i++) {
|
||||||
state[i] = false;
|
state[i] = false;
|
||||||
lastState[i] = false;
|
lastState[i] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onKeyup = function (e) {
|
onKeyup = function (e) {
|
||||||
state[e.which] = false;
|
state[e.which] = false;
|
||||||
if (isUsedKey(e.which))
|
if (isUsedKey(e.which))
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
onKeydown = function (e) {
|
onKeydown = function (e) {
|
||||||
state[e.which] = true;
|
state[e.which] = true;
|
||||||
if (isUsedKey(e.which))
|
if (isUsedKey(e.which))
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,13 +47,13 @@ raycast.keyhandler = (function () {
|
|||||||
return state[codes[keyname]];
|
return state[codes[keyname]];
|
||||||
}
|
}
|
||||||
|
|
||||||
isKeypress = function(keyname) {
|
isKeypress = function(keyname) {
|
||||||
return state[codes[keyname]] && !lastState[codes[keyname]];
|
return state[codes[keyname]] && !lastState[codes[keyname]];
|
||||||
}
|
}
|
||||||
|
|
||||||
tick = function() {
|
tick = function() {
|
||||||
lastState = state.slice();
|
lastState = state.slice();
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
onKeyup: onKeyup,
|
onKeyup: onKeyup,
|
||||||
@ -62,4 +62,4 @@ raycast.keyhandler = (function () {
|
|||||||
isKeypress: isKeypress,
|
isKeypress: isKeypress,
|
||||||
tick: tick
|
tick: tick
|
||||||
};
|
};
|
||||||
})();
|
})();
|
@ -1,349 +1,349 @@
|
|||||||
// http://lodev.org/cgtutor/raycasting.html
|
// http://lodev.org/cgtutor/raycasting.html
|
||||||
var raycast = raycast || {};
|
var raycast = raycast || {};
|
||||||
|
|
||||||
raycast.engine = (function () {
|
raycast.engine = (function () {
|
||||||
var canvas = document.getElementById("viewport");
|
var canvas = document.getElementById("viewport");
|
||||||
var g = canvas.getContext("2d");
|
var g = canvas.getContext("2d");
|
||||||
var filtering = false;
|
var filtering = false;
|
||||||
var mapWidth = 24,
|
var mapWidth = 24,
|
||||||
mapHeight = 24,
|
mapHeight = 24,
|
||||||
texHeight = 64,
|
texHeight = 64,
|
||||||
texWidth = 64;
|
texWidth = 64;
|
||||||
|
|
||||||
var texture;
|
var texture;
|
||||||
function initTexture() {
|
function initTexture() {
|
||||||
texture = raycast.texture.getTextures();
|
texture = raycast.texture.getTextures();
|
||||||
console.log(texture);
|
console.log(texture);
|
||||||
texture.push([]);
|
texture.push([]);
|
||||||
for(var x = 0; x < texWidth; x++) {
|
for(var x = 0; x < texWidth; x++) {
|
||||||
for(var y = 0; y < texHeight; y++) {
|
for(var y = 0; y < texHeight; y++) {
|
||||||
var xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
|
var xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
|
||||||
var d = Math.sqrt((texWidth/2 - x)*(texWidth/2 - x) + (texHeight/2 - y)*(texHeight/2 - y));
|
var d = Math.sqrt((texWidth/2 - x)*(texWidth/2 - x) + (texHeight/2 - y)*(texHeight/2 - y));
|
||||||
var sincolor = 256 * (1 + Math.sin(d/2)) / 2;
|
var sincolor = 256 * (1 + Math.sin(d/2)) / 2;
|
||||||
texture[3][texWidth * y + x] = [xorcolor, 0, sincolor]; // blue sine pattern
|
texture[3][texWidth * y + x] = [xorcolor, 0, sincolor]; // blue sine pattern
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
texture[0] = raycast.texture.load("ground");
|
texture[0] = raycast.texture.load("ground");
|
||||||
texture[1] = raycast.texture.load("brick");
|
texture[1] = raycast.texture.load("brick");
|
||||||
texture[2] = raycast.texture.load("sky");
|
texture[2] = raycast.texture.load("sky");
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var worldMap = [
|
var worldMap = [
|
||||||
[1,1,1,1,1,1,1,1,1,1],
|
[1,1,1,1,1,1,1,1,1,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,0,0,1,0,0,1,0,0,1],
|
[1,0,0,1,0,0,1,0,0,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,0,0,1,0,0,1,0,0,1],
|
[1,0,0,1,0,0,1,0,0,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,0,0,0,0,0,0,0,0,1],
|
[1,0,0,0,0,0,0,0,0,1],
|
||||||
[1,1,1,1,1,1,1,1,1,1]
|
[1,1,1,1,1,1,1,1,1,1]
|
||||||
];
|
];
|
||||||
|
|
||||||
var posX = 2, posY = 2,
|
var posX = 2, posY = 2,
|
||||||
dirX = -1, dirY = 0,
|
dirX = -1, dirY = 0,
|
||||||
planeX = 0, planeY = 0.66,
|
planeX = 0, planeY = 0.66,
|
||||||
time = Date.now(), oldTime = Date.now();
|
time = Date.now(), oldTime = Date.now();
|
||||||
|
|
||||||
var moveSpeed, rotSpeed;
|
var moveSpeed, rotSpeed;
|
||||||
|
|
||||||
var w = canvas.width,
|
var w = canvas.width,
|
||||||
h = canvas.height;
|
h = canvas.height;
|
||||||
|
|
||||||
function verLine(arr, x, yStart, yEnd, color){
|
function verLine(arr, x, yStart, yEnd, color){
|
||||||
//console.log(x, yStart, yEnd);
|
//console.log(x, yStart, yEnd);
|
||||||
for (var y = yStart | 0; y < yEnd | 0; y++) {
|
for (var y = yStart | 0; y < yEnd | 0; y++) {
|
||||||
var i = 4 * (w * y) + 4 * x;
|
var i = 4 * (w * y) + 4 * x;
|
||||||
arr[i + 0] = color[0];
|
arr[i + 0] = color[0];
|
||||||
arr[i + 1] = color[1];
|
arr[i + 1] = color[1];
|
||||||
arr[i + 2] = color[2];
|
arr[i + 2] = color[2];
|
||||||
arr[i + 3] = 255;
|
arr[i + 3] = 255;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
imagedata = g.getImageData(0,0,w,h);
|
imagedata = g.getImageData(0,0,w,h);
|
||||||
var buffer = imagedata.data;
|
var buffer = imagedata.data;
|
||||||
|
|
||||||
var keys = raycast.keyhandler;
|
var keys = raycast.keyhandler;
|
||||||
|
|
||||||
function input() {
|
function input() {
|
||||||
if (keys.isKeydown("up")) {
|
if (keys.isKeydown("up")) {
|
||||||
if(worldMap[(posX + dirX * moveSpeed) | 0][posY | 0] == 0) posX += dirX * moveSpeed;
|
if(worldMap[(posX + dirX * moveSpeed) | 0][posY | 0] == 0) posX += dirX * moveSpeed;
|
||||||
if(worldMap[posX | 0][(posY + dirY * moveSpeed) | 0] == 0) posY += dirY * moveSpeed;
|
if(worldMap[posX | 0][(posY + dirY * moveSpeed) | 0] == 0) posY += dirY * moveSpeed;
|
||||||
}
|
}
|
||||||
//move backwards if no wall behind you
|
//move backwards if no wall behind you
|
||||||
if (keys.isKeydown("down")) {
|
if (keys.isKeydown("down")) {
|
||||||
if(worldMap[(posX - dirX * moveSpeed) | 0][posY | 0] == 0) posX -= dirX * moveSpeed;
|
if(worldMap[(posX - dirX * moveSpeed) | 0][posY | 0] == 0) posX -= dirX * moveSpeed;
|
||||||
if(worldMap[posX | 0][(posY - dirY * moveSpeed) | 0] == 0) posY -= dirY * moveSpeed;
|
if(worldMap[posX | 0][(posY - dirY * moveSpeed) | 0] == 0) posY -= dirY * moveSpeed;
|
||||||
}
|
}
|
||||||
if (keys.isKeydown("right")) {
|
if (keys.isKeydown("right")) {
|
||||||
//both camera direction and camera plane must be rotated
|
//both camera direction and camera plane must be rotated
|
||||||
var oldDirX = dirX;
|
var oldDirX = dirX;
|
||||||
dirX = dirX * Math.cos(-rotSpeed) - dirY * Math.sin(-rotSpeed);
|
dirX = dirX * Math.cos(-rotSpeed) - dirY * Math.sin(-rotSpeed);
|
||||||
dirY = oldDirX * Math.sin(-rotSpeed) + dirY * Math.cos(-rotSpeed);
|
dirY = oldDirX * Math.sin(-rotSpeed) + dirY * Math.cos(-rotSpeed);
|
||||||
var oldPlaneX = planeX;
|
var oldPlaneX = planeX;
|
||||||
planeX = planeX * Math.cos(-rotSpeed) - planeY * Math.sin(-rotSpeed);
|
planeX = planeX * Math.cos(-rotSpeed) - planeY * Math.sin(-rotSpeed);
|
||||||
planeY = oldPlaneX * Math.sin(-rotSpeed) + planeY * Math.cos(-rotSpeed);
|
planeY = oldPlaneX * Math.sin(-rotSpeed) + planeY * Math.cos(-rotSpeed);
|
||||||
}
|
}
|
||||||
if (keys.isKeydown("left")) {
|
if (keys.isKeydown("left")) {
|
||||||
//both camera direction and camera plane must be rotated
|
//both camera direction and camera plane must be rotated
|
||||||
var oldDirX = dirX;
|
var oldDirX = dirX;
|
||||||
dirX = dirX * Math.cos(rotSpeed) - dirY * Math.sin(rotSpeed);
|
dirX = dirX * Math.cos(rotSpeed) - dirY * Math.sin(rotSpeed);
|
||||||
dirY = oldDirX * Math.sin(rotSpeed) + dirY * Math.cos(rotSpeed);
|
dirY = oldDirX * Math.sin(rotSpeed) + dirY * Math.cos(rotSpeed);
|
||||||
var oldPlaneX = planeX;
|
var oldPlaneX = planeX;
|
||||||
planeX = planeX * Math.cos(rotSpeed) - planeY * Math.sin(rotSpeed);
|
planeX = planeX * Math.cos(rotSpeed) - planeY * Math.sin(rotSpeed);
|
||||||
planeY = oldPlaneX * Math.sin(rotSpeed) + planeY * Math.cos(rotSpeed);
|
planeY = oldPlaneX * Math.sin(rotSpeed) + planeY * Math.cos(rotSpeed);
|
||||||
}
|
}
|
||||||
if (keys.isKeypress("d"))
|
if (keys.isKeypress("d"))
|
||||||
filtering = !filtering;
|
filtering = !filtering;
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
for(var x = 0; x < w; x++) {
|
for(var x = 0; x < w; x++) {
|
||||||
var cameraX = 2 * x / w - 1,
|
var cameraX = 2 * x / w - 1,
|
||||||
rayPosX = posX,
|
rayPosX = posX,
|
||||||
rayPosY = posY,
|
rayPosY = posY,
|
||||||
rayDirX = dirX + planeX * cameraX,
|
rayDirX = dirX + planeX * cameraX,
|
||||||
rayDirY = dirY + planeY * cameraX;
|
rayDirY = dirY + planeY * cameraX;
|
||||||
|
|
||||||
var mapX = rayPosX | 0,
|
var mapX = rayPosX | 0,
|
||||||
mapY = rayPosY | 0;
|
mapY = rayPosY | 0;
|
||||||
|
|
||||||
var deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)),
|
var deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)),
|
||||||
deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
|
deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
|
||||||
|
|
||||||
var stepX,
|
var stepX,
|
||||||
stepY,
|
stepY,
|
||||||
sideDistX,
|
sideDistX,
|
||||||
sideDistY;
|
sideDistY;
|
||||||
|
|
||||||
if (rayDirX < 0) {
|
if (rayDirX < 0) {
|
||||||
stepX = -1;
|
stepX = -1;
|
||||||
sideDistX = (rayPosX - mapX) * deltaDistX;
|
sideDistX = (rayPosX - mapX) * deltaDistX;
|
||||||
} else {
|
} else {
|
||||||
stepX = 1;
|
stepX = 1;
|
||||||
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
|
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
|
||||||
}
|
}
|
||||||
if (rayDirY < 0) {
|
if (rayDirY < 0) {
|
||||||
stepY = -1;
|
stepY = -1;
|
||||||
sideDistY = (rayPosY - mapY) * deltaDistY;
|
sideDistY = (rayPosY - mapY) * deltaDistY;
|
||||||
} else {
|
} else {
|
||||||
stepY = 1;
|
stepY = 1;
|
||||||
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
|
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
|
||||||
}
|
}
|
||||||
|
|
||||||
var side, hit = 0;
|
var side, hit = 0;
|
||||||
|
|
||||||
// DDA
|
// DDA
|
||||||
while (hit == 0) {
|
while (hit == 0) {
|
||||||
side = sideDistX > sideDistY;
|
side = sideDistX > sideDistY;
|
||||||
if (side == 0) {
|
if (side == 0) {
|
||||||
sideDistX += deltaDistX;
|
sideDistX += deltaDistX;
|
||||||
mapX += stepX;
|
mapX += stepX;
|
||||||
} else {
|
} else {
|
||||||
sideDistY += deltaDistY;
|
sideDistY += deltaDistY;
|
||||||
mapY += stepY;
|
mapY += stepY;
|
||||||
}
|
}
|
||||||
if (worldMap[mapX][mapY] > 0) {
|
if (worldMap[mapX][mapY] > 0) {
|
||||||
hit = 1;
|
hit = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var perpWallDist;
|
var perpWallDist;
|
||||||
if (side == 0)
|
if (side == 0)
|
||||||
perpWallDist = Math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
|
perpWallDist = Math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
|
||||||
else
|
else
|
||||||
perpWallDist = Math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
|
perpWallDist = Math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
|
||||||
|
|
||||||
//Calculate height of line to draw on screen
|
//Calculate height of line to draw on screen
|
||||||
var lineHeight = Math.abs((h / perpWallDist) | 0);
|
var lineHeight = Math.abs((h / perpWallDist) | 0);
|
||||||
|
|
||||||
//calculate lowest and highest pixel to fill in current stripe
|
//calculate lowest and highest pixel to fill in current stripe
|
||||||
var drawStart = ((h - lineHeight) / 2) | 0;
|
var drawStart = ((h - lineHeight) / 2) | 0;
|
||||||
if(drawStart < 0)
|
if(drawStart < 0)
|
||||||
drawStart = 0;
|
drawStart = 0;
|
||||||
var drawEnd = ((h + lineHeight) / 2) | 0;
|
var drawEnd = ((h + lineHeight) / 2) | 0;
|
||||||
if(drawEnd >= h)
|
if(drawEnd >= h)
|
||||||
drawEnd = h - 1;
|
drawEnd = h - 1;
|
||||||
|
|
||||||
var wallX; // the exact value where the wall was hit
|
var wallX; // the exact value where the wall was hit
|
||||||
if (side == 1)
|
if (side == 1)
|
||||||
wallX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX;
|
wallX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX;
|
||||||
else
|
else
|
||||||
wallX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY;
|
wallX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY;
|
||||||
wallX -= wallX | 0;
|
wallX -= wallX | 0;
|
||||||
|
|
||||||
var texX = (wallX * texWidth)/* | 0*/;
|
var texX = (wallX * texWidth)/* | 0*/;
|
||||||
if(side == 0 && rayDirX > 0)
|
if(side == 0 && rayDirX > 0)
|
||||||
texX = texWidth - texX - 1;
|
texX = texWidth - texX - 1;
|
||||||
if(side == 1 && rayDirY < 0)
|
if(side == 1 && rayDirY < 0)
|
||||||
texX = texWidth - texX - 1;
|
texX = texWidth - texX - 1;
|
||||||
|
|
||||||
var shade = (side == 1 ? 0.6: 1);
|
var shade = (side == 1 ? 0.6: 1);
|
||||||
|
|
||||||
var wallTex = texture[worldMap[mapX][mapY] - 1];
|
var wallTex = texture[worldMap[mapX][mapY] - 1];
|
||||||
|
|
||||||
for (var y = drawStart; y < drawEnd; y++) {
|
for (var y = drawStart; y < drawEnd; y++) {
|
||||||
var d = (y * 256 - h * 128 + lineHeight * 128) | 0;
|
var d = (y * 256 - h * 128 + lineHeight * 128) | 0;
|
||||||
var texY = ((d * texHeight) / (lineHeight * 256))/* | 0*/;
|
var texY = ((d * texHeight) / (lineHeight * 256))/* | 0*/;
|
||||||
if (texY < 0) texY = 0;
|
if (texY < 0) texY = 0;
|
||||||
|
|
||||||
var color;
|
var color;
|
||||||
if (filtering) {
|
if (filtering) {
|
||||||
var ty1 = texY | 0;
|
var ty1 = texY | 0;
|
||||||
var ty2 = (ty1 + 1) % texHeight;
|
var ty2 = (ty1 + 1) % texHeight;
|
||||||
var tx1 = texX | 0;
|
var tx1 = texX | 0;
|
||||||
var tx2 = (tx1 + 1) % texWidth;
|
var tx2 = (tx1 + 1) % texWidth;
|
||||||
var xf = texX - (texX | 0);
|
var xf = texX - (texX | 0);
|
||||||
var yf = texY - (texY | 0);
|
var yf = texY - (texY | 0);
|
||||||
|
|
||||||
color = [0,0,0];
|
color = [0,0,0];
|
||||||
var c1 = wallTex[texWidth * ty1 + tx1];
|
var c1 = wallTex[texWidth * ty1 + tx1];
|
||||||
var c2 = wallTex[texWidth * ty1 + tx2];
|
var c2 = wallTex[texWidth * ty1 + tx2];
|
||||||
var c3 = wallTex[texWidth * ty2 + tx1];
|
var c3 = wallTex[texWidth * ty2 + tx1];
|
||||||
var c4 = wallTex[texWidth * ty2 + tx2];
|
var c4 = wallTex[texWidth * ty2 + tx2];
|
||||||
|
|
||||||
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
||||||
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
||||||
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
||||||
} else {
|
} else {
|
||||||
texX |= 0;
|
texX |= 0;
|
||||||
texY |= 0;
|
texY |= 0;
|
||||||
color = wallTex[texHeight * texY + texX];
|
color = wallTex[texHeight * texY + texX];
|
||||||
}
|
}
|
||||||
var i = 4 * (w * y) + 4 * x;
|
var i = 4 * (w * y) + 4 * x;
|
||||||
|
|
||||||
buffer[i + 0] = color[0] * shade;
|
buffer[i + 0] = color[0] * shade;
|
||||||
buffer[i + 1] = color[1] * shade;
|
buffer[i + 1] = color[1] * shade;
|
||||||
buffer[i + 2] = color[2] * shade;
|
buffer[i + 2] = color[2] * shade;
|
||||||
buffer[i + 3] = 255;
|
buffer[i + 3] = 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
var floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall
|
var floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall
|
||||||
//4 different wall directions possible
|
//4 different wall directions possible
|
||||||
if (side == 0 && rayDirX > 0) {
|
if (side == 0 && rayDirX > 0) {
|
||||||
floorXWall = mapX;
|
floorXWall = mapX;
|
||||||
floorYWall = mapY + wallX;
|
floorYWall = mapY + wallX;
|
||||||
} else if (side == 0 && rayDirX < 0) {
|
} else if (side == 0 && rayDirX < 0) {
|
||||||
floorXWall = mapX + 1.0;
|
floorXWall = mapX + 1.0;
|
||||||
floorYWall = mapY + wallX;
|
floorYWall = mapY + wallX;
|
||||||
} else if (side == 1 && rayDirY > 0) {
|
} else if (side == 1 && rayDirY > 0) {
|
||||||
floorXWall = mapX + wallX;
|
floorXWall = mapX + wallX;
|
||||||
floorYWall = mapY;
|
floorYWall = mapY;
|
||||||
} else /* side == 1 && rayDirY > 0*/{
|
} else /* side == 1 && rayDirY > 0*/{
|
||||||
floorXWall = mapX + wallX;
|
floorXWall = mapX + wallX;
|
||||||
floorYWall = mapY + 1.0;
|
floorYWall = mapY + 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentDist;
|
var currentDist;
|
||||||
var distWall = perpWallDist;
|
var distWall = perpWallDist;
|
||||||
var distPlayer = 0.0;
|
var distPlayer = 0.0;
|
||||||
|
|
||||||
if (drawEnd < 0) drawEnd = h; //becomes < 0 when the integer overflows
|
if (drawEnd < 0) drawEnd = h; //becomes < 0 when the integer overflows
|
||||||
|
|
||||||
var ceilTex = texture[2];
|
var ceilTex = texture[2];
|
||||||
var floorTex = texture[1];
|
var floorTex = texture[1];
|
||||||
|
|
||||||
//draw the floor from drawEnd to the bottom of the screen
|
//draw the floor from drawEnd to the bottom of the screen
|
||||||
for(var y = drawEnd; y < h; y++)
|
for(var y = drawEnd; y < h; y++)
|
||||||
{
|
{
|
||||||
currentDist = h / (2.0 * y - h); //you could make a small lookup table for this instead
|
currentDist = h / (2.0 * y - h); //you could make a small lookup table for this instead
|
||||||
|
|
||||||
var weight = (currentDist - distPlayer) / (distWall - distPlayer);
|
var weight = (currentDist - distPlayer) / (distWall - distPlayer);
|
||||||
var nextWeight = (h / (2.0 * (y+1) - h)) / distWall;
|
var nextWeight = (h / (2.0 * (y+1) - h)) / distWall;
|
||||||
|
|
||||||
var currentFloorX = weight * floorXWall + (1.0 - weight) * posX;
|
var currentFloorX = weight * floorXWall + (1.0 - weight) * posX;
|
||||||
var currentFloorY = weight * floorYWall + (1.0 - weight) * posY;
|
var currentFloorY = weight * floorYWall + (1.0 - weight) * posY;
|
||||||
|
|
||||||
var floorTexX = (currentFloorX * texWidth) % texWidth;
|
var floorTexX = (currentFloorX * texWidth) % texWidth;
|
||||||
var floorTexY = (currentFloorY * texHeight) % texHeight;
|
var floorTexY = (currentFloorY * texHeight) % texHeight;
|
||||||
|
|
||||||
if (floorTexX < 0) floorTexX = 0;
|
if (floorTexX < 0) floorTexX = 0;
|
||||||
if (floorTexY < 0) floorTexY = 0;
|
if (floorTexY < 0) floorTexY = 0;
|
||||||
|
|
||||||
var color;
|
var color;
|
||||||
if (filtering) {
|
if (filtering) {
|
||||||
ty1 = floorTexY | 0;
|
ty1 = floorTexY | 0;
|
||||||
ty2 = (ty1 + 1) % texHeight;
|
ty2 = (ty1 + 1) % texHeight;
|
||||||
tx1 = floorTexX | 0;
|
tx1 = floorTexX | 0;
|
||||||
tx2 = (tx1 + 1) % texWidth;
|
tx2 = (tx1 + 1) % texWidth;
|
||||||
xf = floorTexX - (floorTexX | 0);
|
xf = floorTexX - (floorTexX | 0);
|
||||||
yf = floorTexY - (floorTexY | 0);
|
yf = floorTexY - (floorTexY | 0);
|
||||||
|
|
||||||
color = [0,0,0];
|
color = [0,0,0];
|
||||||
c1 = floorTex[texWidth * ty1 + tx1];
|
c1 = floorTex[texWidth * ty1 + tx1];
|
||||||
c2 = floorTex[texWidth * ty1 + tx2];
|
c2 = floorTex[texWidth * ty1 + tx2];
|
||||||
c3 = floorTex[texWidth * ty2 + tx1];
|
c3 = floorTex[texWidth * ty2 + tx1];
|
||||||
c4 = floorTex[texWidth * ty2 + tx2];
|
c4 = floorTex[texWidth * ty2 + tx2];
|
||||||
|
|
||||||
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
||||||
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
||||||
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
||||||
} else {
|
} else {
|
||||||
color = floorTex[texWidth * (floorTexY|0) + (floorTexX|0)];
|
color = floorTex[texWidth * (floorTexY|0) + (floorTexX|0)];
|
||||||
}
|
}
|
||||||
|
|
||||||
//floor
|
//floor
|
||||||
i = 4 * (w * y) + 4 * x;
|
i = 4 * (w * y) + 4 * x;
|
||||||
|
|
||||||
buffer[i+0] = (color[0])/2;
|
buffer[i+0] = (color[0])/2;
|
||||||
buffer[i+1] = (color[1])/2;
|
buffer[i+1] = (color[1])/2;
|
||||||
buffer[i+2] = (color[2])/2;
|
buffer[i+2] = (color[2])/2;
|
||||||
buffer[i+3] = 255;
|
buffer[i+3] = 255;
|
||||||
|
|
||||||
if (filtering) {
|
if (filtering) {
|
||||||
color = [0,0,0];
|
color = [0,0,0];
|
||||||
c1 = ceilTex[texWidth * ty1 + tx1];
|
c1 = ceilTex[texWidth * ty1 + tx1];
|
||||||
c2 = ceilTex[texWidth * ty1 + tx2];
|
c2 = ceilTex[texWidth * ty1 + tx2];
|
||||||
c3 = ceilTex[texWidth * ty2 + tx1];
|
c3 = ceilTex[texWidth * ty2 + tx1];
|
||||||
c4 = ceilTex[texWidth * ty2 + tx2];
|
c4 = ceilTex[texWidth * ty2 + tx2];
|
||||||
|
|
||||||
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
color[0] = (c1[0]*(1-xf)*(1-yf) + c2[0]*xf*(1-yf) + c3[0]*(1-xf)*yf + c4[0]*xf*yf) | 0;
|
||||||
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
color[1] = (c1[1]*(1-xf)*(1-yf) + c2[1]*xf*(1-yf) + c3[1]*(1-xf)*yf + c4[1]*xf*yf) | 0;
|
||||||
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
color[2] = (c1[2]*(1-xf)*(1-yf) + c2[2]*xf*(1-yf) + c3[2]*(1-xf)*yf + c4[2]*xf*yf) | 0;
|
||||||
} else {
|
} else {
|
||||||
color = ceilTex[texWidth * (floorTexY|0) + (floorTexX|0)];
|
color = ceilTex[texWidth * (floorTexY|0) + (floorTexX|0)];
|
||||||
}
|
}
|
||||||
|
|
||||||
//ceiling (symmetrical!)
|
//ceiling (symmetrical!)
|
||||||
i = 4 * (w * (h - y - 1)) + 4 * x;
|
i = 4 * (w * (h - y - 1)) + 4 * x;
|
||||||
|
|
||||||
buffer[i+0] = color[0]/2;
|
buffer[i+0] = color[0]/2;
|
||||||
buffer[i+1] = color[1]/2;
|
buffer[i+1] = color[1]/2;
|
||||||
buffer[i+2] = color[2]/2;
|
buffer[i+2] = color[2]/2;
|
||||||
buffer[i+3] = 255;
|
buffer[i+3] = 255;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
oldTime = time;
|
oldTime = time;
|
||||||
time = Date.now()
|
time = Date.now()
|
||||||
var frameTime = (time - oldTime) / 1000.0; //frameTime is the time this frame has taken, in seconds
|
var frameTime = (time - oldTime) / 1000.0; //frameTime is the time this frame has taken, in seconds
|
||||||
|
|
||||||
//speed modifiers
|
//speed modifiers
|
||||||
moveSpeed = frameTime * 5.0; //the constant value is in squares/second
|
moveSpeed = frameTime * 5.0; //the constant value is in squares/second
|
||||||
rotSpeed = frameTime * 3.0; //the constant value is in radians/second
|
rotSpeed = frameTime * 3.0; //the constant value is in radians/second
|
||||||
|
|
||||||
g.putImageData(imagedata, 0, 0);
|
g.putImageData(imagedata, 0, 0);
|
||||||
g.font = "bold 30pt Monospace";
|
g.font = "bold 30pt Monospace";
|
||||||
g.fillText(""+((1000 / (time - oldTime))|0), 0, 30);
|
g.fillText(""+((1000 / (time - oldTime))|0), 0, 30);
|
||||||
};
|
};
|
||||||
|
|
||||||
function tick() {
|
function tick() {
|
||||||
draw();
|
draw();
|
||||||
input();
|
input();
|
||||||
keys.tick();
|
keys.tick();
|
||||||
|
|
||||||
window.requestAnimFrame(tick);
|
window.requestAnimFrame(tick);
|
||||||
//window.setTimeout(tick, 1);
|
//window.setTimeout(tick, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
initTexture();
|
initTexture();
|
||||||
tick();
|
tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
return {start: start}
|
return {start: start}
|
||||||
}());
|
}());
|
@ -1,63 +1,63 @@
|
|||||||
var raycast = raycast || {};
|
var raycast = raycast || {};
|
||||||
|
|
||||||
raycast.texture = (function () {
|
raycast.texture = (function () {
|
||||||
function load(id) {
|
function load(id) {
|
||||||
var canvas = document.createElement('canvas');
|
var canvas = document.createElement('canvas');
|
||||||
var image = document.getElementById(id);
|
var image = document.getElementById(id);
|
||||||
canvas.width = image.width;
|
canvas.width = image.width;
|
||||||
canvas.height = image.height;
|
canvas.height = image.height;
|
||||||
var ctx = canvas.getContext('2d');
|
var ctx = canvas.getContext('2d');
|
||||||
ctx.drawImage(image, 0, 0);
|
ctx.drawImage(image, 0, 0);
|
||||||
var imagedata = ctx.getImageData(0, 0, image.width, image.height);
|
var imagedata = ctx.getImageData(0, 0, image.width, image.height);
|
||||||
var rgbArray = new Array(image.width * image.height);
|
var rgbArray = new Array(image.width * image.height);
|
||||||
for( var i = 0; i < image.width * image.height; i++) {
|
for( var i = 0; i < image.width * image.height; i++) {
|
||||||
rgbArray[i] = [imagedata.data[4*i], imagedata.data[4*i+1], imagedata.data[4*i+2]];
|
rgbArray[i] = [imagedata.data[4*i], imagedata.data[4*i+1], imagedata.data[4*i+2]];
|
||||||
}
|
}
|
||||||
image.width = 0;
|
image.width = 0;
|
||||||
image.height = 0;
|
image.height = 0;
|
||||||
return rgbArray;
|
return rgbArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
var images = [];
|
var images = [];
|
||||||
|
|
||||||
function initiateLoad(textures, onSuccess) {
|
function initiateLoad(textures, onSuccess) {
|
||||||
var n = textures.length;
|
var n = textures.length;
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
|
|
||||||
var callback = function() {
|
var callback = function() {
|
||||||
counter++;
|
counter++;
|
||||||
console.log(counter+" of "+n+" textures received")
|
console.log(counter+" of "+n+" textures received")
|
||||||
if (counter == n) onSuccess();
|
if (counter == n) onSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < n; i++) {
|
for (var i = 0; i < n; i++) {
|
||||||
var image = new Image()
|
var image = new Image()
|
||||||
image.onload = callback;
|
image.onload = callback;
|
||||||
image.src = textures[i];
|
image.src = textures[i];
|
||||||
images.push(image);
|
images.push(image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTextures() {
|
function getTextures() {
|
||||||
var n = images.length;
|
var n = images.length;
|
||||||
var textures = [];
|
var textures = [];
|
||||||
for (var i = 0; i < n; i++) {
|
for (var i = 0; i < n; i++) {
|
||||||
var canvas = document.createElement('canvas');
|
var canvas = document.createElement('canvas');
|
||||||
var image = images[i];
|
var image = images[i];
|
||||||
canvas.width = image.width;
|
canvas.width = image.width;
|
||||||
canvas.height = image.height;
|
canvas.height = image.height;
|
||||||
var ctx = canvas.getContext('2d');
|
var ctx = canvas.getContext('2d');
|
||||||
ctx.drawImage(image, 0, 0);
|
ctx.drawImage(image, 0, 0);
|
||||||
var imagedata = ctx.getImageData(0, 0, image.width, image.height);
|
var imagedata = ctx.getImageData(0, 0, image.width, image.height);
|
||||||
var rgbArray = new Array(image.width * image.height);
|
var rgbArray = new Array(image.width * image.height);
|
||||||
for(var j = 0; j < image.width * image.height; j++) {
|
for(var j = 0; j < image.width * image.height; j++) {
|
||||||
rgbArray[j] = [imagedata.data[4*j], imagedata.data[4*j+1], imagedata.data[4*j+2]];
|
rgbArray[j] = [imagedata.data[4*j], imagedata.data[4*j+1], imagedata.data[4*j+2]];
|
||||||
}
|
}
|
||||||
textures.push(rgbArray);
|
textures.push(rgbArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
return textures;
|
return textures;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {load: load, initiateLoad: initiateLoad, getTextures: getTextures};
|
return {load: load, initiateLoad: initiateLoad, getTextures: getTextures};
|
||||||
}());
|
}());
|