Skip to content

Commit

Permalink
https://github.com/kbengine/kbengine/issues/225
Browse files Browse the repository at this point in the history
bots增加移动支持
  • Loading branch information
kbengine committed May 8, 2015
1 parent 513ad2e commit 13dd9b7
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 13 deletions.
1 change: 1 addition & 0 deletions kbe/src/lib/client_lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ SRCS = \
entity \
entity_aspect \
event \
moveto_point_handler \
profile

ifndef KBE_ROOT
Expand Down
2 changes: 2 additions & 0 deletions kbe/src/lib/client_lib/client_lib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<ClCompile Include="entity.cpp" />
<ClCompile Include="entity_aspect.cpp" />
<ClCompile Include="event.cpp" />
<ClCompile Include="moveto_point_handler.cpp" />
<ClCompile Include="profile.cpp" />
<ClCompile Include="script_callbacks.cpp" />
</ItemGroup>
Expand All @@ -112,6 +113,7 @@
<ClInclude Include="entity_aspect.h" />
<ClInclude Include="event.h" />
<ClInclude Include="kbemain.h" />
<ClInclude Include="moveto_point_handler.h" />
<ClInclude Include="profile.h" />
<ClInclude Include="script_callbacks.h" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions kbe/src/lib/client_lib/client_lib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClCompile Include="script_callbacks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="moveto_point_handler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="client_interface.h">
Expand Down Expand Up @@ -83,6 +86,9 @@
<ClInclude Include="script_callbacks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="moveto_point_handler.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
Expand Down
6 changes: 3 additions & 3 deletions kbe/src/lib/client_lib/clientobjectbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ serverDatas_(""),
typeClient_(CLIENT_TYPE_PC),
bufferedCreateEntityMessage_(),
eventHandler_(),
ninterface_(ninterface),
networkInterface_(ninterface),
targetID_(0),
isLoadedGeometry_(false),
timers_(),
Expand Down Expand Up @@ -159,7 +159,7 @@ void ClientObjectBase::reset(void)
}

pServerChannel_ = Network::Channel::ObjPool().createObject();
pServerChannel_->pNetworkInterface(&ninterface_);
pServerChannel_->pNetworkInterface(&networkInterface_);
}

//-------------------------------------------------------------------------------------
Expand Down Expand Up @@ -276,7 +276,7 @@ PyObject* ClientObjectBase::__py_callback(PyObject* self, PyObject* args)

ClientObjectBase* pClientObjectBase = static_cast<ClientObjectBase*>(self);
Py_INCREF(pyCallback);
ScriptID id = pClientObjectBase->scriptCallbacks().addCallback(time, new ScriptCallbackHandler(pClientObjectBase->scriptCallbacks(), pyCallback));
ScriptID id = pClientObjectBase->scriptCallbacks().addCallback(time, 0.0f, new ScriptCallbackHandler(pClientObjectBase->scriptCallbacks(), pyCallback));
return PyLong_FromLong(id);
}

Expand Down
5 changes: 4 additions & 1 deletion kbe/src/lib/client_lib/clientobjectbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ class ClientObjectBase : public script::ScriptObject
virtual void onServerClosed();

uint64 rndUUID() const{ return rndUUID_; }

Network::NetworkInterface* pNetworkInterface()const { return &networkInterface_; }

protected:
int32 appID_;

Expand Down Expand Up @@ -440,7 +443,7 @@ class ClientObjectBase : public script::ScriptObject

EventHandler eventHandler_;

Network::NetworkInterface& ninterface_;
Network::NetworkInterface& networkInterface_;

// 当前客户端所选择的目标
ENTITY_ID targetID_;
Expand Down
196 changes: 195 additions & 1 deletion kbe/src/lib/client_lib/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.

#include "clientapp.h"
#include "entity.h"
#include "config.h"
#include "clientobjectbase.h"
#include "moveto_point_handler.h"
#include "entitydef/entity_mailbox.h"
#include "network/channel.h"
#include "network/bundle.h"
Expand All @@ -41,6 +43,8 @@ namespace client

