From b2c3079310090cfd95b6d96f234d2c20866fc971 Mon Sep 17 00:00:00 2001 From: Sebastian Benjamin Date: Tue, 22 Apr 2025 19:00:06 -0700 Subject: [PATCH] WIP splitting BR mode into files --- server/ffi/ffi.go | 14 ++++---- server/game-modes/battle-royale/mode.go | 32 ++++--------------- .../game-modes/battle-royale/player-stage.go | 27 ++++++++++++++++ 3 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 server/game-modes/battle-royale/player-stage.go diff --git a/server/ffi/ffi.go b/server/ffi/ffi.go index a3d02a0..254d453 100644 --- a/server/ffi/ffi.go +++ b/server/ffi/ffi.go @@ -13,8 +13,8 @@ type Circle struct { cptr *C.Circle } -func NewCircle(x float64, y float64, rad float64) Circle { - return Circle{ +func NewCircle(x float64, y float64, rad float64) *Circle { + return &Circle{ cptr: C.new_circle(C.double(x), C.double(y), C.double(rad)), } } @@ -24,7 +24,7 @@ func (c *Circle) UpdatePos(x float64, y float64) { c.cptr.y = C.double(y) } -func DestroyCircle(circle Circle) { +func DestroyCircle(circle *Circle) { if circle.cptr == nil { return } @@ -41,8 +41,8 @@ type Bullet struct { cptr *C.Bullet } -func NewLinearBullet(tick int64, spawnX float64, spawnY float64, radius float64, velX float64, velY float64) Bullet { - return Bullet{ +func NewLinearBullet(tick int64, spawnX float64, spawnY float64, radius float64, velX float64, velY float64) *Bullet { + return &Bullet{ cptr: C.new_bullet( C.uint8_t(0), C.int64_t(tick), @@ -55,7 +55,7 @@ func NewLinearBullet(tick int64, spawnX float64, spawnY float64, radius float64, } } -func DestroyBullet(bullet Bullet) { +func DestroyBullet(bullet *Bullet) { if bullet.cptr == nil { return } @@ -67,7 +67,7 @@ func (b *Bullet) BeyondKillBoundary(tick int64) bool { return bool(C.bullet_beyond_kill_boundary(b.cptr, C.int64_t(tick))) } -func (b *Bullet) CollidesWith(circle Circle, tick int64) bool { +func (b *Bullet) CollidesWith(circle *Circle, tick int64) bool { return bool(C.bullet_collides_with(b.cptr, C.int64_t(tick), circle.cptr)) } diff --git a/server/game-modes/battle-royale/mode.go b/server/game-modes/battle-royale/mode.go index f5b3d7f..41b4a0a 100644 --- a/server/game-modes/battle-royale/mode.go +++ b/server/game-modes/battle-royale/mode.go @@ -35,17 +35,6 @@ const ( // Interface for registering match handlers type Match struct{} -type PlayerStageState struct { - hitCol ffi.Circle - grazeCol ffi.Circle - bullets []ffi.Bullet - updatePlayerPos bool - health int - graze int - score int - deathTimer int -} - type PlayerUpdate struct { X float64 `json:"x"` Y float64 `json:"y"` @@ -62,8 +51,8 @@ type GameTickUpdate struct { } type PresenceState struct { // present time! hahahahahahahah! - presence runtime.Presence - stageState PlayerStageState + presence *runtime.Presence + stageState *PlayerStageState } type BattleRoyaleMatchState struct { @@ -110,15 +99,8 @@ func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB for i := 0; i < len(presences); i++ { lobbyState.presences[presences[i].GetSessionId()] = &PresenceState{ - presence: presences[i], - stageState: PlayerStageState{ - hitCol: ffi.NewCircle(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1, STAGE_WIDTH*PLAYER_HIT_COL_RADIUS_MULTIPLIER), - grazeCol: ffi.NewCircle(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1, STAGE_WIDTH*PLAYER_GRAZE_COL_RADIUS_MULTIPLIER), - bullets: []ffi.Bullet{}, - updatePlayerPos: true, - health: 3, - deathTimer: -1, - }, + presence: &presences[i], + stageState: NewPlayerStage(), } } @@ -225,7 +207,7 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB // Compute and broadcast per-presence state for _, v := range lobbyState.presences { // Clean up bullets when they pass off the board - v.stageState.bullets = slices.DeleteFunc(v.stageState.bullets, func(b ffi.Bullet) bool { + v.stageState.bullets = slices.DeleteFunc(v.stageState.bullets, func(b *ffi.Bullet) bool { if b.BeyondKillBoundary(tick) { ffi.DestroyBullet(b) return true @@ -243,11 +225,11 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB v.stageState.grazeCol.UpdatePos(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1) v.stageState.updatePlayerPos = true } else if v.stageState.deathTimer == -1 { // If the player is alive, check if the player collided with a bullet and kill them if so - if slices.ContainsFunc(v.stageState.bullets, func(b ffi.Bullet) bool { + if slices.ContainsFunc(v.stageState.bullets, func(b *ffi.Bullet) bool { return b.CollidesWith(v.stageState.hitCol, tick) }) { v.stageState.deathTimer = PLAYER_DEATH_TIMER_MAX - } else if slices.ContainsFunc(v.stageState.bullets, func(b ffi.Bullet) bool { // Otherwise, check the graze col and increment the graze and score + } else if slices.ContainsFunc(v.stageState.bullets, func(b *ffi.Bullet) bool { // Otherwise, check the graze col and increment the graze and score return b.CollidesWith(v.stageState.grazeCol, tick) }) { v.stageState.graze += GRAZE_ADDITION_MULTIPLIER diff --git a/server/game-modes/battle-royale/player-stage.go b/server/game-modes/battle-royale/player-stage.go new file mode 100644 index 0000000..d78e034 --- /dev/null +++ b/server/game-modes/battle-royale/player-stage.go @@ -0,0 +1,27 @@ +package battleroyale + +import ( + "danmaku/ffi" +) + +type PlayerStageState struct { + hitCol *ffi.Circle + grazeCol *ffi.Circle + bullets []*ffi.Bullet + updatePlayerPos bool + health int + graze int + score int + deathTimer int +} + +func NewPlayerStage() *PlayerStageState { + return &PlayerStageState{ + hitCol: ffi.NewCircle(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1, STAGE_WIDTH*PLAYER_HIT_COL_RADIUS_MULTIPLIER), + grazeCol: ffi.NewCircle(STAGE_WIDTH*0.5, STAGE_HEIGHT-STAGE_HEIGHT*0.1, STAGE_WIDTH*PLAYER_GRAZE_COL_RADIUS_MULTIPLIER), + bullets: []*ffi.Bullet{}, + updatePlayerPos: true, + health: 3, + deathTimer: -1, + } +}