-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
134 lines (113 loc) · 2.57 KB
/
entity.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
package main
import (
"fmt"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
type ObjectInfo struct {
GridX int
GridY int
Size int
}
type Object interface {
GetGridX() int
GetGridY() int
GetSize() int
}
type EntityInfo struct {
ObjectInfo
Name string
Defense int
MaxHealth int
CurrentHealth int
Gold int
ExperienceValue int
}
type Entity interface {
Object
GetName() string
GetDefense() int
GetMaxHealth() int
GetCurrentHealth() int
SetCurrentHealth(newHealth int)
GetGold() int
AddGold(newGold int) int
GetExperienceValue() int
Alive() bool
Heal()
}
type AttackInfo struct {
AttackPower int
attackAsset Asset
attackFrame int
}
type Attacker interface {
GetAttackPower() int
Attack(d Entity)
}
func (o *ObjectInfo) GetGridX() int {
return o.GridX
}
func (o *ObjectInfo) GetGridY() int {
return o.GridY
}
func (o *ObjectInfo) GetSize() int {
return o.Size
}
func (e *EntityInfo) Alive() bool {
return e.CurrentHealth > 0
}
func (e *EntityInfo) Heal() {
e.CurrentHealth = e.MaxHealth
}
func (e *EntityInfo) GetName() string {
return e.Name
}
func (e *EntityInfo) GetDefense() int {
return e.Defense
}
func (e *EntityInfo) GetMaxHealth() int {
return e.MaxHealth
}
func (e *EntityInfo) GetCurrentHealth() int {
return e.CurrentHealth
}
func (e *EntityInfo) SetCurrentHealth(newHealth int) {
e.CurrentHealth = newHealth
}
func (e *EntityInfo) GetGold() int {
return e.Gold
}
func (e *EntityInfo) AddGold(newGold int) int {
e.Gold += newGold
return e.Gold
}
func (e *EntityInfo) GetExperienceValue() int {
return e.ExperienceValue
}
func (e *EntityInfo) DrawInfo(screen *ebiten.Image, x, y float32) {
// Draw health inside the character
var infoText string
if e.CurrentHealth > 0 {
infoText = fmt.Sprintf("%s\n%d/%d\n%dg", e.Name, e.CurrentHealth, e.MaxHealth, e.Gold)
} else {
infoText = fmt.Sprintf("Dead\n%s", e.Name)
}
op := &text.DrawOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.LineSpacing = mplusNormalFace.Size * 1.5
text.Draw(screen, infoText, mplusNormalFace, op)
}
func (a *AttackInfo) GetAttackPower() int {
return a.AttackPower
}
func (a *AttackInfo) Attack(d Entity) {
// calculate the attackers's attack value and subtract from defender's health
pAttack := max((rand.IntN(a.AttackPower) + 1 - d.GetDefense()), 0)
d.SetCurrentHealth(max(d.GetCurrentHealth()-pAttack, 0))
incrementFrame(&a.attackFrame, a.attackAsset)
}
func (a *AttackInfo) LoadAttackImage(am AssetManager, name string) {
a.attackAsset = am.GetAssetInfo(name, "attack")
}