xxxxxxxxxx
125
let particle;
let mode;
let walls = [];
const dtheta = 0.01;
let sceneW = window.innerWidth;
let sceneH = window.innerHeight;
const horizon = 0.5;
const stakeWidth = 10;
let hardWalls;
function setup() {
createCanvas(sceneW, sceneH);
particle = new Particle(100, 100, round(2 / dtheta));
mode = 1;
/*
walls.push(new Boundary(300, 200, 100, 400, [255]));
walls.push(new Boundary(100, 400, 220, 400, [255]));
walls.push(new Boundary(220, 400, 300, 200, [255]));
*/
walls.push(new Boundary(sceneW / 2 + 50, sceneH / 2 + 50, sceneW / 2 + 50, sceneH / 2 - 50, [255]));
walls.push(new Boundary(sceneW / 2 + 50, sceneH / 2 - 50, sceneW / 2 - 50, sceneH / 2 - 50, [255]));
walls.push(new Boundary(sceneW / 2 - 50, sceneH / 2 - 50, sceneW / 2 - 50, sceneH / 2 + 50, [255]));
walls.push(new Boundary(sceneW / 2 - 50, sceneH / 2 + 50, sceneW / 2 + 50, sceneH / 2 + 50, [255]));
hardWalls = walls.length;
particle.walls = walls.slice();
}
function draw() {
background(0);
if (keyIsDown(LEFT_ARROW)) {
particle.rotateBy(-0.07);
} else if (keyIsDown(RIGHT_ARROW)) {
particle.rotateBy(0.07);
}
if (keyIsDown(UP_ARROW)) {
particle.move(10);
} else if (keyIsDown(DOWN_ARROW)) {
particle.move(-10);
}
if (keyIsDown(86)) {
particle.moveSide(10);
} else if (keyIsDown(88)) {
particle.moveSide(-10);
}
processTouches();
particle.show(mode);
if (mode == 1) {
for (let ni = 0; ni < walls.length; ni++) {
walls[ni].show();
}
} else if (mode == 2) {
fill(50);
//rect(0, sceneH / 2, sceneW, sceneH);
rectMode(CORNER);
rect(0, sceneH * horizon * 1.05, sceneW, (1 - horizon) * sceneH);
}
for (let ti = 0; ti <= particle.fov; ti++) {
let theta = particle.dir - particle.fov / 2 * dtheta + dtheta * ti;
d = particle.checkTheta(walls, theta);
if (mode == 1) {
strokeWeight(min(100 / d, 10));
line(particle.pos.x, particle.pos.y, particle.pos.x + d * cos(theta), particle.pos.y + d * sin(theta));
} else if (mode == 2) {
d = map(d, 0, sceneW * 10, 0, 1);
fill(0.5 / (d * d));
noStroke();
rectMode(CENTER);
if (particle.ori == 1) {
rect((ti + 0.5) * sceneW / particle.fov, sceneH * horizon, sceneW / particle.fov + 1, 10 / d);
} else if (particle.ori == -1) {
rect(sceneW - (ti + 0.5) * sceneW / particle.fov, sceneH * horizon, sceneW / particle.fov + 1, 10 / d);
}
}
}
}
function keyPressed() {
if (keyCode === ENTER) {
walls.push(new Boundary(particle.pos.x, particle.pos.y + stakeWidth, particle.pos.x + stakeWidth, particle.pos.y + stakeWidth));
walls.push(new Boundary(particle.pos.x + stakeWidth, particle.pos.y, particle.pos.x + stakeWidth, particle.pos.y + stakeWidth));
} else if (keyCode === BACKSPACE) {
if (walls.length > hardWalls) {
walls.pop();
}
if (walls.length > hardWalls) {
walls.pop();
}
}
}
function keyTyped() {
if (key === 's') {
mode = (mode % 2) + 1;
}
}
function processTouches() {
const steerX = 150;
strokeWeight(3);
stroke(255,100);
line(sceneW-steerX-50,sceneH-steerX,sceneW-steerX+50,sceneH-steerX);
line(steerX,sceneH-steerX-50,steerX,sceneH-steerX+50);
if (touches.length > 0) {
let vx = 0;
let vy = 0;
for (let ti = 0; ti < touches.length; ti++) {
let touch = touches[ti];
if (touch.x < sceneW / 2) {
vx += (steerX- touch.x)/sceneW;
} else if (touch.x > sceneW / 2) {
vy += (sceneH / 2 - touch.y) * 2 / sceneH;
}
}
particle.rotateBy(-vx/10);
particle.move(vy*10);
}
}
function touchStarted() {
if (touches.length == 3) {
mode = (mode % 2) + 1;
return
}
}