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:
@@ -1,22 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image/color"
|
||||
_ "image/png"
|
||||
"io/ioutil"
|
||||
_ "image/jpeg" // JPEG-Decoder
|
||||
_ "image/png" // PNG-Decoder
|
||||
"log"
|
||||
mrand "math/rand"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/text"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
@@ -28,8 +24,8 @@ import (
|
||||
|
||||
// --- KONFIGURATION ---
|
||||
const (
|
||||
ScreenWidth = 1280
|
||||
ScreenHeight = 720
|
||||
ScreenWidth = 1280
|
||||
ScreenHeight = 720
|
||||
StateMenu = 0
|
||||
StateLobby = 1
|
||||
StateGame = 2
|
||||
@@ -59,6 +55,7 @@ type InputState struct {
|
||||
type Game struct {
|
||||
appState int
|
||||
conn *nats.EncodedConn
|
||||
wsConn *wsConn // WebSocket für WASM
|
||||
gameState game.GameState
|
||||
stateMutex sync.Mutex
|
||||
connected bool
|
||||
@@ -95,6 +92,21 @@ type Game struct {
|
||||
lastServerSeq uint32 // Letzte vom Server bestätigte Sequenz
|
||||
predictionMutex sync.Mutex // Mutex für pendingInputs
|
||||
|
||||
// Smooth Correction
|
||||
correctionX float64 // Verbleibende Korrektur in X
|
||||
correctionY float64 // Verbleibende Korrektur in Y
|
||||
|
||||
// Particle System
|
||||
particles []Particle
|
||||
particlesMutex sync.Mutex
|
||||
lastGroundState bool // Für Landing-Detection
|
||||
lastCollectedCoins map[string]bool // Für Coin-Partikel
|
||||
lastCollectedPowerups map[string]bool // Für Powerup-Partikel
|
||||
lastPlayerStates map[string]game.PlayerState // Für Death-Partikel
|
||||
|
||||
// Audio System
|
||||
audio *AudioSystem
|
||||
|
||||
// Kamera
|
||||
camX float64
|
||||
|
||||
@@ -104,6 +116,7 @@ type Game struct {
|
||||
joyActive bool
|
||||
joyTouchID ebiten.TouchID
|
||||
btnJumpActive bool
|
||||
keyboardUsed bool // Wurde Tastatur benutzt?
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
@@ -114,67 +127,35 @@ func NewGame() *Game {
|
||||
gameState: game.GameState{Players: make(map[string]game.PlayerState)},
|
||||
|
||||
playerName: "Student",
|
||||
activeField: "name",
|
||||
activeField: "",
|
||||
gameMode: "",
|
||||
pendingInputs: make(map[uint32]InputState),
|
||||
leaderboard: make([]game.LeaderboardEntry, 0),
|
||||
|
||||
// Particle tracking
|
||||
lastCollectedCoins: make(map[string]bool),
|
||||
lastCollectedPowerups: make(map[string]bool),
|
||||
lastPlayerStates: make(map[string]game.PlayerState),
|
||||
|
||||
// Audio System
|
||||
audio: NewAudioSystem(),
|
||||
|
||||
joyBaseX: 150, joyBaseY: ScreenHeight - 150,
|
||||
joyStickX: 150, joyStickY: ScreenHeight - 150,
|
||||
}
|
||||
g.loadAssets()
|
||||
g.loadOrCreatePlayerCode()
|
||||
|
||||
// Gespeicherten Namen laden
|
||||
savedName := g.loadPlayerName()
|
||||
if savedName != "" {
|
||||
g.playerName = savedName
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *Game) loadAssets() {
|
||||
// Pfad anpassen: Wir suchen im relativen Pfad
|
||||
baseDir := "./cmd/client/assets"
|
||||
manifestPath := filepath.Join(baseDir, "assets.json")
|
||||
|
||||
data, err := ioutil.ReadFile(manifestPath)
|
||||
if err == nil {
|
||||
var m game.AssetManifest
|
||||
json.Unmarshal(data, &m)
|
||||
g.world.Manifest = m
|
||||
fmt.Println("✅ Assets Manifest geladen:", len(m.Assets), "Einträge")
|
||||
} else {
|
||||
log.Println("⚠️ assets.json NICHT gefunden! Pfad:", manifestPath)
|
||||
// Fallback: Leeres Manifest, damit das Spiel nicht abstürzt
|
||||
g.world.Manifest = game.AssetManifest{Assets: make(map[string]game.AssetDefinition)}
|
||||
}
|
||||
|
||||
// Chunks laden
|
||||
chunkDir := filepath.Join(baseDir, "chunks")
|
||||
err = g.world.LoadChunkLibrary(chunkDir)
|
||||
if err != nil {
|
||||
log.Println("⚠️ Chunks konnten nicht geladen werden:", err)
|
||||
} else {
|
||||
fmt.Println("✅ Chunks geladen:", len(g.world.ChunkLibrary), "Einträge")
|
||||
// DEBUG: Details der geladenen Chunks
|
||||
for id, chunk := range g.world.ChunkLibrary {
|
||||
fmt.Printf(" 📦 Chunk '%s': Width=%d, Objects=%d\n", id, chunk.Width, len(chunk.Objects))
|
||||
}
|
||||
}
|
||||
|
||||
// Bilder vorladen
|
||||
loadedImages := 0
|
||||
failedImages := 0
|
||||
for id, def := range g.world.Manifest.Assets {
|
||||
if def.Filename != "" {
|
||||
path := filepath.Join(baseDir, def.Filename)
|
||||
img, _, err := ebitenutil.NewImageFromFile(path)
|
||||
if err == nil {
|
||||
g.assetsImages[id] = img
|
||||
loadedImages++
|
||||
} else {
|
||||
log.Printf("⚠️ Bild nicht geladen: %s (%s) - Fehler: %v", id, def.Filename, err)
|
||||
failedImages++
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("🖼️ Bilder: %d geladen, %d fehlgeschlagen\n", loadedImages, failedImages)
|
||||
}
|
||||
// loadAssets() ist jetzt in assets_wasm.go und assets_native.go definiert
|
||||
|
||||
// --- UPDATE ---
|
||||
func (g *Game) Update() error {
|
||||
@@ -220,6 +201,17 @@ func (g *Game) Update() error {
|
||||
}
|
||||
}
|
||||
|
||||
// Musik-Start-Check (unabhängig vom State)
|
||||
if g.gameState.Status == "RUNNING" && g.lastStatus != "RUNNING" {
|
||||
log.Printf("🎮 Spiel startet! Status: %s -> %s", g.lastStatus, g.gameState.Status)
|
||||
g.audio.PlayMusic()
|
||||
}
|
||||
// Musik stoppen wenn Game Over
|
||||
if g.gameState.Status == "GAMEOVER" && g.lastStatus == "RUNNING" {
|
||||
g.audio.StopMusic()
|
||||
}
|
||||
g.lastStatus = g.gameState.Status
|
||||
|
||||
switch g.appState {
|
||||
case StateMenu:
|
||||
g.updateMenu()
|
||||
@@ -236,6 +228,30 @@ func (g *Game) Update() error {
|
||||
func (g *Game) updateMenu() {
|
||||
g.handleMenuInput()
|
||||
|
||||
// Volume Sliders (unten links)
|
||||
volumeX := 20
|
||||
volumeY := ScreenHeight - 100
|
||||
sliderWidth := 200
|
||||
sliderHeight := 10
|
||||
|
||||
// Music Volume Slider
|
||||
musicSliderY := volumeY + 10
|
||||
if isSliderHit(volumeX, musicSliderY, sliderWidth, sliderHeight) {
|
||||
newVolume := getSliderValue(volumeX, sliderWidth)
|
||||
g.audio.SetMusicVolume(newVolume)
|
||||
return
|
||||
}
|
||||
|
||||
// SFX Volume Slider
|
||||
sfxSliderY := volumeY + 50
|
||||
if isSliderHit(volumeX, sfxSliderY, sliderWidth, sliderHeight) {
|
||||
newVolume := getSliderValue(volumeX, sliderWidth)
|
||||
g.audio.SetSFXVolume(newVolume)
|
||||
// Test-Sound abspielen
|
||||
g.audio.PlayCoin()
|
||||
return
|
||||
}
|
||||
|
||||
// Leaderboard Button
|
||||
lbBtnW, lbBtnH := 200, 50
|
||||
lbBtnX := ScreenWidth - lbBtnW - 20
|
||||
@@ -324,7 +340,7 @@ func (g *Game) updateLobby() {
|
||||
|
||||
if isHit(btnX, btnY, btnW, btnH) {
|
||||
// START GAME
|
||||
g.SendCommand("START")
|
||||
g.sendStartRequest()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,18 +363,27 @@ func (g *Game) updateLobby() {
|
||||
|
||||
// --- DRAW ---
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
// In WASM: Nur das Spiel zeichnen, kein Menü/Lobby (HTML übernimmt das)
|
||||
// In Native: Alles zeichnen
|
||||
g.draw(screen)
|
||||
}
|
||||
|
||||
// draw ist die plattform-übergreifende Zeichenfunktion
|
||||
func (g *Game) draw(screen *ebiten.Image) {
|
||||
switch g.appState {
|
||||
case StateMenu:
|
||||
g.DrawMenu(screen)
|
||||
g.drawMenu(screen)
|
||||
case StateLobby:
|
||||
g.DrawLobby(screen)
|
||||
g.drawLobby(screen)
|
||||
case StateGame:
|
||||
g.DrawGame(screen)
|
||||
case StateLeaderboard:
|
||||
g.DrawLeaderboard(screen)
|
||||
g.drawLeaderboard(screen)
|
||||
}
|
||||
}
|
||||
|
||||
// drawMenu, drawLobby, drawLeaderboard sind in draw_wasm.go und draw_native.go definiert
|
||||
|
||||
func (g *Game) DrawMenu(screen *ebiten.Image) {
|
||||
screen.Fill(color.RGBA{20, 20, 30, 255})
|
||||
|
||||
@@ -442,7 +467,19 @@ func (g *Game) DrawMenu(screen *ebiten.Image) {
|
||||
vector.StrokeRect(screen, float32(lbBtnX), float32(lbBtnY), float32(lbBtnW), 50, 2, color.RGBA{255, 215, 0, 255}, false)
|
||||
text.Draw(screen, "🏆 LEADERBOARD", basicfont.Face7x13, lbBtnX+35, lbBtnY+30, color.RGBA{255, 215, 0, 255})
|
||||
|
||||
text.Draw(screen, "WASD / Arrows - SPACE to Jump", basicfont.Face7x13, ScreenWidth/2-100, ScreenHeight-30, color.Gray{150})
|
||||
// Volume Controls (unten links)
|
||||
volumeX := 20
|
||||
volumeY := ScreenHeight - 100
|
||||
|
||||
// Music Volume
|
||||
text.Draw(screen, "Music Volume:", basicfont.Face7x13, volumeX, volumeY, ColText)
|
||||
g.drawVolumeSlider(screen, volumeX, volumeY+10, 200, g.audio.GetMusicVolume())
|
||||
|
||||
// SFX Volume
|
||||
text.Draw(screen, "SFX Volume:", basicfont.Face7x13, volumeX, volumeY+40, ColText)
|
||||
g.drawVolumeSlider(screen, volumeX, volumeY+50, 200, g.audio.GetSFXVolume())
|
||||
|
||||
text.Draw(screen, "WASD / Arrows - SPACE to Jump - M to Mute", basicfont.Face7x13, ScreenWidth/2-130, ScreenHeight-15, color.Gray{150})
|
||||
}
|
||||
|
||||
func (g *Game) DrawLobby(screen *ebiten.Image) {
|
||||
@@ -580,6 +617,10 @@ func (g *Game) handleMenuInput() {
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
|
||||
// Namen speichern wenn geändert
|
||||
if g.activeField == "name" && g.playerName != "" {
|
||||
g.savePlayerName(g.playerName)
|
||||
}
|
||||
g.activeField = ""
|
||||
} else if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
|
||||
if len(*target) > 0 {
|
||||
@@ -614,7 +655,7 @@ func (g *Game) handleGameOverInput() {
|
||||
|
||||
if isHit(submitBtnX, submitBtnY, submitBtnW, 40) {
|
||||
if g.teamName != "" {
|
||||
g.submitTeamScore()
|
||||
g.submitScore() // submitScore behandelt jetzt beide Modi
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -623,7 +664,7 @@ func (g *Game) handleGameOverInput() {
|
||||
if g.activeField == "teamname" {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
|
||||
if g.teamName != "" {
|
||||
g.submitTeamScore()
|
||||
g.submitScore() // submitScore behandelt jetzt beide Modi
|
||||
}
|
||||
g.activeField = ""
|
||||
} else if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
|
||||
@@ -650,68 +691,6 @@ func generateRoomCode() string {
|
||||
}
|
||||
|
||||
func (g *Game) connectAndStart() {
|
||||
// URL: Wasm -> WS, Desktop -> TCP
|
||||
serverURL := "nats://localhost:4222"
|
||||
if runtime.GOARCH == "wasm" || runtime.GOOS == "js" {
|
||||
serverURL = "ws://localhost:9222"
|
||||
}
|
||||
|
||||
nc, err := nats.Connect(serverURL)
|
||||
if err != nil {
|
||||
log.Println("❌ NATS Connect Fehler:", err)
|
||||
return
|
||||
}
|
||||
ec, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
|
||||
g.conn = ec
|
||||
|
||||
// Subscribe nur auf Updates für DIESEN Raum
|
||||
roomChannel := fmt.Sprintf("game.update.%s", g.roomID)
|
||||
log.Printf("👂 Lausche auf Channel: %s", roomChannel)
|
||||
|
||||
sub, err := g.conn.Subscribe(roomChannel, func(state *game.GameState) {
|
||||
// Server Reconciliation für lokalen Spieler (VOR dem Lock)
|
||||
for _, p := range state.Players {
|
||||
if p.Name == g.playerName {
|
||||
// Reconcile mit Server-State (verwendet keinen stateMutex)
|
||||
g.ReconcileWithServer(p)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
g.stateMutex.Lock()
|
||||
oldPlayerCount := len(g.gameState.Players)
|
||||
oldStatus := g.gameState.Status
|
||||
g.gameState = *state
|
||||
g.stateMutex.Unlock()
|
||||
|
||||
// Nur bei Änderungen loggen
|
||||
if len(state.Players) != oldPlayerCount || state.Status != oldStatus {
|
||||
log.Printf("📦 State Update: RoomID=%s, Players=%d, HostID=%s, Status=%s", state.RoomID, len(state.Players), state.HostID, state.Status)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Println("❌ Fehler beim Subscribe:", err)
|
||||
return
|
||||
}
|
||||
log.Printf("👂 Subscription aktiv (Valid: %v)", sub.IsValid())
|
||||
|
||||
// Kurze Pause, damit Subscription aktiv ist
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// JOIN MIT ROOM ID SENDEN
|
||||
joinReq := game.JoinRequest{
|
||||
Name: g.playerName,
|
||||
RoomID: g.roomID,
|
||||
}
|
||||
log.Printf("📤 Sende JOIN Request: Name=%s, RoomID=%s", g.playerName, g.roomID)
|
||||
err = g.conn.Publish("game.join", joinReq)
|
||||
if err != nil {
|
||||
log.Println("❌ Fehler beim Publish:", err)
|
||||
return
|
||||
}
|
||||
g.connected = true
|
||||
|
||||
// Initiale predicted Position
|
||||
g.predictedX = 100
|
||||
g.predictedY = 200
|
||||
@@ -719,7 +698,8 @@ func (g *Game) connectAndStart() {
|
||||
g.predictedVY = 0
|
||||
g.predictedGround = false
|
||||
|
||||
log.Printf("✅ JOIN gesendet. Warte auf Server-Antwort...")
|
||||
// Verbindung über plattformspezifische Implementierung
|
||||
g.connectToServer()
|
||||
}
|
||||
|
||||
func (g *Game) SendCommand(cmdType string) {
|
||||
@@ -727,7 +707,7 @@ func (g *Game) SendCommand(cmdType string) {
|
||||
return
|
||||
}
|
||||
myID := g.getMyPlayerID()
|
||||
g.conn.Publish("game.input", game.ClientInput{PlayerID: myID, Type: cmdType})
|
||||
g.publishInput(game.ClientInput{PlayerID: myID, Type: cmdType})
|
||||
}
|
||||
|
||||
func (g *Game) SendInputWithSequence(input InputState) {
|
||||
@@ -739,28 +719,30 @@ func (g *Game) SendInputWithSequence(input InputState) {
|
||||
|
||||
// Inputs als einzelne Commands senden
|
||||
if input.Left {
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "LEFT_DOWN",
|
||||
Sequence: input.Sequence,
|
||||
})
|
||||
}
|
||||
if input.Right {
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "RIGHT_DOWN",
|
||||
Sequence: input.Sequence,
|
||||
})
|
||||
}
|
||||
if input.Jump {
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "JUMP",
|
||||
Sequence: input.Sequence,
|
||||
})
|
||||
// Jump Sound abspielen
|
||||
g.audio.PlayJump()
|
||||
}
|
||||
if input.Down {
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "DOWN",
|
||||
Sequence: input.Sequence,
|
||||
@@ -769,12 +751,12 @@ func (g *Game) SendInputWithSequence(input InputState) {
|
||||
|
||||
// Wenn weder Links noch Rechts, sende STOP
|
||||
if !input.Left && !input.Right {
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "LEFT_UP",
|
||||
Sequence: input.Sequence,
|
||||
})
|
||||
g.conn.Publish("game.input", game.ClientInput{
|
||||
g.publishInput(game.ClientInput{
|
||||
PlayerID: myID,
|
||||
Type: "RIGHT_UP",
|
||||
Sequence: input.Sequence,
|
||||
@@ -794,113 +776,8 @@ func (g *Game) getMyPlayerID() string {
|
||||
return g.playerName
|
||||
}
|
||||
|
||||
// loadOrCreatePlayerCode wird in storage_*.go implementiert (platform-specific)
|
||||
|
||||
// submitScore sendet den individuellen Score an den Server (für Solo-Mode)
|
||||
func (g *Game) submitScore() {
|
||||
if g.scoreSubmitted || !g.connected {
|
||||
return
|
||||
}
|
||||
|
||||
// Finde eigenen Score
|
||||
myScore := 0
|
||||
for _, p := range g.gameState.Players {
|
||||
if p.Name == g.playerName {
|
||||
myScore = p.Score
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
submission := game.ScoreSubmission{
|
||||
PlayerName: g.playerName,
|
||||
PlayerCode: g.playerCode,
|
||||
Score: myScore,
|
||||
}
|
||||
|
||||
g.conn.Publish("score.submit", submission)
|
||||
g.scoreSubmitted = true
|
||||
log.Printf("📊 Score eingereicht: %d Punkte", myScore)
|
||||
|
||||
// Leaderboard abrufen
|
||||
g.requestLeaderboard()
|
||||
}
|
||||
|
||||
// submitTeamScore sendet den Team-Score an den Server (für Coop-Mode)
|
||||
func (g *Game) submitTeamScore() {
|
||||
if g.scoreSubmitted || !g.connected || g.teamName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Berechne Team-Score (Summe aller Spieler-Scores)
|
||||
teamScore := 0
|
||||
for _, p := range g.gameState.Players {
|
||||
teamScore += p.Score
|
||||
}
|
||||
|
||||
submission := game.ScoreSubmission{
|
||||
PlayerName: g.teamName, // Team-Name statt Spieler-Name
|
||||
PlayerCode: g.playerCode,
|
||||
Score: teamScore,
|
||||
}
|
||||
|
||||
g.conn.Publish("score.submit", submission)
|
||||
g.scoreSubmitted = true
|
||||
g.activeField = ""
|
||||
log.Printf("📊 Team-Score eingereicht: %s - %d Punkte", g.teamName, teamScore)
|
||||
|
||||
// Leaderboard abrufen
|
||||
g.requestLeaderboard()
|
||||
}
|
||||
|
||||
// requestLeaderboard fordert das Leaderboard vom Server an (asynchron)
|
||||
func (g *Game) requestLeaderboard() {
|
||||
if !g.connected {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
inbox := g.conn.Conn.NewRespInbox()
|
||||
sub, err := g.conn.Subscribe(inbox, func(entries *[]game.LeaderboardEntry) {
|
||||
g.leaderboardMutex.Lock()
|
||||
g.leaderboard = *entries
|
||||
g.leaderboardMutex.Unlock()
|
||||
log.Printf("📊 Leaderboard empfangen: %d Einträge", len(*entries))
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Fehler beim Leaderboard-Request: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Request senden
|
||||
g.conn.PublishRequest("leaderboard.get", inbox, &struct{}{})
|
||||
|
||||
// Warte kurz auf Antwort, dann unsubscribe
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
sub.Unsubscribe()
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *Game) connectForLeaderboard() {
|
||||
serverURL := "nats://localhost:4222"
|
||||
nc, err := nats.Connect(serverURL)
|
||||
if err != nil {
|
||||
log.Printf("❌ NATS Verbindung fehlgeschlagen: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
g.conn, err = nats.NewEncodedConn(nc, nats.JSON_ENCODER)
|
||||
if err != nil {
|
||||
log.Printf("❌ EncodedConn Fehler: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
g.connected = true
|
||||
log.Println("✅ Verbunden für Leaderboard")
|
||||
|
||||
// Leaderboard abrufen
|
||||
g.requestLeaderboard()
|
||||
}
|
||||
// submitScore, requestLeaderboard, connectForLeaderboard
|
||||
// sind in connection_native.go und connection_wasm.go definiert
|
||||
|
||||
func (g *Game) updateLeaderboard() {
|
||||
// Back Button (oben links) - Touch Support
|
||||
@@ -977,12 +854,46 @@ func (g *Game) DrawLeaderboard(screen *ebiten.Image) {
|
||||
text.Draw(screen, "ESC oder ZURÜCK-Button = Menü", basicfont.Face7x13, ScreenWidth/2-110, ScreenHeight-40, color.Gray{150})
|
||||
}
|
||||
|
||||
func main() {
|
||||
ebiten.SetWindowSize(ScreenWidth, ScreenHeight)
|
||||
ebiten.SetWindowTitle("Escape From Teacher")
|
||||
ebiten.SetTPS(60) // Tick Per Second auf 60 setzen
|
||||
ebiten.SetVsyncEnabled(true) // VSync aktivieren
|
||||
if err := ebiten.RunGame(NewGame()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// main() ist jetzt in main_wasm.go und main_native.go definiert
|
||||
|
||||
// drawVolumeSlider zeichnet einen Volume-Slider
|
||||
func (g *Game) drawVolumeSlider(screen *ebiten.Image, x, y, width int, volume float64) {
|
||||
// Hintergrund
|
||||
vector.DrawFilledRect(screen, float32(x), float32(y), float32(width), 10, color.RGBA{40, 40, 50, 255}, false)
|
||||
vector.StrokeRect(screen, float32(x), float32(y), float32(width), 10, 1, color.White, false)
|
||||
|
||||
// Füllstand
|
||||
fillWidth := int(float64(width) * volume)
|
||||
vector.DrawFilledRect(screen, float32(x), float32(y), float32(fillWidth), 10, color.RGBA{0, 200, 100, 255}, false)
|
||||
|
||||
// Prozent-Anzeige
|
||||
pct := fmt.Sprintf("%.0f%%", volume*100)
|
||||
text.Draw(screen, pct, basicfont.Face7x13, x+width+10, y+10, ColText)
|
||||
}
|
||||
|
||||
// isSliderHit prüft, ob auf einen Slider geklickt wurde
|
||||
func isSliderHit(x, y, width, height int) bool {
|
||||
// Erweitere den Klickbereich vertikal für bessere Touch-Support
|
||||
return isHit(x, y-10, width, height+20)
|
||||
}
|
||||
|
||||
// getSliderValue berechnet den Slider-Wert basierend auf Mausposition
|
||||
func getSliderValue(sliderX, sliderWidth int) float64 {
|
||||
mx, _ := ebiten.CursorPosition()
|
||||
// Bei Touch: Ersten Touch nutzen
|
||||
touches := ebiten.TouchIDs()
|
||||
if len(touches) > 0 {
|
||||
mx, _ = ebiten.TouchPosition(touches[0])
|
||||
}
|
||||
|
||||
// Berechne relative Position im Slider
|
||||
relX := float64(mx - sliderX)
|
||||
if relX < 0 {
|
||||
relX = 0
|
||||
}
|
||||
if relX > float64(sliderWidth) {
|
||||
relX = float64(sliderWidth)
|
||||
}
|
||||
|
||||
return relX / float64(sliderWidth)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user