aboutsummaryrefslogtreecommitdiff
path: root/entity.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-09-15 08:10:04 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-09-15 08:10:04 +0200
commit2d2a9fe8520f469897f49dc261289d7cc1c5d26c (patch)
tree79cf40889d6bf3cdaa26c158ec271d40f9692f6a /entity.go
parentf7c05f1b9417eff446753b8fafc7829859521aa3 (diff)
refactor: Into ECS
Diffstat (limited to 'entity.go')
-rw-r--r--entity.go39
1 files changed, 39 insertions, 0 deletions
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)
+ }
+ }
+}