Compare commits
No commits in common. "d86cbe15a3dbe60da8026d52b8646adf9964df2d" and "2a0f9348e9199930ba14c0fb84744384841e6018" have entirely different histories.
d86cbe15a3
...
2a0f9348e9
@ -168,12 +168,13 @@ func (c *Chat) Update() (string, bool) {
|
||||
c.inputBuffer = c.inputBuffer[:0]
|
||||
c.cursorPos = 0
|
||||
c.isTyping = false
|
||||
|
||||
return message, true
|
||||
}
|
||||
c.isTyping = false
|
||||
}
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyEscape) && c.isTyping {
|
||||
if rl.IsKeyPressed(rl.KeyEscape) {
|
||||
c.inputBuffer = c.inputBuffer[:0]
|
||||
c.cursorPos = 0
|
||||
c.isTyping = false
|
||||
|
104
game/game.go
104
game/game.go
@ -16,8 +16,6 @@ type Game struct {
|
||||
Models []types.ModelAsset
|
||||
Music rl.Music
|
||||
Chat *Chat
|
||||
MenuOpen bool
|
||||
QuitChan chan struct{} // Channel to signal shutdown
|
||||
}
|
||||
|
||||
func New() *Game {
|
||||
@ -39,7 +37,6 @@ func New() *Game {
|
||||
Projection: rl.CameraPerspective,
|
||||
},
|
||||
Chat: NewChat(),
|
||||
QuitChan: make(chan struct{}),
|
||||
}
|
||||
game.Player.UserData = game
|
||||
game.Chat.userData = game
|
||||
@ -62,17 +59,6 @@ func (g *Game) LoadAssets() error {
|
||||
}
|
||||
|
||||
func (g *Game) Update(deltaTime float32) {
|
||||
// Handle ESC for menu
|
||||
if rl.IsKeyPressed(rl.KeyEscape) {
|
||||
g.MenuOpen = !g.MenuOpen
|
||||
return
|
||||
}
|
||||
|
||||
// Don't process other inputs if menu is open
|
||||
if g.MenuOpen {
|
||||
return
|
||||
}
|
||||
|
||||
if message, sent := g.Chat.Update(); sent {
|
||||
g.Player.Lock()
|
||||
g.Player.ActionQueue = append(g.Player.ActionQueue, &pb.Action{
|
||||
@ -182,17 +168,36 @@ func (g *Game) Render() {
|
||||
}
|
||||
rl.EndMode3D()
|
||||
|
||||
// Draw menu if open
|
||||
if g.MenuOpen {
|
||||
g.DrawMenu()
|
||||
drawFloatingMessage := func(msg *types.FloatingMessage) {
|
||||
if msg == nil || time.Now().After(msg.ExpireTime) {
|
||||
return
|
||||
}
|
||||
pos := msg.ScreenPos
|
||||
text := msg.Content
|
||||
textWidth := rl.MeasureText(text, 20)
|
||||
|
||||
for offsetX := -2; offsetX <= 2; offsetX++ {
|
||||
for offsetY := -2; offsetY <= 2; offsetY++ {
|
||||
rl.DrawText(text,
|
||||
int32(pos.X)-textWidth/2+int32(offsetX),
|
||||
int32(pos.Y)+int32(offsetY),
|
||||
20,
|
||||
rl.Black)
|
||||
}
|
||||
}
|
||||
rl.DrawText(text, int32(pos.X)-textWidth/2, int32(pos.Y), 20, rl.Yellow)
|
||||
}
|
||||
|
||||
// Only draw chat if menu is not open
|
||||
if !g.MenuOpen {
|
||||
g.Chat.Draw(int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()))
|
||||
if g.Player.FloatingMessage != nil {
|
||||
drawFloatingMessage(g.Player.FloatingMessage)
|
||||
}
|
||||
|
||||
for _, other := range g.OtherPlayers {
|
||||
drawFloatingMessage(other.FloatingMessage)
|
||||
}
|
||||
|
||||
rl.DrawFPS(10, 10)
|
||||
g.Chat.Draw(int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()))
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
@ -218,62 +223,3 @@ func (g *Game) HandleInput() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) DrawMenu() {
|
||||
screenWidth := float32(rl.GetScreenWidth())
|
||||
screenHeight := float32(rl.GetScreenHeight())
|
||||
|
||||
// Semi-transparent background
|
||||
rl.DrawRectangle(0, 0, int32(screenWidth), int32(screenHeight), rl.ColorAlpha(rl.Black, 0.7))
|
||||
|
||||
// Menu title
|
||||
title := "Menu"
|
||||
titleSize := int32(40)
|
||||
titleWidth := rl.MeasureText(title, titleSize)
|
||||
rl.DrawText(title, int32(screenWidth/2)-titleWidth/2, 100, titleSize, rl.White)
|
||||
|
||||
// Menu buttons
|
||||
buttonWidth := float32(200)
|
||||
buttonHeight := float32(40)
|
||||
buttonY := float32(200)
|
||||
buttonSpacing := float32(60)
|
||||
|
||||
menuItems := []string{"Resume", "Settings", "Exit Game"}
|
||||
for _, item := range menuItems {
|
||||
buttonRect := rl.Rectangle{
|
||||
X: screenWidth/2 - buttonWidth/2,
|
||||
Y: buttonY,
|
||||
Width: buttonWidth,
|
||||
Height: buttonHeight,
|
||||
}
|
||||
|
||||
// Check mouse hover
|
||||
mousePoint := rl.GetMousePosition()
|
||||
mouseHover := rl.CheckCollisionPointRec(mousePoint, buttonRect)
|
||||
|
||||
// Draw button
|
||||
if mouseHover {
|
||||
rl.DrawRectangleRec(buttonRect, rl.ColorAlpha(rl.White, 0.3))
|
||||
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
switch item {
|
||||
case "Resume":
|
||||
g.MenuOpen = false
|
||||
case "Settings":
|
||||
// TODO: Implement settings
|
||||
case "Exit Game":
|
||||
close(g.QuitChan) // Signal all goroutines to shut down
|
||||
rl.CloseWindow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw button text
|
||||
textSize := int32(20)
|
||||
textWidth := rl.MeasureText(item, textSize)
|
||||
textX := int32(buttonRect.X+buttonRect.Width/2) - textWidth/2
|
||||
textY := int32(buttonRect.Y + buttonRect.Height/2 - float32(textSize)/2)
|
||||
rl.DrawText(item, textX, textY, textSize, rl.White)
|
||||
|
||||
buttonY += buttonSpacing
|
||||
}
|
||||
}
|
||||
|
6
main.go
6
main.go
@ -30,7 +30,6 @@ func main() {
|
||||
}
|
||||
|
||||
rl.InitWindow(1024, 768, "GoonScape")
|
||||
rl.SetExitKey(0)
|
||||
defer rl.CloseWindow()
|
||||
rl.InitAudioDevice()
|
||||
defer rl.CloseAudioDevice()
|
||||
@ -52,7 +51,7 @@ func main() {
|
||||
game.Player.Model = game.Models[modelIndex].Model
|
||||
game.Player.Texture = game.Models[modelIndex].Texture
|
||||
|
||||
go network.HandleServerCommunication(conn, playerID, game.Player, game.OtherPlayers, game.QuitChan)
|
||||
go network.HandleServerCommunication(conn, playerID, game.Player, game.OtherPlayers)
|
||||
|
||||
rl.PlayMusicStream(game.Music)
|
||||
rl.SetMusicVolume(game.Music, 0.5)
|
||||
@ -65,7 +64,4 @@ func main() {
|
||||
game.Update(deltaTime)
|
||||
game.Render()
|
||||
}
|
||||
|
||||
// Wait for clean shutdown
|
||||
<-game.QuitChan
|
||||
}
|
||||
|
@ -56,18 +56,15 @@ func ConnectToServer() (net.Conn, int32, error) {
|
||||
return conn, playerID, nil
|
||||
}
|
||||
|
||||
func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Player, otherPlayers map[int32]*types.Player, quitChan <-chan struct{}) {
|
||||
func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Player, otherPlayers map[int32]*types.Player) {
|
||||
// Create a buffered reader for the connection
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
actionTicker := time.NewTicker(types.ClientTickRate)
|
||||
defer actionTicker.Stop()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-quitChan:
|
||||
return
|
||||
case <-actionTicker.C:
|
||||
for range actionTicker.C {
|
||||
player.Lock()
|
||||
if len(player.ActionQueue) > 0 {
|
||||
actions := make([]*pb.Action, len(player.ActionQueue))
|
||||
@ -90,14 +87,9 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
|
||||
player.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-quitChan:
|
||||
return
|
||||
default:
|
||||
// Read message length (4 bytes)
|
||||
lengthBuf := make([]byte, 4)
|
||||
if _, err := io.ReadFull(reader, lengthBuf); err != nil {
|
||||
@ -149,7 +141,6 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play
|
||||
g.Chat.HandleServerMessages(serverMessage.ChatMessages)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to write length-prefixed messages
|
||||
|
Loading…
x
Reference in New Issue
Block a user