xxxxxxxxxx
111
// 2D Visibility
// Ray Casting
// CT Commuity Contribution
// by Simon Tiger
let walls = [];
let ray;
let particle;
const MOVING = 0;
const MAKING = 1;
const REMOVE = 2;
let state = MOVING;
let txt = '';
let keyPressedCode = null;
function setup() {
createCanvas(640, 480);
// for (let i = 0; i < 5; i++) {
// let x1 = random(width);
// let x2 = random(width);
// let y1 = random(height);
// let y2 = random(height);
// walls[i] = new Boundary(x1, y1, x2, y2);
// }
walls.push(new Boundary(-1, -1, width, -1, NaN));
walls.push(new Boundary(width, -1, width, height, NaN));
walls.push(new Boundary(width, height, -1, height, NaN));
walls.push(new Boundary(-1, height, -1, -1, NaN));
particle = new Particle();
}
function removeWall(id) {
for (let i in walls) {
if (walls[i].id === id) {
walls.splice(i, 1);
// Should I break out of here??
}
}
}
function keyPressed() {
keyPressedCode = keyCode;
if (state === REMOVE) {
if (keyCode >= 48 && keyCode < 58) {
txt += key;
} else if (keyCode === 13) {
const id = Number(txt.substring(7, txt.length));
removeWall(id);
txt = '';
state = MOVING;
} else if (keyCode === 27) {
const id = Number(txt.substring(7, txt.length));
txt = '';
state = MOVING;
} else if (keyCode === 8 && txt.length > 7) {
txt = txt.substring(0, txt.length-1);
}
} else if (key.toLowerCase() === 'r') {
state = REMOVE;
txt = 'Remove ';
}
}
function keyReleased() {
keyPressedCode = null;
}
function mouseReleased() {
if (state === MAKING) {
state = MOVING;
}
}
function draw() {
background(255);
for (let wall of walls) {
wall.show();
}
if (mouseIsPressed) {
if (mouseButton === LEFT && state === MOVING && keyPressedCode !== 16) {
particle.update(mouseX, mouseY);
} else {
if (state === MOVING) {
const id = walls.length-4;
walls.push(new Boundary(mouseX, mouseY, mouseX, mouseY, id));
state = MAKING;
} else if (state === MAKING) {
walls[walls.length-1].b.set(mouseX, mouseY);
}
}
}
// particle.show();
particle.look(walls);
text(txt, 15, 25);
// ray.show();
// ray.lookAt(mouseX, mouseY);
// let pt = ray.cast(wall);
// if (pt) {
// fill(255);
// ellipse(pt.x, pt.y, 8, 8);
// }
}