Repository navigation
Expand file tree
/
Copy pathcreature.go
More file actions
86 lines (73 loc) · 1.87 KB
/
Copy pathcreature.go
File metadata and controls
86 lines (73 loc) · 1.87 KB
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
package tinyrogue
import (
"github.com/firefly-zero/firefly-go/firefly"
)
type CreatureBehavior int
const (
CreatureIgnore CreatureBehavior = iota
CreatureApproach
CreatureAvoid
)
// Creature is the type for all creatures in the game.
type Creature struct {
*character
CurrentBehavior CreatureBehavior
Visible bool
}
// NewCreature creates a new Creature and initializes the data
func NewCreature(name string, kind string, img *firefly.Image, speed int) *Creature {
return &Creature{
character: &character{
name: name,
kind: kind,
Image: img,
speed: speed,
},
}
}
func (c *Creature) IsVisible() bool {
return c.Visible
}
func (c *Creature) SetVisible(visible bool) {
c.Visible = visible
}
// SetBehavior sets the behavior of the creature.
func (c *Creature) SetBehavior(b CreatureBehavior) {
c.CurrentBehavior = b
}
// Update updates the creature.
func (c *Creature) Update() {
switch c.CurrentBehavior {
case CreatureApproach:
c.Approach()
case CreatureAvoid:
c.Avoid()
}
}
// Approach moves the creature towards the player.
func (c *Creature) Approach() {
level := CurrentGame().CurrentLevel()
playerPosition := CurrentGame().Player.GetPosition()
creaturePos := c.GetPosition()
if c.Visible || !CurrentGame().UseFOV {
if creaturePos.GetManhattanDistance(playerPosition) < 2 {
// The creature is right next to the player. Now what?
if CurrentGame().ActionSystem != nil {
CurrentGame().ActionSystem.Action(c, CurrentGame().Player)
}
} else {
path := AStar{}.GetPath(level, creaturePos, playerPosition)
if len(path) > 1 {
nextTile := level.Tiles[level.GetIndexFromXY(path[1].X, path[1].Y)]
if !nextTile.Blocked {
level.Block(creaturePos, false)
c.MoveTo(Position{X: path[1].X, Y: path[1].Y})
nextTile.Blocked = true
}
}
}
}
}
func (c *Creature) Avoid() {
logDebug("Avoid is not implemented yet")
}