/
Monster.go
168 lines (136 loc) · 3.77 KB
/
Monster.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
package main
import (
"time"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
)
// Monster type
type Monster struct {
entity.Entity // Entity type should always inherit entity.Entity
movingToTarget *entity.Entity
attackingTarget *entity.Entity
lastTickTime time.Time
attackCD time.Duration
lastAttackTime time.Time
}
func (monster *Monster) DescribeEntityType(desc *entity.EntityTypeDesc) {
desc.SetUseAOI(true)
desc.DefineAttr("name", "AllClients")
desc.DefineAttr("lv", "AllClients")
desc.DefineAttr("hp", "AllClients")
desc.DefineAttr("hpmax", "AllClients")
desc.DefineAttr("action", "AllClients")
}
func (monster *Monster) OnEnterSpace() {
monster.setDefaultAttrs()
monster.AddTimer(time.Millisecond*100, "AI")
monster.lastTickTime = time.Now()
monster.AddTimer(time.Millisecond*30, "Tick")
}
func (monster *Monster) setDefaultAttrs() {
monster.Attrs.SetDefaultStr("name", "minion")
monster.Attrs.SetDefaultInt("lv", 1)
monster.Attrs.SetDefaultInt("hpmax", 100)
monster.Attrs.SetDefaultInt("hp", 100)
monster.Attrs.SetDefaultStr("action", "idle")
monster.attackCD = time.Second
monster.lastAttackTime = time.Now()
}
func (monster *Monster) AI() {
var nearestPlayer *entity.Entity
for entity := range monster.Neighbors {
if entity.TypeName != "Player" {
continue
}
if entity.GetInt("hp") <= 0 {
// dead
continue
}
if nearestPlayer == nil || nearestPlayer.DistanceTo(&monster.Entity) > entity.DistanceTo(&monster.Entity) {
nearestPlayer = entity
}
}
if nearestPlayer == nil {
monster.Idling()
return
}
if nearestPlayer.DistanceTo(&monster.Entity) > monster.GetAttackRange() {
monster.MovingTo(nearestPlayer)
} else {
monster.Attacking(nearestPlayer)
}
}
func (monster *Monster) Tick() {
if monster.attackingTarget != nil && monster.IsNeighbor(monster.attackingTarget) {
now := time.Now()
if !now.Before(monster.lastAttackTime.Add(monster.attackCD)) {
monster.FaceTo(monster.attackingTarget)
monster.attack(monster.attackingTarget.I.(*Player))
monster.lastAttackTime = now
}
return
}
if monster.movingToTarget != nil && monster.IsNeighbor(monster.movingToTarget) {
mypos := monster.GetPosition()
direction := monster.movingToTarget.GetPosition().Sub(mypos)
direction.Y = 0
t := direction.Normalized().Mul(monster.GetSpeed() * 30 / 1000.0)
monster.SetPosition(mypos.Add(t))
monster.FaceTo(monster.movingToTarget)
return
}
}
func (monster *Monster) GetSpeed() entity.Coord {
return 2
}
func (monster *Monster) GetAttackRange() entity.Coord {
return 3
}
func (monster *Monster) Idling() {
if monster.movingToTarget == nil && monster.attackingTarget == nil {
return
}
monster.movingToTarget = nil
monster.attackingTarget = nil
monster.Attrs.SetStr("action", "idle")
}
func (monster *Monster) MovingTo(player *entity.Entity) {
if monster.movingToTarget == player {
// moving target not changed
return
}
monster.movingToTarget = player
monster.attackingTarget = nil
monster.Attrs.SetStr("action", "move")
}
func (monster *Monster) Attacking(player *entity.Entity) {
if monster.attackingTarget == player {
return
}
monster.movingToTarget = nil
monster.attackingTarget = player
monster.Attrs.SetStr("action", "move")
}
func (monster *Monster) attack(player *Player) {
monster.CallAllClients("DisplayAttack", player.ID)
if player.GetInt("hp") <= 0 {
return
}
player.TakeDamage(monster.GetDamage())
}
func (monster *Monster) GetDamage() int64 {
return 10
}
func (monster *Monster) TakeDamage(damage int64) {
hp := monster.GetInt("hp")
hp = hp - damage
if hp < 0 {
hp = 0
}
monster.Attrs.SetInt("hp", hp)
gwlog.Infof("%s TakeDamage %d => hp=%d", monster, damage, hp)
if hp <= 0 {
monster.Attrs.SetStr("action", "death")
monster.Destroy()
}
}