Private
Public Access
1
0
Files
EscapeFromTeacher/cmd/levelbuilder/main.go

823 lines
25 KiB
Go

package main
import (
"encoding/json"
"fmt"
"image/color"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"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"
"golang.org/x/image/font/basicfont"
"git.zb-server.de/ZB-Server/EscapeFromTeacher/pkg/game"
)
// --- CONFIG ---
const (
AssetFile = "./cmd/client/assets/assets.json"
ChunkDir = "./cmd/client/assets/chunks"
LeftSidebarWidth = 250
RightSidebarWidth = 250
TopBarHeight = 40
CanvasHeight = 720
CanvasWidth = 1280
TileSize = 64
RefFloorY = 540
)
var (
ColBgSidebar = color.RGBA{40, 44, 52, 255}
ColBgTop = color.RGBA{35, 35, 40, 255}
ColBgCanvas = color.RGBA{30, 30, 30, 255}
ColGrid = color.RGBA{60, 60, 60, 255}
ColFloor = color.RGBA{0, 255, 0, 150}
ColText = color.RGBA{220, 220, 220, 255}
ColHighlight = color.RGBA{80, 120, 200, 255}
ColHitbox = color.RGBA{255, 0, 0, 200}
ColHitboxFill = color.RGBA{255, 0, 0, 50}
ColOrigin = color.RGBA{255, 255, 0, 255} // Gelb für Referenzpunkt
)
type LevelEditor struct {
assetManifest game.AssetManifest
assetList []string
assetsImages map[string]*ebiten.Image
currentChunk game.Chunk
currentChunkFile string // Aktuell geladene Datei
chunkFiles []string // Liste aller Chunk-Dateien
scrollX float64
zoom float64
listScroll float64
chunkListScroll float64 // Scroll für Chunk-Liste
statusMsg string
showGrid bool
enableSnap bool
showHitbox bool
showPlayerRef bool // NEU: Spieler Ghost anzeigen
activeField string
inputBuffer string
isDragging bool
dragType string
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
movingPlatformObjIndex: -1,
}
le.LoadAssets()
le.RefreshChunkList()
if len(le.chunkFiles) > 0 {
le.LoadChunk(le.chunkFiles[0])
} else {
le.currentChunkFile = "chunk_new.json"
}
return le
}
func (le *LevelEditor) LoadAssets() {
data, err := ioutil.ReadFile(AssetFile)
if err != nil {
fmt.Println("⚠️ Konnte Assets nicht laden:", AssetFile)
le.assetManifest = game.AssetManifest{Assets: make(map[string]game.AssetDefinition)}
} else {
json.Unmarshal(data, &le.assetManifest)
}
baseDir := "./cmd/client/assets"
le.assetList = []string{}
for id, def := range le.assetManifest.Assets {
le.assetList = append(le.assetList, id)
if def.Filename != "" {
fullPath := filepath.Join(baseDir, def.Filename)
img, _, err := ebitenutil.NewImageFromFile(fullPath)
if err == nil {
le.assetsImages[id] = img
}
}
}
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 - LeftSidebarWidth)
screenY := float64(my - TopBarHeight)
worldX := (screenX / le.zoom) + le.scrollX
worldY := screenY / le.zoom
return worldX, worldY
}
func (le *LevelEditor) GetSmartSnap(x, y float64, objHeight float64) (float64, float64) {
shouldSnap := le.enableSnap
if ebiten.IsKeyPressed(ebiten.KeyShift) {
shouldSnap = !shouldSnap
}
if shouldSnap {
sx := math.Floor(x/TileSize) * TileSize
// Y Smart Snap: Unterkante ans Raster
gridLine := math.Round(y/TileSize) * TileSize
sy := gridLine - objHeight
return sx, sy
}
return x, y
}
func (le *LevelEditor) GetAssetSize(id string) (float64, float64) {
def, ok := le.assetManifest.Assets[id]
if !ok {
return 64, 64
}
w := def.Hitbox.W
h := def.Hitbox.H
if w == 0 {
w = def.ProcWidth
}
if h == 0 {
h = def.ProcHeight
}
if w == 0 {
w = 64
}
if h == 0 {
h = 64
}
return w, h
}
func (le *LevelEditor) Update() error {
mx, my := ebiten.CursorPosition()
// Text Input
if le.activeField != "" {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
if le.activeField == "id" {
le.currentChunk.ID = le.inputBuffer
}
if le.activeField == "width" {
if v, err := strconv.Atoi(le.inputBuffer); err == nil {
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 = ""
} else if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
if len(le.inputBuffer) > 0 {
le.inputBuffer = le.inputBuffer[:len(le.inputBuffer)-1]
}
} else {
le.inputBuffer += string(ebiten.InputChars())
}
return nil
}
// Hotkeys
canvasStartX := LeftSidebarWidth
canvasEndX := CanvasWidth - RightSidebarWidth
if mx > canvasStartX && mx < canvasEndX {
_, wy := ebiten.Wheel()
if wy != 0 {
le.zoom += wy * 0.1
if le.zoom < 0.2 {
le.zoom = 0.2
}
if le.zoom > 3.0 {
le.zoom = 3.0
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
le.SaveChunk()
}
if inpututil.IsKeyJustPressed(ebiten.KeyG) {
le.showGrid = !le.showGrid
}
if inpututil.IsKeyJustPressed(ebiten.KeyH) {
le.showHitbox = !le.showHitbox
}
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
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
le.scrollX -= 10 / le.zoom
if le.scrollX < 0 {
le.scrollX = 0
}
}
// UI
if my < TopBarHeight {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
if mx >= 70 && mx < 220 {
le.activeField = "id"
le.inputBuffer = le.currentChunk.ID
}
if mx >= 300 && mx < 360 {
le.activeField = "width"
le.inputBuffer = fmt.Sprintf("%d", le.currentChunk.Width)
}
}
return nil
}
// Left Sidebar - Asset Palette
if mx < LeftSidebarWidth {
_, wy := ebiten.Wheel()
le.listScroll -= wy * 20
if le.listScroll < 0 {
le.listScroll = 0
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
clickY := float64(my-TopBarHeight) + le.listScroll - 40.0
if clickY >= 0 {
idx := int(clickY / 25)
if idx >= 0 && idx < len(le.assetList) {
le.isDragging = true
le.dragType = "new"
le.dragAssetID = le.assetList[idx]
}
}
}
return nil
}
// 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)
// 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 && !le.movingPlatformMode {
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.isDragging = true
le.dragType = "move"
le.dragTargetIndex = i
le.dragAssetID = obj.AssetID
le.dragOffset = game.Vec2{X: worldX - obj.X, Y: worldY - obj.Y}
return nil
}
}
}
// DRAGGING
if le.isDragging {
rawWX, rawWY := le.ScreenToWorld(mx, my)
_, h := le.GetAssetSize(le.dragAssetID)
if le.dragType == "move" {
targetX := rawWX - le.dragOffset.X
targetY := rawWY - le.dragOffset.Y
snapX, snapY := le.GetSmartSnap(targetX, targetY+h, h)
if !le.enableSnap {
snapX = targetX
snapY = targetY
}
if le.dragTargetIndex < len(le.currentChunk.Objects) {
le.currentChunk.Objects[le.dragTargetIndex].X = snapX
le.currentChunk.Objects[le.dragTargetIndex].Y = snapY
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
if le.dragType == "new" {
finalX, finalY := le.GetSmartSnap(rawWX, rawWY, h)
newObj := game.LevelObject{AssetID: le.dragAssetID, X: finalX, Y: finalY}
le.currentChunk.Objects = append(le.currentChunk.Objects, newObj)
}
le.isDragging = false
le.dragType = ""
}
}
return nil
}
func (le *LevelEditor) Draw(screen *ebiten.Image) {
// UI HINTERGRUND
vector.DrawFilledRect(screen, 0, 0, CanvasWidth, TopBarHeight, ColBgTop, 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)
// 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)
if y < float64(TopBarHeight) || y > CanvasHeight {
continue
}
col := ColText
if le.isDragging && le.dragType == "new" && le.dragAssetID == id {
col = ColHighlight
}
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(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(float64(canvasWidth)/le.zoom)+TileSize; x += TileSize {
drawX := float32((float64(x)-le.scrollX)*le.zoom + canvasOffX)
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, canvasEndX, drawY, 1, ColGrid, false)
}
}
// BODEN LINIE
floorScreenY := float32((RefFloorY * le.zoom) + canvasOffY)
vector.StrokeLine(screen, float32(canvasOffX), floorScreenY, canvasEndX, floorScreenY, 2, ColFloor, false)
// PLAYER REFERENCE (GHOST)
// PLAYER REFERENCE (GHOST)
if le.showPlayerRef {
playerDef, ok := le.assetManifest.Assets["player"]
if ok {
// 1. Richtige Y-Position berechnen, damit Füße auf dem Boden stehen
// Formel: Boden - HitboxHöhe - Alle Offsets
// Weil: (Pos + DrawOff + HitboxOff) + HitboxH = Boden
pH := playerDef.Hitbox.H
if pH == 0 {
pH = 64
} // Fallback
// Hier ist die korrigierte Mathe:
pY := float64(RefFloorY) - pH - playerDef.Hitbox.OffsetY - playerDef.DrawOffY
// Zeichne Referenz-Spieler bei Welt-X = ScrollX + 200
refWorldX := le.scrollX + 200
// Wir übergeben pY direkt. DrawAsset addiert dann wieder DrawOffY dazu.
// Dadurch gleicht es sich aus und die Hitbox landet exakt auf dem Boden.
le.DrawAsset(screen, "player", refWorldX, pY, canvasOffX, canvasOffY, 0.5)
// Label
sX := (refWorldX-le.scrollX)*le.zoom + canvasOffX
text.Draw(screen, "REF PLAYER", basicfont.Face7x13, int(sX), int(floorScreenY)+20, ColHighlight)
}
}
// OBJEKTE
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 > LeftSidebarWidth && mx < CanvasWidth-RightSidebarWidth && my > TopBarHeight {
wRawX, wRawY := le.ScreenToWorld(mx, my)
_, h := le.GetAssetSize(le.dragAssetID)
snapX, snapY := le.GetSmartSnap(wRawX, wRawY, h)
le.DrawAsset(screen, le.dragAssetID, snapX, snapY, canvasOffX, canvasOffY, 0.6)
txt := fmt.Sprintf("Y: %.0f", snapY)
text.Draw(screen, txt, basicfont.Face7x13, mx+10, my, ColHighlight)
}
}
// STATUS
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
// Culling: Nicht zeichnen wenn außerhalb Canvas
if sX < float64(LeftSidebarWidth)-100 || sX > float64(CanvasWidth-RightSidebarWidth)+100 {
return
}
def, ok := le.assetManifest.Assets[id]
if !ok {
return
}
// 1. SPRITE
if def.Filename != "" {
img := le.assetsImages[id]
if img != nil {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(def.Scale*le.zoom, def.Scale*le.zoom)
drawX := sX + (def.DrawOffX * le.zoom)
drawY := sY + (def.DrawOffY * le.zoom)
op.GeoM.Translate(drawX, drawY)
op.ColorScale.ScaleAlpha(alpha)
screen.DrawImage(img, op)
}
} else {
col := def.Color.ToRGBA()
col.A = uint8(float32(col.A) * alpha)
w := float32((def.ProcWidth) * le.zoom)
h := float32((def.ProcHeight) * le.zoom)
if w == 0 {
w = 64 * float32(le.zoom)
h = 64 * float32(le.zoom)
}
vector.DrawFilledRect(screen, float32(sX), float32(sY), w, h, col, false)
}
// 2. HITBOX (Rot)
if le.showHitbox {
hx := float32(sX + (def.DrawOffX * le.zoom) + (def.Hitbox.OffsetX * le.zoom))
hy := float32(sY + (def.DrawOffY * le.zoom) + (def.Hitbox.OffsetY * le.zoom))
hw := float32(def.Hitbox.W * le.zoom)
hh := float32(def.Hitbox.H * le.zoom)
if hw == 0 {
if def.ProcWidth > 0 {
hw = float32(def.ProcWidth * le.zoom)
hh = float32(def.ProcHeight * le.zoom)
} else {
hw = 64 * float32(le.zoom)
hh = 64 * float32(le.zoom)
}
}
vector.StrokeRect(screen, hx, hy, hw, hh, 2, ColHitbox, false)
vector.DrawFilledRect(screen, hx, hy, hw, hh, ColHitboxFill, false)
}
// 3. REFERENZPUNKT (Gelbes Kreuz) <-- DAS WOLLTEST DU!
// Zeigt exakt die Koordinate (X, Y) des Objekts
cX := float32(sX)
cY := float32(sY)
vector.StrokeLine(screen, cX-5, cY, cX+5, cY, 2, ColOrigin, false)
vector.StrokeLine(screen, cX, cY-5, cX, cY+5, 2, ColOrigin, false)
}
func (le *LevelEditor) Layout(w, h int) (int, int) { return CanvasWidth, CanvasHeight }
func main() {
os.MkdirAll(ChunkDir, 0755)
ebiten.SetWindowSize(CanvasWidth, CanvasHeight)
ebiten.SetWindowTitle("Escape Level Editor - Reference Point Added")
if err := ebiten.RunGame(NewLevelEditor()); err != nil {
log.Fatal(err)
}
}