Skip to content

Commit eae8ec3

Browse files
committed
Add blocking terrain to the js runtime
1 parent 2b8a272 commit eae8ec3

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/js/compilers/js/run.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
drawWorld,
1616
updateWorld,
1717
clamp,
18+
canWalk,
1819
} = fur
1920

2021
function init () {
@@ -62,45 +63,52 @@
6263
const context = canvas.getContext('2d')
6364

6465
window.addEventListener('keydown', ({ key }) => {
65-
const prevPosition = { ...state.player.position }
66+
const { position } = state.player
67+
const prevPosition = { ...position }
6668

67-
let moveKey = false
6869
if (key === 'ArrowUp') {
69-
state.player.position.direction = 0
70-
state.player.position.y -= 1
71-
moveKey = true
70+
position.direction = 0
71+
if (canWalk(objects, state, { x: position.x, y: position.y - 1 })) {
72+
position.y -= 1
73+
}
7274
} else if (key === 'ArrowLeft') {
73-
state.player.position.direction = 1
74-
state.player.position.x -= 1
75-
moveKey = true
75+
position.direction = 1
76+
if (canWalk(objects, state, { x: position.x - 1, y: position.y })) {
77+
position.x -= 1
78+
}
7679
} else if (key === 'ArrowDown') {
77-
state.player.position.direction = 2
78-
state.player.position.y += 1
79-
moveKey = true
80+
position.direction = 2
81+
if (canWalk(objects, state, { x: position.x, y: position.y + 1 })) {
82+
position.y += 1
83+
}
8084
} else if (key === 'ArrowRight') {
81-
state.player.position.direction = 3
82-
state.player.position.x += 1
83-
moveKey = true
85+
position.direction = 3
86+
if (canWalk(objects, state, { x: position.x + 1, y: position.y })) {
87+
position.x += 1
88+
}
8489
} else if (key === 'a') {
85-
applyUseRules(state.player.position)
90+
applyUseRules(position)
8691
}
8792

88-
if (moveKey) {
93+
if (
94+
prevPosition.x !== position.x ||
95+
prevPosition.y !== position.y
96+
) {
8997
clamp(
90-
state.player.position,
98+
position,
9199
{ x: 0, y: 0 },
92-
levelSize[state.player.position.level],
100+
levelSize[position.level],
93101
)
94102

95103
if (
96-
prevPosition.x !== state.player.position.x ||
97-
prevPosition.y !== state.player.position.y
104+
prevPosition.x !== position.x ||
105+
prevPosition.y !== position.y
98106
) {
99107
applyLeaveRules(prevPosition)
100108

101-
applyEnterRules(state.player.position)
109+
applyEnterRules(position)
102110

103-
applyNearRules(state.player.position)
111+
applyNearRules(position)
104112
}
105113
}
106114

src/js/compilers/js/static.js

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
return a
104104
}
105105

106+
function canWalk (objects, state, { x, y }) {
107+
const current = state.levels[state.player.position.level][y][x]
108+
return !objects[current].blocking
109+
}
110+
106111
function drawSprite (context, sprite, { x, y }) {
107112
context.drawImage(sprite, x * sprite.width, y * sprite.height)
108113
}
@@ -181,5 +186,6 @@
181186
drawWorld,
182187
updateWorld,
183188
clamp,
189+
canWalk,
184190
})
185191
})()

0 commit comments

Comments
 (0)