Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/animations_ecs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ type Game struct {
}

func (g *Game) Draw(screen *ebiten.Image) {
systems.RenderSystem(g.world.World, screen)
systems.RenderSystem(g.world.World, nil, false, g.rotation)
}

// func (g *Game) DrawBuildingInfo(screen *ebiten.Image) {
Expand Down
103 changes: 34 additions & 69 deletions cmd/island_ecs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ package cmd

import (
"fmt"
"image/color"
"log"
"os"
"path/filepath"
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/siredmar/mdcii-engine/pkg/bsh"
"github.com/siredmar/mdcii-engine/pkg/cod"
buildingsCod "github.com/siredmar/mdcii-engine/pkg/cod/buildings"
Expand All @@ -33,11 +30,12 @@ import (
"github.com/siredmar/mdcii-engine/pkg/ecs/world"
"github.com/siredmar/mdcii-engine/pkg/files"
"github.com/siredmar/mdcii-engine/pkg/gam"
r3d "github.com/siredmar/mdcii-engine/pkg/renderer/raylib"
animations "github.com/siredmar/mdcii-engine/pkg/texture/animations"
"github.com/siredmar/mdcii-engine/pkg/texture/atlas"
"github.com/siredmar/mdcii-engine/pkg/world/rotation"
"github.com/siredmar/mdcii-engine/pkg/world/zoom"
"github.com/spf13/cobra"
"golang.org/x/image/font/basicfont"

donburi "github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
Expand Down Expand Up @@ -116,6 +114,12 @@ var rootCmd = &cobra.Command{
fmt.Println("Error:", err)
return
}

rl.InitWindow(int32(ScreenWidth), int32(ScreenHeight), "animations")
rl.SetTargetFPS(60)
renderer := r3d.NewRenderer(float32(zoom.TileSize()))
defer rl.CloseWindow()

ani, err := animations.New(atlas)
if err != nil {
fmt.Println("Error:", err)
Expand All @@ -140,9 +144,6 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}

ebiten.SetWindowSize(ScreenWidth, ScreenHeight)
ebiten.SetWindowTitle("animations")

w := world.New()
// Create an entity and get its Entry
w.World.Create(components.AnimationType, components.TileType, components.PositionType, components.BuildingType, components.IslandType)
Expand Down Expand Up @@ -178,16 +179,13 @@ var rootCmd = &cobra.Command{
// }

game := &Game{
world: w,
animations: ani,
// animation: nil,
world: w,
animations: ani,
renderer: renderer,
buildingIndex: buildingIndex,
// buildingId: id,
// count: 0,
buildings: buildings,
rotation: rotation.Rotation(rotationArg),
// entry: entry,
grid: true,
buildings: buildings,
rotation: rotation.Rotation(rotationArg),
grid: true,
}

// components.BuildingType.Set(entry, &components.Building{
Expand Down Expand Up @@ -219,8 +217,9 @@ var rootCmd = &cobra.Command{
// return
// }
// game.animation = game.animations.GetAnimation(buildingParam, rotation.DEG0)
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
for !rl.WindowShouldClose() {
game.Update()
game.Draw()
}
},
}
Expand All @@ -234,21 +233,16 @@ func Execute() {
}

type Game struct {
world *world.World
animations *animations.Animations
ScreenWidth int
ScreenHeight int
// lastKeyPressTime time.Time
// buildingId int
world *world.World
animations *animations.Animations
renderer *r3d.Renderer
buildingIndex int
rotation rotation.Rotation
// lastTime time.Time
// entry *donburi.Entry
buildings *buildingsCod.Buildings
grid bool
buildings *buildingsCod.Buildings
grid bool
}

func (g *Game) Draw(screen *ebiten.Image) {
func (g *Game) Draw() {
var rot rotation.Rotation
var grid bool

Expand All @@ -258,9 +252,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
rot = ctrl.Rotation
grid = ctrl.GridVisible
})
systems.RenderSystem(g.world.World, screen, grid, rot)
// systems.MouseSelectorSystem(g.world.World) // Add the mouse selector system
// systems.RenderSystemAscii(g.world.World)

rl.BeginDrawing()
rl.ClearBackground(rl.Black)
systems.RenderSystem(g.world.World, g.renderer, grid, rot)
g.DrawUsage()
rl.EndDrawing()
// systems.MouseSelectorSystem(g.world.World)
}

// func (g *Game) DrawBuildingInfo(screen *ebiten.Image) {
Expand All @@ -278,44 +276,11 @@ func (g *Game) Draw(screen *ebiten.Image) {
// // text.Draw(screen, fmt.Sprintf("CurrentAnimationStep: %d", g.Building.CurrentAnimationStep), face, 10, 80, textColor)
// }

func (g *Game) DrawUsage(screen *ebiten.Image) {
textColor := color.RGBA{255, 255, 255, 255}
face := basicfont.Face7x13

text.Draw(screen, "Up: Animation Step, Left/Right: Rotate, N: next, M: previous", face, 10, ScreenHeight-20, textColor)

func (g *Game) DrawUsage() {
rl.DrawText("Up: Animation Step, Left/Right: Rotate, N: next, M: previous", 10, int32(ScreenHeight-20), 10, rl.White)
}

func (g *Game) Update() error {
func (g *Game) Update() {
systems.AnimationSystem(g.world.World, g.animations, 1.0/60.0)
systems.InputSystem(g.world.World)
return nil
// const debounceDuration = time.Millisecond * 250
// now := time.Now()

// if now.Sub(g.lastKeyPressTime) >= debounceDuration {
// if ebiten.IsKeyPressed(ebiten.KeyLeft) {
// fmt.Println(int(g.rotation))
// g.rotation.Increment()
// fmt.Println(int(g.rotation))
// g.lastKeyPressTime = now

// } else if ebiten.IsKeyPressed(ebiten.KeyRight) {
// fmt.Println(int(g.rotation))
// g.rotation.Decrement()
// fmt.Println(int(g.rotation))
// g.lastKeyPressTime = now
// } else if ebiten.IsKeyPressed(ebiten.KeyG) {
// g.grid = !g.grid
// g.lastKeyPressTime = now
// } else if ebiten.IsKeyPressed(ebiten.KeyEscape) {
// os.Exit(0)
// }
// }
// g.lastTime = now
// return nil
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return ScreenWidth, ScreenHeight
}
3 changes: 2 additions & 1 deletion pkg/ecs/components/island.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

rl "github.com/gen2brain/raylib-go/raylib"
"github.com/siredmar/mdcii-engine/pkg/building"
island5 "github.com/siredmar/mdcii-engine/pkg/chunks"
"github.com/siredmar/mdcii-engine/pkg/cod/buildings"
Expand Down Expand Up @@ -248,7 +249,7 @@ func CreateIslandFromChunk(world donburi.World, ani *animations.Animations, i *i
PositionType.Set(occupyEntry, p)
TileType.Set(occupyEntry, &Tile{
Size: Size{Width: 1, Height: 1, Z: size.H - zoom.TileHeight()},
Image: nil,
Image: rl.Texture2D{},
Occupation: true,
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/ecs/components/tile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package components

import (
"github.com/hajimehoshi/ebiten/v2"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/yohamta/donburi"
)

Expand All @@ -12,7 +12,7 @@ type Size struct {
}

type Tile struct {
Image *ebiten.Image // Placeholder for current frame
Image rl.Texture2D // Placeholder for current frame
Size Size
Occupation bool
// Parent *donburi.Entry
Expand Down
31 changes: 17 additions & 14 deletions pkg/ecs/systems/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"os"
"time"

"github.com/hajimehoshi/ebiten/v2"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/siredmar/mdcii-engine/pkg/ecs/components"
"github.com/siredmar/mdcii-engine/pkg/world/zoom"
"github.com/yohamta/donburi"
"github.com/yohamta/donburi/filter"
)
Expand All @@ -16,37 +17,39 @@ var cameraQuery = donburi.NewQuery(filter.Contains(components.CameraType))

func InputSystem(world donburi.World) {
const debounce = time.Millisecond * 250
const cameraSpeed = 10.0
const cameraSpeed = 0.1

// 🔁 Handle control input (rotation, toggles, etc.)
inputQuery.Each(world, func(entry *donburi.Entry) {
ctrl := components.ControlType.Get(entry)
now := time.Now()

if now.Sub(ctrl.LastKeyPressTime) >= debounce {
if ebiten.IsKeyPressed(ebiten.KeyQ) {
if rl.IsKeyDown(rl.KeyQ) {
ctrl.Rotation.Increment()
fmt.Println("Rotation Incremented:", ctrl.Rotation)
ctrl.LastKeyPressTime = now
}
if ebiten.IsKeyPressed(ebiten.KeyE) {
if rl.IsKeyDown(rl.KeyE) {
ctrl.Rotation.Decrement()
fmt.Println("Rotation Decremented:", ctrl.Rotation)
ctrl.LastKeyPressTime = now
}
if ebiten.IsKeyPressed(ebiten.KeyG) {
if rl.IsKeyDown(rl.KeyG) {
ctrl.GridVisible = !ctrl.GridVisible
ctrl.LastKeyPressTime = now
}
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
if rl.IsKeyDown(rl.KeyEscape) {
os.Exit(0)
}
}

// 🖱️ Right mouse drag for scrolling
mouseX, mouseY := ebiten.CursorPosition()
mouse := rl.GetMousePosition()
mouseX := int(mouse.X)
mouseY := int(mouse.Y)

if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
if rl.IsMouseButtonDown(rl.MouseButtonRight) {
if !ctrl.Dragging {
// Start drag
ctrl.Dragging = true
Expand All @@ -59,8 +62,8 @@ func InputSystem(world donburi.World) {

cameraQuery.Each(world, func(camEntry *donburi.Entry) {
cam := components.CameraType.Get(camEntry)
cam.X -= dx
cam.Y -= dy
cam.X -= dx / float64(zoom.TileSize())
cam.Y -= dy / float64(zoom.TileSize())
})

// Update last position
Expand All @@ -76,16 +79,16 @@ func InputSystem(world donburi.World) {
cameraQuery.Each(world, func(entry *donburi.Entry) {
cam := components.CameraType.Get(entry)

if ebiten.IsKeyPressed(ebiten.KeyW) || ebiten.IsKeyPressed(ebiten.KeyArrowUp) {
if rl.IsKeyDown(rl.KeyW) || rl.IsKeyDown(rl.KeyUp) {
cam.Y -= cameraSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyS) || ebiten.IsKeyPressed(ebiten.KeyArrowDown) {
if rl.IsKeyDown(rl.KeyS) || rl.IsKeyDown(rl.KeyDown) {
cam.Y += cameraSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
if rl.IsKeyDown(rl.KeyA) || rl.IsKeyDown(rl.KeyLeft) {
cam.X -= cameraSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
if rl.IsKeyDown(rl.KeyD) || rl.IsKeyDown(rl.KeyRight) {
cam.X += cameraSpeed
}
})
Expand Down
5 changes: 3 additions & 2 deletions pkg/ecs/systems/mouseSelector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package systems
import (
"fmt"

"github.com/hajimehoshi/ebiten/v2"
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/siredmar/mdcii-engine/pkg/ecs/components"
"github.com/siredmar/mdcii-engine/pkg/world/zoom"
"github.com/yohamta/donburi"
Expand All @@ -21,7 +21,8 @@ func MouseSelectorSystem(world donburi.World) {
tileHeight := zoom.TileHeight() // Tile height in pixels

// Get the mouse position
mouseX, mouseY := ebiten.CursorPosition()
mouse := rl.GetMousePosition()
mouseX, mouseY := int(mouse.X), int(mouse.Y)

// Reverse isometric projection to get grid coordinates
gridX, gridY := getMouseTilePosition(mouseX, mouseY, tileWidth, tileHeight)
Expand Down
Loading