Skip to content

Commit

Permalink
refactor(gameobjects): add game
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 28, 2025
1 parent 37a1d98 commit 3725322
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/gameobjects/badbubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/gameobjects/bubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/gameobjects/drain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Sprite, Tag } from '../constants'
import { insideCoordinates } from '../helpers'
import type { Enemy } from '../types'
import { game } from '.'

enum Lifespan {
Min = 5,
Max = 15,
}

export function addDrain() {
const drain = add([
const drain = game.add([
sprite(Sprite.Drain),
pos(insideCoordinates()),
area({ scale: 0.7 }),
Expand Down
5 changes: 2 additions & 3 deletions src/gameobjects/enemy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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)
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([
const enemy = game.add([
sprite(sprites[randi(sprites.length)]),
pos(outsideCoordinates()),
anchor('center'),
Expand Down
5 changes: 5 additions & 0 deletions src/gameobjects/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export let game = add([])

export function addGame() {
game = add([])
}
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 3 additions & 2 deletions src/gameobjects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/gameobjects/projectile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
addAvatar,
addDrain,
addEnemy,
addGame,
addHealth,
addPlayer,
addScore,
Expand All @@ -12,6 +13,7 @@ import {
import { gameState, levels } from '../helpers'

scene(Scene.Game, () => {
addGame()
gameState.init()

addText({
Expand Down
4 changes: 3 additions & 1 deletion src/scenes/tutorial.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 3725322

Please sign in to comment.