aboutsummaryrefslogtreecommitdiff
path: root/selection.go
blob: ef270c6c3e3cba98939a95f7849c23590f4bb9c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

package main

import (
	"age_of_empires/physics"

	"github.com/hajimehoshi/ebiten/v2"
	"golang.org/x/exp/slog"
)

const SELECTION_HALO_WIDTH = 10

type Selection struct {
	IsSelected bool
	Halo       *ebiten.Image
}

func (e *Entity) SelectMultiple(cursor physics.Point, selection GlobalSelection) {
	if !e.Selection.IsEnabled {
		return
	}
	e.Selection.Value.IsSelected = false
	selectionBounds := physics.Rectangle{Min: selection.Start, Max: cursor}.Canon()
	if selectionBounds.Overlaps(e.Bounds()) {
		e.Selection.Value.IsSelected = true
		slog.Info("entity selected", slog.String("position", e.Position.Value.String()))
	}
}

func (e *Entity) SelectSingle(cursor physics.Point, canBeSelected bool) bool {
	if !e.Selection.IsEnabled {
		return false
	}
	e.Selection.Value.IsSelected = false
	if !canBeSelected || !cursor.In(e.Bounds()) {
		return false
	}
	e.Selection.Value.IsSelected = true
	slog.Info("entity selected", slog.String("position", e.Position.Value.String()))
	return true
}

func DrawSelection(screen *ebiten.Image, e *Entity) {
	if !e.Image.IsEnabled || !e.Position.IsEnabled || !e.Selection.IsEnabled {
		return
	}
	if e.Selection.Value.IsSelected {
		opt := &ebiten.DrawImageOptions{}
		opt.GeoM.Translate(float64(e.Position.Value.X-SELECTION_HALO_WIDTH/2), float64(e.Position.Value.Y-SELECTION_HALO_WIDTH/2))
		screen.DrawImage(e.Selection.Value.Halo, opt)
	}
}