The rabbit is moving on it's own

This commit is contained in:
2016-03-29 20:19:13 +11:00
parent a532d33579
commit 4794965166
8 changed files with 78 additions and 5 deletions
+3
View File
@@ -779,6 +779,7 @@ void World::hCharacterMovement(CharacterPacket* const argPacket) {
//------------------------- //-------------------------
void World::hCreatureUpdate(CreaturePacket* const argPacket) { void World::hCreatureUpdate(CreaturePacket* const argPacket) {
std::cout << "hCreatureUpdate" << std::endl;
//TODO: (1) Authentication //TODO: (1) Authentication
//check that this character exists //check that this character exists
@@ -868,6 +869,8 @@ void World::hQueryCreatureExists(CreaturePacket* const argPacket) {
} }
void World::hCreatureMovement(CreaturePacket* const argPacket) { void World::hCreatureMovement(CreaturePacket* const argPacket) {
std::cout << "hCreatureMovement" << std::endl;
//ignore if this creature doesn't exist //ignore if this creature doesn't exist
std::map<int, BaseCreature>::iterator creatureIt = creatureMap.find(argPacket->creatureIndex); std::map<int, BaseCreature>::iterator creatureIt = creatureMap.find(argPacket->creatureIndex);
if (creatureIt == creatureMap.end()) { if (creatureIt == creatureMap.end()) {
+39 -1
View File
@@ -38,11 +38,49 @@ doorUtility = require("door_utility")
creatureAPI = require("creature") creatureAPI = require("creature")
creatureManagerAPI = require("creature_manager") creatureManagerAPI = require("creature_manager")
--testing creature tags
local function bunnySquare(creature)
--return value
local ret = 0
--these tags are strings
local direction = creatureAPI.GetTag(creature, "direction")
local timestamp = creatureAPI.GetTag(creature, "timestamp")
--has this creature been set up yet?
if string.len(direction) == 0 then
direction = "south"
timestamp = tostring(os.time())
creatureAPI.SetMotion(creature, 0, 1)
end
--is it time to change direction?
if os.time() - tonumber(timestamp) > 3 then
-- print("changing directions")
if string.match("south", direction) then
direction = "north"
creatureAPI.SetMotion(creature, 0, -1)
else
direction = "south"
creatureAPI.SetMotion(creature, 0, 1)
end
timestamp = tostring(os.time())
ret = 1
end
creatureAPI.SetTag(creature, "direction", direction)
creatureAPI.SetTag(creature, "timestamp", timestamp)
return ret
end
--test the room hooks --test the room hooks
roomManagerAPI.SetOnCreate(function(room, index) roomManagerAPI.SetOnCreate(function(room, index)
print("", "Creating room: ", roomAPI.GetName(room), index) print("", "Creating room: ", roomAPI.GetName(room), index)
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", function() --[[]] end) creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
roomAPI.SetOnTick(room, function(room) roomAPI.SetOnTick(room, function(room)
roomAPI.ForEachCharacter(room, function(character) roomAPI.ForEachCharacter(room, function(character)
+15
View File
@@ -52,11 +52,26 @@ static int getScript(lua_State* L) {
return 1; return 1;
} }
//TODO: autogen docs
static int setTag(lua_State* L) {
CreatureData* creature = static_cast<CreatureData*>(lua_touserdata(L, 1));
creature->SetTag(lua_tostring(L, 2), lua_tostring(L, 3));
return 0;
}
static int getTag(lua_State* L) {
CreatureData* creature = static_cast<CreatureData*>(lua_touserdata(L, 1));
lua_pushstring(L, creature->GetTag(lua_tostring(L, 2)).c_str());
return 1;
}
static const luaL_Reg creatureLib[] = { static const luaL_Reg creatureLib[] = {
{"SetAvatar", setAvatar}, {"SetAvatar", setAvatar},
{"GetAvatar", getAvatar}, {"GetAvatar", getAvatar},
{"SetScript", setScript}, {"SetScript", setScript},
{"GetScript", getScript}, {"GetScript", getScript},
{"SetTag", setTag},
{"GetTag", getTag},
{nullptr, nullptr} {nullptr, nullptr}
}; };
+9 -1
View File
@@ -52,7 +52,7 @@ int CreatureData::Update(lua_State* L) {
ret += lua_tonumber(L, -1); ret += lua_tonumber(L, -1);
} }
ret += Entity::Update(); Entity::Update();
return ret; return ret;
} }
@@ -76,3 +76,11 @@ int CreatureData::SetScriptReference(int i) {
int CreatureData::GetScriptReference() { int CreatureData::GetScriptReference() {
return scriptRef; return scriptRef;
} }
std::string CreatureData::SetTag(std::string key, std::string value) {
return tags[key] = value;
}
std::string CreatureData::GetTag(std::string key) {
return tags[key];
}
+5
View File
@@ -25,6 +25,7 @@
#include "lua.hpp" #include "lua.hpp"
#include <map>
#include <string> #include <string>
/* DOCS: Creature attributes, read more /* DOCS: Creature attributes, read more
@@ -49,9 +50,13 @@ public:
int SetScriptReference(int); int SetScriptReference(int);
int GetScriptReference(); int GetScriptReference();
std::string SetTag(std::string key, std::string value);
std::string GetTag(std::string key);
private: private:
friend class CreatureManager; friend class CreatureManager;
std::string avatar; std::string avatar;
int scriptRef = LUA_NOREF; int scriptRef = LUA_NOREF;
std::map<std::string, std::string> tags;
}; };
+1 -1
View File
@@ -30,7 +30,7 @@ CreatureManager::~CreatureManager() {
} }
//arg: a list of creatures to be updated in the clients //arg: a list of creatures to be updated in the clients
int CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList) { void CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList) {
int ret; int ret;
for (auto& it : elementMap) { for (auto& it : elementMap) {
ret = it.second.Update(lua); ret = it.second.Update(lua);
+1 -1
View File
@@ -37,7 +37,7 @@ public:
~CreatureManager(); ~CreatureManager();
//common public methods //common public methods
int Update(std::list<std::pair<const int, CreatureData*>>* creatureList); void Update(std::list<std::pair<const int, CreatureData*>>* creatureList);
int Create(std::string avatar, int scriptRef); int Create(std::string avatar, int scriptRef);
void Unload(int uid); void Unload(int uid);
+5 -1
View File
@@ -25,8 +25,12 @@
//args: mgr, avatar, script //args: mgr, avatar, script
static int create(lua_State* L) { static int create(lua_State* L) {
//register the function at the top of the stack
lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
//create the actual creature
CreatureManager* mgr = static_cast<CreatureManager* const>(lua_touserdata(L, 1)); CreatureManager* mgr = static_cast<CreatureManager* const>(lua_touserdata(L, 1));
int index = mgr->Create(lua_tostring(L, 2), lua_tointeger(L, 3)); int index = mgr->Create(lua_tostring(L, 2), lua_tointeger(L, 3)); //3 should be the unique reference within LUA_REGISTRYINDEX
CreatureData* creature = mgr->Get(index); CreatureData* creature = mgr->Get(index);
lua_pushlightuserdata(L, static_cast<void*>(creature)); lua_pushlightuserdata(L, static_cast<void*>(creature));
lua_pushinteger(L, index); lua_pushinteger(L, index);