add a bunch of mutexes and shit but still have racist conditions and panics LOL

This commit is contained in:
2025-01-22 20:14:58 +01:00
parent 417bf4ea63
commit 220a451475
7 changed files with 309 additions and 161 deletions

View File

@ -2,6 +2,7 @@ package game
import (
"fmt"
"sync"
"time"
"gitea.boner.be/bdnugget/goonscape/types"
@ -25,6 +26,7 @@ type Chat struct {
cursorPos int
scrollOffset int
userData interface{}
mutex sync.RWMutex
}
func NewChat() *Chat {
@ -49,6 +51,9 @@ func (c *Chat) AddMessage(playerID int32, content string) {
}
func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
c.mutex.Lock()
defer c.mutex.Unlock()
// Convert protobuf messages to our local type
for _, msg := range messages {
localMsg := types.ChatMessage{
@ -94,6 +99,9 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
}
func (c *Chat) Draw(screenWidth, screenHeight int32) {
c.mutex.RLock()
defer c.mutex.RUnlock()
// Calculate chat window width based on screen width
chatWindowWidth := screenWidth - (chatMargin * 2)

View File

@ -1,7 +1,8 @@
package game
import (
"os"
"fmt"
"sync"
"time"
"gitea.boner.be/bdnugget/goonscape/assets"
@ -19,9 +20,10 @@ type Game struct {
Music rl.Music
Chat *Chat
MenuOpen bool
QuitChan chan struct{} // Channel to signal shutdown
quitChan chan struct{}
loginScreen *LoginScreen
isLoggedIn bool
cleanupOnce sync.Once
}
func New() *Game {
@ -36,7 +38,7 @@ func New() *Game {
Projection: rl.CameraPerspective,
},
Chat: NewChat(),
QuitChan: make(chan struct{}),
quitChan: make(chan struct{}),
loginScreen: NewLoginScreen(),
}
game.Chat.userData = game
@ -44,15 +46,32 @@ func New() *Game {
}
func (g *Game) LoadAssets() error {
var err error
g.Models, err = assets.LoadModels()
if err != nil {
return err
var loadErr error
defer func() {
if r := recover(); r != nil {
loadErr = fmt.Errorf("panic during asset loading: %v", r)
// Cleanup any partially loaded assets
g.Cleanup()
}
}()
// Load models with better error handling
g.Models, loadErr = assets.LoadModels()
if loadErr != nil {
return fmt.Errorf("failed to load models: %v", loadErr)
}
g.Music, err = assets.LoadMusic("resources/audio/GoonScape2.mp3")
if err != nil {
return err
// Verify model loading
for i, model := range g.Models {
if model.Model.Meshes == nil {
return fmt.Errorf("model %d failed to load properly", i)
}
}
// Load music with better error handling
g.Music, loadErr = assets.LoadMusic("resources/audio/GoonScape2.mp3")
if loadErr != nil {
return fmt.Errorf("failed to load music: %v", loadErr)
}
return nil
@ -77,11 +96,9 @@ func (g *Game) Update(deltaTime float32) {
}
g.AssignModelToPlayer(g.Player)
go network.HandleServerCommunication(conn, playerID, g.Player, g.OtherPlayers, g.QuitChan)
go network.HandleServerCommunication(conn, playerID, g.Player, g.OtherPlayers, g.quitChan)
g.isLoggedIn = true
return
}
g.loginScreen.Draw()
return
}
@ -274,8 +291,20 @@ func (g *Game) Render() {
}
func (g *Game) Cleanup() {
assets.UnloadModels(g.Models)
assets.UnloadMusic(g.Music)
g.cleanupOnce.Do(func() {
// Stop music first
if g.Music.Stream.Buffer != nil {
rl.StopMusicStream(g.Music)
rl.UnloadMusicStream(g.Music)
}
// Unload textures
for _, model := range g.Models {
if model.Texture.ID > 0 {
rl.UnloadTexture(model.Texture)
}
}
})
}
func (g *Game) HandleInput() {
@ -355,10 +384,7 @@ func (g *Game) DrawMenu() {
}
func (g *Game) Shutdown() {
close(g.QuitChan)
<-g.Player.QuitDone
rl.CloseWindow()
os.Exit(0)
close(g.quitChan)
}
func (g *Game) HandleServerMessages(messages []*pb.ChatMessage) {
@ -373,3 +399,7 @@ func (g *Game) AssignModelToPlayer(player *types.Player) {
player.Model = modelAsset.Model
player.Texture = modelAsset.Texture
}
func (g *Game) QuitChan() <-chan struct{} {
return g.quitChan
}