3 Star 3 Fork 1

Gitee 极速下载/kbengine-unity3d-plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/kbengine/kbengine_unity3d_plugins
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Entity.cs 10.22 KB
一键复制 编辑 原始数据 按行查看 历史
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_)
{
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/mirrors/kbengine-unity3d-plugins.git
git@gitee.com:mirrors/kbengine-unity3d-plugins.git
mirrors
kbengine-unity3d-plugins
kbengine-unity3d-plugins
master

搜索帮助