111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"gitea.boner.be/bdnugget/goonscape/game"
|
|
"gitea.boner.be/bdnugget/goonscape/network"
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
func main() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("Recovered from panic in main: %v", r)
|
|
}
|
|
}()
|
|
|
|
// Parse command line flags
|
|
verbose := flag.Bool("v", false, "Also show info logs (spammy)")
|
|
local := flag.Bool("local", false, "Connect to local server")
|
|
addr := flag.String("addr", "", "Server address (host or host:port)")
|
|
flag.Parse()
|
|
|
|
if *verbose {
|
|
rl.SetTraceLogLevel(rl.LogTrace)
|
|
} else {
|
|
rl.SetTraceLogLevel(rl.LogWarning)
|
|
}
|
|
|
|
// Set server address based on flags
|
|
if *local {
|
|
if *addr != "" {
|
|
log.Fatal("Cannot use -local and -addr together")
|
|
}
|
|
network.SetServerAddr("localhost:6969")
|
|
} else if *addr != "" {
|
|
// If port is not specified, append default port
|
|
if !strings.Contains(*addr, ":") {
|
|
*addr += ":6969"
|
|
}
|
|
network.SetServerAddr(*addr)
|
|
}
|
|
|
|
rl.InitWindow(1024, 768, "GoonScape")
|
|
rl.SetExitKey(0)
|
|
rl.InitAudioDevice()
|
|
|
|
gameInstance := game.New()
|
|
if err := gameInstance.LoadAssets(); err != nil {
|
|
log.Printf("Failed to load assets: %v", err)
|
|
return
|
|
}
|
|
|
|
defer func() {
|
|
gameInstance.Cleanup()
|
|
rl.CloseWindow()
|
|
rl.CloseAudioDevice()
|
|
}()
|
|
|
|
rl.SetTargetFPS(60)
|
|
|
|
rl.PlayMusicStream(gameInstance.Music)
|
|
rl.SetMusicVolume(gameInstance.Music, 0.5)
|
|
|
|
// Handle OS signals for clean shutdown
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
go func() {
|
|
<-sigChan
|
|
if gameInstance != nil {
|
|
gameInstance.Shutdown()
|
|
}
|
|
}()
|
|
|
|
// Keep game loop in main thread for Raylib
|
|
for !rl.WindowShouldClose() {
|
|
deltaTime := rl.GetFrameTime()
|
|
rl.UpdateMusicStream(gameInstance.Music)
|
|
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("Recovered from panic in game update: %v", r)
|
|
}
|
|
}()
|
|
gameInstance.Update(deltaTime)
|
|
}()
|
|
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("Recovered from panic in game render: %v", r)
|
|
}
|
|
}()
|
|
gameInstance.Render()
|
|
}()
|
|
|
|
// Check if game requested shutdown
|
|
select {
|
|
case <-gameInstance.QuitChan():
|
|
return
|
|
default:
|
|
}
|
|
}
|
|
}
|