-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
185 lines (152 loc) · 3.82 KB
/
item.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"image"
"image/color"
"os"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"gopkg.in/yaml.v3"
)
type ItemInfo struct {
ObjectInfo
Value int
Used bool
asset Asset
}
type Item interface {
Object
Draw(screen *ebiten.Image, b Board) error
Select(screen *ebiten.Image, b Board) error
GetAsset() Asset
GetValue() int
GetUsed() bool
Use(e Entity)
inRange(e Entity, reach int) bool
Refresh()
}
type TreasureInfo struct {
ItemInfo
}
type HealthPackInfo struct {
ItemInfo
}
func (h *HealthPackInfo) Use(e Entity) {
if !h.Used {
e.SetCurrentHealth(int(min(e.GetCurrentHealth()+h.Value, e.GetMaxHealth())))
h.Used = true
}
}
func (t *TreasureInfo) Use(e Entity) {
if !t.Used {
e.AddGold(t.Value)
t.Used = true
}
}
func (i *ItemInfo) GetValue() int {
return i.Value
}
func (i *ItemInfo) GetUsed() bool {
return i.Used
}
func (i *ItemInfo) Refresh() {
i.Used = false
}
func (i *ItemInfo) inRange(e Entity, reach int) bool {
return inRange(i, e, reach)
}
func (t *TreasureInfo) Draw(screen *ebiten.Image, b Board) error {
return drawItem(screen, t, color.RGBA{255, 215, 0, 255}, b)
}
func (h *HealthPackInfo) Draw(screen *ebiten.Image, b Board) error {
return drawItem(screen, h, color.RGBA{100, 255, 100, 255}, b)
}
func drawItem(screen *ebiten.Image, i Item, c color.Color, b Board) error {
if !i.GetUsed() {
x, y, err := b.GridToXY(i.GetGridX(), i.GetGridY())
if err != nil {
return err
}
s := float32(i.GetSize() * b.GetGridSize())
vector.DrawFilledRect(screen, x, y, s, s, c, true)
size := float64(i.GetSize())
opts := &ebiten.DrawImageOptions{}
opts.GeoM.Translate(float64(x)/size, float64(y)/size)
opts.GeoM.Scale(size, size)
img := i.GetAsset().GetImage()
rect := image.Rect(0, 0, b.GetGridSize(), b.GetGridSize())
screen.DrawImage(img.SubImage(rect).(*ebiten.Image), opts)
drawInfo(screen, i, x+4, y-10)
}
return nil
}
func drawInfo(screen *ebiten.Image, i Item, x, y float32) {
var units string
switch i.(type) {
case *TreasureInfo:
units = "g"
case *HealthPackInfo:
units = "hp"
}
infoText := fmt.Sprintf("%d %s\n", i.GetValue(), units)
op := &text.DrawOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.LineSpacing = mplusNormalFace.Size * 2
text.Draw(screen, infoText, mplusNormalFace, op)
}
func (i *ItemInfo) GetAsset() Asset {
return i.asset
}
func (i *ItemInfo) Select(screen *ebiten.Image, b Board) error {
if !i.Used {
x, y, err := b.GridToXY(i.GridX, i.GridY)
if err != nil {
return err
}
s := float32(i.Size * b.GetGridSize())
vector.StrokeRect(screen, x, y, s, s, 2, color.RGBA{0, 255, 255, 255}, true)
}
return nil
}
func NewTreasure(b Board, am AssetManager, gold, x, y, size int) *TreasureInfo {
t := &TreasureInfo{ItemInfo: ItemInfo{Value: gold, ObjectInfo: ObjectInfo{x, y, size}}}
b.AddObjectToBoard(t)
t.LoadImages(am, "Treasure")
return t
}
func LoadTreasures(b Board, am AssetManager) ([]*TreasureInfo, error) {
yamlFile, err := os.ReadFile("config/treasures.yml")
if err != nil {
return nil, err
}
var treasures []*TreasureInfo
err = yaml.Unmarshal(yamlFile, &treasures)
if err != nil {
return nil, err
}
for _, t := range treasures {
b.AddObjectToBoard(t)
t.LoadImages(am, "Treasure")
}
return treasures, nil
}
func LoadHealthPacks(b Board, am AssetManager) ([]*HealthPackInfo, error) {
yamlFile, err := os.ReadFile("config/healthpacks.yml")
if err != nil {
return nil, err
}
var healthPacks []*HealthPackInfo
err = yaml.Unmarshal(yamlFile, &healthPacks)
if err != nil {
return nil, err
}
for _, h := range healthPacks {
b.AddObjectToBoard(h)
h.LoadImages(am, "HealthPack")
}
return healthPacks, nil
}
func (i *ItemInfo) LoadImages(am AssetManager, name string) {
i.asset = am.GetAssetInfo(name, "item")
}