aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 718e261a310b07feef7fc8be29f33e2e8eb40386 (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
114
115
116
117
118
119
120

package main

import (
	"image/color"

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

type Tile struct {
	*ebiten.Image
	Size     Point
	Position Point
}

func NewTile(size Point, position Point, color color.Color) Tile {
	image := ebiten.NewImage(int(size.X), int(size.Y))
	image.Fill(color)
	return Tile{Size: size, Position: position, Image: image}
}

var soilColor = color.RGBA{0x60, 0x40, 0x20, 0xff}
var soil = NewTile(Point{2800, 2000}, Point{200, 200}, soilColor)

type Selection struct {
	IsActive bool
	Start    Point
}

type Game struct {
	Persons   []*Person
	Selection Selection
}

func (g *Game) Update() error {
	x, y := ebiten.CursorPosition()
	if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		g.Selection.Start = Point{x, y}
		g.Selection.IsActive = true
	}
	if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
		tileClick := Point{x - soil.Position.X, y - soil.Position.Y}
		for _, p := range g.Persons {
			futureBounds := Rectangle{
				tileClick.Sub(p.size.Div(2)),
				tileClick.Add(p.size.Div(2)),
			}
			if p.IsSelected && futureBounds.In(soil.Bounds()) {
				p.MoveTo(tileClick)
			}
		}
	}
	if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
		tileClick := Point{x - soil.Position.X, y - soil.Position.Y}
		if g.Selection.IsActive && Distance(g.Selection.Start, Point{x, y}) > 10 {
			for _, p := range g.Persons {
				p.IsSelected = false
				selectionBounds := Rectangle{g.Selection.Start.Sub(soil.Position), tileClick}.Canon()
				if selectionBounds.Overlaps(p.CollisionBounds()) {
					p.IsSelected = true
				}
			}
		} else {
			canBeSelected := true
			for _, p := range g.Persons {
				p.IsSelected = false
				if tileClick.In(p.CollisionBounds()) {
					p.IsSelected = canBeSelected
					canBeSelected = false
				}
			}
		}
		g.Selection.IsActive = false

	}
	for _, p := range g.Persons {
		p.Update()
	}
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{0x80, 0xa0, 0xc0, 0xff})
	soil.Fill(soilColor)
	for _, p := range g.Persons {
		bounds := p.Image().Bounds()
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Translate(float64(p.Position.X-bounds.Dx()/2), float64(p.Position.Y-bounds.Dy()/2))
		soil.DrawImage(p.Image(), op)
		if p.IsSelected && p.move.IsActive {
			vector.StrokeLine(soil.Image, float32(p.Position.X), float32(p.Position.Y), float32(p.move.Destination.X), float32(p.move.Destination.Y), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
		}
	}
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(float64(soil.Position.X), float64(soil.Position.Y))
	screen.DrawImage(soil.Image, op)
	if g.Selection.IsActive {
		x, y := ebiten.CursorPosition()
		screenClick := Point{x, y}
		vector.StrokeRect(screen, float32(g.Selection.Start.X), float32(g.Selection.Start.Y), float32(screenClick.X-g.Selection.Start.X), float32(screenClick.Y-g.Selection.Start.Y), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
	}
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 3200, 2400
}

func main() {
	ebiten.SetWindowSize(640, 480)
	ebiten.SetWindowTitle("Age of Empire")
	game := &Game{}
	mainPerson := NewPerson(Point{soil.Image.Bounds().Dx() / 2, soil.Image.Bounds().Dy() / 2})
	nonPlayerPerson := NewPerson(Point{400, 400})
	game.Persons = append(game.Persons, &mainPerson)
	game.Persons = append(game.Persons, &nonPlayerPerson)
	if err := ebiten.RunGame(game); err != nil {
		panic(err)
	}
}