diff --git a/src/gameobjects/badbubble.ts b/src/gameobjects/badbubble.ts index 3e1cb6c..c3e172a 100644 --- a/src/gameobjects/badbubble.ts +++ b/src/gameobjects/badbubble.ts @@ -2,13 +2,14 @@ import { Sound, Sprite, Tag } from '../constants' import { getDirection } from '../helpers' import type { Bubble, Enemy, Player } from '../types' import { getChildBubble, getPlayer, hurtPlayer } from '.' +import { game } from '.' const SPEED = 200 export function addBadBubble(enemy: Enemy) { play(Sound.Shoot, { detune: rand(-100, 100) }) - const badBubble = add([ + const badBubble = game.add([ sprite(Sprite.BadBubble), pos(enemy.pos), move(getDirection(enemy.screenPos()!, getPlayer()!.screenPos()!), SPEED), diff --git a/src/gameobjects/bubble.ts b/src/gameobjects/bubble.ts index 1ad9151..63789de 100644 --- a/src/gameobjects/bubble.ts +++ b/src/gameobjects/bubble.ts @@ -3,11 +3,12 @@ import type { GameObj } from 'kaplay' import { Sound, Sprite, Tag } from '../constants' import { getDirection } from '../helpers' import type { Bubble, ChildBubble, Enemy, Player } from '../types' +import { game } from '.' const SPEED = 200 export function addBubble(player: Player) { - const bubble = add([ + const bubble = game.add([ sprite(Sprite.Bubble), pos(player.pos), move(getDirection(player.screenPos()!, mousePos()), SPEED), diff --git a/src/gameobjects/drain.ts b/src/gameobjects/drain.ts index 291d6fb..ad9a7c3 100644 --- a/src/gameobjects/drain.ts +++ b/src/gameobjects/drain.ts @@ -1,6 +1,7 @@ import { Sprite, Tag } from '../constants' import { insideCoordinates } from '../helpers' import type { Enemy } from '../types' +import { game } from '.' enum Lifespan { Min = 5, @@ -8,7 +9,7 @@ enum Lifespan { } export function addDrain() { - const drain = add([ + const drain = game.add([ sprite(Sprite.Drain), pos(insideCoordinates()), area({ scale: 0.7 }), diff --git a/src/gameobjects/enemy.ts b/src/gameobjects/enemy.ts index cc1a050..ed93de8 100644 --- a/src/gameobjects/enemy.ts +++ b/src/gameobjects/enemy.ts @@ -1,8 +1,7 @@ import { Sound, Sprite, State, Tag } from '../constants' import { addEnemyState } from '../events' import { gameState, outsideCoordinates } from '../helpers' -import { getChildBubble, hurtPlayer } from '.' -import { incrementScore } from './score' +import { game, getChildBubble, hurtPlayer, incrementScore } from '.' export function addEnemy() { const damage = randi(1, 10) @@ -10,7 +9,7 @@ export function addEnemy() { const speed = randi(100, 300) * gameState.enemy.multiplier.speed const { sprites } = gameState.enemy - const enemy = add([ + const enemy = game.add([ sprite(sprites[randi(sprites.length)]), pos(outsideCoordinates()), anchor('center'), diff --git a/src/gameobjects/game.ts b/src/gameobjects/game.ts new file mode 100644 index 0000000..6f2d67c --- /dev/null +++ b/src/gameobjects/game.ts @@ -0,0 +1,5 @@ +export let game = add([]) + +export function addGame() { + game = add([]) +} diff --git a/src/gameobjects/index.ts b/src/gameobjects/index.ts index 5ce38c9..6a64812 100644 --- a/src/gameobjects/index.ts +++ b/src/gameobjects/index.ts @@ -4,6 +4,7 @@ export * from './bubble' export * from './button' export * from './drain' export * from './enemy' +export * from './game' export * from './health' export * from './music' export * from './player' diff --git a/src/gameobjects/player.ts b/src/gameobjects/player.ts index 3b7bd20..f4406b8 100644 --- a/src/gameobjects/player.ts +++ b/src/gameobjects/player.ts @@ -3,11 +3,12 @@ import { addAttack, addCursorKeys } from '../events' import { getAvatar, getChildBubble, stopMusic } from '../gameobjects' import { gameState } from '../helpers' import type { Player } from '../types' +import { game } from '.' const HEALTH = 100 export function addPlayer(x = center().x, y = center().y) { - const player = add([ + const player = game.add([ sprite(Sprite.Kiki), pos(x, y), anchor('center'), @@ -57,7 +58,7 @@ export function addPlayer(x = center().x, y = center().y) { } export function getPlayer() { - return get(Tag.Player)[0] as Player | undefined + return game.get(Tag.Player)[0] as Player | undefined } export function hurtPlayer(damage: number) { diff --git a/src/gameobjects/projectile.ts b/src/gameobjects/projectile.ts index 3c65441..8decba9 100644 --- a/src/gameobjects/projectile.ts +++ b/src/gameobjects/projectile.ts @@ -2,6 +2,7 @@ import { Sound, Sprite, Tag } from '../constants' import { getDirection } from '../helpers' import type { Enemy } from '../types' import { getPlayer, hurtPlayer } from '.' +import { game } from '.' const SPEED = 500 const DAMAGE = 5 @@ -10,7 +11,7 @@ export function addProjectile(enemy: Enemy) { play(Sound.Sneeze, { detune: rand(-100, 100) }) const direction = getDirection(enemy.screenPos()!, getPlayer()!.screenPos()!) - const projectile = add([ + const projectile = game.add([ sprite(Sprite.Projectile), pos(enemy.pos), move(direction, SPEED), diff --git a/src/scenes/game.ts b/src/scenes/game.ts index ca13741..46eb02d 100644 --- a/src/scenes/game.ts +++ b/src/scenes/game.ts @@ -3,6 +3,7 @@ import { addAvatar, addDrain, addEnemy, + addGame, addHealth, addPlayer, addScore, @@ -12,6 +13,7 @@ import { import { gameState, levels } from '../helpers' scene(Scene.Game, () => { + addGame() gameState.init() addText({ diff --git a/src/scenes/tutorial.ts b/src/scenes/tutorial.ts index e0bc18e..130557c 100644 --- a/src/scenes/tutorial.ts +++ b/src/scenes/tutorial.ts @@ -1,7 +1,9 @@ import { Scene, Sound } from '../constants' -import { addButton, addPlayer, addText } from '../gameobjects' +import { addButton, addGame, addPlayer, addText } from '../gameobjects' scene(Scene.Tutorial, () => { + addGame() + const { x } = center() addButton({