Monster API clones from Entity API, read more

This is my solution for handling inheritance via lua. The Entity class is
only a base class, so the entity API is designed to be copied from, rather
than used directly.

linit.c: It should be noted that the Entity API must always be placed
before the utilizing child APIs. I don't know about how lua handles things
internally, but I'm assuming that this is the case.

There's no real meat in the API code yet, since that's just busy-work.
Right now I feel beter about writing the connective tissue. This case
could aslo extend to the waypoint and monster APIs.

The waypoint system had some API and class methods removed for brevity.
This commit is contained in:
Kayne Ruse
2015-01-11 19:08:31 +11:00
parent 8ea667a0b5
commit 051ed0f14c
10 changed files with 165 additions and 86 deletions
+44
View File
@@ -23,11 +23,55 @@
#include "monster_data.hpp"
#include "entity_api.hpp"
static int setAvatar(lua_State* L) {
//TODO
}
static int getAvatar(lua_State* L) {
//TODO
}
static int setScript(lua_State* L) {
//TODO
}
static int getScript(lua_State* L) {
//TODO
}
static const luaL_Reg monsterLib[] = {
{"SetAvatar", setAvatar},
{"GetAvatar", getAvatar},
{"SetScript", setScript},
{"GetScript", getScript},
{nullptr, nullptr}
};
LUAMOD_API int openMonsterAPI(lua_State* L) {
//the local table
luaL_newlib(L, monsterLib);
//get the parent table
luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false);
//clone the parent table into the local table
lua_pushnil(L); //first key
while(lua_next(L, -2)) {
//copy the key-value pair
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
//push the copy to the local table
lua_settable(L, -6);
//pop the original value before continuing
lua_pop(L, 1);
}
//remove the parent table, leaving the expanded child table
lua_pop(L, 1);
return 1;
}
+4 -4
View File
@@ -25,14 +25,14 @@ std::string MonsterData::SetAvatar(std::string s) {
return avatar = s;
}
int MonsterData::SetScriptReference(int i) {
return scriptRef = i;
}
std::string MonsterData::GetAvatar() {
return avatar;
}
int MonsterData::SetScriptReference(int i) {
return scriptRef = i;
}
int MonsterData::GetScriptReference() {
return scriptRef;
}
+2 -2
View File
@@ -32,9 +32,9 @@ public:
~MonsterData() = default;
std::string SetAvatar(std::string);
int SetScriptReference(int);
std::string GetAvatar();
int SetScriptReference(int);
int GetScriptReference();
private:
-16
View File
@@ -25,22 +25,10 @@ int MonsterManager::Create(std::string) {
//TODO
}
int MonsterManager::Load(std::string) {
//TODO
}
int MonsterManager::Save(int uid) {
//TODO
}
void MonsterManager::Unload(int uid) {
//TODO
}
void MonsterManager::Delete(int uid) {
//TODO
}
void MonsterManager::UnloadAll() {
//TODO
}
@@ -57,10 +45,6 @@ int MonsterManager::GetLoadedCount() {
//TODO
}
int MonsterManager::GetTotalCount() {
//TODO
}
std::map<int, MonsterData>* MonsterManager::GetContainer() {
//TODO
}
-4
View File
@@ -36,10 +36,7 @@ class MonsterManager: public Singleton<MonsterManager> {
public:
//common public methods
int Create(std::string);
int Load(std::string);
int Save(int uid);
void Unload(int uid);
void Delete(int uid);
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, MonsterData const&>)> fn);
@@ -47,7 +44,6 @@ public:
//accessors & mutators
MonsterData* Get(int uid);
int GetLoadedCount();
int GetTotalCount();
std::map<int, MonsterData>* GetContainer();
//hooks
+55
View File
@@ -0,0 +1,55 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "monster_system_api.hpp"
//all monster API headers
#include "monster_api.hpp"
#include "monster_manager_api.hpp"
//useful "globals"
//...
//This mimics linit.c to create a nested collection of all monster modules.
static const luaL_Reg funcs[] = {
{nullptr, nullptr}
};
static const luaL_Reg libs[] = {
{"Monster", openMonsterAPI},
{"MonsterManager", openMonsterManagerAPI},
{nullptr, nullptr}
};
int openMonsterSystemAPI(lua_State* L) {
//create the table
luaL_newlibtable(L, libs);
//push the "global" functions
luaL_setfuncs(L, funcs, 0);
//push the substable
for (const luaL_Reg* lib = libs; lib->func; lib++) {
lib->func(L);
lua_setfield(L, -2, lib->name);
}
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2013-2015
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef MONSTERSYSTEMAPI_HPP_
#define MONSTERSYSTEMAPI_HPP_
#include "lua.hpp"
#define TORTUGA_MONSTER_SYSTEM_API "monster_system"
LUAMOD_API int openMonsterSystemAPI(lua_State* L);
#endif