Skip to content

Commit

Permalink
Add collision between player and tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Gonzalez committed Apr 19, 2024
1 parent 7cee364 commit 8213c5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/game/components/levels/level.001.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1],
[1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
}
1 change: 1 addition & 0 deletions src/game/scenes/play.scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default class ScenePlay extends Scene {
);
this.currentGame = new GameLogic(this.level.playerInitialPosition);
this.currentGame.player.component = this.player;
this.currentGame.level = this.level;

// score component
const score = new Score(
Expand Down
24 changes: 22 additions & 2 deletions src/game/scenes/shared/game.logic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addVectors, detectCollision, lerpVector, multiplyVector, normalizeVector } from "../../utils/math";
import { SCREEN_HEIGHT, SCREEN_WIDTH } from "../../utils/variables";
import DirectionKeys from "./direction-keys";

const GAME_STOP = "3";
Expand All @@ -20,6 +19,9 @@ export default class GameLogic {
/** @member {DirectionKeys} */
this.directionKeys = new DirectionKeys();

/** @member {Level} */
this.level = null;

this.player = {
position: playerPosition,
rotation: Math.PI / 2, // todo: eliminate this property
Expand Down Expand Up @@ -61,12 +63,30 @@ export default class GameLogic {
if (this.directionKeys.hasPressedKeys()) {
const rot = lerpVector(this.player.directionVector, this.directionKeys.directionVector(), 0.95);
this.player.directionVector = multiplyVector(normalizeVector(rot), VELOCITY);

// check collision
const prevPosition = this.player.position;
this.player.position = addVectors(this.player.directionVector, this.player.position);
if (this.checkCollisionWithMap()) {
this.player.position = prevPosition;
}
}

this.player.component.updateCoordinates(this.player.position);
this.player.component.updateDirectionVector({ ...this.player.directionVector, y: -this.player.directionVector.y });
// toDo (gonzalezext)[18.04.24]: validate collision
}

/**
* @returns {boolean}
*/
checkCollisionWithMap() {
// toDo (gonzalezext)[19.04.24]: this need to be optimized (check only with the nearest components)
for (const component of this.level.components) {
if (this.checkCollisionInProjections(this.player.component.getProjection(), component.getProjection())) {
return true;
}
}
return false;
}

canPauseGame() {
Expand Down

0 comments on commit 8213c5e

Please sign in to comment.