663 lines
19 KiB
Go
663 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
"log"
|
|
"math"
|
|
"sort"
|
|
"time"
|
|
|
|
"git.zb-server.de/ZB-Server/EscapeFromTeacher/pkg/game"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
|
"golang.org/x/image/font/basicfont"
|
|
)
|
|
|
|
// --- INPUT & UPDATE LOGIC ---
|
|
|
|
func (g *Game) UpdateGame() {
|
|
// --- 1. MUTE TOGGLE ---
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyM) {
|
|
g.audio.ToggleMute()
|
|
}
|
|
|
|
// --- 2. KEYBOARD INPUT ---
|
|
keyLeft := ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft)
|
|
keyRight := ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight)
|
|
keyDown := inpututil.IsKeyJustPressed(ebiten.KeyS) || inpututil.IsKeyJustPressed(ebiten.KeyDown)
|
|
keyJump := inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyW) || inpututil.IsKeyJustPressed(ebiten.KeyUp)
|
|
|
|
// Tastatur-Nutzung erkennen (für Mobile Controls ausblenden)
|
|
if keyLeft || keyRight || keyDown || keyJump {
|
|
g.keyboardUsed = true
|
|
}
|
|
|
|
// --- 3. TOUCH INPUT HANDLING ---
|
|
g.handleTouchInput()
|
|
|
|
// --- 4. INPUT STATE ERSTELLEN ---
|
|
joyDir := 0.0
|
|
if g.joyActive {
|
|
diffX := g.joyStickX - g.joyBaseX
|
|
if diffX < -20 {
|
|
joyDir = -1
|
|
}
|
|
if diffX > 20 {
|
|
joyDir = 1
|
|
}
|
|
}
|
|
|
|
isJoyDown := g.joyActive && (g.joyStickY-g.joyBaseY) > 40
|
|
|
|
// Input State zusammenbauen
|
|
input := InputState{
|
|
Sequence: g.inputSequence,
|
|
Left: keyLeft || joyDir == -1,
|
|
Right: keyRight || joyDir == 1,
|
|
Jump: keyJump || g.btnJumpActive,
|
|
Down: keyDown || isJoyDown,
|
|
}
|
|
g.btnJumpActive = false
|
|
|
|
// --- 4. CLIENT PREDICTION ---
|
|
if g.connected {
|
|
g.predictionMutex.Lock()
|
|
// Sequenznummer erhöhen
|
|
g.inputSequence++
|
|
input.Sequence = g.inputSequence
|
|
|
|
// Input speichern für später Reconciliation
|
|
g.pendingInputs[input.Sequence] = input
|
|
|
|
// Lokale Physik sofort anwenden (Prediction)
|
|
g.ApplyInput(input)
|
|
|
|
// Sanfte Korrektur anwenden (20% pro Frame)
|
|
const smoothingFactor = 0.2
|
|
if g.correctionX != 0 || g.correctionY != 0 {
|
|
g.predictedX += g.correctionX * smoothingFactor
|
|
g.predictedY += g.correctionY * smoothingFactor
|
|
|
|
g.correctionX *= (1.0 - smoothingFactor)
|
|
g.correctionY *= (1.0 - smoothingFactor)
|
|
|
|
// Korrektur beenden wenn sehr klein
|
|
if g.correctionX*g.correctionX+g.correctionY*g.correctionY < 0.01 {
|
|
g.correctionX = 0
|
|
g.correctionY = 0
|
|
}
|
|
}
|
|
|
|
// Landing Detection für Partikel
|
|
if !g.lastGroundState && g.predictedGround {
|
|
// Gerade gelandet! Partikel direkt unter dem Spieler (an den Füßen)
|
|
// Füße sind bei: Y + DrawOffY + Hitbox.OffsetY + Hitbox.H
|
|
// = Y - 231 + 42 + 184 = Y - 5
|
|
feetY := g.predictedY - 231 + 42 + 184
|
|
centerX := g.predictedX - 56 + 68 + 73/2
|
|
g.SpawnLandingParticles(centerX, feetY)
|
|
}
|
|
g.lastGroundState = g.predictedGround
|
|
|
|
g.predictionMutex.Unlock()
|
|
|
|
// Input an Server senden
|
|
g.SendInputWithSequence(input)
|
|
}
|
|
|
|
// --- 5. KAMERA LOGIK ---
|
|
g.stateMutex.Lock()
|
|
targetCam := g.gameState.ScrollX
|
|
g.stateMutex.Unlock()
|
|
|
|
// Negative Kamera verhindern
|
|
if targetCam < 0 {
|
|
targetCam = 0
|
|
}
|
|
|
|
// Kamera hart setzen
|
|
g.camX = targetCam
|
|
|
|
// --- 6. PARTIKEL UPDATEN ---
|
|
g.UpdateParticles(1.0 / 60.0) // Delta time: ~16ms
|
|
|
|
// --- 7. PARTIKEL SPAWNEN (State Changes Detection) ---
|
|
g.DetectAndSpawnParticles()
|
|
}
|
|
|
|
// Verarbeitet Touch-Eingaben für Joystick und Buttons
|
|
func (g *Game) handleTouchInput() {
|
|
touches := ebiten.TouchIDs()
|
|
|
|
// Reset, wenn keine Finger mehr auf dem Display sind
|
|
if len(touches) == 0 {
|
|
g.joyActive = false
|
|
g.joyStickX = g.joyBaseX
|
|
g.joyStickY = g.joyBaseY
|
|
return
|
|
}
|
|
|
|
joyFound := false
|
|
|
|
for _, id := range touches {
|
|
x, y := ebiten.TouchPosition(id)
|
|
fx, fy := float64(x), float64(y)
|
|
|
|
// 1. RECHTE SEITE: JUMP BUTTON
|
|
// Alles rechts der Bildschirmmitte ist "Springen"
|
|
if fx > ScreenWidth/2 {
|
|
// Prüfen, ob dieser Touch gerade NEU dazu gekommen ist
|
|
for _, justID := range inpututil.JustPressedTouchIDs() {
|
|
if id == justID {
|
|
g.btnJumpActive = true
|
|
break
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// 2. LINKE SEITE: JOYSTICK
|
|
// Wenn wir noch keinen Joystick-Finger haben, prüfen wir, ob dieser Finger startet
|
|
if !g.joyActive {
|
|
// Prüfen ob Touch in der Nähe der Joystick-Basis ist (Radius 150 Toleranz)
|
|
dist := math.Sqrt(math.Pow(fx-g.joyBaseX, 2) + math.Pow(fy-g.joyBaseY, 2))
|
|
if dist < 150 {
|
|
g.joyActive = true
|
|
g.joyTouchID = id
|
|
}
|
|
}
|
|
|
|
// Wenn das der Joystick-Finger ist -> Stick bewegen
|
|
if g.joyActive && id == g.joyTouchID {
|
|
joyFound = true
|
|
|
|
// Vektor berechnen (Wie weit ziehen wir weg?)
|
|
dx := fx - g.joyBaseX
|
|
dy := fy - g.joyBaseY
|
|
dist := math.Sqrt(dx*dx + dy*dy)
|
|
maxDist := 60.0 // Maximaler Radius des Sticks
|
|
|
|
// Begrenzen auf Radius
|
|
if dist > maxDist {
|
|
scale := maxDist / dist
|
|
dx *= scale
|
|
dy *= scale
|
|
}
|
|
|
|
g.joyStickX = g.joyBaseX + dx
|
|
g.joyStickY = g.joyBaseY + dy
|
|
}
|
|
}
|
|
|
|
// Wenn der Joystick-Finger losgelassen wurde, Joystick resetten
|
|
if !joyFound {
|
|
g.joyActive = false
|
|
g.joyStickX = g.joyBaseX
|
|
g.joyStickY = g.joyBaseY
|
|
}
|
|
}
|
|
|
|
// --- RENDERING LOGIC ---
|
|
|
|
func (g *Game) DrawGame(screen *ebiten.Image) {
|
|
// WICHTIG: GAMEOVER-Check ZUERST, bevor wir Locks holen!
|
|
g.stateMutex.Lock()
|
|
status := g.gameState.Status
|
|
g.stateMutex.Unlock()
|
|
|
|
if status == "GAMEOVER" {
|
|
// Game Over Screen - komplett separates Rendering ohne weitere Locks
|
|
g.stateMutex.Lock()
|
|
myScore := 0
|
|
for _, p := range g.gameState.Players {
|
|
if p.Name == g.playerName {
|
|
myScore = p.Score
|
|
break
|
|
}
|
|
}
|
|
g.stateMutex.Unlock()
|
|
|
|
// In WASM: HTML Game Over Screen anzeigen
|
|
if !g.scoreSubmitted {
|
|
g.scoreSubmitted = true
|
|
g.submitScore()
|
|
g.sendGameOverToJS(myScore) // Zeigt HTML Game Over Screen
|
|
}
|
|
|
|
g.DrawGameOverLeaderboard(screen, myScore)
|
|
return // Früher Return, damit Game-UI nicht mehr gezeichnet wird
|
|
}
|
|
|
|
// State Locken für Datenzugriff
|
|
g.stateMutex.Lock()
|
|
|
|
// Prüfe ob Spieler tot ist
|
|
isDead := false
|
|
myScore := 0
|
|
for _, p := range g.gameState.Players {
|
|
if p.Name == g.playerName {
|
|
isDead = !p.IsAlive || p.IsSpectator
|
|
myScore = p.Score
|
|
break
|
|
}
|
|
}
|
|
g.stateMutex.Unlock()
|
|
|
|
// 1. Hintergrund (wechselt alle 5000 Punkte)
|
|
backgroundID := "background"
|
|
if myScore >= 10000 {
|
|
backgroundID = "background2"
|
|
} else if myScore >= 5000 {
|
|
backgroundID = "background1"
|
|
}
|
|
|
|
// Hintergrundbild zeichnen (skaliert auf Bildschirmgröße)
|
|
if bgImg, exists := g.assetsImages[backgroundID]; exists && bgImg != nil {
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
// Skalierung berechnen, um Bildschirm zu füllen
|
|
bgW, bgH := bgImg.Size()
|
|
scaleX := float64(ScreenWidth) / float64(bgW)
|
|
scaleY := float64(ScreenHeight) / float64(bgH)
|
|
scale := math.Max(scaleX, scaleY) // Größere Skalierung verwenden, um zu füllen
|
|
|
|
op.GeoM.Scale(scale, scale)
|
|
|
|
// Zentrieren
|
|
scaledW := float64(bgW) * scale
|
|
scaledH := float64(bgH) * scale
|
|
offsetX := (float64(ScreenWidth) - scaledW) / 2
|
|
offsetY := (float64(ScreenHeight) - scaledH) / 2
|
|
op.GeoM.Translate(offsetX, offsetY)
|
|
|
|
screen.DrawImage(bgImg, op)
|
|
} else {
|
|
// Fallback: Einfarbiger Himmel
|
|
screen.Fill(ColSky)
|
|
}
|
|
|
|
// Boden zeichnen (prozedural mit Dirt und Steinen, bewegt sich mit Kamera)
|
|
g.RenderGround(screen, g.camX)
|
|
|
|
// State Locken für Datenzugriff
|
|
g.stateMutex.Lock()
|
|
defer g.stateMutex.Unlock()
|
|
|
|
// 2. Chunks (Welt-Objekte)
|
|
for _, activeChunk := range g.gameState.WorldChunks {
|
|
chunkDef, exists := g.world.ChunkLibrary[activeChunk.ChunkID]
|
|
if !exists {
|
|
log.Printf("⚠️ Chunk '%s' nicht in Library gefunden!", activeChunk.ChunkID)
|
|
continue
|
|
}
|
|
|
|
// Start-Chunk hat absichtlich keine Objekte
|
|
|
|
for objIdx, obj := range chunkDef.Objects {
|
|
// Skip Moving Platforms - die werden separat gerendert
|
|
if obj.MovingPlatform != nil {
|
|
continue
|
|
}
|
|
|
|
// Prüfe ob Coin/Powerup bereits eingesammelt wurde
|
|
assetDef, hasAsset := g.world.Manifest.Assets[obj.AssetID]
|
|
if hasAsset {
|
|
key := fmt.Sprintf("%s_%d", activeChunk.ChunkID, objIdx)
|
|
|
|
if assetDef.Type == "coin" && g.gameState.CollectedCoins[key] {
|
|
// Coin wurde eingesammelt, nicht zeichnen
|
|
continue
|
|
}
|
|
|
|
if assetDef.Type == "powerup" && g.gameState.CollectedPowerups[key] {
|
|
// Powerup wurde eingesammelt, nicht zeichnen
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Asset zeichnen
|
|
g.DrawAsset(screen, obj.AssetID, activeChunk.X+obj.X, obj.Y)
|
|
}
|
|
}
|
|
|
|
// 2.5 Bewegende Plattformen (von Server synchronisiert)
|
|
for _, mp := range g.gameState.MovingPlatforms {
|
|
g.DrawAsset(screen, mp.AssetID, mp.X, mp.Y)
|
|
}
|
|
|
|
// 3. Spieler
|
|
// MyID ohne Lock holen (wir haben bereits den stateMutex)
|
|
myID := ""
|
|
for id, p := range g.gameState.Players {
|
|
if p.Name == g.playerName {
|
|
myID = id
|
|
break
|
|
}
|
|
}
|
|
|
|
for id, p := range g.gameState.Players {
|
|
// Für lokalen Spieler: Verwende vorhergesagte Position
|
|
posX, posY := p.X, p.Y
|
|
vy := p.VY
|
|
onGround := p.OnGround
|
|
if id == myID && g.connected {
|
|
posX = g.predictedX
|
|
posY = g.predictedY
|
|
vy = g.predictedVY
|
|
onGround = g.predictedGround
|
|
}
|
|
|
|
// Wähle Sprite basierend auf Sprung-Status
|
|
sprite := "player" // Default: am Boden
|
|
|
|
// Nur Jump-Animation wenn wirklich in der Luft
|
|
// (nicht auf Boden, nicht auf Platform mit VY ~= 0)
|
|
isInAir := !onGround && (vy < -1.0 || vy > 1.0)
|
|
|
|
if isInAir {
|
|
if vy < -2.0 {
|
|
// Springt nach oben
|
|
sprite = "jump0"
|
|
} else {
|
|
// Fällt oder höchster Punkt
|
|
sprite = "jump1"
|
|
}
|
|
}
|
|
|
|
g.DrawAsset(screen, sprite, posX, posY)
|
|
|
|
// Name Tag
|
|
name := p.Name
|
|
if name == "" {
|
|
name = id
|
|
}
|
|
text.Draw(screen, name, basicfont.Face7x13, int(posX-g.camX), int(posY-25), ColText)
|
|
|
|
// DEBUG: Rote Hitbox
|
|
if def, ok := g.world.Manifest.Assets["player"]; ok {
|
|
hx := float32(posX + def.DrawOffX + def.Hitbox.OffsetX - g.camX)
|
|
hy := float32(posY + def.DrawOffY + def.Hitbox.OffsetY)
|
|
vector.StrokeRect(screen, hx, hy, float32(def.Hitbox.W), float32(def.Hitbox.H), 2, color.RGBA{255, 0, 0, 255}, false)
|
|
}
|
|
}
|
|
|
|
// 4. UI Status
|
|
if g.gameState.Status == "COUNTDOWN" {
|
|
msg := fmt.Sprintf("GO IN: %d", g.gameState.TimeLeft)
|
|
text.Draw(screen, msg, basicfont.Face7x13, ScreenWidth/2-40, ScreenHeight/2, color.RGBA{255, 255, 0, 255})
|
|
} else if g.gameState.Status == "RUNNING" {
|
|
dist := fmt.Sprintf("Distance: %.0f m", g.camX/64.0)
|
|
text.Draw(screen, dist, basicfont.Face7x13, ScreenWidth-150, 30, ColText)
|
|
|
|
// Score anzeigen
|
|
scoreStr := fmt.Sprintf("Score: %d", myScore)
|
|
text.Draw(screen, scoreStr, basicfont.Face7x13, ScreenWidth-150, 50, ColText)
|
|
|
|
// Spectator Overlay wenn tot
|
|
if isDead {
|
|
// Halbtransparenter roter Overlay
|
|
vector.DrawFilledRect(screen, 0, 0, ScreenWidth, 80, color.RGBA{150, 0, 0, 180}, false)
|
|
text.Draw(screen, "☠ DU BIST TOT - SPECTATOR MODE ☠", basicfont.Face7x13, ScreenWidth/2-140, 30, color.White)
|
|
text.Draw(screen, fmt.Sprintf("Dein Final Score: %d", myScore), basicfont.Face7x13, ScreenWidth/2-90, 55, color.RGBA{255, 255, 0, 255})
|
|
}
|
|
}
|
|
|
|
// 5. DEBUG: TODES-LINIE
|
|
vector.StrokeLine(screen, 0, 0, 0, float32(ScreenHeight), 10, color.RGBA{255, 0, 0, 128}, false)
|
|
text.Draw(screen, "! DEATH ZONE !", basicfont.Face7x13, 10, ScreenHeight/2, color.RGBA{255, 0, 0, 255})
|
|
|
|
// 6. PARTIKEL RENDERN (vor UI)
|
|
g.RenderParticles(screen)
|
|
|
|
// 7. TOUCH CONTROLS OVERLAY (nur wenn Tastatur nicht benutzt wurde)
|
|
if !g.keyboardUsed {
|
|
// A) Joystick Base (dunkelgrau und durchsichtig)
|
|
baseCol := color.RGBA{80, 80, 80, 50} // Dunkelgrau und durchsichtig
|
|
vector.DrawFilledCircle(screen, float32(g.joyBaseX), float32(g.joyBaseY), 60, baseCol, false)
|
|
vector.StrokeCircle(screen, float32(g.joyBaseX), float32(g.joyBaseY), 60, 2, color.RGBA{100, 100, 100, 100}, false)
|
|
|
|
// B) Joystick Knob (dunkelgrau, außer wenn aktiv)
|
|
knobCol := color.RGBA{100, 100, 100, 80} // Dunkelgrau und durchsichtig
|
|
if g.joyActive {
|
|
knobCol = color.RGBA{100, 255, 100, 120} // Grün wenn aktiv, aber auch durchsichtig
|
|
}
|
|
vector.DrawFilledCircle(screen, float32(g.joyStickX), float32(g.joyStickY), 30, knobCol, false)
|
|
|
|
// C) Jump Button (Rechts, ausgeblendet bei Tastatur-Nutzung)
|
|
jumpX := float32(ScreenWidth - 150)
|
|
jumpY := float32(ScreenHeight - 150)
|
|
vector.DrawFilledCircle(screen, jumpX, jumpY, 50, color.RGBA{255, 0, 0, 50}, false)
|
|
vector.StrokeCircle(screen, jumpX, jumpY, 50, 2, color.RGBA{255, 0, 0, 100}, false)
|
|
text.Draw(screen, "JUMP", basicfont.Face7x13, int(jumpX)-15, int(jumpY)+5, color.RGBA{255, 255, 255, 150})
|
|
}
|
|
|
|
// 8. DEBUG INFO (Oben Links)
|
|
myPosStr := "N/A"
|
|
for _, p := range g.gameState.Players {
|
|
myPosStr = fmt.Sprintf("X:%.0f Y:%.0f", p.X, p.Y)
|
|
break
|
|
}
|
|
|
|
debugMsg := fmt.Sprintf(
|
|
"FPS: %.2f\nState: %s\nPlayers: %d\nCamX: %.0f\nPos: %s",
|
|
ebiten.CurrentFPS(),
|
|
g.gameState.Status,
|
|
len(g.gameState.Players),
|
|
g.camX,
|
|
myPosStr,
|
|
)
|
|
|
|
vector.DrawFilledRect(screen, 10, 10, 200, 90, color.RGBA{0, 0, 0, 180}, false)
|
|
text.Draw(screen, debugMsg, basicfont.Face7x13, 20, 30, color.White)
|
|
}
|
|
|
|
// --- ASSET HELPER ---
|
|
|
|
func (g *Game) DrawAsset(screen *ebiten.Image, assetID string, worldX, worldY float64) {
|
|
// 1. Definition laden
|
|
def, ok := g.world.Manifest.Assets[assetID]
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// 2. Screen Position berechnen (Welt - Kamera)
|
|
screenX := worldX - g.camX
|
|
screenY := worldY
|
|
|
|
// Optimierung: Nicht zeichnen, wenn komplett außerhalb
|
|
if screenX < -200 || screenX > ScreenWidth+200 {
|
|
return
|
|
}
|
|
|
|
// 3. Bild holen
|
|
img := g.assetsImages[assetID]
|
|
|
|
if img != nil {
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
// Filter für bessere Skalierung (besonders bei großen Sprites)
|
|
op.Filter = ebiten.FilterLinear
|
|
|
|
// Skalieren
|
|
op.GeoM.Scale(def.Scale, def.Scale)
|
|
|
|
// Positionieren: ScreenPos + DrawOffset
|
|
op.GeoM.Translate(
|
|
screenX+def.DrawOffX,
|
|
screenY+def.DrawOffY,
|
|
)
|
|
|
|
// Farbe anwenden (nur wenn explizit gesetzt)
|
|
// Wenn Color leer ist (R=G=B=A=0), nicht anwenden (Bild bleibt original)
|
|
if def.Color.R != 0 || def.Color.G != 0 || def.Color.B != 0 || def.Color.A != 0 {
|
|
op.ColorScale.ScaleWithColor(def.Color.ToRGBA())
|
|
}
|
|
|
|
screen.DrawImage(img, op)
|
|
} else {
|
|
// FALLBACK (Buntes Rechteck)
|
|
vector.DrawFilledRect(screen,
|
|
float32(screenX+def.Hitbox.OffsetX),
|
|
float32(screenY+def.Hitbox.OffsetY),
|
|
float32(def.Hitbox.W),
|
|
float32(def.Hitbox.H),
|
|
def.Color.ToRGBA(),
|
|
false,
|
|
)
|
|
}
|
|
}
|
|
|
|
// DrawGameOverLeaderboard zeigt Leaderboard mit Team-Name-Eingabe
|
|
func (g *Game) DrawGameOverLeaderboard(screen *ebiten.Image, myScore int) {
|
|
screen.Fill(color.RGBA{20, 20, 30, 255})
|
|
|
|
// Leaderboard immer beim ersten Mal anfordern (ohne Lock hier!)
|
|
if !g.scoreSubmitted && g.gameMode == "solo" {
|
|
g.submitScore() // submitScore() ruft requestLeaderboard() auf
|
|
} else {
|
|
// Für Coop: Nur Leaderboard anfordern, nicht submitten
|
|
g.leaderboardMutex.Lock()
|
|
needsLeaderboard := len(g.leaderboard) == 0 && g.connected
|
|
g.leaderboardMutex.Unlock()
|
|
|
|
if needsLeaderboard {
|
|
g.requestLeaderboard()
|
|
}
|
|
}
|
|
|
|
// Großes GAME OVER
|
|
text.Draw(screen, "GAME OVER", basicfont.Face7x13, ScreenWidth/2-50, 60, color.RGBA{255, 0, 0, 255})
|
|
|
|
// Linke Seite: Raum-Ergebnisse - Daten KOPIEREN mit Lock, dann außerhalb zeichnen
|
|
text.Draw(screen, "=== RAUM ERGEBNISSE ===", basicfont.Face7x13, 50, 120, color.RGBA{255, 255, 0, 255})
|
|
|
|
type playerScore struct {
|
|
name string
|
|
score int
|
|
}
|
|
|
|
// Lock NUR für Datenkopie
|
|
g.stateMutex.Lock()
|
|
players := make([]playerScore, 0, len(g.gameState.Players))
|
|
for _, p := range g.gameState.Players {
|
|
players = append(players, playerScore{name: p.Name, score: p.Score})
|
|
}
|
|
g.stateMutex.Unlock()
|
|
|
|
// Sortieren und Zeichnen OHNE Lock
|
|
sort.Slice(players, func(i, j int) bool {
|
|
return players[i].score > players[j].score
|
|
})
|
|
|
|
y := 150
|
|
for i, p := range players {
|
|
medal := ""
|
|
if i == 0 {
|
|
medal = "🥇 "
|
|
} else if i == 1 {
|
|
medal = "🥈 "
|
|
} else if i == 2 {
|
|
medal = "🥉 "
|
|
}
|
|
scoreMsg := fmt.Sprintf("%d. %s%s: %d pts", i+1, medal, p.name, p.score)
|
|
text.Draw(screen, scoreMsg, basicfont.Face7x13, 50, y, color.White)
|
|
y += 20
|
|
}
|
|
|
|
// Rechte Seite: Global Leaderboard - Daten KOPIEREN mit Lock, dann außerhalb zeichnen
|
|
text.Draw(screen, "=== TOP 10 BESTENLISTE ===", basicfont.Face7x13, 650, 120, color.RGBA{255, 215, 0, 255})
|
|
|
|
// Lock NUR für Datenkopie
|
|
g.leaderboardMutex.Lock()
|
|
leaderboardCopy := make([]game.LeaderboardEntry, len(g.leaderboard))
|
|
copy(leaderboardCopy, g.leaderboard)
|
|
g.leaderboardMutex.Unlock()
|
|
|
|
// Zeichnen OHNE Lock
|
|
ly := 150
|
|
if len(leaderboardCopy) == 0 {
|
|
text.Draw(screen, "Laden...", basicfont.Face7x13, 700, ly, color.Gray{150})
|
|
} else {
|
|
for i, entry := range leaderboardCopy {
|
|
if i >= 10 {
|
|
break
|
|
}
|
|
var col color.Color = color.White
|
|
marker := ""
|
|
if entry.PlayerCode == g.playerCode {
|
|
col = color.RGBA{0, 255, 0, 255}
|
|
marker = " ← DU"
|
|
}
|
|
medal := ""
|
|
if i == 0 {
|
|
medal = "🥇 "
|
|
} else if i == 1 {
|
|
medal = "🥈 "
|
|
} else if i == 2 {
|
|
medal = "🥉 "
|
|
}
|
|
leaderMsg := fmt.Sprintf("%d. %s%s: %d%s", i+1, medal, entry.PlayerName, entry.Score, marker)
|
|
text.Draw(screen, leaderMsg, basicfont.Face7x13, 650, ly, col)
|
|
ly += 20
|
|
}
|
|
}
|
|
|
|
// Team-Name-Eingabe nur für Coop-Host (in der Mitte unten)
|
|
if g.gameMode == "coop" && g.isHost {
|
|
text.Draw(screen, "Host: Gib Team-Namen ein", basicfont.Face7x13, ScreenWidth/2-100, ScreenHeight-180, color.RGBA{255, 215, 0, 255})
|
|
|
|
// Team-Name Feld
|
|
fieldW := 300
|
|
fieldX := ScreenWidth/2 - fieldW/2
|
|
fieldY := ScreenHeight - 140
|
|
|
|
col := color.RGBA{70, 70, 80, 255}
|
|
if g.activeField == "teamname" {
|
|
col = color.RGBA{90, 90, 100, 255}
|
|
}
|
|
vector.DrawFilledRect(screen, float32(fieldX), float32(fieldY), float32(fieldW), 40, col, false)
|
|
vector.StrokeRect(screen, float32(fieldX), float32(fieldY), float32(fieldW), 40, 2, color.RGBA{255, 215, 0, 255}, false)
|
|
|
|
display := g.teamName
|
|
if g.activeField == "teamname" && (time.Now().UnixMilli()/500)%2 == 0 {
|
|
display += "|"
|
|
}
|
|
if display == "" {
|
|
display = "Team Name..."
|
|
}
|
|
text.Draw(screen, display, basicfont.Face7x13, fieldX+10, fieldY+25, color.White)
|
|
|
|
// Submit Button
|
|
submitBtnY := ScreenHeight - 85
|
|
submitBtnW := 200
|
|
submitBtnX := ScreenWidth/2 - submitBtnW/2
|
|
|
|
btnCol := color.RGBA{0, 150, 0, 255}
|
|
if g.teamName == "" {
|
|
btnCol = color.RGBA{100, 100, 100, 255} // Grau wenn kein Name
|
|
}
|
|
vector.DrawFilledRect(screen, float32(submitBtnX), float32(submitBtnY), float32(submitBtnW), 40, btnCol, false)
|
|
vector.StrokeRect(screen, float32(submitBtnX), float32(submitBtnY), float32(submitBtnW), 40, 2, color.White, false)
|
|
text.Draw(screen, "SUBMIT SCORE", basicfont.Face7x13, submitBtnX+50, submitBtnY+25, color.White)
|
|
} else if g.gameMode == "solo" && g.scoreSubmitted {
|
|
// Solo: Zeige Bestätigungsmeldung
|
|
text.Draw(screen, "Score eingereicht!", basicfont.Face7x13, ScreenWidth/2-70, ScreenHeight-100, color.RGBA{0, 255, 0, 255})
|
|
} else if g.gameMode == "coop" && !g.isHost {
|
|
// Coop Non-Host: Warten auf Host
|
|
text.Draw(screen, "Warte auf Host...", basicfont.Face7x13, ScreenWidth/2-70, ScreenHeight-100, color.Gray{180})
|
|
}
|
|
|
|
// Back Button (oben links)
|
|
backBtnW, backBtnH := 120, 40
|
|
backBtnX, backBtnY := 20, 20
|
|
vector.DrawFilledRect(screen, float32(backBtnX), float32(backBtnY), float32(backBtnW), float32(backBtnH), color.RGBA{150, 0, 0, 255}, false)
|
|
vector.StrokeRect(screen, float32(backBtnX), float32(backBtnY), float32(backBtnW), float32(backBtnH), 2, color.White, false)
|
|
text.Draw(screen, "< ZURÜCK", basicfont.Face7x13, backBtnX+20, backBtnY+25, color.White)
|
|
|
|
// Unten: Anleitung
|
|
text.Draw(screen, "ESC oder ZURÜCK-Button = Menü", basicfont.Face7x13, ScreenWidth/2-110, ScreenHeight-30, color.Gray{180})
|
|
}
|