Initial Commit
This commit is contained in:
172
src/game/game.cpp
Normal file
172
src/game/game.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "ifilesystem.h"
|
||||
#include "core.h"
|
||||
#include "log.h"
|
||||
#include "game.h"
|
||||
#include "game_lua_help.h"
|
||||
#include "ientity.h"
|
||||
#include "entitymanager.h"
|
||||
#include "world.h"
|
||||
|
||||
#include <pugixml.hpp>
|
||||
|
||||
static Game s_game;
|
||||
Game* g_game = &s_game;
|
||||
|
||||
void consoleMsg(const char* msg)
|
||||
{
|
||||
Logger::Msg("%s", msg);
|
||||
}
|
||||
|
||||
void engineError(const char* msg)
|
||||
{
|
||||
Core::Error("%s", msg);
|
||||
}
|
||||
|
||||
void engineWarning(const char* msg)
|
||||
{
|
||||
Core::Warning("%s", msg);
|
||||
}
|
||||
|
||||
LuaPlus::LuaObject engineCreateEntity(const char* classname)
|
||||
{
|
||||
LuaPlus::LuaObject entityObject;
|
||||
entityObject.AssignNewTable(GetLuaState());
|
||||
|
||||
//GameObject* entity = (GameObject*)g_entityManager->createEntity(classname);
|
||||
//entity->init_from_lua(entityObject);
|
||||
//entity->init();
|
||||
|
||||
return entityObject;
|
||||
}
|
||||
|
||||
void engineAddEntityToWorld(LuaPlus::LuaObject& object)
|
||||
{
|
||||
LuaPlus::LuaObject cppclass = object["__cpp_entity"];
|
||||
|
||||
// todo
|
||||
}
|
||||
|
||||
void registerEngine()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
|
||||
// register engine functions
|
||||
LuaObject engineTable = GetLuaState().GetGlobals().CreateTable("engine");
|
||||
engineTable.RegisterDirect("error", &engineError);
|
||||
engineTable.RegisterDirect("warning", &engineWarning);
|
||||
engineTable.RegisterDirect("create_entity", &engineCreateEntity);
|
||||
engineTable.RegisterDirect("add_entity_to_world", &engineAddEntityToWorld);
|
||||
|
||||
LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console");
|
||||
consoleTable.RegisterDirect("print", &consoleMsg);
|
||||
}
|
||||
|
||||
void registerClasses()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
|
||||
// base thing
|
||||
GetLuaState().GetGlobals().RegisterDirect("load_script", &luaLoadScript);
|
||||
|
||||
registerEngine();
|
||||
}
|
||||
|
||||
void initializeEntityPrototypesFromLua()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
LuaObject entitiesTable = GetLuaState().GetGlobal("g_entity_table");
|
||||
|
||||
for (LuaTableIterator it(entitiesTable); it; it.Next()) {
|
||||
LuaObject entityTable = it.GetValue();
|
||||
assert(entityTable.IsTable());
|
||||
|
||||
for (LuaTableIterator it2(entityTable); it2; it2.Next()) {
|
||||
Logger::Msg("%s", it2.GetValue().ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initializeEntitiesTable()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
|
||||
LuaObject entitiesTable = GetLuaState().GetGlobal("g_entity_table");
|
||||
if (entitiesTable.IsNil()) {
|
||||
Core::Error("initializeEntitiesTable: failed initialize the entity table! g_entity_table is nil");
|
||||
}
|
||||
|
||||
if (!entitiesTable.IsTable()) {
|
||||
Core::Error("initializeEntitiesTable: failed initialize the entity table! g_entity_table is not a table");
|
||||
}
|
||||
|
||||
initializeEntityPrototypesFromLua();
|
||||
}
|
||||
|
||||
void initializeLua()
|
||||
{
|
||||
using namespace LuaPlus;
|
||||
|
||||
// register base classes
|
||||
registerClasses();
|
||||
|
||||
// load game base script
|
||||
luaLoadScript("game_init.lua");
|
||||
}
|
||||
|
||||
void Game::Init()
|
||||
{
|
||||
initializeLua();
|
||||
initializeEntitiesTable();
|
||||
}
|
||||
|
||||
void Game::InitForNewMap(const char* mapname)
|
||||
{
|
||||
LoadLevelXML(mapname);
|
||||
}
|
||||
|
||||
void Game::LoadLevelXML(const char* mapname)
|
||||
{
|
||||
// Load XML file of the level
|
||||
char buffer[kMaxPathLength];
|
||||
snprintf(buffer, kMaxPathLength, "data/levels/%s/%s.xml", mapname, mapname);
|
||||
|
||||
FileHandle_t file = GetFileSystem()->OpenFile(buffer, "rb");
|
||||
SDL_assert_always(file != kInvalidFileHandleValue);
|
||||
|
||||
size_t length = GetFileSystem()->GetFileLength(file);
|
||||
|
||||
char* filedata = new char[length + 1];
|
||||
GetFileSystem()->ReadFile(file, filedata, length);
|
||||
filedata[length] = '\0';
|
||||
|
||||
GetFileSystem()->CloseFile(file);
|
||||
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_parse_result result = doc.load_buffer(filedata, length);
|
||||
delete[] filedata;
|
||||
if (!result) {
|
||||
Core::Error("Game::LoadLevelXML: Error while reading level description file '%s'\nError: %s:%i",
|
||||
buffer, result.description(), result.offset);
|
||||
}
|
||||
|
||||
for (pugi::xml_node entity : doc.child("Entities").children("Entity")) {
|
||||
pugi::xml_attribute entityname = entity.attribute("name");
|
||||
pugi::xml_attribute classname = entity.attribute("classname");
|
||||
|
||||
if (classname.empty()) {
|
||||
if (!entityname.empty()) {
|
||||
Core::Error(" Game::LoadLevelXML: Classname is not specified for entity '%s'", entityname.as_string());
|
||||
}
|
||||
else {
|
||||
Core::Error(" Game::LoadLevelXML: Classname is not specified for entity");
|
||||
}
|
||||
}
|
||||
|
||||
IEntityBase* entity = g_entityManager->CreateEntity(classname.as_string());
|
||||
g_world->AddEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
void Game::Shutdown()
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user