Skip to content

Commit

Permalink
fix: Use "current-player" tag for onUpdate events
Browse files Browse the repository at this point in the history
By setting a "current-player" tag for the current player, we ensure that
onUpdate events are only handled by the current player.

In particular, this fixes a bug in the handling of enemy speed based on
how long the player has played in the level.
  • Loading branch information
joshuacurtiss committed Jun 8, 2024
1 parent 545baef commit f9ceaf8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const {
z,
} = k;

const CURRENT_PLAYER_TAG = 'current-player';

const levelConf: LevelOpt = {
tileWidth: 8,
tileHeight: 8,
Expand Down Expand Up @@ -192,7 +194,7 @@ export default function(options: Partial<GameSceneOpt>) {
const ENEMY_SPEED_INC = 3;
const enemyInitialSpeed = enemies[0].speed;
let lastEnemySpeedCheck=0;
onUpdate('player', p=>{
onUpdate(CURRENT_PLAYER_TAG, p=>{
const levelTime = Math.floor(p.levelTime);
if (levelTime-lastEnemySpeedCheck>=ENEMY_SPEED_CHECK_FREQUENCY) {
lastEnemySpeedCheck = levelTime;
Expand Down Expand Up @@ -220,28 +222,30 @@ export default function(options: Partial<GameSceneOpt>) {
// Player setup
opt.players.forEach(p=>{
p.freeze();
p.unuse(CURRENT_PLAYER_TAG);
p.pos = vec2(-20);
});
player.use(CURRENT_PLAYER_TAG);
player.level = levelNumber;
player.pos = vec2(128, 165);
player.setObjects({ floors, stairs, stairtops });
player.setAnim(vec2(0));
player.isAlive = true;
player.isFrozen = true;
on(ON_DIE, 'player', ()=>{
on(ON_DIE, CURRENT_PLAYER_TAG, ()=>{
enemies.forEach(enemy=>enemy.freeze());
wait(0.5, ()=>music.stop());
wait(5, ()=>goNextScene('die'));
});
on(ON_WIN, 'player', ()=>{
on(ON_WIN, CURRENT_PLAYER_TAG, ()=>{
music.stop();
enemies.forEach(enemy=>enemy.freeze());
wait(5, ()=>goNextScene('win'));
});

// Controls
onKeyPress(player.controls.keyboard.action, ()=>player.action());
onUpdate('player', p=>{
onUpdate(CURRENT_PLAYER_TAG, p=>{
if (p.isFrozen || !p.isAlive) return;
const { up, down, left, right } = p.controls.keyboard;
let dir = vec2(0);
Expand Down

0 comments on commit f9ceaf8

Please sign in to comment.