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