Finally wrote the monster manager API
This commit is contained in:
@@ -27,6 +27,8 @@ print("Lua script check")
|
||||
--requirements
|
||||
roomManagerAPI = require("room_manager")
|
||||
roomAPI = require("room")
|
||||
monsterManagerAPI = require("monster_manager")
|
||||
monsterAPI = require("monster")
|
||||
|
||||
mapMaker = require("map_maker")
|
||||
mapSaver = require("map_saver")
|
||||
@@ -43,7 +45,7 @@ roomManagerAPI.SetOnCreate(function(room, index)
|
||||
end)
|
||||
|
||||
roomAPI.ForEachMonster(room, function(monster)
|
||||
--
|
||||
--TODO: move ForEachMonster to the monster manager API
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
@@ -62,4 +64,15 @@ roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrass
|
||||
--call the monstrosity
|
||||
doorUtility.createDoorPair("pair 1", overworld, -64, -64, underworld, 64, 64)
|
||||
|
||||
--testing the monster creation
|
||||
print("testing monsters")
|
||||
local monsterMgr = roomAPI.GetMonsterMgr(overworld)
|
||||
|
||||
local monster, mIndex = monsterManagerAPI.Create(monsterMgr, "skume.bmp", 0)
|
||||
|
||||
print("Monster count: ", monsterManagerAPI.GetLoadedCount(monsterMgr))
|
||||
print("Monster avatar: ", monsterAPI.GetAvatar(monster), mIndex)
|
||||
|
||||
monsterManagerAPI.UnloadAll(monsterMgr)
|
||||
|
||||
print("Finished the lua script")
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
const char* GetType() const;
|
||||
|
||||
protected:
|
||||
Entity(const char*);
|
||||
Entity(const char* type);
|
||||
virtual ~Entity() = default;
|
||||
|
||||
int roomIndex = -1;
|
||||
|
||||
@@ -21,10 +21,23 @@
|
||||
*/
|
||||
#include "monster_data.hpp"
|
||||
|
||||
MonsterData::MonsterData(): Entity("monster") {
|
||||
MonsterData::MonsterData(std::string _avatar, int _scriptRef):
|
||||
Entity("monster"),
|
||||
avatar(_avatar),
|
||||
scriptRef(_scriptRef)
|
||||
{
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void MonsterData::Update() {
|
||||
Entity::Update();
|
||||
//TODO: (0) call the script reference
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//accessors & mutators
|
||||
//-------------------------
|
||||
|
||||
std::string MonsterData::SetAvatar(std::string s) {
|
||||
return avatar = s;
|
||||
}
|
||||
|
||||
@@ -28,11 +28,22 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
/* DOCS: Monster attributes, read more
|
||||
* species (avatar, script)
|
||||
* level
|
||||
* health/mana
|
||||
* permadeath/respawn
|
||||
*/
|
||||
|
||||
class MonsterData: public Entity {
|
||||
public:
|
||||
MonsterData();
|
||||
MonsterData(std::string avatar, int scriptRef);
|
||||
~MonsterData() = default;
|
||||
|
||||
virtual void Update();
|
||||
|
||||
//accessors & mutators
|
||||
|
||||
std::string SetAvatar(std::string);
|
||||
std::string GetAvatar();
|
||||
|
||||
|
||||
@@ -29,10 +29,16 @@ MonsterManager::~MonsterManager() {
|
||||
UnloadAll();
|
||||
}
|
||||
|
||||
int MonsterManager::Create(std::string s) {
|
||||
//TODO: (1) MonsterManager::Create()
|
||||
int MonsterManager::Create(std::string avatar, int scriptRef) {
|
||||
//implicitly create the new
|
||||
elementMap.emplace(counter, MonsterData(avatar, scriptRef));
|
||||
|
||||
//TODO: do various things like saving to the database
|
||||
return counter++;
|
||||
}
|
||||
|
||||
//TODO: (1) monster load, save
|
||||
|
||||
void MonsterManager::Unload(int uid) {
|
||||
elementMap.erase(uid);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
~MonsterManager();
|
||||
|
||||
//common public methods
|
||||
int Create(std::string);
|
||||
int Create(std::string avatar, int scriptRef);
|
||||
void Unload(int uid);
|
||||
|
||||
void UnloadAll();
|
||||
@@ -57,6 +57,7 @@ public:
|
||||
private:
|
||||
//members
|
||||
std::map<int, MonsterData> elementMap;
|
||||
int counter = 0;
|
||||
lua_State* lua = nullptr;
|
||||
sqlite3* database = nullptr;
|
||||
};
|
||||
|
||||
@@ -23,7 +23,54 @@
|
||||
|
||||
#include "monster_manager.hpp"
|
||||
|
||||
static int create(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
int index = mgr->Create(lua_tostring(L, 2), lua_tointeger(L, 3));
|
||||
MonsterData* monster = mgr->Get(index);
|
||||
lua_pushlightuserdata(L, static_cast<void*>(monster));
|
||||
lua_pushinteger(L, index);
|
||||
return 2;
|
||||
}
|
||||
|
||||
//TOOD: this needs to take the userdata as a parameter too
|
||||
static int unload(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
mgr->Unload(lua_tointeger(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unloadAll(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
mgr->UnloadAll();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unloadIf(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
//TODO: unloadIf
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
MonsterData* monster = mgr->Get(lua_tointeger(L, 2));
|
||||
lua_pushlightuserdata(L, static_cast<void*>(monster));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getLoadedCount(lua_State* L) {
|
||||
MonsterManager* mgr = static_cast<MonsterManager* const>(lua_touserdata(L, 1));
|
||||
lua_pushinteger(L, mgr->GetLoadedCount());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg monsterManagerLib[] = {
|
||||
{"Create", create},
|
||||
{"Unload", unload},
|
||||
{"UnloadAll", unloadAll},
|
||||
// {"UnloadIf", unloadIf},
|
||||
{"Get", get},
|
||||
{"GetLoadedCount", getLoadedCount},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user