aboutsummaryrefslogtreecommitdiff
path: root/draw.go
blob: 970974f91b95c9e5892a2fb18a5c098b2f87e3bb (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/physics"
	"age_of_empires/selection"

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

func (e *Entity) Draw(g *Game, screen *ebiten.Image) {
	if !e.Image.IsEnabled || !e.Position.IsEnabled {
		return
	}
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(float64(e.Position.Value.X), float64(e.Position.Value.Y))
	screen.DrawImage(e.Image.Value, op)
	if e.Selection.IsEnabled && e.Selection.Value.IsSelected {
		opt := &ebiten.DrawImageOptions{}
		opt.GeoM.Translate(float64(e.Position.Value.X-selection.HaloWidth/2), float64(e.Position.Value.Y-selection.HaloWidth/2))
		screen.DrawImage(e.Selection.Value.Halo, opt)
		if e.ResourceSource.IsEnabled {
			resourceText := fmt.Sprintf("%d", e.ResourceSource.Value.Remaining)
			op := &text.DrawOptions{}
			op.GeoM.Translate(float64(e.Position.Value.X+5), float64(e.Position.Value.Y+30))
			op.ColorScale.ScaleWithColor(color.White)
			text.Draw(screen, resourceText, &text.GoTextFace{
				Source: g.FaceSource,
				Size:   40,
			}, op)
		}
		if e.Spawn.IsEnabled {
			spawnText := fmt.Sprintf("%d", len(e.Spawn.Value.Requests))
			op := &text.DrawOptions{}
			op.GeoM.Translate(float64(e.Position.Value.X+5), float64(e.Position.Value.Y+70))
			op.ColorScale.ScaleWithColor(color.White)
			text.Draw(screen, spawnText, &text.GoTextFace{
				Source: g.FaceSource,
				Size:   40,
			}, op)
			if e.Spawn.Value.SpawnTarget.IsEnabled {
				spawnTarget := e.Spawn.Value.SpawnTarget.Value
				vector.DrawFilledCircle(screen, float32(spawnTarget.X+50), float32(spawnTarget.Y+50), 30, Red, true)
			}
		}
		if e.Move.Value.IsActive {
			last := e.Position.Value
			dx := +e.Image.Value.Bounds().Dx() / 2
			dy := +e.Image.Value.Bounds().Dy() / 2
			for _, point := range e.Move.Value.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
			}
		}
	}
}

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)
}

func (g *Game) Draw(screen *ebiten.Image) {
	x, y := ebiten.CursorPosition()
	cursor := physics.Point{X: x, Y: y}
	screen.Fill(soilColor)
	for _, e := range g.Entities {
		e.Draw(g, screen)
	}
	if g.Selection.IsActive(cursor) {
		g.Selection.Draw(screen, cursor)
	}

	drawTopBanner(screen, g)

	selected := make([]*Entity, 0, len(g.Entities))
	for _, e := range g.Entities {
		if e.Selection.IsEnabled && e.Selection.Value.IsSelected {
			selected = append(selected, e)
		}
	}
	if len(selected) > 0 {
		drawBottomBanner(screen, g)
	}
}