goonscape/main.go

80 lines
1.9 KiB
Go
Raw Normal View History

2024-09-30 10:04:38 +02:00
package main
import (
"flag"
2024-10-11 14:47:52 +02:00
"log"
2025-01-18 20:38:35 +01:00
"strings"
2024-09-30 10:04:38 +02:00
2025-02-14 13:35:09 +01:00
"gitea.boner.be/bdnugget/goonscape/config"
2025-01-13 11:10:48 +01:00
"gitea.boner.be/bdnugget/goonscape/game"
2025-02-14 13:35:09 +01:00
"gitea.boner.be/bdnugget/goonscape/logging"
2025-01-13 11:10:48 +01:00
"gitea.boner.be/bdnugget/goonscape/network"
2024-09-30 10:04:38 +02:00
rl "github.com/gen2brain/raylib-go/raylib"
2024-09-30 15:34:16 +02:00
)
func main() {
2025-02-14 13:35:09 +01:00
logging.Info.Println("Starting GoonScape client")
// Raylib log level warn
rl.SetTraceLogLevel(rl.LogWarning)
2025-01-19 21:23:47 +01:00
// Parse command line flags
local := flag.Bool("local", false, "Connect to local server")
addr := flag.String("addr", "", "Server address (host or host:port)")
2025-02-14 13:35:09 +01:00
noMusic := flag.Bool("no-music", false, "Disable music playback")
flag.Parse()
2025-02-14 13:35:09 +01:00
// Set config before any game initialization
config.Current.PlayMusic = !*noMusic
2025-01-19 21:23:47 +01:00
// Set server address based on flags
if *local {
2025-01-19 21:23:47 +01:00
if *addr != "" {
log.Fatal("Cannot use -local and -addr together")
}
network.SetServerAddr("localhost:6969")
2025-01-18 20:38:35 +01:00
} else if *addr != "" {
2025-01-19 21:23:47 +01:00
// If port is not specified, append default port
2025-01-18 20:38:35 +01:00
if !strings.Contains(*addr, ":") {
2025-01-19 21:23:47 +01:00
*addr += ":6969"
2025-01-18 20:38:35 +01:00
}
network.SetServerAddr(*addr)
}
2025-02-14 13:35:09 +01:00
logging.Info.Println("Initializing window")
2024-10-03 11:53:36 +02:00
rl.InitWindow(1024, 768, "GoonScape")
2025-02-14 13:35:09 +01:00
defer func() {
logging.Info.Println("Closing window")
rl.CloseWindow()
}()
2025-02-14 13:35:09 +01:00
// Initialize audio device first
if !rl.IsAudioDeviceReady() {
rl.InitAudioDevice()
if !rl.IsAudioDeviceReady() {
log.Fatal("Failed to initialize audio device")
}
}
defer rl.CloseAudioDevice()
2024-09-30 15:34:16 +02:00
game := game.New()
2025-02-14 13:35:09 +01:00
logging.Info.Println("Loading game assets")
if err := game.LoadAssets(); err != nil {
log.Fatalf("Failed to load assets: %v", err)
}
2025-02-14 13:35:09 +01:00
defer func() {
logging.Info.Println("Cleaning up game resources")
game.Cleanup()
}()
2024-09-30 15:34:16 +02:00
2025-02-14 13:35:09 +01:00
if config.Current.PlayMusic {
logging.Info.Println("Starting music playback")
rl.PlayMusicStream(game.Music)
rl.SetMusicVolume(game.Music, 0.5)
}
2025-02-14 13:35:09 +01:00
rl.SetTargetFPS(60)
logging.Info.Println("Starting game loop")
game.Run()
logging.Info.Println("Game exited cleanly")
2024-09-30 15:34:16 +02:00
}