Creatures update server-side

This commit is contained in:
2016-03-27 09:53:24 +11:00
parent 57f92a8b2e
commit 4ced27a905
7 changed files with 49 additions and 14 deletions
+26 -3
View File
@@ -21,6 +21,9 @@
*/ */
#include "creature_data.hpp" #include "creature_data.hpp"
#include <sstream>
#include <stdexcept>
CreatureData::CreatureData(std::string _avatar, int _scriptRef): CreatureData::CreatureData(std::string _avatar, int _scriptRef):
Entity("creature"), Entity("creature"),
avatar(_avatar), avatar(_avatar),
@@ -29,9 +32,29 @@ CreatureData::CreatureData(std::string _avatar, int _scriptRef):
//EMPTY //EMPTY
} }
void CreatureData::Update() { int CreatureData::Update(lua_State* L) {
Entity::Update(); int ret = 0;
//TODO: (0) call the script reference
if (scriptRef != 0) {
//Call the script reference
lua_pushinteger(L, scriptRef);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, reinterpret_cast<void*>(this));
//check for errors
if(lua_pcall(L, 1, 1, 0) != LUA_OK) {
std::ostringstream msg;
msg << "Error running creature script: " << lua_tostring(L, -1);
lua_pop(L, 1);
throw(std::runtime_error(msg.str()));
}
ret += lua_tonumber(L, -1);
}
ret += Entity::Update();
return ret;
} }
//------------------------- //-------------------------
+1 -1
View File
@@ -39,7 +39,7 @@ public:
CreatureData(std::string avatar, int scriptRef); CreatureData(std::string avatar, int scriptRef);
~CreatureData() = default; ~CreatureData() = default;
virtual void Update(); virtual int Update(lua_State*);
//accessors & mutators //accessors & mutators
+7 -2
View File
@@ -29,9 +29,14 @@ CreatureManager::~CreatureManager() {
UnloadAll(); UnloadAll();
} }
void CreatureManager::Update() { //arg: a list of creatures to be updated in the clients
int CreatureManager::Update(std::list<CreatureData*>* creatureList) {
int ret;
for (auto& it : elementMap) { for (auto& it : elementMap) {
it.second.Update(); ret = it.second.Update(lua);
if (ret) {
creatureList->push_back(&it.second);
}
} }
} }
+2 -1
View File
@@ -27,6 +27,7 @@
#include "sqlite3.h" #include "sqlite3.h"
#include <functional> #include <functional>
#include <list>
#include <map> #include <map>
#include <string> #include <string>
@@ -36,7 +37,7 @@ public:
~CreatureManager(); ~CreatureManager();
//common public methods //common public methods
void Update(); int Update(std::list<CreatureData*>* creatureList);
int Create(std::string avatar, int scriptRef); int Create(std::string avatar, int scriptRef);
void Unload(int uid); void Unload(int uid);
+3 -1
View File
@@ -25,8 +25,10 @@ Entity::Entity(const char* _type): type(_type) {
//EMPTY //EMPTY
} }
void Entity::Update() { int Entity::Update() {
origin += motion; origin += motion;
return motion != 0;
} }
int Entity::SetRoomIndex(int i) { int Entity::SetRoomIndex(int i) {
+1 -1
View File
@@ -29,7 +29,7 @@
//The base class for all objects in the world //The base class for all objects in the world
class Entity { class Entity {
public: public:
virtual void Update(); virtual int Update();
//accessors & mutators //accessors & mutators
int SetRoomIndex(int i); int SetRoomIndex(int i);
+9 -5
View File
@@ -43,20 +43,17 @@ void RoomData::RunFrame() {
} }
//update the entities in the room //update the entities in the room
creatureMgr.Update();
for (auto& it : characterList) { for (auto& it : characterList) {
it->Update(); it->Update();
} }
//TODO: (3) trigger script for monsters
//build a list of game entities //build a list of characters for use with the triggers
std::stack<Entity*> entityStack; std::stack<Entity*> entityStack;
for (auto& it : characterList) { for (auto& it : characterList) {
entityStack.push(it); entityStack.push(it);
} }
//TODO: (3) push the monster entities
//compare the triggers to the entities, using their real hitboxes //Compare the triggers to the entities, using their real hitboxes
//NOTE: this stack solution should prevent problems when modifying the various lists //NOTE: this stack solution should prevent problems when modifying the various lists
while(entityStack.size()) { while(entityStack.size()) {
//get the entity & hitbox //get the entity & hitbox
@@ -101,6 +98,12 @@ void RoomData::RunFrame() {
entityStack.pop(); entityStack.pop();
} }
//a list of creatures that need to be updated client-side
std::list<CreatureData*> creatureList;
creatureMgr.Update(&creatureList);
//TODO: (0) send the updates
//TODO: creature/character collisions //TODO: creature/character collisions
} }
@@ -138,6 +141,7 @@ TriggerManager* RoomData::GetTriggerMgr() {
lua_State* RoomData::SetLuaState(lua_State* L) { lua_State* RoomData::SetLuaState(lua_State* L) {
lua = L; lua = L;
creatureMgr.SetLuaState(lua);
pager.SetLuaState(lua); pager.SetLuaState(lua);
triggerMgr.SetLuaState(lua); triggerMgr.SetLuaState(lua);
return lua; return lua;