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:
160
cmd/client/ground_system.go
Normal file
160
cmd/client/ground_system.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
// GroundTile repräsentiert ein Boden-Segment
|
||||
type GroundTile struct {
|
||||
X float64
|
||||
DirtVariants []DirtPatch
|
||||
Stones []Stone
|
||||
}
|
||||
|
||||
// DirtPatch ist ein Dirt-Fleck im Boden
|
||||
type DirtPatch struct {
|
||||
OffsetX float64
|
||||
OffsetY float64
|
||||
Width float64
|
||||
Height float64
|
||||
Color color.RGBA
|
||||
}
|
||||
|
||||
// Stone ist ein Stein auf dem Boden
|
||||
type Stone struct {
|
||||
X float64
|
||||
Y float64
|
||||
Size float64
|
||||
Color color.RGBA
|
||||
Shape int // 0=rund, 1=eckig
|
||||
}
|
||||
|
||||
// GroundCache speichert generierte Tiles
|
||||
var groundCache = make(map[int]GroundTile)
|
||||
|
||||
// ClearGroundCache leert den Cache (z.B. bei Änderungen)
|
||||
func ClearGroundCache() {
|
||||
groundCache = make(map[int]GroundTile)
|
||||
}
|
||||
|
||||
// GenerateGroundTile generiert ein prozedurales Boden-Segment (gecacht)
|
||||
func GenerateGroundTile(tileIdx int) GroundTile {
|
||||
// Prüfe Cache
|
||||
if cached, exists := groundCache[tileIdx]; exists {
|
||||
return cached
|
||||
}
|
||||
|
||||
// Deterministischer Seed basierend auf Tile-Index
|
||||
rng := rand.New(rand.NewSource(int64(tileIdx * 12345)))
|
||||
|
||||
tile := GroundTile{
|
||||
X: float64(tileIdx) * 128.0,
|
||||
DirtVariants: make([]DirtPatch, 0),
|
||||
Stones: make([]Stone, 0),
|
||||
}
|
||||
|
||||
// Zufällige Dirt-Patches generieren (15-25 pro Tile, über die ganze Höhe)
|
||||
numDirt := 15 + rng.Intn(10)
|
||||
dirtHeight := float64(ScreenHeight - RefFloorY - 20) // Gesamte Dirt-Höhe
|
||||
for i := 0; i < numDirt; i++ {
|
||||
darkness := uint8(70 + rng.Intn(40)) // Verschiedene Brauntöne
|
||||
tile.DirtVariants = append(tile.DirtVariants, DirtPatch{
|
||||
OffsetX: rng.Float64() * 128,
|
||||
OffsetY: rng.Float64()*dirtHeight + 20, // Über die ganze Dirt-Schicht verteilt
|
||||
Width: 10 + rng.Float64()*30,
|
||||
Height: 10 + rng.Float64()*25,
|
||||
Color: color.RGBA{darkness, darkness - 10, darkness - 20, 255},
|
||||
})
|
||||
}
|
||||
|
||||
// Keine Steine mehr auf dem Gras
|
||||
|
||||
// In Cache speichern
|
||||
groundCache[tileIdx] = tile
|
||||
return tile
|
||||
}
|
||||
|
||||
// RenderGround rendert den Boden mit Bewegung
|
||||
func (g *Game) RenderGround(screen *ebiten.Image, cameraX float64) {
|
||||
floorY := float32(RefFloorY)
|
||||
floorH := float32(ScreenHeight - RefFloorY)
|
||||
|
||||
// 1. Basis Gras-Schicht
|
||||
vector.DrawFilledRect(screen, 0, floorY, float32(ScreenWidth), floorH, ColGrass, false)
|
||||
|
||||
// 2. Dirt-Schicht (Basis)
|
||||
vector.DrawFilledRect(screen, 0, floorY+20, float32(ScreenWidth), floorH-20, ColDirt, false)
|
||||
|
||||
// 3. Prozedurale Dirt-Patches und Steine (bewegen sich mit Kamera)
|
||||
// Berechne welche Tiles sichtbar sind
|
||||
tileWidth := 128.0
|
||||
startTile := int(math.Floor(cameraX / tileWidth))
|
||||
endTile := int(math.Ceil((cameraX + float64(ScreenWidth)) / tileWidth))
|
||||
|
||||
// Tiles rendern
|
||||
for tileIdx := startTile; tileIdx <= endTile; tileIdx++ {
|
||||
tile := GenerateGroundTile(tileIdx)
|
||||
|
||||
// Dirt-Patches rendern
|
||||
for _, dirt := range tile.DirtVariants {
|
||||
worldX := tile.X + dirt.OffsetX
|
||||
screenX := float32(worldX - cameraX)
|
||||
screenY := float32(RefFloorY) + float32(dirt.OffsetY)
|
||||
|
||||
// Nur rendern wenn im sichtbaren Bereich
|
||||
if screenX+float32(dirt.Width) > 0 && screenX < float32(ScreenWidth) {
|
||||
vector.DrawFilledRect(screen, screenX, screenY, float32(dirt.Width), float32(dirt.Height), dirt.Color, false)
|
||||
}
|
||||
}
|
||||
|
||||
// Steine rendern (auf dem Gras)
|
||||
for _, stone := range tile.Stones {
|
||||
worldX := tile.X + stone.X
|
||||
screenX := float32(worldX - cameraX)
|
||||
screenY := float32(RefFloorY) + float32(stone.Y)
|
||||
|
||||
// Nur rendern wenn im sichtbaren Bereich
|
||||
if screenX > -20 && screenX < float32(ScreenWidth)+20 {
|
||||
if stone.Shape == 0 {
|
||||
// Runder Stein
|
||||
vector.DrawFilledCircle(screen, screenX, screenY, float32(stone.Size/2), stone.Color, false)
|
||||
// Highlight für 3D-Effekt
|
||||
highlightCol := color.RGBA{
|
||||
uint8(math.Min(float64(stone.Color.R)+40, 255)),
|
||||
uint8(math.Min(float64(stone.Color.G)+40, 255)),
|
||||
uint8(math.Min(float64(stone.Color.B)+40, 255)),
|
||||
200,
|
||||
}
|
||||
vector.DrawFilledCircle(screen, screenX-float32(stone.Size*0.15), screenY-float32(stone.Size*0.15), float32(stone.Size/4), highlightCol, false)
|
||||
} else {
|
||||
// Eckiger Stein
|
||||
vector.DrawFilledRect(screen, screenX-float32(stone.Size/2), screenY-float32(stone.Size/2), float32(stone.Size), float32(stone.Size), stone.Color, false)
|
||||
// Schatten für 3D-Effekt
|
||||
shadowCol := color.RGBA{
|
||||
uint8(float64(stone.Color.R) * 0.6),
|
||||
uint8(float64(stone.Color.G) * 0.6),
|
||||
uint8(float64(stone.Color.B) * 0.6),
|
||||
150,
|
||||
}
|
||||
vector.DrawFilledRect(screen, screenX-float32(stone.Size/2)+2, screenY-float32(stone.Size/2)+2, float32(stone.Size), float32(stone.Size), shadowCol, false)
|
||||
// Original drüber
|
||||
vector.DrawFilledRect(screen, screenX-float32(stone.Size/2), screenY-float32(stone.Size/2), float32(stone.Size), float32(stone.Size), stone.Color, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cache aufräumen (nur Tiles außerhalb des Sichtbereichs entfernen)
|
||||
if len(groundCache) > 100 {
|
||||
for idx := range groundCache {
|
||||
if idx < startTile-10 || idx > endTile+10 {
|
||||
delete(groundCache, idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user