Skip to content

Commit d265889

Browse files
committed
add safe and fuse box objects
1 parent 5cd534f commit d265889

File tree

7 files changed

+316
-28
lines changed

7 files changed

+316
-28
lines changed

engine/game.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const ctxObjects = document.getElementById('objects').getContext('2d')
44
const ctxSight = document.getElementById('sight').getContext('2d')
5-
const [drawCollitions, noShadows] = [false, false]
5+
const [drawCollitions, noShadows] = [true, true]
66

77
// eslint-disable-next-line no-unused-vars
88
const game = {
@@ -95,29 +95,51 @@ const game = {
9595
},
9696
activateObject (x, y) {
9797
const ply = Player.getCurrent()
98+
x = x + ply.position.col.x
99+
y = y + ply.position.col.y
100+
101+
const left1 = x
102+
const right1 = x + ply.bound.w
103+
const top1 = y
104+
const bottom1 = y + ply.bound.h
105+
98106
const obj = this.objects.find(itm => {
99-
return itm.col.x <= x &&
100-
itm.col.x + itm.col.w >= x &&
101-
itm.col.y <= y &&
102-
itm.col.y + itm.col.h >= y
107+
const left2 = itm.col.x
108+
const right2 = itm.col.x + itm.col.w
109+
const top2 = itm.col.y
110+
const bottom2 = itm.col.y + itm.col.h
111+
return left1 < right2 && right1 > left2 && top1 < bottom2 && bottom1 > top2
103112
})
104113

105114
if (!obj) return false
115+
116+
if (obj.properties.restrictFrom?.includes(ply.movement)) return
117+
106118
let direction = { x: 32, y: 0 }
107119
if (x < (ply.position.x + 8)) direction.x = -32
108120
if (y < (ply.position.y + 32)) direction = { x: 0, y: -48 }
109121
if (y > (ply.position.y + 32)) direction = { x: 0, y: 48 }
110122

111123
ply.interactingWith = obj
112-
if (obj.state === true && ['window', 'door'].includes(obj.type)) {
113-
ply.move({ x: ply.position.x + direction.x, y: ply.position.y + direction.y }, true)
114-
if (obj.type === 'door') obj.state = false
115-
this.drawObject(obj)
116-
return false
124+
125+
if (obj.state === true) {
126+
if (['window', 'door'].includes(obj.type)) {
127+
ply.move({ x: ply.position.x + direction.x, y: ply.position.y + direction.y }, true)
128+
}
129+
130+
if (obj.properties.returnState) {
131+
obj.state = false
132+
this.drawObject(obj)
133+
return false
134+
}
117135
}
118136

119137
obj.state = true
120138
this.drawObject(obj)
139+
if (obj.properties.interactive) {
140+
ply.stance = 'kneeled'
141+
ply.draw()
142+
}
121143
return false
122144
},
123145
toggleVisibility (obj) {

engine/player.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
class Player {
55
constructor (ctx, x, y, active = false) {
66
this.position = {
7-
x, y, flip: true, inside: false, col: { x: 8, y: 32 }
7+
x, y, flip: true, inside: false, col: { x: 8, y: 32, w: 16, h: 16 }
88
}
9+
this.stance = 'standing'
910
this.active = active
1011
this.ctx = ctx
1112
if (!Player.instances) Player.instances = []
@@ -23,7 +24,9 @@ class Player {
2324
setBound () {
2425
this.bound = {
2526
x: this.position.x + this.position.col.x,
26-
y: this.position.y + this.position.col.y
27+
y: this.position.y + this.position.col.y,
28+
w: this.position.col.w,
29+
h: this.position.col.h
2730
}
2831
}
2932

@@ -62,6 +65,7 @@ class Player {
6265
(relative === 'down' && this.movement === 'up')
6366
) {
6467
this.interactingWith.state = false
68+
this.stance = 'standing'
6569
game.drawObject(this.interactingWith)
6670
delete this.interactingWith
6771
return
@@ -74,7 +78,8 @@ class Player {
7478
if (!jump) {
7579
const computed = game.computePosition(x, y + 16)
7680
if (computed.x < 1 || computed.x > 32 || computed.y < 2 || computed.y > 32) return
77-
if (game.activateObject(x + this.position.col.x, y + this.position.col.y)) return
81+
// if (game.activateObject(x + this.position.col.x, y + this.position.col.y)) return
82+
if (game.activateObject(x, y)) return
7883
if (game.collitionMap[computed.collitionIndex] !== 0) return
7984
}
8085
this.position.x = x
@@ -88,17 +93,39 @@ class Player {
8893
this.position.inside = game.insideMap[collitionIndex] !== 0
8994
this.ctx.beginPath()
9095
this.ctx.clearRect(0, 0, 512, 512)
91-
const [spriteX, spriteY] = {
96+
let w = 32
97+
let h = 48
98+
let kneelOffset = { x: 0, y: 0 }
99+
let [spriteX, spriteY] = {
92100
// flipped, inside
93101
'false,false': [64, 80],
94102
'true,false': [32, 80],
95103
'true,true': [32, 128],
96104
'false,true': [64, 128]
97105
}[[this.position.flip, this.position.inside].join(',')]
98-
this.ctx.drawImage(tileset, spriteX, spriteY, 32, 48, this.position.x, this.position.y, 32, 48)
106+
if (this.stance === 'kneeled') {
107+
w = 32
108+
h = 32
109+
spriteX = 0
110+
spriteY = 96
111+
if (this.position.inside) spriteY = 144
112+
kneelOffset = { x: 4, y: 8 }
113+
}
114+
115+
this.ctx.drawImage(
116+
tileset,
117+
spriteX,
118+
spriteY,
119+
w,
120+
h,
121+
this.position.x + kneelOffset.x,
122+
this.position.y + kneelOffset.y,
123+
w,
124+
h
125+
)
99126
if (drawCollitions) {
100127
// draw collition on player
101-
this.ctx.rect(this.bound.x, this.bound.y, 16, 16)
128+
this.ctx.rect(this.bound.x, this.bound.y, this.bound.w, this.bound.h)
102129
this.ctx.strokeStyle = game.palette[1]
103130
this.ctx.stroke()
104131
}

engine/user.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ const setPalette = tileset => {
9898
}
9999

100100
const preload = level => {
101-
Player.create(ctxPlayer1, -8, 0, true)
101+
Player.create(ctxPlayer1, 280, 160, true)
102+
// Player.create(ctxPlayer1, -8, 0, true)
102103
Player.create(ctxPlayer2, -8, 32)
103104
fetch(`${level}/map.json`)
104105
.then(response => response.json())

levels/0001/0001.tiled-session

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
13
2323
],
2424
"scale": 1.38974609375,
25-
"selectedLayer": 8,
25+
"selectedLayer": 5,
2626
"viewCenter": {
27-
"x": 285.3039139905839,
28-
"y": 232.4165554072096
27+
"x": 254.72278827910904,
28+
"y": 225.22099641627435
2929
}
3030
},
3131
"paleta.tsx": {

0 commit comments

Comments
 (0)