xxxxxxxxxx
251
var setup, draw, spawnPoints, drawWalls, moveParticle;
let gameSize, partPos, particle, margin, cols, rows, walls, points, wallsPos;
(function () {
var _$0 = this;
var _8 = function () {
createCanvas(gameSize * 2, gameSize);
partPos = createVector(gameSize / 2, gameSize / 2);
particle = new Particle(gameSize / 2, gameSize / 2);
margin = createVector(gameSize / cols, gameSize / rows);
spawnPoints();
drawWalls();
};
var _9 = function () {
background(0);
particle.update(partPos.x, partPos.y);
moveParticle();
const scene = particle.look(walls);
const w = gameSize / scene.length;
push();
translate(gameSize, 0);
for (let i = 0; i < scene.length; i++) {
noStroke();
const b = map(scene[i], 0, gameSize * 1.41, 255, 0);
const h = map(scene[i], 0, gameSize * 1.41, gameSize * 1.41, 0);
colorMode(HSB, 255);
fill(b, 255, b);
rectMode(CENTER);
rect(i * w + w / 2, gameSize / 2, w, h);
}
pop();
particle.show();
for (let wall of walls) {
wall.show();
}
};
var _A = function () {
push();
translate(margin.x / 2, margin.y / 2);
let p = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (points[p] == 1) {
stroke(255);
point(j * margin.x, i * margin.y);
wallsPos.push(createVector(j * margin.x, i * margin.y));
}
p++;
}
}
pop();
};
var _B = function () {
for (let p of wallsPos) {
let pos = createVector(p.x + margin.x / 2, p.y + margin.y / 2);
let size = createVector(margin.x / 2, margin.y / 2);
walls.push(new Wall(pos.x - size.x, pos.y - size.y, pos.x + size.x, pos.y - size.y));
walls.push(new Wall(pos.x + size.x, pos.y - size.y, pos.x + size.x, pos.y + size.y));
walls.push(new Wall(pos.x + size.x, pos.y + size.y, pos.x - size.x, pos.y + size.y));
walls.push(new Wall(pos.x - size.x, pos.y + size.y, pos.x - size.x, pos.y - size.y));
}
};
var _C = function () {
if (keyIsPressed == true) {
if (key === "a") {
particle.spin(-0.05);
} else if (key === "d") {
particle.spin(0.05);
}
}
if (keyIsDown(LEFT_ARROW) && partPos.x > 0) {
partPos.x -= 2;
} else if (keyIsDown(RIGHT_ARROW) && partPos.x < gameSize) {
partPos.x += 2;
} else if (keyIsDown(UP_ARROW) && partPos.y > 0) {
partPos.y -= 2;
} else if (keyIsDown(DOWN_ARROW) && partPos.y < gameSize) {
partPos.y += 2;
}
if (partPos.x < 0) {
partPos.x = 0;
} else if (partPos.x > gameSize) {
partPos.x = gameSize;
} else if (partPos.y < 0) {
partPos.y = 0;
} else if (partPos.y > gameSize) {
partPos.y = gameSize;
}
};
_$0.setup = _8;
_$0.draw = _9;
_$0.spawnPoints = _A;
_$0.drawWalls = _B;
_$0.moveParticle = _C;
var _D = class {
constructor(pos, angle) {
this.pos = pos;
this.dir = p5.Vector.fromAngle(angle);
}
show() {
push();
strokeWeight(1);
translate(this.pos.x, this.pos.y);
line(0, 0, this.dir.x, this.dir.y);
pop();
}
setAngle(angle) {
this.dir = p5.Vector.fromAngle(angle);
}
cast(wall) {
const x1 = wall.a.x;
const y1 = wall.a.y;
const x2 = wall.b.x;
const y2 = wall.b.y;
const x3 = this.pos.x;
const y3 = this.pos.y;
const x4 = this.pos.x + this.dir.x;
const y4 = this.pos.y + this.dir.y;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (den == 0) {
return;
}
const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
if (t > 0 && t < 1 && u > 0) {
const pt = createVector();
pt.x = x1 + t * (x2 - x1);
pt.y = y1 + t * (y2 - y1);
return pt;
} else {
return;
}
}
};
_$0.Ray = _D;
var _F = class {
constructor(x, y) {
this.pos = createVector(x, y);
this.rays = [];
this.heading = 0;
for (let a = 0; a < 40; a += 0.1) {
this.rays.push(new Ray(this.pos, radians(a)));
}
}
spin(angle) {
this.heading += angle;
for (let i = 0; i < this.rays.length; i++) {
this.rays[i].setAngle(radians(i) / 10 + this.heading);
}
}
show() {
for (let ray of this.rays) {
ray.show();
}
}
update(x, y) {
this.pos.set(x, y);
}
look(walls) {
const scene = [];
for (let i = 0; i < this.rays.length; i++) {
const ray = this.rays[i];
let record = Infinity;
let closest = null;
for (let wall of walls) {
const pt = ray.cast(wall);
if (pt) {
const d = p5.Vector.dist(this.pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
}
if (closest) {
const di = map(p5.Vector.dist(this.pos, closest), 0, width / 2 * 1.41, 255, 0);
colorMode(HSB, 255);
stroke(di, 255, di);
strokeWeight(1);
line(this.pos.x, this.pos.y, closest.x, closest.y);
}
scene[i] = record;
}
return scene;
}
};
_$0.Particle = _F;
var _H = class {
constructor(x1, y1, x2, y2) {
this.a = createVector(x1, y1);
this.b = createVector(x2, y2);
}
show() {
stroke(255);
strokeWeight(1);
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
};
_$0.Wall = _H;
gameSize = 400;
partPos = void 0;
particle = void 0;
margin = void 0;
cols = 25;
rows = 25;
walls = [];
points = [1, 1, 1, 1, 1, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0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