-
Notifications
You must be signed in to change notification settings - Fork 0
/
monster.go
96 lines (81 loc) · 2.09 KB
/
monster.go
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
package main
import (
"image"
"image/color"
"os"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"gopkg.in/yaml.v3"
)
type MonsterInfo struct {
EntityInfo
AttackInfo
}
type Monster interface {
Entity
Attacker
Draw(screen *ebiten.Image, b Board) error
Select(screen *ebiten.Image, b Board) error
Loot(b Board, am AssetManager) Item
}
func (m *MonsterInfo) Draw(screen *ebiten.Image, b Board) error {
x, y, err := b.GridToXY(m.GridX, m.GridY)
if err != nil {
return err
}
if m.Alive() {
s := float32(m.Size * b.GetGridSize())
vector.DrawFilledRect(screen, x, y, s, s, color.RGBA{255, 0, 0, 255}, true)
size := float64(m.GetSize())
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(float64(x)/size, float64(y)/size)
opts.GeoM.Scale(size, size)
frame := m.attackFrame
img := m.attackAsset.GetImage()
rect := image.Rect(frame*b.GetGridSize(), 0, (frame+1)*b.GetGridSize(), b.GetGridSize())
screen.DrawImage(img.SubImage(rect).(*ebiten.Image), opts)
}
m.DrawInfo(screen, x, y)
return nil
}
func (m *MonsterInfo) Select(screen *ebiten.Image, b Board) error {
if m.Alive() {
x, y, err := b.GridToXY(m.GridX, m.GridY)
if err != nil {
return err
}
s := float32(m.Size * b.GetGridSize())
vector.StrokeRect(screen, x, y, s, s, 2, color.RGBA{0, 255, 255, 255}, true)
}
return nil
}
func (m *MonsterInfo) Loot(b Board, am AssetManager) Item {
return NewTreasure(b, am, m.Gold, m.GridX, m.GridY, m.Size)
}
func LoadMonsters(b Board, am AssetManager) ([]*MonsterInfo, error) {
yamlFile, err := os.ReadFile("config/monsters.yml")
if err != nil {
return nil, err
}
var monsters []*MonsterInfo
err = yaml.Unmarshal(yamlFile, &monsters)
if err != nil {
return nil, err
}
// place monsters on the board
for _, m := range monsters {
b.AddObjectToBoard(m)
m.LoadImages(am)
}
return monsters, nil
}
func (m *MonsterInfo) LoadImages(am AssetManager) {
var monsterIcon string
if strings.HasPrefix(m.Name, "Barbol") {
monsterIcon = "SteamMan"
} else {
monsterIcon = "GraveRobber"
}
m.LoadAttackImage(am, monsterIcon)
}