プレイヤー正面の配列を取得する。
let p1, arr;
const maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
function setup() {
createCanvas(400, 400);
p1 = new Player();
arr = p1.getFrontView(maze,p1.direction,p1.position[0],p1.position[1]);
p1.printPosition();
p1.printDirection();
p1.printFrontView(maze,p1.direction,p1.position[0],p1.position[1]);
}
function draw() {
background(220);
if (arr[0][0] === 1) {
fill(255);
rect(0,150,150,100);
noFill();
}
if (arr[0][2] === 1) {
fill(255);
rect(width-150,150,150,100);
noFill();
}
if (arr[0][1] === 1) {
fill(255);
rect(150,150,100,100);
noFill();
}
if (arr[1][0] === 1) {
fill(255);
rect(0,100,100,200);
beginShape();
vertex(100,100);
vertex(150,150);
vertex(150,height-150);
vertex(100,height-100);
endShape();
noFill();
}
if (arr[1][2] === 1) {
fill(255);
rect(width-100,100,100,200);
beginShape();
vertex(width-100,100);
vertex(width-150,150);
vertex(width-150,height-150);
vertex(width-100,height-100);
endShape();
noFill();
}
if (arr[1][1] === 1) {
fill(255);
rect(100,100,200,200);
noFill();
}
if (arr[2][0] === 1) {
fill(255);
rect(0,50,50,300);
beginShape();
vertex(50,50);
vertex(100,100);
vertex(100,height-100);
vertex(50,height-50);
endShape();
noFill();
noFill();
}
if (arr[2][2] === 1) {
fill(255);
rect(width-50,50,50,300);
beginShape();
vertex(width-50,50);
vertex(width-100,100);
vertex(width-100,height-100);
vertex(width-50,height-50);
endShape();
noFill();
}
if (arr[2][1] === 1) {
fill(255);
rect(50,50,300,300);
noFill();
}
if (arr[3][0] === 1) {
fill(255);
beginShape();
vertex(0,0);
vertex(50,50);
vertex(50,height-50);
vertex(0,height);
endShape();
noFill();
}
if (arr[3][2] === 1) {
fill(255);
beginShape();
vertex(width,0);
vertex(width-50,50);
vertex(width-50,height-50);
vertex(width,height);
endShape();
noFill();
}
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
p1.turnLeft();
}
if (keyCode === RIGHT_ARROW) {
p1.turnRight();
}
if (keyCode === UP_ARROW) {
if (p1.direction === p1.NORTH) {
if (maze[p1.position[1]-1][p1.position[0]] === 0) {
p1.position[1]--;
}
} else if (p1.direction === p1.SOUTH) {
if (maze[p1.position[1]+1][p1.position[0]] === 0) {
p1.position[1]++;
}
} else if (p1.direction === p1.WEST) {
if (maze[p1.position[1]][p1.position[0]-1] === 0) {
p1.position[0]--;
}
} else if (p1.direction === p1.EAST) {
if (maze[p1.position[1]][p1.position[0]+1] === 0) {
p1.position[0]++;
}
}
}
if (keyCode === DOWN_ARROW) {
if (p1.direction === p1.NORTH) {
if (maze[p1.position[1]+1][p1.position[0]] === 0) {
p1.position[1]++;
}
} else if (p1.direction === p1.SOUTH) {
if (maze[p1.position[1]-1][p1.position[0]] === 0) {
p1.position[1]--;
}
} else if (p1.direction === p1.WEST) {
if (maze[p1.position[1]][p1.position[0]+1] === 0) {
p1.position[0]++;
}
} else if (p1.direction === p1.EAST) {
if (maze[p1.position[1]][p1.position[0]-1] === 0) {
p1.position[0]--;
}
}
}
arr = p1.getFrontView(maze,p1.direction,p1.position[0],p1.position[1]);
p1.printPosition();
p1.printDirection();
p1.printFrontView(maze,p1.direction,p1.position[0],p1.position[1]);
}
class Player {
constructor() {
this.NORTH = 0;
this.SOUTH = 1;
this.WEST = 2;
this.EAST = 3;
this.direction = this.SOUTH;
this.position = [1, 1];
}
getFrontView(arr,d,x,y) {
if (d === 0) {
const px = x-1;
const py = y-3;
const front_arr = [];
let tmp = [];
for (let y = Math.max(py,0); y < Math.min(py+4,arr.length); y++) {
for (let x = Math.max(px,0); x < Math.min(px+3,arr[0].length); x++) {
tmp.push(arr[y][x]);
}
front_arr.push(tmp)
tmp = [];
}
const view_arr = [...Array(4)].map(() => Array(3).fill(1));
const flip_arr = this.flip(front_arr);
for (let y = 0; y < front_arr.length; y++) {
for (let x = 0; x < 3; x++) {
view_arr[y][x] = flip_arr[y][x];
}
}
return this.flip(view_arr);
} else if (d === 1) {
const px = x-1;
const py = y;
const front_arr = [];
let tmp = [];
for (let y = Math.max(py,0); y < Math.min(py+4,arr.length); y++) {
for (let x = Math.max(px,0); x < Math.min(px+3,arr[0].length); x++) {
tmp.push(arr[y][x]);
}
front_arr.push(tmp)
tmp = [];
}
const view_arr = [...Array(4)].map(() => Array(3).fill(1));
for (let y = 0; y < front_arr.length; y++) {
for (let x = 0; x < 3; x++) {
view_arr[y][x] = front_arr[y][x];
}
}
return this.flip(view_arr);
} else if (d === 2) {
const px = x-3;
const py = y-1;
const front_arr = [];
let tmp = [];
for (let y = Math.max(py,0); y < Math.min(py+3,arr.length); y++) {
for (let x = Math.max(px,0); x < Math.min(px+4,arr[0].length); x++) {
tmp.push(arr[y][x]);
}
front_arr.push(tmp)
tmp = [];
}
const view_arr = [...Array(4)].map(() => Array(3).fill(1));
const rot_arr = this.flip(this.rot90(this.rot90(this.rot90(front_arr))));
for (let y = 0; y < rot_arr.length; y++) {
for (let x = 0; x < 3; x++) {
view_arr[y][x] = rot_arr[y][x];
}
}
return this.flip(view_arr);
} else if (d === 3) {
const px = x;
const py = y-1;
const front_arr = [];
let tmp = [];
for (let y = Math.max(py,0); y < Math.min(py+3,arr.length); y++) {
for (let x = Math.max(px,0); x < Math.min(px+4,arr[0].length); x++) {
tmp.push(arr[y][x]);
}
front_arr.push(tmp)
tmp = [];
}
const view_arr = [...Array(4)].map(() => Array(3).fill(1));
const rot_arr = this.flip(this.rot90(front_arr));
for (let y = 0; y < rot_arr.length; y++) {
for (let x = 0; x < 3; x++) {
view_arr[y][x] = rot_arr[y][x];
}
}
return this.flip(view_arr);
}
}
turnLeft() {
if (this.direction === this.NORTH) {
this.direction = this.WEST;
} else if (this.direction === this.WEST) {
this.direction = this.SOUTH;
} else if (this.direction === this.SOUTH) {
this.direction = this.EAST;
} else if (this.direction === this.EAST) {
this.direction = this.NORTH;
}
}
turnRight() {
if (this.direction === this.NORTH) {
this.direction = this.EAST;
} else if (this.direction === this.EAST) {
this.direction = this.SOUTH;
} else if (this.direction === this.SOUTH) {
this.direction = this.WEST;
} else if (this.direction === this.WEST) {
this.direction = this.NORTH;
}
}
rot90(arr) {
return arr[0].map((col,i) => arr.map(row => row[row.length-1-i]));
}
flip(arr) {
return arr.map((row,i) => arr[arr.length-1-i]).map(row => arr[0].map((col,i) => row[row.length-1-i]));
}
printPosition() {
console.log('position: ' + p1.position);
}
printDirection() {
if (this.direction === this.NORTH) {
console.log('direction: NORTH');
} else if (this.direction === this.SOUTH) {
console.log('direction: SOUTH');
} else if (this.direction === this.WEST) {
console.log('direction: WEST');
} else if (this.direction === this.EAST) {
console.log('direction: EAST');
}
}
printFrontView(arr,d,x,y) {
const view_arr = this.getFrontView(arr,d,x,y);
for (let i = 0; i < view_arr.length; i++) {
console.log(...view_arr[i]);
}
}
}
get-front-view by inoha_naito -p5.js Web Editor
A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators,...