Private
Public Access
1
0

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:
Sebastian Unterschütz
2026-01-04 01:25:04 +01:00
parent 85d697df19
commit 3232ee7c2f
86 changed files with 4931 additions and 486 deletions

View File

@@ -28,10 +28,11 @@ const (
AssetFile = "./cmd/client/assets/assets.json"
ChunkDir = "./cmd/client/assets/chunks"
SidebarWidth = 250
TopBarHeight = 40
CanvasHeight = 720
CanvasWidth = 1280
LeftSidebarWidth = 250
RightSidebarWidth = 250
TopBarHeight = 40
CanvasHeight = 720
CanvasWidth = 1280
TileSize = 64
RefFloorY = 540
@@ -55,12 +56,15 @@ type LevelEditor struct {
assetList []string
assetsImages map[string]*ebiten.Image
currentChunk game.Chunk
currentChunk game.Chunk
currentChunkFile string // Aktuell geladene Datei
chunkFiles []string // Liste aller Chunk-Dateien
scrollX float64
zoom float64
listScroll float64
statusMsg string
scrollX float64
zoom float64
listScroll float64
chunkListScroll float64 // Scroll für Chunk-Liste
statusMsg string
showGrid bool
enableSnap bool
@@ -75,20 +79,31 @@ type LevelEditor struct {
dragAssetID string
dragTargetIndex int
dragOffset game.Vec2
// Bewegende Plattform-Modus
movingPlatformMode bool // Ist Bewegende-Plattform-Modus aktiv?
movingPlatformObjIndex int // Index des aktuellen Plattform-Objekts
movingPlatformSetStart bool // true = setze Start, false = setze End
}
func NewLevelEditor() *LevelEditor {
le := &LevelEditor{
assetsImages: make(map[string]*ebiten.Image),
currentChunk: game.Chunk{ID: "chunk_new", Width: 50, Objects: []game.LevelObject{}},
zoom: 1.0,
showGrid: true,
enableSnap: true,
showHitbox: true,
showPlayerRef: true, // Standardmäßig an
assetsImages: make(map[string]*ebiten.Image),
currentChunk: game.Chunk{ID: "chunk_new", Width: 50, Objects: []game.LevelObject{}},
zoom: 1.0,
showGrid: true,
enableSnap: true,
showHitbox: true,
showPlayerRef: true, // Standardmäßig an
movingPlatformObjIndex: -1,
}
le.LoadAssets()
le.LoadChunk("chunk_01.json")
le.RefreshChunkList()
if len(le.chunkFiles) > 0 {
le.LoadChunk(le.chunkFiles[0])
} else {
le.currentChunkFile = "chunk_new.json"
}
return le
}
@@ -117,29 +132,82 @@ func (le *LevelEditor) LoadAssets() {
sort.Strings(le.assetList)
}
func (le *LevelEditor) RefreshChunkList() {
le.chunkFiles = []string{}
files, err := ioutil.ReadDir(ChunkDir)
if err != nil {
return
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".json") {
le.chunkFiles = append(le.chunkFiles, f.Name())
}
}
sort.Strings(le.chunkFiles)
}
func (le *LevelEditor) LoadChunk(filename string) {
path := filepath.Join(ChunkDir, filename)
data, err := ioutil.ReadFile(path)
if err == nil {
json.Unmarshal(data, &le.currentChunk)
le.currentChunkFile = filename
le.statusMsg = "Geladen: " + filename
} else {
le.currentChunk.ID = strings.TrimSuffix(filename, filepath.Ext(filename))
le.currentChunk.Width = 50
le.currentChunk.Objects = []game.LevelObject{}
le.currentChunkFile = filename
le.statusMsg = "Neu erstellt: " + le.currentChunk.ID
}
}
func (le *LevelEditor) CreateNewChunk(name string) {
if name == "" {
name = "chunk_new"
}
filename := name + ".json"
le.currentChunk = game.Chunk{
ID: name,
Width: 50,
Objects: []game.LevelObject{},
}
le.currentChunkFile = filename
le.SaveChunk()
le.RefreshChunkList()
le.statusMsg = "Neuer Chunk erstellt: " + filename
}
func (le *LevelEditor) DeleteChunk(filename string) {
path := filepath.Join(ChunkDir, filename)
err := os.Remove(path)
if err == nil {
le.statusMsg = "Gelöscht: " + filename
le.RefreshChunkList()
// Lade ersten verfügbaren Chunk oder erstelle neuen
if len(le.chunkFiles) > 0 {
le.LoadChunk(le.chunkFiles[0])
} else {
le.CreateNewChunk("chunk_new")
}
} else {
le.statusMsg = "Fehler beim Löschen: " + err.Error()
}
}
func (le *LevelEditor) SaveChunk() {
os.MkdirAll(ChunkDir, 0755)
filename := le.currentChunk.ID + ".json"
path := filepath.Join(ChunkDir, filename)
data, _ := json.MarshalIndent(le.currentChunk, "", " ")
ioutil.WriteFile(path, data, 0644)
le.currentChunkFile = filename
le.RefreshChunkList()
le.statusMsg = "GESPEICHERT als " + filename
}
func (le *LevelEditor) ScreenToWorld(mx, my int) (float64, float64) {
screenX := float64(mx - SidebarWidth)
screenX := float64(mx - LeftSidebarWidth)
screenY := float64(my - TopBarHeight)
worldX := (screenX / le.zoom) + le.scrollX
worldY := screenY / le.zoom
@@ -200,6 +268,17 @@ func (le *LevelEditor) Update() error {
le.currentChunk.Width = v
}
}
if le.activeField == "newchunk" {
le.CreateNewChunk(le.inputBuffer)
}
if le.activeField == "mpspeed" {
if v, err := strconv.ParseFloat(le.inputBuffer, 64); err == nil && le.movingPlatformObjIndex != -1 {
if le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform != nil {
le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform.Speed = v
le.statusMsg = fmt.Sprintf("Speed gesetzt: %.0f", v)
}
}
}
le.activeField = ""
} else if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
le.activeField = ""
@@ -214,7 +293,9 @@ func (le *LevelEditor) Update() error {
}
// Hotkeys
if mx > SidebarWidth {
canvasStartX := LeftSidebarWidth
canvasEndX := CanvasWidth - RightSidebarWidth
if mx > canvasStartX && mx < canvasEndX {
_, wy := ebiten.Wheel()
if wy != 0 {
le.zoom += wy * 0.1
@@ -238,6 +319,15 @@ func (le *LevelEditor) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
le.showPlayerRef = !le.showPlayerRef
} // NEU: Toggle Player
if inpututil.IsKeyJustPressed(ebiten.KeyM) {
le.movingPlatformMode = !le.movingPlatformMode
if !le.movingPlatformMode {
le.movingPlatformObjIndex = -1
le.statusMsg = "Moving Platform Mode deaktiviert"
} else {
le.statusMsg = "Moving Platform Mode: Plattform auswählen"
}
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
le.scrollX += 10 / le.zoom
}
@@ -263,8 +353,8 @@ func (le *LevelEditor) Update() error {
return nil
}
// Palette
if mx < SidebarWidth {
// Left Sidebar - Asset Palette
if mx < LeftSidebarWidth {
_, wy := ebiten.Wheel()
le.listScroll -= wy * 20
if le.listScroll < 0 {
@@ -284,23 +374,119 @@ func (le *LevelEditor) Update() error {
return nil
}
// Canvas Logic
// Right Sidebar - Chunk Manager
if mx > CanvasWidth-RightSidebarWidth {
_, wy := ebiten.Wheel()
le.chunkListScroll -= wy * 20
if le.chunkListScroll < 0 {
le.chunkListScroll = 0
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
// "Neuer Chunk" Button (Y: TopBarHeight+30 bis TopBarHeight+60)
if mx >= CanvasWidth-RightSidebarWidth+10 && mx < CanvasWidth-RightSidebarWidth+240 &&
my >= TopBarHeight+30 && my < TopBarHeight+60 {
le.activeField = "newchunk"
le.inputBuffer = ""
return nil
}
// Chunk-Liste (startet bei TopBarHeight+70)
if my >= TopBarHeight+70 {
clickY := float64(my-TopBarHeight-70) + le.chunkListScroll
idx := int(clickY / 30)
if idx >= 0 && idx < len(le.chunkFiles) {
le.LoadChunk(le.chunkFiles[idx])
}
}
}
// Rechtsklick zum Löschen in Chunk-Liste
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) && my >= TopBarHeight+70 {
clickY := float64(my-TopBarHeight-70) + le.chunkListScroll
idx := int(clickY / 30)
if idx >= 0 && idx < len(le.chunkFiles) {
if len(le.chunkFiles) > 1 || le.chunkFiles[idx] != le.currentChunkFile {
le.DeleteChunk(le.chunkFiles[idx])
} else {
le.statusMsg = "Kann einzigen Chunk nicht löschen!"
}
}
}
return nil
}
// Canvas Logic (nur wenn wir wirklich im Canvas-Bereich sind)
if mx < LeftSidebarWidth || mx > CanvasWidth-RightSidebarWidth || my < TopBarHeight {
return nil
}
worldX, worldY := le.ScreenToWorld(mx, my)
// DELETE
// MOVING PLATFORM MODE
if le.movingPlatformMode && inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
// Keine Plattform ausgewählt? → Plattform auswählen
if le.movingPlatformObjIndex == -1 {
for i := len(le.currentChunk.Objects) - 1; i >= 0; i-- {
obj := le.currentChunk.Objects[i]
assetDef, ok := le.assetManifest.Assets[obj.AssetID]
if !ok || assetDef.Type != "platform" {
continue
}
w, h := le.GetAssetSize(obj.AssetID)
if worldX >= obj.X && worldX <= obj.X+w && worldY >= obj.Y && worldY <= obj.Y+h {
le.movingPlatformObjIndex = i
le.movingPlatformSetStart = true
// Wenn noch keine MovingPlatform-Daten → initialisiere
if obj.MovingPlatform == nil {
le.currentChunk.Objects[i].MovingPlatform = &game.MovingPlatformData{
StartX: obj.X,
StartY: obj.Y,
EndX: obj.X + 200, // Default Endpunkt
EndY: obj.Y,
Speed: 100, // Default Speed
}
}
le.statusMsg = "Plattform gewählt - Klicke Start-Punkt"
le.activeField = "mpspeed"
le.inputBuffer = fmt.Sprintf("%.0f", le.currentChunk.Objects[i].MovingPlatform.Speed)
return nil
}
}
} else {
// Plattform ist ausgewählt → setze Start oder End
if le.movingPlatformSetStart {
le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform.StartX = worldX
le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform.StartY = worldY
le.movingPlatformSetStart = false
le.statusMsg = "Start gesetzt - Klicke End-Punkt"
} else {
le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform.EndX = worldX
le.currentChunk.Objects[le.movingPlatformObjIndex].MovingPlatform.EndY = worldY
le.statusMsg = "End gesetzt - Drücke M zum Beenden oder wähle neue Plattform"
le.movingPlatformObjIndex = -1
}
return nil
}
return nil
}
// DELETE mit Rechtsklick
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
for i := len(le.currentChunk.Objects) - 1; i >= 0; i-- {
obj := le.currentChunk.Objects[i]
w, h := le.GetAssetSize(obj.AssetID)
if worldX >= obj.X && worldX <= obj.X+w && worldY >= obj.Y && worldY <= obj.Y+h {
le.currentChunk.Objects = append(le.currentChunk.Objects[:i], le.currentChunk.Objects[i+1:]...)
le.statusMsg = fmt.Sprintf("Objekt gelöscht: %s", obj.AssetID)
return nil
}
}
}
// MOVE
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && !le.isDragging {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && !le.isDragging && !le.movingPlatformMode {
for i := len(le.currentChunk.Objects) - 1; i >= 0; i-- {
obj := le.currentChunk.Objects[i]
w, h := le.GetAssetSize(obj.AssetID)
@@ -352,11 +538,13 @@ func (le *LevelEditor) Update() error {
func (le *LevelEditor) Draw(screen *ebiten.Image) {
// UI HINTERGRUND
vector.DrawFilledRect(screen, 0, 0, CanvasWidth, TopBarHeight, ColBgTop, false)
vector.DrawFilledRect(screen, 0, TopBarHeight, SidebarWidth, CanvasHeight-TopBarHeight, ColBgSidebar, false)
vector.DrawFilledRect(screen, 0, TopBarHeight, LeftSidebarWidth, CanvasHeight-TopBarHeight, ColBgSidebar, false)
vector.DrawFilledRect(screen, CanvasWidth-RightSidebarWidth, TopBarHeight, RightSidebarWidth, CanvasHeight-TopBarHeight, ColBgSidebar, false)
text.Draw(screen, "ID: "+le.currentChunk.ID, basicfont.Face7x13, 75, 25, color.White)
// ASSET LISTE
// LEFT SIDEBAR - ASSET LISTE
text.Draw(screen, "ASSETS", basicfont.Face7x13, 10, TopBarHeight+20, ColHighlight)
startY := float64(TopBarHeight+40) - le.listScroll
for i, id := range le.assetList {
y := startY + float64(i*25)
@@ -370,26 +558,81 @@ func (le *LevelEditor) Draw(screen *ebiten.Image) {
text.Draw(screen, id, basicfont.Face7x13, 10, int(y+15), col)
}
// RIGHT SIDEBAR - CHUNK MANAGER
rightX := CanvasWidth - RightSidebarWidth
text.Draw(screen, "CHUNKS", basicfont.Face7x13, rightX+10, TopBarHeight+20, ColHighlight)
// "Neuer Chunk" Button
btnX := float32(rightX + 10)
btnY := float32(TopBarHeight + 30)
btnW := float32(230)
btnH := float32(30)
vector.DrawFilledRect(screen, btnX, btnY, btnW, btnH, color.RGBA{60, 120, 80, 255}, false)
vector.StrokeRect(screen, btnX, btnY, btnW, btnH, 2, ColHighlight, false)
if le.activeField == "newchunk" {
text.Draw(screen, "Name: "+le.inputBuffer+"_", basicfont.Face7x13, rightX+15, TopBarHeight+50, color.White)
} else {
text.Draw(screen, "[+] Neuer Chunk", basicfont.Face7x13, rightX+65, TopBarHeight+50, color.White)
}
// Chunk-Liste
chunkStartY := float64(TopBarHeight+70) - le.chunkListScroll
for i, filename := range le.chunkFiles {
y := chunkStartY + float64(i*30)
if y < float64(TopBarHeight+70) || y > CanvasHeight {
continue
}
col := ColText
bgCol := color.RGBA{50, 54, 62, 255}
if filename == le.currentChunkFile {
col = color.RGBA{100, 255, 100, 255}
bgCol = color.RGBA{40, 80, 40, 255}
}
// Hintergrund für aktuellen Chunk
vector.DrawFilledRect(screen, float32(rightX+5), float32(y), float32(RightSidebarWidth-10), 28, bgCol, false)
// Dateiname
displayName := strings.TrimSuffix(filename, ".json")
if len(displayName) > 20 {
displayName = displayName[:20] + "..."
}
text.Draw(screen, displayName, basicfont.Face7x13, rightX+10, int(y+18), col)
}
// Hinweis
text.Draw(screen, "L-Click: Load", basicfont.Face7x13, rightX+10, CanvasHeight-40, color.Gray{100})
text.Draw(screen, "R-Click: Delete", basicfont.Face7x13, rightX+10, CanvasHeight-25, color.Gray{100})
// CANVAS
canvasOffX := float64(SidebarWidth)
canvasOffX := float64(LeftSidebarWidth)
canvasOffY := float64(TopBarHeight)
canvasWidth := float32(CanvasWidth - LeftSidebarWidth - RightSidebarWidth)
// Canvas Hintergrund
vector.DrawFilledRect(screen, float32(canvasOffX), float32(canvasOffY), canvasWidth, CanvasHeight-TopBarHeight, ColBgCanvas, false)
// GRID
canvasEndX := float32(CanvasWidth - RightSidebarWidth)
if le.showGrid {
startGridX := int(le.scrollX/TileSize) * TileSize
for x := startGridX; x < startGridX+int(CanvasWidth/le.zoom)+TileSize; x += TileSize {
for x := startGridX; x < startGridX+int(float64(canvasWidth)/le.zoom)+TileSize; x += TileSize {
drawX := float32((float64(x)-le.scrollX)*le.zoom + canvasOffX)
vector.StrokeLine(screen, drawX, float32(canvasOffY), drawX, CanvasHeight, 1, ColGrid, false)
if drawX >= float32(canvasOffX) && drawX <= canvasEndX {
vector.StrokeLine(screen, drawX, float32(canvasOffY), drawX, CanvasHeight, 1, ColGrid, false)
}
}
for y := 0; y < int(CanvasHeight/le.zoom); y += TileSize {
drawY := float32(float64(y)*le.zoom + canvasOffY)
vector.StrokeLine(screen, float32(canvasOffX), drawY, CanvasWidth, drawY, 1, ColGrid, false)
vector.StrokeLine(screen, float32(canvasOffX), drawY, canvasEndX, drawY, 1, ColGrid, false)
}
}
// BODEN LINIE
floorScreenY := float32((RefFloorY * le.zoom) + canvasOffY)
vector.StrokeLine(screen, float32(canvasOffX), floorScreenY, float32(CanvasWidth), floorScreenY, 2, ColFloor, false)
vector.StrokeLine(screen, float32(canvasOffX), floorScreenY, canvasEndX, floorScreenY, 2, ColFloor, false)
// PLAYER REFERENCE (GHOST)
// PLAYER REFERENCE (GHOST)
@@ -422,14 +665,54 @@ func (le *LevelEditor) Draw(screen *ebiten.Image) {
}
// OBJEKTE
for _, obj := range le.currentChunk.Objects {
for i, obj := range le.currentChunk.Objects {
le.DrawAsset(screen, obj.AssetID, obj.X, obj.Y, canvasOffX, canvasOffY, 1.0)
// MOVING PLATFORM MARKER
if obj.MovingPlatform != nil {
mpd := obj.MovingPlatform
// Start-Punkt (grün)
sxStart := float32((mpd.StartX-le.scrollX)*le.zoom + canvasOffX)
syStart := float32(mpd.StartY*le.zoom + canvasOffY)
// End-Punkt (rot)
sxEnd := float32((mpd.EndX-le.scrollX)*le.zoom + canvasOffX)
syEnd := float32(mpd.EndY*le.zoom + canvasOffY)
// Linie zwischen Start und End (gelb gestrichelt)
vector.StrokeLine(screen, sxStart, syStart, sxEnd, syEnd, 2, color.RGBA{255, 255, 0, 200}, false)
// Start-Marker (grüner Kreis)
vector.DrawFilledCircle(screen, sxStart, syStart, 8, color.RGBA{0, 255, 0, 255}, false)
vector.StrokeCircle(screen, sxStart, syStart, 8, 2, color.RGBA{0, 200, 0, 255}, false)
// End-Marker (roter Kreis)
vector.DrawFilledCircle(screen, sxEnd, syEnd, 8, color.RGBA{255, 0, 0, 255}, false)
vector.StrokeCircle(screen, sxEnd, syEnd, 8, 2, color.RGBA{200, 0, 0, 255}, false)
// Speed Label
midX := int((sxStart + sxEnd) / 2)
midY := int((syStart + syEnd) / 2)
speedLabel := fmt.Sprintf("%.0f u/s", mpd.Speed)
text.Draw(screen, speedLabel, basicfont.Face7x13, midX-20, midY-10, color.RGBA{255, 255, 0, 255})
}
// Highlight wenn ausgewählt im Moving Platform Mode
if le.movingPlatformMode && i == le.movingPlatformObjIndex {
w, h := le.GetAssetSize(obj.AssetID)
sX := float32((obj.X-le.scrollX)*le.zoom + canvasOffX)
sY := float32(obj.Y*le.zoom + canvasOffY)
sW := float32(w * le.zoom)
sH := float32(h * le.zoom)
vector.StrokeRect(screen, sX, sY, sW, sH, 3, color.RGBA{255, 255, 0, 255}, false)
}
}
// DRAG GHOST
if le.isDragging && le.dragType == "new" {
mx, my := ebiten.CursorPosition()
if mx > SidebarWidth && my > TopBarHeight {
if mx > LeftSidebarWidth && mx < CanvasWidth-RightSidebarWidth && my > TopBarHeight {
wRawX, wRawY := le.ScreenToWorld(mx, my)
_, h := le.GetAssetSize(le.dragAssetID)
snapX, snapY := le.GetSmartSnap(wRawX, wRawY, h)
@@ -441,15 +724,33 @@ func (le *LevelEditor) Draw(screen *ebiten.Image) {
}
// STATUS
text.Draw(screen, "[S]ave | [G]rid | [H]itbox | [P]layer Ref | R-Click=Del", basicfont.Face7x13, 400, 25, color.Gray{100})
text.Draw(screen, le.statusMsg, basicfont.Face7x13, SidebarWidth+10, CanvasHeight-10, ColHighlight)
statusText := "[S]ave | [G]rid | [H]itbox | [P]layer | [M]oving Platform | R-Click=Del"
text.Draw(screen, statusText, basicfont.Face7x13, 380, 25, color.Gray{100})
// Moving Platform Mode Indicator
if le.movingPlatformMode {
modeText := "🟡 MOVING PLATFORM MODE"
text.Draw(screen, modeText, basicfont.Face7x13, LeftSidebarWidth+10, TopBarHeight+20, color.RGBA{255, 255, 0, 255})
// Speed Input Field wenn Plattform ausgewählt
if le.movingPlatformObjIndex != -1 && le.activeField == "mpspeed" {
speedFieldX := LeftSidebarWidth + 10
speedFieldY := TopBarHeight + 40
fieldText := "Speed: " + le.inputBuffer + "_"
text.Draw(screen, fieldText, basicfont.Face7x13, speedFieldX, speedFieldY, color.RGBA{0, 255, 0, 255})
text.Draw(screen, "[Enter] to confirm", basicfont.Face7x13, speedFieldX, speedFieldY+20, color.Gray{150})
}
}
text.Draw(screen, le.statusMsg, basicfont.Face7x13, LeftSidebarWidth+10, CanvasHeight-10, ColHighlight)
}
func (le *LevelEditor) DrawAsset(screen *ebiten.Image, id string, wX, wY, offX, offY float64, alpha float32) {
sX := (wX-le.scrollX)*le.zoom + offX
sY := wY*le.zoom + offY
if sX < SidebarWidth-100 || sX > CanvasWidth {
// Culling: Nicht zeichnen wenn außerhalb Canvas
if sX < float64(LeftSidebarWidth)-100 || sX > float64(CanvasWidth-RightSidebarWidth)+100 {
return
}