aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: f2b6c647f17c0e67c5f2a97442fd623ef211fc1c (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

package main

import (
	"fmt"
	"image/color"

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

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

func NewTile(size Vec, position Vec, 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(Vec{X: 280, Y: 200}, Vec{X: 20, Y: 20}, soilColor)

func GetDrawImageOptions(size Vec, position Vec) *ebiten.DrawImageOptions {
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(position.X-size.X/2, position.Y-size.Y/2)

	return op
}

type Game struct {
	Persons []*Person
}

func (g *Game) Update() error {
	if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
		x, y := ebiten.CursorPosition()
		tileClick := Vec{X: float64(x) - soil.Position.X, Y: float64(y) - soil.Position.Y}
		fmt.Println("Click: ", tileClick)
		for _, p := range g.Persons {
			p.IsSelected = false
			fmt.Println("Person: ", p.Position)
			if p.Bounds().Contains(tileClick) {
				fmt.Println("Person selected")
				p.IsSelected = true
			}
		}
	}
	return nil
}

var shallowWaterColor = color.RGBA{0x80, 0xa0, 0xc0, 0xff}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(shallowWaterColor)
	soil.Fill(soilColor)
	for _, p := range g.Persons {
		bounds := p.Image().Bounds()
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Translate(p.Position.X-float64(bounds.Dx())/2, p.Position.Y-float64(bounds.Dy())/2)
		soil.DrawImage(p.Image(), op)
	}
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(soil.Position.X, soil.Position.Y)
	screen.DrawImage(soil.Image, op)
}

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

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