Add platform-specific implementations for assets, audio, WebSocket, and rendering on Desktop and WebAssembly platforms. Introduce embedded assets for WebAssembly and native file handling for Desktop. Add platform-specific chunk loading and game state synchronization.
This commit is contained in:
@@ -19,16 +19,26 @@ import (
|
||||
// --- INPUT & UPDATE LOGIC ---
|
||||
|
||||
func (g *Game) UpdateGame() {
|
||||
// --- 1. KEYBOARD INPUT ---
|
||||
// --- 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)
|
||||
|
||||
// --- 2. TOUCH INPUT HANDLING ---
|
||||
// Tastatur-Nutzung erkennen (für Mobile Controls ausblenden)
|
||||
if keyLeft || keyRight || keyDown || keyJump {
|
||||
g.keyboardUsed = true
|
||||
}
|
||||
|
||||
// --- 3. TOUCH INPUT HANDLING ---
|
||||
g.handleTouchInput()
|
||||
|
||||
// --- 3. INPUT STATE ERSTELLEN ---
|
||||
// --- 4. INPUT STATE ERSTELLEN ---
|
||||
joyDir := 0.0
|
||||
if g.joyActive {
|
||||
diffX := g.joyStickX - g.joyBaseX
|
||||
@@ -64,6 +74,34 @@ func (g *Game) UpdateGame() {
|
||||
|
||||
// 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
|
||||
@@ -72,10 +110,8 @@ func (g *Game) UpdateGame() {
|
||||
|
||||
// --- 5. KAMERA LOGIK ---
|
||||
g.stateMutex.Lock()
|
||||
defer g.stateMutex.Unlock()
|
||||
|
||||
// Wir folgen strikt dem Server-Scroll.
|
||||
targetCam := g.gameState.ScrollX
|
||||
g.stateMutex.Unlock()
|
||||
|
||||
// Negative Kamera verhindern
|
||||
if targetCam < 0 {
|
||||
@@ -84,6 +120,12 @@ func (g *Game) UpdateGame() {
|
||||
|
||||
// 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
|
||||
@@ -178,6 +220,13 @@ func (g *Game) DrawGame(screen *ebiten.Image) {
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -197,12 +246,41 @@ func (g *Game) DrawGame(screen *ebiten.Image) {
|
||||
}
|
||||
g.stateMutex.Unlock()
|
||||
|
||||
// 1. Hintergrund & Boden
|
||||
screen.Fill(ColSky)
|
||||
// 1. Hintergrund (wechselt alle 5000 Punkte)
|
||||
backgroundID := "background"
|
||||
if myScore >= 10000 {
|
||||
backgroundID = "background2"
|
||||
} else if myScore >= 5000 {
|
||||
backgroundID = "background1"
|
||||
}
|
||||
|
||||
floorH := float32(ScreenHeight - RefFloorY)
|
||||
vector.DrawFilledRect(screen, 0, float32(RefFloorY), float32(ScreenWidth), floorH, ColGrass, false)
|
||||
vector.DrawFilledRect(screen, 0, float32(RefFloorY)+20, float32(ScreenWidth), floorH-20, ColDirt, false)
|
||||
// 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()
|
||||
@@ -218,12 +296,38 @@ func (g *Game) DrawGame(screen *ebiten.Image) {
|
||||
|
||||
// Start-Chunk hat absichtlich keine Objekte
|
||||
|
||||
for _, obj := range chunkDef.Objects {
|
||||
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 := ""
|
||||
@@ -237,12 +341,33 @@ func (g *Game) DrawGame(screen *ebiten.Image) {
|
||||
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
|
||||
}
|
||||
|
||||
g.DrawAsset(screen, "player", posX, posY)
|
||||
// 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
|
||||
@@ -284,28 +409,32 @@ func (g *Game) DrawGame(screen *ebiten.Image) {
|
||||
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. TOUCH CONTROLS OVERLAY
|
||||
// 6. PARTIKEL RENDERN (vor UI)
|
||||
g.RenderParticles(screen)
|
||||
|
||||
// A) Joystick Base
|
||||
baseCol := color.RGBA{255, 255, 255, 50}
|
||||
vector.DrawFilledCircle(screen, float32(g.joyBaseX), float32(g.joyBaseY), 60, baseCol, true)
|
||||
vector.StrokeCircle(screen, float32(g.joyBaseX), float32(g.joyBaseY), 60, 2, color.RGBA{255, 255, 255, 100}, true)
|
||||
// 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
|
||||
knobCol := color.RGBA{255, 255, 255, 150}
|
||||
if g.joyActive {
|
||||
knobCol = color.RGBA{100, 255, 100, 200}
|
||||
// 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})
|
||||
}
|
||||
vector.DrawFilledCircle(screen, float32(g.joyStickX), float32(g.joyStickY), 30, knobCol, true)
|
||||
|
||||
// C) Jump Button (Rechts)
|
||||
jumpX := float32(ScreenWidth - 150)
|
||||
jumpY := float32(ScreenHeight - 150)
|
||||
vector.DrawFilledCircle(screen, jumpX, jumpY, 50, color.RGBA{255, 0, 0, 50}, true)
|
||||
vector.StrokeCircle(screen, jumpX, jumpY, 50, 2, color.RGBA{255, 0, 0, 100}, true)
|
||||
text.Draw(screen, "JUMP", basicfont.Face7x13, int(jumpX)-15, int(jumpY)+5, color.White)
|
||||
|
||||
// 7. DEBUG INFO (Oben Links)
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user