aboutsummaryrefslogtreecommitdiff
path: root/draw.go
blob: 4f97cb830c247dfa1a2630cd8f2ee98006070bc8 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

package main

import (
	"fmt"
	"image/color"

	"age_of_empires/ecs"
	"age_of_empires/physics"
	"age_of_empires/selection"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text/v2"
	"github.com/hajimehoshi/ebiten/v2/vector"
)

func (g *Game) Draw(screen *ebiten.Image) {
	x, y := ebiten.CursorPosition()
	cursor := physics.Point{X: x, Y: y}
	screen.Fill(soilColor)

	required := ecs.CM_Image | ecs.CM_Position
	for i, mask := range g.Mask {
		if mask&required == required {
			position := g.Position[i]
			image := g.Image[i]
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Translate(float64(position.X), float64(position.Y))
			screen.DrawImage(image, op)
			if mask&ecs.CM_Selection == ecs.CM_Selection {
				selectable := g.Selection[i]
				if selectable.IsSelected {
					opt := &ebiten.DrawImageOptions{}
					opt.GeoM.Translate(float64(position.X-selection.HaloWidth/2), float64(position.Y-selection.HaloWidth/2))
					screen.DrawImage(selectable.Halo, opt)
					if mask&ecs.CM_ResourceSource == ecs.CM_ResourceSource {
						source := g.ResourceSource[i]
						resourceText := fmt.Sprintf("%d", source.Remaining)
						op := &text.DrawOptions{}
						op.GeoM.Translate(float64(position.X+5), float64(position.Y+30))
						op.ColorScale.ScaleWithColor(color.White)
						text.Draw(screen, resourceText, &text.GoTextFace{
							Source: g.FaceSource,
							Size:   40,
						}, op)
					}
					if mask&ecs.CM_Spawn == ecs.CM_Spawn {
						spawn := g.Spawn[i]
						spawnText := fmt.Sprintf("%d", len(spawn.Requests))
						op := &text.DrawOptions{}
						op.GeoM.Translate(float64(position.X+5), float64(position.Y+70))
						op.ColorScale.ScaleWithColor(color.White)
						text.Draw(screen, spawnText, &text.GoTextFace{
							Source: g.FaceSource,
							Size:   40,
						}, op)
						if spawn.SpawnTarget.Ok {
							spawnTarget := spawn.SpawnTarget.Value
							vector.DrawFilledCircle(screen, float32(spawnTarget.X+50), float32(spawnTarget.Y+50), 30, Red, true)
						}
					}
					if mask&ecs.CM_Move == ecs.CM_Move {
						move := g.Move[i]
						dx := +image.Bounds().Dx() / 2
						dy := +image.Bounds().Dy() / 2
						last := position
						for _, point := range move.Path {
							vector.StrokeLine(screen, float32(last.X+dx), float32(last.Y+dy), float32(point.X+dx), float32(point.Y+dy), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
							last = point
						}
					}
				}
			}
		}
	}
	if g.GlobalSelection.IsActive(cursor) {
		g.GlobalSelection.Draw(screen, cursor)
	}

	drawTopBanner(screen, g)

	if len(g.GlobalSelection.Selected) > 0 {
		drawBottomBanner(screen, g)
	}
}

func drawTopBanner(screen *ebiten.Image, g *Game) {
	screenBounds := screen.Bounds()
	bannerHeight := float32(200)
	vector.DrawFilledRect(screen, 0, 0, float32(screenBounds.Dx()), bannerHeight, color.White, true)
	resourceText := fmt.Sprintf("Resources: %d", g.ResourceAmount)
	op := &text.DrawOptions{}
	op.GeoM.Translate(25, 25)
	op.ColorScale.ScaleWithColor(color.Black)
	text.Draw(screen, resourceText, &text.GoTextFace{
		Source: g.FaceSource,
		Size:   100,
	}, op)
}

func drawBottomBanner(screen *ebiten.Image, g *Game) {
	screenBounds := screen.Bounds()
	bannerHeight := float32(200)
	bannerTop := screenBounds.Dy() - int(bannerHeight)
	vector.DrawFilledRect(screen, 0, float32(bannerTop), float32(screenBounds.Dx()), bannerHeight, color.White, true)
	selectedText := "Selection"
	op := &text.DrawOptions{}
	op.GeoM.Translate(25, float64(float32(bannerTop)+25))
	op.ColorScale.ScaleWithColor(color.Black)
	text.Draw(screen, selectedText, &text.GoTextFace{
		Source: g.FaceSource,
		Size:   100,
	}, op)
}