-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharacter.go
271 lines (231 loc) · 6.16 KB
/
character.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package rpg
import (
"fmt"
"log"
"math"
"strconv"
"time"
"github.com/aerth/rpc/librpg/common"
"github.com/faiface/pixel"
"github.com/faiface/pixel/text"
)
type Character struct {
Phys charPhys // properties
Stats Stats
Sprite *pixel.Sprite // current stamp
Matrix pixel.Matrix // location in canvas/map
Frame pixel.Rect // size (for animation)
Rect pixel.Rect // size (for collision)
Dir Direction // Running direction (Idle down)
Sheet pixel.Picture // all frames of animation (4 for each 4 direction, total 16)
Anims map[Direction][]pixel.Rect // animation
Rate float64 // animation
counter float64 // in animation
State animState // Idle or Running
Inventory []Item // inventory
Health uint // hp
Mana uint // mp
Invisible bool // hidden from enemies
Level uint
tick time.Time
textbuf *text.Text
W *World
}
type charPhys struct {
RunSpeed float64
Rect pixel.Rect
Vel pixel.Vec
Gravity float64
CanFly bool
Rate float64
}
var DefaultStats = Stats{
Intelligence: 60,
Strength: 60,
Wisdom: 60,
Vitality: 60,
}
// DefaultPhys character
var DefaultPhys = charPhys{
RunSpeed: 60.5,
Rect: pixel.R(-8, -8, 8, 8),
Gravity: 50.00,
Rate: 2,
}
func (c *Character) StatsReport() string {
s := (c.Stats.String())
s += fmt.Sprintf("Level %v\nHealth: %v\nMana: %v\nXP: %v/%v", c.Level, c.Health, c.Mana, c.Stats.XP, c.NextLevel())
for _, item := range c.Inventory {
if item.Effect != nil {
s += fmt.Sprintf("\n Effects: %q", item.Name)
}
}
return s
}
func NewCharacter(skin string) *Character {
// get main character asset
sheet, anims, err := LoadCharacterSheet("sprites/"+skin+".png", 32)
if err != nil {
panic(fmt.Errorf("error loading character sheet: %v", err))
}
c := new(Character)
c.Sheet = sheet
c.Anims = anims
//log.Printf("Anims: %v", len(anims))
c.Sprite = pixel.NewSprite(nil, pixel.Rect{})
c.Rect = DefaultPhys.Rect
c.State = Idle
c.Frame = c.Anims[DOWN][0]
c.Phys = DefaultPhys
c.Rate = 0.1
c.Health = 255
c.Mana = 255
c.Stats = DefaultStats
return c
}
func (char *Character) Draw(t pixel.Target) {
if char.Sprite == nil {
char.Sprite = pixel.NewSprite(nil, pixel.Rect{})
}
// draw the correct frame with the correct position and direction
char.Sprite.Set(char.Sheet, char.Frame)
char.Sprite.Draw(t, char.Matrix)
}
func (char *Character) Update(dt float64, dir Direction, world *World) {
for _, i := range char.Inventory {
if i.Effect != nil {
char.Stats = i.Effect(char.Stats)
}
}
if time.Since(char.tick) >= time.Second*1 {
if char.Mana < 255 {
char.Mana++
}
char.tick = time.Now()
}
char.counter += dt
// determine the new animation state
var newState animState
switch {
default:
newState = char.State
case -2 < char.Phys.Vel.Len() && char.Phys.Vel.Len() < 2:
char.Phys.Vel = pixel.ZV
newState = Idle
case char.Phys.Vel.Len() > 2:
newState = Running
case char.Phys.Vel.Len() < -2:
newState = Running
}
// reset the time counter if the state changed
if char.State != newState || char.Dir != dir {
char.State = newState
char.Dir = dir
//log.Println(char.State, char.Dir)
char.counter = 0
}
// determine the correct animation frame
if char.State == Idle {
char.Frame = char.Anims[dir][0]
} else if char.State == Running {
// count 0 1 2 3 0 1 2 3...
i := int(math.Floor(char.counter / char.Rate))
char.Frame = char.Anims[dir][i%len(char.Anims[dir])]
// gradually lose momentum
char.Phys.Vel = pixel.Lerp(char.Phys.Vel, pixel.ZV, 1-math.Pow(1.0/char.Phys.Gravity, dt))
next := char.Rect.Moved(char.Phys.Vel.Scaled(dt))
f := func(nextblock pixel.Rect) bool {
for _, c := range world.Blocks {
area := c.Rect.Norm().Intersect(nextblock.Norm()).Norm().Area()
if area != 0 {
// log.Printf("%f %s %s blocked by: %v at rect: %s", area, char.Rect, nextblock, c.SpriteNum, c.Rect)
return false
}
}
return true
}
if char.Phys.CanFly {
char.Rect = next
return
}
// only walk on tiles
f2 := func(nexttile pixel.Rect) bool {
for _, c := range world.Tiles {
if c.Type == common.O_TILE && c.Rect.Intersect(nexttile).Norm().Area() != 0 {
return true
}
}
// out of map
// log.Println("no tile to step on", dot)
return false
}
if f(next) && f2(next) {
// log.Println("passed:", next)
char.Rect = next
} else {
char.Phys.Vel = pixel.ZV
}
}
}
func (char *Character) Damage(n uint, from string) {
if from != "" {
from = fmt.Sprintf("from %s", from)
}
if char.Health < n {
char.Health = 0
log.Printf("Player took critical hit %s!", from)
return
}
char.Health -= n
char.W.Message(fmt.Sprintf("ouch! took %v damage, now at %v", n, char.Health))
}
func (char *Character) ResetLocation() {
char.Rect = DefaultPhys.Rect
char.Phys.Vel = pixel.ZV
}
func (char *Character) CountGold() string {
var madlootyo uint64
for _, item := range char.Inventory {
if item.Type == GOLD {
madlootyo += item.Quantity
}
}
return strconv.FormatInt(int64(madlootyo), 10)
}
func (char *Character) ExpUp(amount uint64) {
log.Println("Gained experience:", amount)
char.Stats.XP += amount
char.Stats.Score += amount
}
func (w *World) checkLevel() {
if w.Char.Stats.XP == 0 {
return
}
nextlvl := w.Char.NextLevel()
if w.Char.Stats.XP > nextlvl {
w.Char.Level++
w.Char.Health = 255
w.Message("LVL UP")
log.Printf("level up (%v)! next lvl at %v xp", w.Char.Level, nextlvl)
if xp := w.Char.Stats.XP - nextlvl; xp > 0 {
w.Char.Stats.XP = xp
} else {
w.Char.Stats.XP = 0
}
switch w.Char.Level {
default:
w.Char.Stats.Intelligence += float64(10 * w.Char.Level)
}
log.Println(w.Char.Stats)
}
}
func (c *Character) NextLevel() uint64 {
return uint64(150 * c.Level)
}
func (c *Character) MaxHealth() uint64 {
return uint64(c.Health * c.Level)
}
func (c *Character) PickUp(items []Item) string {
c.Inventory = StackItems(c.Inventory, items)
return fmt.Sprint(items)
}