//-------------------------------------------------------------------------------------
CLIENT_ENTITY_METHOD_DECLARE_BEGIN(ClientApp, Entity)
SCRIPT_METHOD_DECLARE("moveToPoint", pyMoveToPoint, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("cancelController", pyCancelController, METH_VARARGS, 0)
CLIENT_ENTITY_METHOD_DECLARE_END()

SCRIPT_MEMBER_DECLARE_BEGIN(Entity)
Expand Down Expand Up @@ -69,7 +73,8 @@ pClientApp_(NULL),
aspect_(id),
velocity_(3.0f),
enterworld_(false),
isOnGound_(true)
isOnGound_(true),
pMoveHandlerID_(0)
{
ENTITY_INIT_PROPERTYS(Entity);
script::PyGC::incTracing("Entity");
Expand Down Expand Up @@ -542,6 +547,195 @@ void Entity::onBecomeNonPlayer()
SCRIPT_ERROR_CHECK();
}

//-------------------------------------------------------------------------------------
bool Entity::stopMove()
{
if(pMoveHandlerID_ > 0)
{
pClientApp_->scriptCallbacks().delCallback(pMoveHandlerID_);
pMoveHandlerID_ = 0;
return true;
}

return false;
}

//-------------------------------------------------------------------------------------
uint32 Entity::moveToPoint(const Position3D& destination, float velocity, float distance, PyObject* userData,
bool faceMovement, bool moveVertically)
{
stopMove();

int hertz = 0;
if(g_componentType == BOTS_TYPE)
hertz = g_kbeSrvConfig.gameUpdateHertz();
else
hertz = Config::getSingleton().gameUpdateHertz();

velocity = velocity / hertz;

pMoveHandlerID_ = pClientApp_->scriptCallbacks().addCallback(0.0f, 0.1f, new MoveToPointHandler(pClientApp_->scriptCallbacks(), this, 0, destination, velocity,
distance, faceMovement, moveVertically, userData));

return pMoveHandlerID_;
}

//-------------------------------------------------------------------------------------
PyObject* Entity::pyMoveToPoint(PyObject_ptr pyDestination, float velocity, float distance, PyObject_ptr userData,
int32 faceMovement, int32 moveVertically)
{
if(this->isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s::moveToPoint: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}

Position3D destination;

if(!PySequence_Check(pyDestination))
{
PyErr_Format(PyExc_TypeError, "%s::moveToPoint: args1(position) not is PySequence!", scriptName());
PyErr_PrintEx(0);
return 0;
}

if(PySequence_Size(pyDestination) != 3)
{
PyErr_Format(PyExc_TypeError, "%s::moveToPoint: args1(position) invalid!", scriptName());
PyErr_PrintEx(0);
return 0;
}

// 将坐标信息提取出来
script::ScriptVector3::convertPyObjectToVector3(destination, pyDestination);
Py_INCREF(userData);

return PyLong_FromLong(moveToPoint(destination, velocity, distance, userData, faceMovement > 0, moveVertically > 0));
}

//-------------------------------------------------------------------------------------
void Entity::onMove(uint32 controllerId, int layer, const Position3D& oldPos, PyObject* userarg)
{
if(this->isDestroyed())
return;

AUTO_SCOPED_PROFILE("onMove");

SCRIPT_OBJECT_CALL_ARGS2(this, const_cast<char*>("onMove"),
const_cast<char*>("IO"), controllerId, userarg);
}

//-------------------------------------------------------------------------------------
void Entity::onMoveOver(uint32 controllerId, int layer, const Position3D& oldPos, PyObject* userarg)
{
if(this->isDestroyed())
return;

stopMove();

SCOPED_PROFILE(SCRIPTCALL_PROFILE);
SCRIPT_OBJECT_CALL_ARGS2(this, const_cast<char*>("onMoveOver"),
const_cast<char*>("IO"), controllerId, userarg);
}

//-------------------------------------------------------------------------------------
void Entity::onMoveFailure(uint32 controllerId, PyObject* userarg)
{
if(this->isDestroyed())
return;

stopMove();

SCOPED_PROFILE(SCRIPTCALL_PROFILE);
SCRIPT_OBJECT_CALL_ARGS2(this, const_cast<char*>("onMoveFailure"),
const_cast<char*>("IO"), controllerId, userarg);
}

//-------------------------------------------------------------------------------------
void Entity::cancelController(uint32 id)
{
if(this->isDestroyed())
{
return;
}

// 暂时只有回调, 主要是因为用在了移动中,当前可能不是非常合适
if(id == pMoveHandlerID_)
this->stopMove();
}

//-------------------------------------------------------------------------------------
PyObject* Entity::__py_pyCancelController(PyObject* self, PyObject* args)
{
uint16 currargsSize = PyTuple_Size(args);
Entity* pobj = static_cast<Entity*>(self);

uint32 id = 0;
PyObject* pyargobj = NULL;

if(currargsSize != 1)
{
PyErr_Format(PyExc_AssertionError, "%s::cancel: args require 1 args(controllerID|int or \"Movement\"|str), gived %d! is script[%s].\n",
pobj->scriptName(), currargsSize);

PyErr_PrintEx(0);
return 0;
}

if(PyArg_ParseTuple(args, "O", &pyargobj) == -1)
{
PyErr_Format(PyExc_TypeError, "%s::cancel: args(controllerID|int or \"Movement\"|str) is error!", pobj->scriptName());
PyErr_PrintEx(0);
return 0;
}

if(pyargobj == NULL)
{
PyErr_Format(PyExc_TypeError, "%s::cancel: args(controllerID|int or \"Movement\"|str) is error!", pobj->scriptName());
PyErr_PrintEx(0);
return 0;
}

if(PyUnicode_Check(pyargobj))
{
wchar_t* PyUnicode_AsWideCharStringRet0 = PyUnicode_AsWideCharString(pyargobj, NULL);
char* s = strutil::wchar2char(PyUnicode_AsWideCharStringRet0);
PyMem_Free(PyUnicode_AsWideCharStringRet0);

if(strcmp(s, "Movement") == 0)
{
pobj->stopMove();
}
else
{
PyErr_Format(PyExc_TypeError, "%s::cancel: args not is \"Movement\"!", pobj->scriptName());
PyErr_PrintEx(0);
free(s);
return 0;
}

free(s);

S_Return;
}
else
{
if(!PyLong_Check(pyargobj))
{
PyErr_Format(PyExc_TypeError, "%s::cancel: args(controllerID|int) is error!", pobj->scriptName());
PyErr_PrintEx(0);
return 0;
}

id = PyLong_AsLong(pyargobj);
}

pobj->cancelController(id);
S_Return;
}

//-------------------------------------------------------------------------------------
}
}
Expand Down
37 changes: 37 additions & 0 deletions kbe/src/lib/client_lib/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ class Entity : public script::ScriptObject
INLINE ClientObjectBase* pClientApp() const;

