OctopusBomb/public/src/player.js

119 lines
3.0 KiB
JavaScript

const player1 = {
x: 0,
y: 0,
isMoving: false,
isAlive: true,
};
function movePlayer() {
let isMoving = false;
let stepp = Math.floor(deltaTime / 16 + steppOnStart - 1) + 1;
let dx = 0;
let dy = 0;
// 88 - x key - kill player
if (keyIsDown(88)) {
player1.isAlive = false;
let isMoving = true;
}
if (player1.isAlive) {
// ENTER key - drop bomb - space malfunction on any, dell, keyboard
if (keyIsDown(ENTER)) {
placeBomb(player1.x, player1.y);
}
if (keyIsDown(RIGHT_ARROW)) {
dx += 1;
isMoving = true;
}
if (keyIsDown(LEFT_ARROW)) {
dx -= 1;
isMoving = true;
}
if (keyIsDown(DOWN_ARROW)) {
dy += 1;
isMoving = true;
}
if (keyIsDown(UP_ARROW)) {
dy -= 1;
isMoving = true;
}
for (let i = 0; i < stepp; i++) {
const destinationLeftTop = positionsToCellsIdx({
x: player1.x + dx + offsetCollision - halfCellSize,
y: player1.y + dy + offsetCollision - halfCellSize,
});
const destinationRightTop = positionsToCellsIdx({
x: player1.x + dx - offsetCollision + halfCellSize,
y: player1.y + dy + offsetCollision - halfCellSize,
});
const destinationLeftBottom = positionsToCellsIdx({
x: player1.x + dx + offsetCollision - halfCellSize,
y: player1.y + dy - offsetCollision + halfCellSize,
});
const destinationRightBottom = positionsToCellsIdx({
x: player1.x + dx - offsetCollision + halfCellSize,
y: player1.y + dy - offsetCollision + halfCellSize,
});
player1.isMoving = isMoving;
//console.log(destinationLeftTop);
if (
//true
isCollidingWithObstractle(destinationLeftTop) ||
isCollidingWithObstractle(destinationRightTop) ||
isCollidingWithObstractle(destinationLeftBottom) ||
isCollidingWithObstractle(destinationRightBottom)
) {
return;
//console.log(positionsToCellsIdx(player1));
}
player1.x += dx;
player1.y += dy;
const player1overBorder = overBorder({ x: player1.x, y: player1.y });
player1.x = player1overBorder.x;
player1.y = player1overBorder.y;
//console.log(positionsToCellsIdx({
// x: player1.x,
// y: player1.y,
//}))
//console.log(player1overBorder);
console.log([player1.x, player1.y]);
}
}
}
function drawPlayer() {
let dampingX = 0;
let dampingY = 0;
if (player1.isMoving || !player1.isAlive) {
dampingX = Math.sin(millis() / (1800 / gameFrameRate)) * damping;
dampingY = Math.cos(millis() / (1800 / gameFrameRate)) * damping;
}
textSize(cellSize / 1.2);
if (player1.isAlive) {
text("🐙", player1.x + dampingX - halfCellSize, player1.y + dampingY + yOffsetDraw - halfCellSize);
if(debug){fill(0, 255, 0);
circle(player1.x + dampingX, player1.y + dampingY, 8);}
} else {
text("💀", player1.x - halfCellSize, player1.y + dampingY + yOffsetDraw - halfCellSize);
}
}