Skip to content

Commit

Permalink
feat: add working checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Stani committed Jul 14, 2024
1 parent 0c152be commit b0bb484
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 36 deletions.
56 changes: 52 additions & 4 deletions public/bobber/level1.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"point":true,
"rotation":0,
"type":"",
"visible":false,
"visible":true,
"width":0,
"x":9.5,
"y":152.5
Expand All @@ -105,19 +105,67 @@
"point":true,
"rotation":0,
"type":"checkpoint",
"visible":false,
"visible":true,
"width":0,
"x":816,
"y":128
},
{
"height":0,
"id":19,
"name":"2",
"point":true,
"rotation":0,
"type":"checkpoint",
"visible":true,
"width":0,
"x":1368,
"y":128
},
{
"height":0,
"id":20,
"name":"3",
"point":true,
"rotation":0,
"type":"checkpoint",
"visible":true,
"width":0,
"x":1720,
"y":112
},
{
"height":0,
"id":21,
"name":"4",
"point":true,
"rotation":0,
"type":"checkpoint",
"visible":true,
"width":0,
"x":1904,
"y":192
},
{
"height":0,
"id":22,
"name":"5",
"point":true,
"rotation":0,
"type":"checkpoint",
"visible":true,
"width":0,
"x":2432,
"y":128
}],
"opacity":0.39,
"type":"objectgroup",
"visible":false,
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":7,
"nextobjectid":19,
"nextobjectid":23,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.10.2",
Expand Down
20 changes: 16 additions & 4 deletions public/bobber/level1.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="170" height="15" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#f9e3d7" nextlayerid="7" nextobjectid="19">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="170" height="15" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#f9e3d7" nextlayerid="7" nextobjectid="23">
<editorsettings>
<export target="level1.json" format="json"/>
</editorsettings>
Expand Down Expand Up @@ -64,11 +64,23 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,49,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,49,0,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,49,49,49,49,49,49,0,0,49,0,0,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,0,0,0,0,0,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
<objectgroup id="4" name="Spawn" visible="0" opacity="0.39">
<object id="14" name="playerSpawn" x="9.5" y="152.5" visible="0">
<objectgroup id="4" name="Spawn" opacity="0.39">
<object id="14" name="playerSpawn" x="9.5" y="152.5">
<point/>
</object>
<object id="18" name="1" type="checkpoint" x="816" y="128" visible="0">
<object id="18" name="1" type="checkpoint" x="816" y="128">
<point/>
</object>
<object id="19" name="2" type="checkpoint" x="1368" y="128">
<point/>
</object>
<object id="20" name="3" type="checkpoint" x="1720" y="112">
<point/>
</object>
<object id="21" name="4" type="checkpoint" x="1904" y="192">
<point/>
</object>
<object id="22" name="5" type="checkpoint" x="2432" y="128">
<point/>
</object>
</objectgroup>
Expand Down
94 changes: 66 additions & 28 deletions src/games/bobber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface IPlayer
isDoneBobbing: boolean
respawn?: (coordArr?: [number, number]) => void
respawnedPreviousFrame?: boolean
respawnDestination?: [number, number]
}

