-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathliving.go
388 lines (337 loc) · 8.35 KB
/
living.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package rpg
import (
"fmt"
"log"
"math"
"math/rand"
"time"
"golang.org/x/image/colornames"
"github.com/aerth/rpc/librpg/common"
astar "github.com/beefsack/go-astar"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var DefaultEntityRectangle = pixel.R(-16, -16, 16, 16)
type Entity struct {
Name string
Type EntityType
Rate float64
State animState
Frame pixel.Rect // of sprite sheet
counter float64 // inside animation
Rect pixel.Rect // for damage, collisions
Program EntityState
P EntityProperties
Phys ePhys
Dir Direction // facing, attacking
paths []pixel.Vec // path finding
calculated time.Time // last path calculation time
ticker <-chan time.Time // attack speed
w *World // to read/write world and char
imd *imdraw.IMDraw // health bar
}
func (e *Entity) pathcalc(target pixel.Vec) {
var (
maxcost = 1000.00
)
if !e.calculated.IsZero() && time.Since(e.calculated) < time.Millisecond {
return
}
e.calculated = time.Now()
// get tiles, give world
tile := e.w.Tile(e.Rect.Center())
tile.W = e.w
targett := e.w.Tile(target)
targett.W = e.w
// check
if tile.Type == common.O_NONE {
// bad spawn, respawn
e.P.Health = 0
return
}
if targett.Type == common.O_NONE {
// player must be flying
e.calculated = time.Now().Add(3 * time.Second)
return
}
est := tile.PathEstimatedCost(targett)
if est < 64 {
//log.Println("direct to char", e, est)
e.paths = []pixel.Vec{e.w.Char.Rect.Center(), e.w.Char.Rect.Center(), e.w.Char.Rect.Center()}
return
}
if tile.PathEstimatedCost(targett) > 400 {
// too far
//log.Println("path too expensive, trying in 3 seconds")
e.calculated = time.Now().Add(1 * time.Second)
return
}
// calculate path
path, distance, found := astar.Path(tile, targett)
if found {
if distance > maxcost { // cost path
e.calculated = time.Now().Add(3 * time.Second)
log.Println("too far")
e.paths = nil
return
}
//log.Println("distance:", distance)
var paths []pixel.Vec
for _, p := range path {
//log.Println(p)
center := p.(common.Object).Loc.Add(common.DefaultSpriteRectangle.Center())
paths = append(paths, center)
}
e.paths = paths
return
}
//log.Println(e.Name, "no path found, distance:", distance)
}
type EntityType int
type EntityState int
const (
SKELETON EntityType = iota
SKELETON_GUARD
DOBJECT
)
type EntityProperties struct {
XP uint64
Health, MaxHealth float64
Mana float64
Loot []Item
IsDead bool
Strength float64
AttackSpeed uint64
CanFly bool
Friendly bool
}
const (
S_IDLE EntityState = iota
S_RUN
S_WANDER
S_GUARD
S_SUSPECT
S_HUNT
S_DEAD
)
func (e *Entity) String() string {
return fmt.Sprintf("%s at %v,%v", e.Name, int(e.Rect.Center().X), int(e.Rect.Center().Y))
}
func (w *World) NewEntity(t EntityType) *Entity {
n := len(w.Entities)
var e *Entity
switch t {
default:
log.Println("unimplemented entity type")
case SKELETON, SKELETON_GUARD:
if w.Sheets[t] == nil || w.Anims[t] == nil {
log.Println("New sheet:", t)
sheet, anims, err := LoadEntitySheet("sprites/"+t.String()+".png", 13, 21)
if err != nil {
panic(fmt.Errorf("error loading skeleton sheet: %v", err))
}
w.Sheets[t] = sheet
w.Anims[t] = anims
log.Printf("New Skeleton Animation Frames: %v", len(anims[S_RUN]))
}
e = &Entity{
Name: fmt.Sprintf("%s #%v", t, n),
w: w,
Type: t,
P: EntityProperties{
Health: 255,
Mana: 255,
Strength: 2,
XP: 20,
MaxHealth: 255,
AttackSpeed: 550,
},
Rect: DefaultEntityRectangle,
State: Running,
Frame: w.Anims[t][S_RUN][DOWN][0],
Phys: DefaultMobPhys,
Rate: 0.1,
}
e.ticker = time.Tick(time.Millisecond * time.Duration(e.P.AttackSpeed))
if t == SKELETON_GUARD {
e.P.XP = 200
e.P.MaxHealth = 1000
e.P.Health = 1000
e.P.Strength = 2
}
}
if e == nil {
return nil
}
e.P.Loot = RandomLoot()
w.Entities = append(w.Entities, e)
return e
}
type ePhys struct {
RunSpeed float64
Vel pixel.Vec
Gravity float64
Rate float64
}
// DefaultPhys character
var DefaultMobPhys = ePhys{
RunSpeed: 40.5,
Gravity: 50.00,
Rate: 2,
}
func (e *Entity) Draw(t pixel.Target, w *World) {
sprite := pixel.NewSprite(nil, pixel.Rect{})
// draw the correct frame with the correct position and direction
scaling := 0.5
if e.Type == SKELETON_GUARD {
scaling = 0.7
}
sprite.Set(w.Sheets[e.Type], e.Frame)
sprite.Draw(t, pixel.IM.Scaled(pixel.ZV, scaling).Moved(e.Rect.Center()))
//sprite.Draw(t, pixel.IM.Scaled(pixel.ZV, 0.5).Moved(e.Rect.Center()))
// HP bars
if e.imd == nil {
e.imd = imdraw.New(nil)
}
e.imd.Clear()
rect := e.Rect.Norm()
rect.Max.Y = rect.Min.Y + 2
rect.Max.X = rect.Min.X + 30
if e.P.Health > 0 {
common.DrawBar(e.imd, colornames.Red, e.P.Health, e.P.MaxHealth, rect)
e.imd.Draw(t)
}
/* good debug square
e.imd.Color = colornames.Green
e.imd.Push(e.Rect.Min, e.Rect.Max)
e.imd.Rectangle(1)
e.imd.Draw(t)
*/
}
func (e *Entity) Center() pixel.Vec {
return e.Rect.Center()
}
func (e *Entity) ChangeMind(dt float64) {
if t := e.w.Tile(e.Center()); t.Type != common.O_TILE {
e.Phys.Vel = pixel.ZV
return
}
if e.w.Char.Invisible {
e.Phys.Vel = pixel.ZV
return
}
r := pixel.Rect{e.Rect.Center(), e.w.Char.Rect.Center()}
if r.Size().Len() < e.Rect.Size().Len()/2 {
// log.Println("in attack range", r.Size().Len())
e.Phys.Vel = e.Rect.Center().Sub(e.w.Char.Rect.Center()).Unit().Scaled(-e.Phys.RunSpeed)
select {
case <-e.ticker:
e.w.Char.Damage(uint(rand.Intn(10*int(e.P.Strength))), e.Name)
default:
}
return
}
if e.P.CanFly {
log.Println("FLYING", e.Name)
if !e.w.Char.Invisible {
e.Phys.Vel = e.Rect.Center().Sub(e.w.Char.Rect.Center()).Unit().Scaled(-e.Phys.RunSpeed)
} else {
e.Phys.Vel = pixel.ZV
}
return
}
e.pathcalc(e.w.Char.Rect.Center())
if len(e.paths) > 2 {
e.Phys.Vel = e.Rect.Center().Sub(e.paths[len(e.paths)-2]).Unit().Scaled(-e.Phys.RunSpeed)
}
}
func (e *Entity) Update(dt float64) {
blk := e.w.Tile(e.Rect.Center())
if blk.Type != common.O_TILE {
old := e.Rect.Center()
e.Rect = DefaultEntityRectangle.Moved(common.TileNear(e.w.Tiles, e.Center()).Loc)
log.Println("Moved skel:", old, "to", e.Rect.Center())
return
}
e.counter += dt
w := e.w
i := int(math.Floor(e.counter / e.Rate))
//frame := i % len(e.Anims[e.Program][e.Dir])
if e.Phys.Vel.X != 0 || e.Phys.Vel.Y != 0 {
e.Program = S_RUN
} else {
e.Program = S_IDLE
}
if len(w.Anims[e.Type][e.Program][e.Dir]) == 0 {
log.Println("bad sprite:", e.Name, e.Program, e.Dir)
return
}
// choose frame
e.Frame = w.Anims[e.Type][e.Program][e.Dir][i%len(w.Anims[e.Type][e.Program][e.Dir])]
// move
next := e.Rect.Moved(e.Phys.Vel.Scaled(dt))
t := w.Tile(next.Center())
if t.Type == common.O_NONE && !e.P.CanFly {
return
}
if !e.P.CanFly && t.Type == common.O_BLOCK {
log.Println(e.Type, "got blocked", t.Loc)
next = e.Rect.Moved(e.Phys.Vel.Scaled(-dt * 10))
if w.Tile(next.Center()).Type != common.O_TILE {
log.Println("returning")
return
}
}
// log.Println(e.Name, "wants to go", next.Center(), "from", e.Rect.Center())
f := func(dot pixel.Vec) bool {
if e.P.CanFly {
return true
}
for _, c := range w.Blocks {
if c.Type == common.O_BLOCK && c.Rect.Contains(dot) {
return false
}
}
return true
}
// only walk on tiles
f2 := func(dot pixel.Vec) bool {
if e.P.CanFly {
return true
}
for _, c := range w.Tiles {
if c.Type == common.O_TILE && c.Rect.Contains(dot) {
return true
}
}
return false
}
if f(next.Center()) && f2(next.Center()) {
e.Rect = next
} else {
//log.Println("cant move", e.Name, "to ", next.Center(), w.Tile(next.Center()), e.paths[0])
if len(e.paths) > 0 {
e.paths = e.paths[:len(e.paths)-1]
}
}
}
func (w *World) NewMobs(n int) {
if w.Settings.NumEnemy == 0 {
w.Settings.NumEnemy = n
}
if n != 0 {
npc := w.NewEntity(SKELETON_GUARD)
npc.Phys.RunSpeed = 10
npc.P.Health = 2000
npc.P.MaxHealth = 2000
// npc.CanFly = true
npc.Rect = npc.Rect.Moved(common.FindRandomTile(w.Tiles))
for i := 1; i < n; i++ {
npc = w.NewEntity(SKELETON)
npc.Rect = npc.Rect.Moved(common.FindRandomTile(w.Tiles))
}
}
}