feat(#8): added speed control

This commit is contained in:
Juan Sebastián Montoya 2024-09-14 21:19:46 -05:00
parent 731a32303a
commit 580bea0848

View file

@ -2,13 +2,21 @@ import { TILE_SIZE } from "../constants.js";
import { GameObject } from "./game-object.js"; import { GameObject } from "./game-object.js";
export class Camera extends GameObject { export class Camera extends GameObject {
constructor({ gameObjects = [], x = 0, y = 0, width = 160, height = 120 }) { constructor({
gameObjects = [],
x = 0,
y = 0,
width = 160,
height = 120,
speed = 1,
}) {
super({ x, y }); super({ x, y });
this.gameObjects = gameObjects; this.gameObjects = gameObjects;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.speed = speed;
this.keys = [ this.keys = [
{ key: "ArrowUp", pressed: false, value: [0, -1] }, { key: "ArrowUp", pressed: false, value: [0, -1] },
{ key: "ArrowDown", pressed: false, value: [0, 1] }, { key: "ArrowDown", pressed: false, value: [0, 1] },
@ -49,11 +57,11 @@ export class Camera extends GameObject {
const [item] = this.gameObjects; const [item] = this.gameObjects;
const { height, width } = item.selected ?? item; const { height, width } = item.selected ?? item;
this.x = Math.min( this.x = Math.min(
Math.max(this.x + dx * Math.floor(delta * 100), 0), Math.max(this.x + dx * Math.floor(delta * this.speed * 100), 0),
width * TILE_SIZE - this.width width * TILE_SIZE - this.width
); );
this.y = Math.min( this.y = Math.min(
Math.max(this.y + dy * Math.floor(delta * 100), 0), Math.max(this.y + dy * Math.floor(delta * this.speed * 100), 0),
height * TILE_SIZE - this.height height * TILE_SIZE - this.height
); );
} }