const EntityAspect* getAspect() const{ return &aspect_; }

/**
entity移动到某个点
*/
uint32 moveToPoint(const Position3D& destination, float velocity, float distance,
PyObject* userData, bool faceMovement, bool moveVertically);

DECLARE_PY_MOTHOD_ARG6(pyMoveToPoint, PyObject_ptr, float, float, PyObject_ptr, int32, int32);

/**
停止任何移动行为
*/
bool stopMove();

/**
entity的一次移动完成
*/
void onMove(uint32 controllerId, int layer, const Position3D& oldPos, PyObject* userarg);

/**
entity的移动完成
*/
void onMoveOver(uint32 controllerId, int layer, const Position3D& oldPos, PyObject* userarg);

/**
entity移动失败
*/
void onMoveFailure(uint32 controllerId, PyObject* userarg);

/**
删除一个控制器
*/
void cancelController(uint32 id);
static PyObject* __py_pyCancelController(PyObject* self, PyObject* args);

/**
销毁这个entity
*/
Expand Down Expand Up @@ -153,6 +188,8 @@ class Entity : public script::ScriptObject
bool enterworld_; // 是否已经enterworld了, restore时有用

bool isOnGound_;

ScriptID pMoveHandlerID_;
};

}
Expand Down
Loading

0 comments on commit 13dd9b7

Please sign in to comment.