Archived
1
0
Fork 0

WIP splitting BR mode into files

This commit is contained in:
Sebastian Benjamin 2025-04-22 19:00:06 -07:00
parent 58c11048a9
commit b2c3079310
3 changed files with 41 additions and 32 deletions

View file

@ -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))
}

View file

@ -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

View file

@ -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,
}
}