Skip to content

Commit

Permalink
refactor(helpers): tidy gameState
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 28, 2025
1 parent de63e5b commit 37a1d98
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/gameobjects/enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { incrementScore } from './score'

export function addEnemy() {
const damage = randi(1, 10)
const hp = randi(20, 100) * gameState.enemyHealthMultiplier
const speed = randi(100, 300) * gameState.enemySpeedMultiplier
const { enemySprites } = gameState
const hp = randi(20, 100) * gameState.enemy.multiplier.health
const speed = randi(100, 300) * gameState.enemy.multiplier.speed
const { sprites } = gameState.enemy

const enemy = add([
sprite(enemySprites[randi(enemySprites.length)]),
sprite(sprites[randi(sprites.length)]),
pos(outsideCoordinates()),
anchor('center'),
health(hp, hp),
Expand Down
2 changes: 1 addition & 1 deletion src/gameobjects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function getPlayer() {
}

export function hurtPlayer(damage: number) {
getPlayer()?.hurt(damage * gameState.enemyDamageMultiplier)
getPlayer()?.hurt(damage * gameState.enemy.multiplier.damage)
}

function onHit(player: Player) {
Expand Down
20 changes: 12 additions & 8 deletions src/helpers/gameState.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Sprite } from '../constants'

class GameState {
enemyDamageMultiplier = 1
enemyHealthMultiplier = 1
enemySpeedMultiplier = 1
enemySprites: Sprite[] = []
enemy = {
multiplier: {
damage: 1,
health: 1,
speed: 1,
},
sprites: [] as Sprite[],
}

init() {
this.enemyDamageMultiplier = 1
this.enemyHealthMultiplier = 1
this.enemySpeedMultiplier = 1
this.enemySprites = []
this.enemy.multiplier.damage = 1
this.enemy.multiplier.health = 1
this.enemy.multiplier.speed = 1
this.enemy.sprites = []
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ scene(Scene.Game, () => {

levels.forEach((level) => {
wait(level.start, () => {
gameState.enemyDamageMultiplier = level.multiplier.damage
gameState.enemyHealthMultiplier = level.multiplier.health
gameState.enemySpeedMultiplier = level.multiplier.speed
gameState.enemySprites = level.enemies
gameState.enemy.multiplier.damage = level.multiplier.damage
gameState.enemy.multiplier.health = level.multiplier.health
gameState.enemy.multiplier.speed = level.multiplier.speed
gameState.enemy.sprites = level.enemies
const duration = level.end && level.end - level.start

loop(
Expand Down

0 comments on commit 37a1d98

Please sign in to comment.