Skip to content

Commit

Permalink
refactor for organization/clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanaltice committed Nov 5, 2023
1 parent 3203389 commit 6a831e4
Show file tree
Hide file tree
Showing 8 changed files with 114,583 additions and 39 deletions.
114,533 changes: 114,533 additions & 0 deletions defs/phaser.d.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FSM</title>
<script src="./lib/phaser.js"></script>
<script src="./src/scenes/Load.js"></script>
<script src="./lib/StateMachine.js"></script>
<script src="./src/prefabs/Hero.js"></script>
<script src="./src/scenes/CharacterFSM.js"></script>
<script src="./src/scenes/Play.js"></script>
<script src="./src/main.js"></script>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 2 additions & 2 deletions lib/StateMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class StateMachine {
// parent class structure for all `State` subclasses
class State {
enter() {
// what happens when we enter the state
// this code happens *once* when we enter the state
}
execute() {
// what happens on each execute step
// this code happens each update step (ie every frame)
}
}
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const config = {
debug: true
}
},
scene: [ CharacterFSM ]
scene: [ Load, Play ]
}

const game = new Phaser.Game(config)
9 changes: 9 additions & 0 deletions src/prefabs/Hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class Hero extends Phaser.Physics.Arcade.Sprite {
this.heroVelocity = 100 // in pixels
this.dashCooldown = 300 // in ms
this.hurtTimer = 250 // in ms

// initialize state machine managing hero (initial state, possible states, state args[])
scene.heroFSM = new StateMachine('idle', {
idle: new IdleState(),
move: new MoveState(),
swing: new SwingState(),
dash: new DashState(),
hurt: new HurtState(),
}, [scene, this]) // pass these as arguments to maintain scene/object context in the FSM
}
}

Expand Down
39 changes: 4 additions & 35 deletions src/scenes/CharacterFSM.js → src/scenes/Load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CharacterFSM extends Phaser.Scene {
class Load extends Phaser.Scene {
constructor() {
super("characterFSMScene")
super('loadScene')
}

preload() {
Expand All @@ -13,21 +13,6 @@ class CharacterFSM extends Phaser.Scene {
}

create() {
// change background color
this.cameras.main.setBackgroundColor('#FACADE')

// add new Hero to scene (scene, x, y, key, frame, direction)
this.hero = new Hero(this, 200, 150, 'hero', 0, 'down')

// initialize state machine managing hero (initial state, possible states, state args[])
this.heroFSM = new StateMachine('idle', {
idle: new IdleState(),
move: new MoveState(),
swing: new SwingState(),
dash: new DashState(),
hurt: new HurtState(),
}, [this, this.hero]) // we pass in `this` and `this.hero` so we can maintain scene/object context in the FSM

// hero animations (walking)
this.anims.create({
key: 'walk-down',
Expand Down Expand Up @@ -80,23 +65,7 @@ class CharacterFSM extends Phaser.Scene {
frames: this.anims.generateFrameNumbers('hero', { start: 28, end: 31 }),
})

// setup keyboard input
this.keys = this.input.keyboard.createCursorKeys()
this.keys.HKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.H)

// debug key listener (assigned to D key)
this.input.keyboard.on('keydown-D', function() {
console.log('d key')
this.physics.world.drawDebug = this.physics.world.drawDebug ? false : true
this.physics.world.debugGraphic.clear()
}, this)

// update instruction text
document.getElementById('info').innerHTML = '<strong>CharacterFSM.js:</strong> Arrows: move | SPACE: attack | SHIFT: dash attack | H: hurt (knockback) | D: debug (toggle)'
}

update() {
// make sure we step (ie update) the hero's state machine
this.heroFSM.step()
// proceed once loading completes
this.scene.start('playScene')
}
}
31 changes: 31 additions & 0 deletions src/scenes/Play.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Play extends Phaser.Scene {
constructor() {
super("playScene")
}

create() {
// change background color
this.cameras.main.setBackgroundColor('#FACADE')

// add new Hero to scene (scene, x, y, key, frame, direction)
this.hero = new Hero(this, 200, 150, 'hero', 0, 'down')

// setup keyboard input
this.keys = this.input.keyboard.createCursorKeys()
this.keys.HKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.H)

// debug key listener (assigned to D key)
this.input.keyboard.on('keydown-D', function() {
this.physics.world.drawDebug = this.physics.world.drawDebug ? false : true
this.physics.world.debugGraphic.clear()
}, this)

// update instruction text
document.getElementById('info').innerHTML = '<strong>CharacterFSM.js:</strong> Arrows: move | SPACE: attack | SHIFT: dash attack | H: hurt (knockback) | D: debug (toggle)'
}

update() {
// make sure we step (ie update) the hero's state machine
this.heroFSM.step()
}
}

0 comments on commit 6a831e4

Please sign in to comment.