Skip to content

Commit 279a347

Browse files
authored
Merge pull request #43 from neagix/main
fix: do not run onTurn events when map changed
2 parents 3363b29 + 726e4c5 commit 279a347

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

packages/odyc/src/gameLoop.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class GameLoop<T extends string> {
3737

3838
const onTurnEvents = this.#gameState.cells
3939
.get()
40-
.map((el) => this.#gameState.cells.getEvent(...el.position, 'onTurn'))
40+
.filter((cell) => cell.onTurn)
4141

4242
if (this.#isCellOnworld(to.value)) {
4343
const cell = this.#gameState.cells.getCellAt(...to.value)
@@ -54,8 +54,8 @@ class GameLoop<T extends string> {
5454
await this.#gameState.cells.getEvent(...to.value, 'onCollide')?.()
5555
else await this.#gameState.cells.getEvent(...to.value, 'onEnter')?.()
5656
}
57-
for (const event of onTurnEvents) {
58-
await event?.()
57+
for (const cell of onTurnEvents) {
58+
await this.#gameState.cells.getEvent(...cell.position, 'onTurn')?.()
5959
}
6060
this.#gameState.player.dispatchOnTurn()
6161
if (!this.#ender.ending) this.#gameState.turn.next()

tests/odyc-e2e/functional/on-turn/index.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,50 @@ describe('onTurn events', () => {
3939
expect(turnSpy).toHaveBeenCalledOnce()
4040
})
4141

42-
// test('should call onTurn for newly created actors on subsequent turns', async () => {})
42+
test('should call onTurn for newly created actors on subsequent turns', async () => {
43+
const turnSpy = vi.fn()
44+
const game = createGame({
45+
templates: {
46+
x: {
47+
onTurn() {
48+
game.setCellAt(1, 0, 'y')
49+
},
50+
},
51+
'.': {},
52+
y: {
53+
onTurn: turnSpy,
54+
},
55+
},
56+
map: `x.`,
57+
})
58+
59+
// First turn - x creates y
60+
await userEvent.keyboard('[ArrowLeft]')
61+
expect(turnSpy).not.toHaveBeenCalled()
62+
63+
// Second turn - y should have onTurn called
64+
await userEvent.keyboard('[ArrowLeft]')
65+
expect(turnSpy).toHaveBeenCalledOnce()
66+
})
67+
68+
test('should not call onTurn for cells removed during the turn', async () => {
69+
const turnSpy = vi.fn()
70+
const game = createGame({
71+
templates: {
72+
x: {
73+
onCollide() {
74+
game.clearCellAt(0, 0)
75+
},
76+
},
77+
y: {
78+
onTurn: turnSpy,
79+
},
80+
},
81+
map: `yx`,
82+
})
83+
84+
// Move right to collide with x, which clears y during the collision
85+
await userEvent.keyboard('[ArrowRight]')
86+
expect(turnSpy).not.toHaveBeenCalled()
87+
})
4388
})

0 commit comments

Comments
 (0)