Skip to content

Commit

Permalink
refactor: clean up type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Stani committed Jun 7, 2024
1 parent 0f76870 commit c7f99e2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
},
"dependencies": {
"phaser": "^3.70.0",
"prettier": "^3.0.3"
"prettier": "^3.0.3",
"tweakpane": "^3.0.0"
},
"devDependencies": {
"phaser-plugin-inspector": "^2.0.1",
"typescript": "^5.0.2",
"vite": "^4.4.5"
},
Expand Down
44 changes: 33 additions & 11 deletions src/games/bobber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
const WIDTH = 256
const HEIGHT = 240
const GRAVITY = 128
// @ts-ignore
import { AddArcadeBody } from '../../node_modules/phaser-plugin-inspector/dist/phaser-plugin-inspector.esm.js'
import { Pane } from 'tweakpane'

interface spawnLocation extends GameObjects.GameObject {
x: number
Expand All @@ -22,9 +25,10 @@ interface IPlayer extends Phaser.Types.Physics.Arcade.SpriteWithDynamicBody {
keyInfo: {
right: boolean
left: boolean
space: boolean
down: boolean
numPad1: boolean
numPad2: boolean
numPadOne: boolean
numPadTwo: boolean
}
isImmersed: boolean
isDoneBobbing: boolean
Expand All @@ -39,6 +43,7 @@ export class BobberScene extends Scene {
static teleportCheat: [boolean, number, number] = [false, 0, 0]
static HAS_LOCAL_STORAGE = false

inspectorScene: any
#textbox?: GameObjects.Text
#playerOne?: IPlayer
// @ts-ignore
Expand All @@ -47,7 +52,7 @@ export class BobberScene extends Scene {
})[] = []
#platforms?: Phaser.Tilemaps.TilemapLayer
#water?: Phaser.Tilemaps.TilemapLayer
#kill?: Phaser.Tilemaps.TilemapLayer
#kill?: Phaser.Tilemaps.TilemapLayer | null
initialSpawn?: spawnLocation[]

isRunning = false
Expand Down Expand Up @@ -141,6 +146,7 @@ export class BobberScene extends Scene {
}

create() {
const pane = this.inspectorScene.pane
this.physics.world.setBounds(0, 0, WIDTH * 11, HEIGHT)
this.cameras.main.setBounds(0, 0, 1024 * 4, HEIGHT)

Expand All @@ -149,7 +155,7 @@ export class BobberScene extends Scene {
if (tileset) {
this.initialSpawn = map.createFromObjects('Spawn', {
name: 'playerSpawn',
})
}) as spawnLocation[]
this.#playerOne = this.physics.add.sprite(
this.initialSpawn[0].x,
this.initialSpawn[0].y - 50,
Expand All @@ -170,6 +176,7 @@ export class BobberScene extends Scene {
this.#playerOne.keyInfo = {
left: false,
right: false,
down: false,
space: false,
numPadOne: false,
numPadTwo: false,
Expand Down Expand Up @@ -197,17 +204,24 @@ export class BobberScene extends Scene {
this.#water!.setCollisionByExclusion([-1], true)
this.#platforms = map.createLayer('platforms', tileset)!
this.#platforms!.setCollisionByExclusion([-1], true)
this.#kill = map.createLayer('kill', tileset)
this.#kill!.setCollisionByExclusion([-1], true)
this.#kill = map.createLayer('kill', tileset)

if (!this.#kill) {
throw new Error(`kill is ${this.#kill} but cannot be falsy`)
}

this.#kill.setCollisionByExclusion([-1], true)


this.physics.add.collider(this.#playerOne, this.#kill, () => {
this.#playerOne?.setPosition(
this.initialSpawn[0].x,
this.initialSpawn[0].y - 50
this.initialSpawn?.[0].x,
this.initialSpawn?.[0].y ?? 0 - 50
)
this.#playerOne?.setVelocity(0, 0)
})
}
if (!this.#playerOne) throw new Error()
if (!this.initialSpawn) throw new Error()
this.textures.generate('ground', {
data: ['1'],
Expand Down Expand Up @@ -359,7 +373,10 @@ export class BobberScene extends Scene {
this.#playerOne.y,
]
if (BobberScene.HAS_LOCAL_STORAGE) {
localStorage.setItem('teleport-cheat', JSON.stringify([true, ...dest]))
localStorage.setItem(
'teleport-cheat',
JSON.stringify([true, ...dest])
)
}
this.teleportDestination = dest
}
Expand Down Expand Up @@ -411,8 +428,12 @@ export class BobberScene extends Scene {
// }
// }



const playerBody = this.#playerOne.body as Phaser.Physics.Arcade.Body

AddArcadeBody(playerBody, pane)

playerBody.onCollide = true
if (this.#platforms) {
this.physics.add.collider(this.#playerOne, this.#platforms, () => {
Expand Down Expand Up @@ -472,7 +493,7 @@ export class BobberScene extends Scene {
// }
}

update(_time: number, delta: number) {
update(_time: number, _delta: number) {
stickyMessage(
'playerOne Net Gravity:',
(this.#playerOne?.body.gravity.y ?? 0) + GRAVITY
Expand Down Expand Up @@ -580,7 +601,8 @@ export class BobberScene extends Scene {
if (
this.#playerOne.body.velocity.y < 10 &&
this.#playerOne.isImmersed &&
this.#playerOne.isImmersed !== wasImmersedPreviousFrame && !this.#playerOne.respawnedPreviousFrame
this.#playerOne.isImmersed !== wasImmersedPreviousFrame &&
!this.#playerOne.respawnedPreviousFrame
) {
this.#playerOne.respawnedPreviousFrame = false
this.#playerOne.setGravityY(-GRAVITY)
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import './style.css'
import { Game, Scene, WEBGL } from 'phaser'
import { PlatformerTestScene } from './games/platformerTest'
import { BobberScene } from './games/bobber'
//@ts-ignore
import {DefaultPluginsConfig} from '../node_modules/phaser-plugin-inspector/dist/phaser-plugin-inspector.esm.js'

interface IMenuItemSeed {
id?: string
text: string
Expand Down Expand Up @@ -332,6 +335,7 @@ const config: Phaser.Types.Core.GameConfig = {
width: WIDTH,
height: HEIGHT,
},
plugins: DefaultPluginsConfig
}

new Game(config)
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ nanoid@^3.3.6:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==

phaser-plugin-inspector@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/phaser-plugin-inspector/-/phaser-plugin-inspector-2.0.1.tgz#7fd5360fbdc4f4698840f8962cc53d2b8d4f970e"
integrity sha512-V7jQ0jKSoRT2NFiXn58OTm4vO9fLj0tpVzFA4Lm/bvC78rF+PRiUdmEmiCcwJ2Q3iYq/V3NdH5ITgluYytoaOg==

phaser@^3.70.0:
version "3.70.0"
resolved "https://registry.yarnpkg.com/phaser/-/phaser-3.70.0.tgz#8d187308375171d268d5c509ecdd796aac164ab9"
Expand Down Expand Up @@ -193,6 +198,11 @@ source-map-js@^1.0.2:
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==

tweakpane@^3.0.0:
version "3.1.10"
resolved "https://registry.yarnpkg.com/tweakpane/-/tweakpane-3.1.10.tgz#d2e11a3ee668017f7572ce53417f06eed41f5ab8"
integrity sha512-rqwnl/pUa7+inhI2E9ayGTqqP0EPOOn/wVvSWjZsRbZUItzNShny7pzwL3hVlaN4m9t/aZhsP0aFQ9U5VVR2VQ==

typescript@^5.0.2:
version "5.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
Expand Down

0 comments on commit c7f99e2

Please sign in to comment.