export class BobberScene extends Scene {
Expand All @@ -56,6 +57,59 @@ export class BobberScene extends Scene {
isOnGround = false
teleportDestination = BobberScene.teleportCheat?.slice(1) as [number, number]

makeBuoyComposite(x: number, y: number) {
if (!this.#platforms) {
throw new Error('#Platforms is falsy')
}
if (!this.playerOne) {
throw new Error('playerOne is falsy')
}
const buoy = this.physics.add.sprite(x, y, 'buoy')
buoy.setImmovable(true)

buoy.body.setAllowGravity(false)
buoy.setDepth(-2)
const buoy1RelativeOrigin = [0.5 * buoy.width, 0.85 * buoy.height]
buoy.setDisplayOrigin(...buoy1RelativeOrigin)
this.anims.createFromAseprite('buoy', undefined, buoy)

const buoy1AbsoluteMiddleX = buoy.x
const buoy1AbsoluteTopY = buoy.y - buoy1RelativeOrigin[1]
const buoy1LightAbsoluteMiddleX = buoy1AbsoluteMiddleX - 2
const buoy1LightAbsoluteMiddleY = buoy1AbsoluteTopY + 1
const activationParticles = this.add.sprite(
buoy1LightAbsoluteMiddleX,
buoy1LightAbsoluteMiddleY,
'buoyActivate'
)
activationParticles.setVisible(false)

this.anims.createFromAseprite(
'buoyActivate',
undefined,
activationParticles
)

this.physics.add.collider(buoy, this.#platforms)

const overlap = this.physics.add.overlap(buoy, this.playerOne, () => {
if (!this.playerOne) {
return
}
buoy.play({ key: 'default', repeat: -1, startFrame: 1 })

activationParticles.play({
key: 'default',
hideOnComplete: true,
showOnStart: true,
})

this.playerOne.respawnDestination = [x, y]

overlap.destroy()
})
}

/** Attempts to set the acceleration of the player in the given direction */
setHorizontalAcceleration(direction: 'left' | 'right') {
if (!this.playerOne) return
Expand Down Expand Up @@ -175,15 +229,12 @@ export class BobberScene extends Scene {
console.log('hellod')
console.log('hellod')
console.log('hellod')
const checkpoint = spawnLayer.objects.find(
const checkpoints = spawnLayer.objects.filter(
(obj): obj is spawnLocation =>
obj.type === 'checkpoint' &&
typeof obj.x === 'number' &&
typeof obj.y === 'number'
)
if (!checkpoint) {
throw new Error('checkpoint is not defined')
}

if (playerSpawn && tileset) {
this.initialSpawn = playerSpawn
Expand Down Expand Up @@ -232,33 +283,15 @@ export class BobberScene extends Scene {

if (!this.initialSpawn) throw new Error()

const buoy1 = this.add.sprite(checkpoint.x, checkpoint.y, 'buoy')
const buoy1RelativeOrigin = [0.5 * buoy1.width, 0.85 * buoy1.height]
buoy1.setDisplayOrigin(...buoy1RelativeOrigin)
this.anims.createFromAseprite('buoy', undefined, buoy1)

const buoy1AbsoluteMiddleX = buoy1.x
const buoy1AbsoluteTopY = buoy1.y - buoy1RelativeOrigin[1]
const buoy1LightAbsoluteMiddleX = buoy1AbsoluteMiddleX - 2
const buoy1LightAbsoluteMiddleY = buoy1AbsoluteTopY + 1
const activate1 = this.add.sprite(
buoy1LightAbsoluteMiddleX,
buoy1LightAbsoluteMiddleY,
'buoyActivate'
)

this.anims.createFromAseprite('buoyActivate', undefined, activate1)

buoy1.play({ key: 'default', repeat: -1 })
activate1.play({ key: 'default', repeat: -1 })

this.#water = map.createLayer('water', tileset)!
this.#water!.setCollisionByExclusion([-1], true)
this.#platforms = map.createLayer('platforms', tileset)!
this.#platforms!.setCollisionByExclusion([-1], true)
this.#kill = map.createLayer('kill', tileset)

this.physics.add.collider(buoy1, this.#platforms)
checkpoints.forEach((cp) => {
this.makeBuoyComposite(cp.x, cp.y)
})

if (!this.#kill) {
throw new Error(`kill is ${this.#kill} but cannot be falsy`)
Expand All @@ -267,9 +300,14 @@ export class BobberScene extends Scene {
this.#kill.setCollisionByExclusion([-1], true)

this.physics.add.collider(this.playerOne, this.#kill, () => {
this.playerOne?.setPosition(
this.initialSpawn?.x,
this.initialSpawn?.y ?? 0 - 50
if (!this.playerOne?.respawn) {
return
}
this.playerOne?.respawn(
this.playerOne?.respawnDestination || [
this.initialSpawn?.x ?? 0,
this.initialSpawn?.y ?? 0 - 50,
]
)
this.playerOne?.setVelocity(0, 0)
})
Expand Down

0 comments on commit b0bb484

Please sign in to comment.