xxxxxxxxxx
258
let cheat = false;
class Player {
constructor(name, color, manager) {
this.name = name;
// color
this.color = color;
// manager
this.manager = manager;
this.init();
}
init() {
// location
this.i = this.j = 0;
// direction
this.dir = 'B';
// moving
this.movingCnt = 0;
this.updateMovingCnt();
// score
this.scoreCnt = 0;
this.updateScoreCnt();
// eating Item
this.eating = false;
}
eatItem(item) {
if (this.i === item.i && this.j === item.j && this.eating) {
print("Player eats item!");
this.manager.reloadMaze();
}
}
registerMaze(maze) {
/*
해당 player객체를 maze로
*/
this.maze = maze;
}
respawn() {
if (this.dir === 'B') {
this.i = this.maze.cols - 1;
this.j = this.maze.rows - 1;
} else {
this.i = this.j = 0;
}
}
move(dir) {
let currentCell =
this.maze.grid[this.maze.index(this.i, this.j)];
if (currentCell.walls[dir]) {
return;
}
let nextCell = this.maze.getDirCell(currentCell, dir);
this.i = nextCell.i;
this.j = nextCell.j;
this.checkExit();
// print(this.name + ": " + (currentCell !== nextCell));
// 움직였는지 boolean값으로 return
// move: true
// not move: false
if (currentCell !== nextCell) {
this.movingCnt++;
this.updateMovingCnt();
}
}
show() {
let offset = 5;
noStroke();
fill(this.color);
rect(this.i * this.maze.size + offset,
this.j * this.maze.size + offset,
this.maze.size - (2 * offset),
this.maze.size - (2 * offset));
// draw Dir (A, B)
let realX = this.i * this.maze.size;
let realY = this.j * this.maze.size;
textSize(this.size);
fill(255);
text(this.dir,
realX + (this.maze.size * (1 / 6)),
realY + (this.maze.size * (5 / 6)));
}
changeDir() {
if (this.dir === 'A') {
this.dir = 'B';
print("player: " + this.name + ", " + "B dir");
} else if (this.dir === 'B') {
this.dir = 'A';
print("player: " + this.name + ", " + "A dir");
// reset loc (maze's rows, cols can being add)
this.i = this.maze.cols - 1;
this.j = this.maze.rows - 1;
}
}
checkExit() {
if (
((this.i === 0) && (this.j === 0) && this.dir === 'A') ||
((this.i === this.maze.cols - 1) &&
(this.j === this.maze.rows - 1) && this.dir === 'B')) {
// setting
this.manager.reloadMaze(this.maze.size - 5);
this.changeDir();
// Score
this.scoreCnt++;
this.updateScoreCnt();
}
}
updateMovingCnt() {
let id = this.name + "MovingCnt";
let element = document.getElementById(id);
element.innerHTML = this.movingCnt;
}
updateScoreCnt() {
let id = this.name + "ScoreCnt";
let element = document.getElementById(id);
element.innerHTML = this.scoreCnt;
}
}
// #################
// key function list
// #################
function keyPressed() {
let dir1;
let dir2;
print("keyCode: " + keyCode);
switch (keyCode) {
// player1
case UP_ARROW:
dir1 = 0;
break;
case RIGHT_ARROW:
dir1 = 1;
break;
case DOWN_ARROW:
dir1 = 2;
break;
case LEFT_ARROW:
dir1 = 3;
break;
case 49: // number 1
player1.respawn();
break;
case 191: // /(slash)
break;
// player2
case 87: // w
dir2 = 0;
break;
case 68: // d
dir2 = 1;
break;
case 83: // s
dir2 = 2;
break;
case 65: // a
dir2 = 3;
break;
case 50: // number 2
player2.respawn();
break;
case 69: // e
break;
// common
case 77: // m
maze.setPathDrawing(!maze.pathDrawing);
case 189: // -
break;
case 187: // +
break;
case 48: // 0
manager.resetGame();
break;
}
let p1move = player1.move(dir1);
let p2move = player2.move(dir2);
// check which keyIsDown
checkKeyIsDown();
}
function checkKeyIsDown() {
if(keyIsDown(191)) {
player1.eating = true;
return;
}
if(keyIsDown(69)) {
player2.eating = true;
return;
}
player1.eating = false;
player2.eating = false;
}
// function mouseDragged() {
// let dx = movedX;
// let dy = movedY;
// let dir;
// let offset = 8;
// if (dx > offset)
// dir = 1;
// else if (dx < -offset)
// dir = 3;
// else if (dy > offset)
// dir = 2;
// else if (dy < -offset)
// dir = 0;
// // print(dx + " : " + dy);
// player1.move(dir);
// }