Simplificar movimiento de jugadorx y movimiento de enemigos
This commit is contained in:
parent
244523bc00
commit
05ca9e1973
1 changed files with 13 additions and 6 deletions
|
@ -14,6 +14,8 @@ const larretaSprite = loadSprite(larretaImg, 800, 1069, juego => juego.canvas.he
|
|||
const semillaSprite = loadSprite(semillaImg, 480, 640, juego => juego.canvas.height / 8)
|
||||
|
||||
const ENEMIES_NUM = 20
|
||||
const ENEMY_SPEED = 40
|
||||
const PLAYER_SPEED = 200
|
||||
const SEED_SPEED = 1000
|
||||
const SEED_COOLDOWN = 300
|
||||
const MAP_MIN = 1000
|
||||
|
@ -24,7 +26,6 @@ export type State = {
|
|||
current: "jugando"
|
||||
pos: { x: number }
|
||||
view: { x: number }
|
||||
velocity: { x: number }
|
||||
side: "left" | "right"
|
||||
enemies: { x: number }[]
|
||||
seeds: { x: number; velocity: { x: number } }[]
|
||||
|
@ -36,7 +37,6 @@ export function createJugandoState(): State {
|
|||
current: "jugando",
|
||||
pos: { x: MAP_MIN + MAP_SIZE / 2 },
|
||||
view: { x: 0 },
|
||||
velocity: { x: 0 },
|
||||
side: "right",
|
||||
enemies: [],
|
||||
seeds: [],
|
||||
|
@ -45,17 +45,15 @@ export function createJugandoState(): State {
|
|||
}
|
||||
|
||||
export function update(juego: Juego<State>, dt: number) {
|
||||
juego.state.velocity.x = 0
|
||||
if (juego.keyboard.keys.d || juego.keyboard.keys.ArrowRight) {
|
||||
juego.state.side = "right"
|
||||
juego.state.velocity.x += 200
|
||||
juego.state.pos.x += (dt / 1000) * PLAYER_SPEED
|
||||
}
|
||||
if (juego.keyboard.keys.a || juego.keyboard.keys.ArrowLeft) {
|
||||
juego.state.side = "left"
|
||||
juego.state.velocity.x -= 200
|
||||
juego.state.pos.x -= (dt / 1000) * PLAYER_SPEED
|
||||
}
|
||||
|
||||
juego.state.pos.x += (dt / 1000) * juego.state.velocity.x
|
||||
if (juego.state.pos.x < MAP_MIN) juego.state.pos.x = MAP_MIN
|
||||
if (juego.state.pos.x > MAP_MAX) juego.state.pos.x = MAP_MAX
|
||||
|
||||
|
@ -97,6 +95,15 @@ export function update(juego: Juego<State>, dt: number) {
|
|||
juego.state.enemies.push({ x })
|
||||
}
|
||||
|
||||
for (const enemy of juego.state.enemies) {
|
||||
const distance = enemy.x - juego.state.pos.x
|
||||
if (distance < 0) {
|
||||
enemy.x += (dt / 1000) * ENEMY_SPEED
|
||||
} else {
|
||||
enemy.x -= (dt / 1000) * ENEMY_SPEED
|
||||
}
|
||||
}
|
||||
|
||||
juego.state.view.x = -juego.state.pos.x + juego.canvas.width / 2 - jugadorxSprite.getWidth(juego) / 2
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue