From 2d2a9fe8520f469897f49dc261289d7cc1c5d26c Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Fri, 15 Sep 2023 08:10:04 +0200 Subject: refactor: Into ECS --- entity.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 entity.go (limited to 'entity.go') diff --git a/entity.go b/entity.go new file mode 100644 index 0000000..6066772 --- /dev/null +++ b/entity.go @@ -0,0 +1,39 @@ +package main + +import "github.com/hajimehoshi/ebiten/v2" + +type Component[T any] struct { + IsEnabled bool + Value T +} + +func C[T any](t T) Component[T] { + return Component[T]{IsEnabled: true, Value: t} +} + +type Entity struct { + Position Component[Point] + Image Component[*ebiten.Image] + Selection Component[Selection] + Move Component[Move] +} + +func (e Entity) Bounds() Rectangle { + return Rectangle{ + e.Position.Value, + e.Position.Value.Add(e.Image.Value.Bounds().Size()), + } +} + +func Draw(screen *ebiten.Image, e *Entity) { + if e.Image.IsEnabled && e.Position.IsEnabled { + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(e.Position.Value.X), float64(e.Position.Value.Y)) + if e.Selection.IsEnabled && e.Selection.Value.IsSelected { + op.GeoM.Translate(-10, -10) + screen.DrawImage(e.Selection.Value.SelectedImage, op) + } else { + screen.DrawImage(e.Image.Value, op) + } + } +} -- cgit v1.3