/
entity.go
201 lines (182 loc) · 4.64 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
package entity
import (
"fmt"
. "github.com/daniel840829/gameServer/msg"
"github.com/daniel840829/gameServer/physic"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"os"
"sync"
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.DebugLevel)
}
type Entity struct {
sync.RWMutex
EntityInfo *Character
TypeName string
Health float32
Alive bool
I IEntity
GM *GameManager
Room *Room
World *physic.World
Obj *physic.Obj
Skill map[string]AttackBehavier
}
type IEntity interface {
IGameBehavier
Hit(int32)
GetInfo() *Character
Init(*GameManager, *Room, *Character)
Move(in *Input)
GetTransform() *TransForm
Harm(blood float32)
}
func (e *Entity) GetInfo() *Character {
e.RLock()
entityInfo := proto.Clone(e.EntityInfo).(*Character)
e.RUnlock()
return entityInfo
}
func (e *Entity) Hit(damage int32) {
fmt.Println("-", damage)
}
func (e *Entity) Harm(blood float32) {
e.Lock()
e.Health -= blood
if e.Health <= 0 {
//Dead
e.Alive = false
e.Unlock()
e.Destroy()
return
}
f := &CallFuncInfo{
Func: "Health",
Value: e.Health,
TargetId: e.EntityInfo.Uuid,
}
e.Room.SendFuncToAll(f)
e.Unlock()
}
func (e *Entity) Init(gm *GameManager, room *Room, entityInfo *Character) {
e.GM = gm
e.EntityInfo = entityInfo
e.Room = room
e.World = room.World
var ok bool
e.Obj, ok = room.World.Objs.Get(entityInfo.Uuid)
if !ok {
log.Fatal("[entity]{init} Get obj ", entityInfo.Uuid, " is not found. ")
}
//call All client create enitity at some point
e.costumeInit()
}
func (e *Entity) costumeInit() {
log.Warn("Please define your costumeInit")
}
func (e *Entity) Tick() {
}
func (e *Entity) Destroy() {
e.Lock()
e.GM.DestroyEntity(e.EntityInfo.Uuid)
e.Room.DestroyEntity(e.EntityInfo.Uuid)
e.World.DeleteObj(e.EntityInfo.Uuid)
e.Obj.Destroy()
e.Obj = nil
e.Unlock()
e = nil
}
func (e *Entity) Run() {
}
func (e *Entity) PhysicUpdate() {
}
func (e *Entity) GetTransform() *TransForm {
return &TransForm{}
}
func (e *Entity) Move(in *Input) {
turnSpeed := e.EntityInfo.Ability.TSPD
moveSpeed := e.EntityInfo.Ability.SPD
moveValue := in.V_Movement
turnValue := in.H_Movement
e.Room.World.Move(e.EntityInfo.Uuid, float64(moveValue*moveSpeed), float64(turnValue*turnSpeed))
}
/*
type EntityInfo struct {
//mathod's name map to Mathod's info
MethodMap map[string]EntityMathod
Type reflect.Type
}
type EntityMathod struct {
Func reflect.Value
Type reflect.Type
Args int
}
type EntityManager struct {
EntityTypeMap map[string]EntityInfo
EntityIdMap map[uuid.UUID]reflect.Value
}
var eManager *EntityManager = &EntityManager{
EntityTypeMap: make(map[string]EntityInfo),
EntityIdMap: make(map[uuid.UUID]reflect.Value),
}
func (em *EntityManager) Call(entityTypeName string, id uuid.UUID, fName string, args ...reflect.Value) {
e, ok := em.EntityIdMap[id]
eInfo, ok := em.EntityTypeMap[entityTypeName]
if !ok {
panic("Id not found")
}
f := eInfo.MethodMap[fName]
fmt.Println("f:", f)
in := make([]reflect.Value, f.Args)
in[0] = e
for i := 1; i < f.Args; i++ {
in[i] = args[i-1]
}
f.Func.Call(in)
}
func (em *EntityManager) CreateEnitity(entityTypeName string, isClient bool) (id uuid.UUID) {
entityInfo, ok := em.EntityTypeMap[entityTypeName]
if !ok {
fmt.Println(entityTypeName, "is not regist.")
}
vEntityPtr := reflect.New(entityInfo.Type)
//check uuid repeat
err := error(nil)
id, err = uuid.NewV4()
fmt.Println(id, err)
for _, ok := em.EntityIdMap[id]; ok; {
id, _ = uuid.NewV4()
fmt.Println(id, err)
}
em.EntityIdMap[id] = vEntityPtr
vEntityPtr.Elem().FieldByName("Id").Set(reflect.ValueOf(id))
vEntityPtr.Elem().FieldByName("TypeName").Set(reflect.ValueOf(vEntityPtr.Type().Elem().Name()))
em.Call(entityTypeName, id, "Init")
return
}
func RegisterEnitity(iEntity IEntity) {
rEntity := reflect.ValueOf(iEntity)
tEntity := rEntity.Type()
entityName := tEntity.Elem().Name()
rEntity.Elem().FieldByName("TypeName").Set(reflect.ValueOf(entityName))
fmt.Println("t:", tEntity, "v:", rEntity, "m:", rEntity.NumMethod())
entityInfo := &EntityInfo{MethodMap: make(map[string]EntityMathod)}
entityInfo.Type = tEntity.Elem()
for i := 0; i < rEntity.NumMethod(); i++ {
m := tEntity.Method(i)
em := EntityMathod{m.Func, m.Type, m.Type.NumIn()}
entityInfo.MethodMap[tEntity.Method(i).Name] = em
}
fmt.Println(entityInfo)
eManager.EntityTypeMap[entityName] = *entityInfo
fmt.Println(eManager)
}
*/