/
Entity.go
243 lines (198 loc) · 5.32 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
sf "bitbucket.org/krepa098/gosfml2"
)
//Faction represents the different groups of factions an NPC or player can belong to.
type Faction string
const (
//ORCS faction belongs to any orc.
ORCS Faction = "orcs"
//PLAYER faction represents allies to the player, and the player itself.
PLAYER Faction = "player"
)
//Entity contains the data that represents any entity that can appear on an Area that is not a tile.
type Entity struct {
x int
y int
name string
area *Area
*Mob
*Item
sprite *Graph
}
//Mob contains the data that an entity of type mob can use, meaning, any NPC.
type Mob struct {
maxhp int
curhp int
atk int
def int
inventory Inventory
faction []Faction
}
//NewEntity initializes an Entity with the given data.
func NewEntity(name string, spriteX, spriteY, posX, posY int, a *Area) *Entity {
sprite := NewGraph(spriteX, spriteY)
sprite.setSprite(spriteX, spriteY)
sprite.SetPosition(sf.Vector2f{float32(posX * sprite.size), float32(posY * sprite.size)})
m := new(Mob)
m.maxhp, m.curhp = 30, 30
m.atk = 10
m.def = 4
m.faction = append(m.faction, PLAYER)
m.inventory = make(Inventory)
return &Entity{x: posX, y: posY, area: a, sprite: sprite, Mob: m, name: name}
}
//NewEntityFromFile initializes an Entity with the data stored in the given JSON file.
func NewEntityFromFile(name string, x, y int, a *Area) *Entity {
data := ReadJSON("entities", name)
e := &Entity{x: x, y: y, area: a, name: name}
sx, sy := int(data["spriteX"].(float64)), int(data["spriteY"].(float64))
//e.sprite, _ = sf.NewGraph(EntitiesTexture)
e.sprite = NewGraph(sx, sy)
e.sprite.SetPosition(sf.Vector2f{float32(e.x * e.sprite.size), float32(e.y * e.sprite.size)})
e.Mob = nil
if data["type"].(string) == "mob" {
e.Mob = new(Mob)
e.maxhp, e.curhp = int(data["hp"].(float64)), int(data["hp"].(float64))
e.atk = int(data["atk"].(float64))
e.def = int(data["def"].(float64))
e.faction = make([]Faction, 1)
//e.faction = append(e.faction, data["faction"].([]interface{})...)
for _, v := range data["faction"].([]interface{}) {
e.faction = append(e.faction, Faction(v.(string)))
}
}
e.Item = nil
if data["type"].(string) == "item" {
e.Item = new(Item)
e.stack = 1
e.itype = ItemType(data["itemType"].(string))
switch e.itype {
case "potion":
e.effect = potionEffect
e.potency = int(data["potency"].(float64))
}
}
return e
}
//Move should take ints between -1 and 1. That is, the direction where to move.
//To specify any tile in the map Place or SetPosition should be used.
func (e *Entity) Move(x, y int, g *Game) {
dx := e.x + x
dy := e.y + y
ents := append(g.mobs, g.items...)
for _, oe := range ents {
if dx == oe.Position().X && dy == oe.Position().Y {
if e.name == "cursor" {
g.describe(oe)
} else if oe.Mob != nil {
e.attack(oe)
return
}
}
}
if !e.area.IsBlocked(e.x+x, e.y+y) {
e.Place(dx, dy)
}
}
func (e *Entity) moveTowards(oe *Entity, g *Game) {
if e.isAlliedWith(oe) {
return
}
ep := e.Position() //Entity Position
oep := oe.Position() //Other Entity Position.
dx, dy := 0, 0
switch {
case ep.X < oep.X:
dx = 1
case ep.X > oep.X:
dx = -1
case ep.X == oep.X:
dx = 0
}
switch {
case ep.Y < oep.Y:
dy = 1
case ep.Y > oep.Y:
dy = -1
case ep.Y == oep.Y:
dy = 0
}
e.Move(dx, dy, g)
}
func (attacker *Entity) attack(defender *Entity) {
if !attacker.isAlliedWith(defender) {
curhp := defender.curhp
afterhp := curhp - (attacker.atk - defender.def)
if afterhp <= 0 {
defender.die()
return
}
if afterhp > curhp {
defender.curhp = curhp
return
}
defender.curhp = afterhp
damaged := curhp - defender.curhp
log(fmt.Sprintf("%v attacks %v for %v damage.", attacker.name, defender.name, damaged))
}
}
func (e *Entity) die() {
e.Mob = nil
e.sprite.SetColor(sf.ColorRed())
//Becomes a corpse
e.name = e.name + "'s corpse"
e.Item = new(Item)
}
func (e *Entity) pickUp(i *Entity) {
log(fmt.Sprintf("You pickup: %v", i.name))
if e.inventory[i.name] != nil {
e.inventory[i.name].stack++ //Add a stack to it if we already had the item.
return
}
e.inventory[i.name] = i.Item
}
func (e *Entity) use(i *Item) {
i.effect(i, e.Mob)
}
func (m *Mob) heal(amount int) {
m.curhp += amount
if m.curhp > m.maxhp {
m.curhp = m.maxhp
}
}
func (e *Entity) isAlliedWith(oe *Entity) bool {
tef := e.faction
toef := oe.faction
for _, ef := range tef {
for _, oef := range toef {
if ef == oef {
return true
}
}
}
return false
}
//Draw draws the sprite on the window.
func (e *Entity) Draw(w *sf.RenderWindow) {
e.sprite.Draw(w, sf.DefaultRenderStates())
}
//Place places the entity in the given coordinates, as well as set the sprite position to its correct place.
func (e *Entity) Place(x, y int) {
e.x = x
e.y = y
e.sprite.setPosition(x, y)
}
//PosVector returns the position of the sprite, without using the tiled coordinate system, but the position based on the pixels of the window.
func (e *Entity) PosVector() sf.Vector2f {
return e.sprite.GetPosition()
}
//Position returns the position of the entity in the tile coordinate system.
func (e *Entity) Position() sf.Vector2i {
return sf.Vector2i{e.x, e.y}
}
//SetPosition places the entity in the given coordinates, as well as set the sprite position to its correct place.
func (e *Entity) SetPosition(pos sf.Vector2i) {
e.Place(pos.X, pos.Y)
}