-
Notifications
You must be signed in to change notification settings - Fork 172
/
Entity.cs
416 lines (346 loc) · 10.2 KB
/
Entity.cs
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
namespace KBEngine
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/*
KBEngine逻辑层的实体基础类
所有扩展出的游戏实体都应该继承于该模块
*/
public class Entity
{
// 当前玩家最后一次同步到服务端的位置与朝向
// 这两个属性是给引擎KBEngine.cs用的,别的地方不要修改
public Vector3 _entityLastLocalPos = new Vector3(0f, 0f, 0f);
public Vector3 _entityLastLocalDir = new Vector3(0f, 0f, 0f);
public Int32 id = 0;
public string className = "";
public Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
public Vector3 direction = new Vector3(0.0f, 0.0f, 0.0f);
public float velocity = 0.0f;
public bool isOnGround = true;
public object renderObj = null;
public EntityCall baseEntityCall = null;
public EntityCall cellEntityCall = null;
// enterworld之后设置为true
public bool inWorld = false;
/// <summary>
/// 对于玩家自身来说,它表示是否自己被其它玩家控制了;
/// 对于其它entity来说,表示我本机是否控制了这个entity
/// </summary>
public bool isControlled = false;
// __init__调用之后设置为true
public bool inited = false;
// entityDef属性,服务端同步过来后存储在这里
private Dictionary<string, Property> defpropertys_ =
new Dictionary<string, Property>();
private Dictionary<UInt16, Property> iddefpropertys_ =
new Dictionary<UInt16, Property>();
public static void clear()
{
}
public Entity()
{
foreach(Property e in EntityDef.moduledefs[GetType().Name].propertys.Values)
{
Property newp = new Property();
newp.name = e.name;
newp.utype = e.utype;
newp.properUtype = e.properUtype;
newp.properFlags = e.properFlags;
newp.aliasID = e.aliasID;
newp.defaultValStr = e.defaultValStr;
newp.setmethod = e.setmethod;
newp.val = newp.utype.parseDefaultValStr(newp.defaultValStr);
defpropertys_.Add(e.name, newp);
iddefpropertys_.Add(e.properUtype, newp);
}
}
public virtual void onDestroy ()
{
}
public bool isPlayer()
{
return id == KBEngineApp.app.entity_id;
}
public void addDefinedProperty(string name, object v)
{
Property newp = new Property();
newp.name = name;
newp.properUtype = 0;
newp.val = v;
newp.setmethod = null;
defpropertys_.Add(name, newp);
}
public object getDefinedProperty(string name)
{
Property obj = null;
if(!defpropertys_.TryGetValue(name, out obj))
{
return null;
}
return defpropertys_[name].val;
}
public void setDefinedProperty(string name, object val)
{
defpropertys_[name].val = val;
}
public object getDefinedPropertyByUType(UInt16 utype)
{
Property obj = null;
if(!iddefpropertys_.TryGetValue(utype, out obj))
{
return null;
}
return iddefpropertys_[utype].val;
}
public void setDefinedPropertyByUType(UInt16 utype, object val)
{
iddefpropertys_[utype].val = val;
}
/*
KBEngine的实体构造函数,与服务器脚本对应。
存在于这样的构造函数是因为KBE需要创建好实体并将属性等数据填充好才能告诉脚本层初始化
*/
public virtual void __init__()
{
}
public virtual void callPropertysSetMethods()
{
foreach(Property prop in iddefpropertys_.Values)
{
object oldval = getDefinedPropertyByUType(prop.properUtype);
System.Reflection.MethodInfo setmethod = prop.setmethod;
if(setmethod != null)
{
if(prop.isBase())
{
if(inited && !inWorld)
{
//Dbg.DEBUG_MSG(className + "::callPropertysSetMethods(" + prop.name + ")");
setmethod.Invoke(this, new object[]{oldval});
}
}
else
{
if(inWorld)
{
if(prop.isOwnerOnly() && !isPlayer())
continue;
setmethod.Invoke(this, new object[]{oldval});
}
}
}
else
{
//Dbg.DEBUG_MSG(className + "::callPropertysSetMethods(" + prop.name + ") not found set_*");
}
}
}
public void baseCall(string methodname, params object[] arguments)
{
if(KBEngineApp.app.currserver == "loginapp")
{
Dbg.ERROR_MSG(className + "::baseCall(" + methodname + "), currserver=!" + KBEngineApp.app.currserver);
return;
}
ScriptModule module = null;
if(!EntityDef.moduledefs.TryGetValue(className, out module))
{
Dbg.ERROR_MSG("entity::baseCall: entity-module(" + className + ") error, can not find from EntityDef.moduledefs");
return;
}
Method method = null;
if(!module.base_methods.TryGetValue(methodname, out method))
{
Dbg.ERROR_MSG(className + "::baseCall(" + methodname + "), not found method!");
return;
}
UInt16 methodID = method.methodUtype;
if(arguments.Length != method.args.Count)
{
Dbg.ERROR_MSG(className + "::baseCall(" + methodname + "): args(" + (arguments.Length) + "!= " + method.args.Count + ") size is error!");
return;
}
baseEntityCall.newCall();
baseEntityCall.bundle.writeUint16(methodID);
try
{
for(var i=0; i<method.args.Count; i++)
{
if(method.args[i].isSameType(arguments[i]))
{
method.args[i].addToStream(baseEntityCall.bundle, arguments[i]);
}
else
{
throw new Exception("arg" + i + ": " + method.args[i].ToString());
}
}
}
catch(Exception e)
{
Dbg.ERROR_MSG(className + "::baseCall(method=" + methodname + "): args is error(" + e.Message + ")!");
baseEntityCall.bundle = null;
return;
}
baseEntityCall.sendCall(null);
}
public void cellCall(string methodname, params object[] arguments)
{
if(KBEngineApp.app.currserver == "loginapp")
{
Dbg.ERROR_MSG(className + "::cellCall(" + methodname + "), currserver=!" + KBEngineApp.app.currserver);
return;
}
ScriptModule module = null;
if(!EntityDef.moduledefs.TryGetValue(className, out module))
{
Dbg.ERROR_MSG("entity::cellCall: entity-module(" + className + ") error, can not find from EntityDef.moduledefs!");
return;
}
Method method = null;
if(!module.cell_methods.TryGetValue(methodname, out method))
{
Dbg.ERROR_MSG(className + "::cellCall(" + methodname + "), not found method!");
return;
}
UInt16 methodID = method.methodUtype;
if(arguments.Length != method.args.Count)
{
Dbg.ERROR_MSG(className + "::cellCall(" + methodname + "): args(" + (arguments.Length) + "!= " + method.args.Count + ") size is error!");
return;
}
if(cellEntityCall == null)
{
Dbg.ERROR_MSG(className + "::cellCall(" + methodname + "): no cell!");
return;
}
cellEntityCall.newCall();
cellEntityCall.bundle.writeUint16(methodID);
try
{
for(var i=0; i<method.args.Count; i++)
{
if(method.args[i].isSameType(arguments[i]))
{
method.args[i].addToStream(cellEntityCall.bundle, arguments[i]);
}
else
{
throw new Exception("arg" + i + ": " + method.args[i].ToString());
}
}
}
catch(Exception e)
{
Dbg.ERROR_MSG(className + "::cellCall(" + methodname + "): args is error(" + e.Message + ")!");
cellEntityCall.bundle = null;
return;
}
cellEntityCall.sendCall(null);
}
public void enterWorld()
{
// Dbg.DEBUG_MSG(className + "::enterWorld(" + getDefinedProperty("uid") + "): " + id);
inWorld = true;
try{
onEnterWorld();
}
catch (Exception e)
{
Dbg.ERROR_MSG(className + "::onEnterWorld: error=" + e.ToString());
}
Event.fireOut("onEnterWorld", new object[]{this});
}
public virtual void onEnterWorld()
{
}
public void leaveWorld()
{
// Dbg.DEBUG_MSG(className + "::leaveWorld: " + id);
inWorld = false;
try{
onLeaveWorld();
}
catch (Exception e)
{
Dbg.ERROR_MSG(className + "::onLeaveWorld: error=" + e.ToString());
}
Event.fireOut("onLeaveWorld", new object[]{this});
}
public virtual void onLeaveWorld()
{
}
public virtual void enterSpace()
{
// Dbg.DEBUG_MSG(className + "::enterSpace(" + getDefinedProperty("uid") + "): " + id);
inWorld = true;
try{
onEnterSpace();
}
catch (Exception e)
{
Dbg.ERROR_MSG(className + "::onEnterSpace: error=" + e.ToString());
}
Event.fireOut("onEnterSpace", new object[]{this});
// 要立即刷新表现层对象的位置
Event.fireOut("set_position", new object[]{this});
Event.fireOut("set_direction", new object[]{this});
}
public virtual void onEnterSpace()
{
}
public virtual void leaveSpace()
{
// Dbg.DEBUG_MSG(className + "::leaveSpace: " + id);
inWorld = false;
try{
onLeaveSpace();
}
catch (Exception e)
{
Dbg.ERROR_MSG(className + "::onLeaveSpace: error=" + e.ToString());
}
Event.fireOut("onLeaveSpace", new object[]{this});
}
public virtual void onLeaveSpace()
{
}
public virtual void set_position(object old)
{
Vector3 v = (Vector3)getDefinedProperty("position");
position = v;
//Dbg.DEBUG_MSG(className + "::set_position: " + old + " => " + v);
if(isPlayer())
KBEngineApp.app.entityServerPos(position);
if(inWorld)
Event.fireOut("set_position", new object[]{this});
}
public virtual void onUpdateVolatileData()
{
}
public virtual void set_direction(object old)
{
Vector3 v = (Vector3)getDefinedProperty("direction");
direction.x = v.x * 360 / ((float)System.Math.PI * 2);
direction.y = v.y * 360 / ((float)System.Math.PI * 2);
direction.z = v.z * 360 / ((float)System.Math.PI * 2);
//Dbg.DEBUG_MSG(className + "::set_direction: " + old + " => " + v);
if(inWorld)
Event.fireOut("set_direction", new object[]{this});
}
/// <summary>
/// This callback method is called when the local entity control by the client has been enabled or disabled.
/// See the Entity.controlledBy() method in the CellApp server code for more infomation.
/// </summary>
/// <param name="isControlled">
/// 对于玩家自身来说,它表示是否自己被其它玩家控制了;
/// 对于其它entity来说,表示我本机是否控制了这个entity
/// </param>
public virtual void onControlled(bool isControlled_)
{
}
}
}