aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp8
-rw-r--r--src/inventory.cpp46
-rw-r--r--src/player.cpp6
-rw-r--r--src/world.cpp82
4 files changed, 120 insertions, 22 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index 54181c8..a367e27 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -3,7 +3,7 @@
#include <config.hpp>
#include <world.hpp>
#include <ui.hpp>
-//#include <inventory.hpp>
+#include <inventory.hpp>
#include <window.hpp>
#include <components.hpp>
#include <player.hpp>
@@ -22,11 +22,10 @@ void Engine::init(void) {
systems.add<WindowSystem>();
systems.add<RenderSystem>();
systems.add<InputSystem>();
-// systems.add<InventorySystem>();
+ systems.add<InventorySystem>();
systems.add<WorldSystem>();
systems.add<PlayerSystem>();
systems.add<MovementSystem>();
-// systems.add<PlayerSystem>(&player);
systems.configure();
@@ -37,6 +36,7 @@ void Engine::render(entityx::TimeDelta dt)
{
systems.update<RenderSystem>(dt);
systems.update<WindowSystem>(dt);
+ systems.update<InventorySystem>(dt);
ui::fadeUpdate();
}
@@ -44,8 +44,6 @@ void Engine::render(entityx::TimeDelta dt)
void Engine::update(entityx::TimeDelta dt)
{
systems.update<InputSystem>(dt);
-// systems.update<InventorySystem>(dt);
-// systems.update<PlayerSystem>(dt);
systems.update<MovementSystem>(dt);
systems.update<WorldSystem>(dt);
systems.update<PlayerSystem>(dt);
diff --git a/src/inventory.cpp b/src/inventory.cpp
new file mode 100644
index 0000000..91e81da
--- /dev/null
+++ b/src/inventory.cpp
@@ -0,0 +1,46 @@
+#include <inventory.hpp>
+
+#include <common.hpp>
+#include <events.hpp>
+#include <texture.hpp>
+#include <render.hpp>
+
+constexpr const char* ICON_TEX_FILE_PATH = "config/invIcons.txt";
+
+static std::vector<GLuint> iconTextures;
+
+void InventorySystem::configure(entityx::EventManager &ev)
+{
+ ev.subscribe<KeyDownEvent>(*this);
+}
+
+void InventorySystem::loadIcons(void) {
+ iconTextures.clear();
+ auto icons = readFileA(ICON_TEX_FILE_PATH);
+ for (const auto& s : icons)
+ iconTextures.push_back(Texture::loadTexture(s));
+}
+
+void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
+{
+ (void)en;
+ (void)ev;
+ (void)dt;
+
+ static auto color = Texture::genColor(Color(0, 0, 0));
+ vec2 start = vec2(offset.x, 100);// - game::SCREEN_WIDTH / 2 + 20, game::SCREEN_HEIGHT - 40);
+
+ //std::cout << start.x << ' ' << start.y << std::endl;
+
+ Render::textShader.use();
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, color);
+ Render::useShader(&Render::textShader);
+ Render::drawRect(start, start + 20, -9.9f);
+ Render::textShader.unuse();
+}
+
+void InventorySystem::receive(const KeyDownEvent &kde)
+{
+ (void)kde;
+}
diff --git a/src/player.cpp b/src/player.cpp
index 82de470..bc4ae76 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -142,3 +142,9 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
game::time::tick(50);
}
}
+
+vec2 PlayerSystem::getPosition(void) const
+{
+ auto loc = *game::entities.get(pid).component<Position>().get();
+ return vec2 {loc.x, loc.y};
+}
diff --git a/src/world.cpp b/src/world.cpp
index 508e2ac..78de9ed 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -205,6 +205,14 @@ loadWorldFromXMLNoTakeover(std::string path)
void WorldSystem::load(const std::string& file)
{
+ auto str2coord = [](std::string s) -> vec2 {
+ auto cpos = s.find(',');
+ s[cpos] = '\0';
+ return vec2 (std::stof(s), std::stof(s.substr(cpos + 1)));
+ };
+
+ entityx::Entity entity;
+
std::string xmlRaw;
std::string xmlPath;
@@ -222,6 +230,19 @@ void WorldSystem::load(const std::string& file)
if (xmlDoc.Parse(xmlRaw.data()) != XML_NO_ERROR)
UserError("XML Error: Failed to parse file (not your fault though..?)");
+ // include headers
+ auto ixml = xmlDoc.FirstChildElement("include");
+ while (ixml) {
+ auto file = ixml->Attribute("file");
+ if (file != nullptr) {
+ DEBUG_printf("Including file: %s\n", file);
+ xmlRaw.append(readFile((xmlFolder + file).c_str()));
+ }
+ ixml = ixml->NextSiblingElement();
+ }
+
+ xmlDoc.Parse(xmlRaw.data());
+
// look for an opening world tag
auto wxml = xmlDoc.FirstChildElement("World");
if (wxml != nullptr) {
@@ -238,6 +259,9 @@ void WorldSystem::load(const std::string& file)
}
world.toLeft = world.toRight = "";
+ currentXMLFile = file;
+
+
// iterate through tags
while (wxml) {
@@ -302,25 +326,47 @@ void WorldSystem::load(const std::string& file)
game::time::setTickCount(std::stoi(wxml->GetText()));
}
- else if (tagName == "entity") {
- auto str2coord = [](std::string s) -> vec2 {
- auto cpos = s.find(',');
- s[cpos] = '\0';
- return vec2 (std::stof(s), std::stof(s.substr(cpos + 1)));
- };
-
- auto entity = game::entities.create();
+ // custom entity tags
+ else {
+ auto cxml = xmlDoc.FirstChildElement(tagName.c_str());
+ if (cxml != nullptr) {
+ DEBUG_printf("Using custom tag <%s>\n", tagName.c_str());
+
+ entity = game::entities.create();
+ auto abcd = cxml->FirstChildElement();
+
+ while (abcd) {
+ std::string tname = abcd->Name();
+
+ if (tname == "Position") {
+ vec2 coords;
+
+ if (wxml->Attribute("position") != nullptr) {
+ coords = str2coord(wxml->StrAttribute("position"));
+ } else {
+ coords = str2coord(abcd->StrAttribute("value"));
+ }
+
+ float cdat[2] = {coords.x, coords.y};
+ entity.assign<Position>(cdat[0], cdat[1]);
+ } else if (tname == "Visible") {
+ entity.assign<Visible>(abcd->FloatAttribute("value"));
+ } else if (tname == "Sprite") {
+ auto sprite = entity.assign<Sprite>();
+ auto tex = abcd->Attribute("image");
+ auto dim = Texture::imageDim(tex);
+ sprite->addSpriteSegment(SpriteData(game::sprite_l.loadSprite(tex),
+ vec2(0, 0),
+ vec2(dim.x, dim.y) * 2),
+ vec2(0, 0));
+ }
+
+ abcd = abcd->NextSiblingElement();
+ }
- auto loc = wxml->Attribute("loc");
- if (loc != nullptr) {
- auto locVec = str2coord(loc);
- float locDat[2] = {locVec.x, locVec.y};
- entity.assign<Position>(locDat[0], locDat[1]);
+ } else {
+ UserError("Unknown tag <" + tagName + "> in file " + currentXMLFile);
}
-
- unsigned int health;
- if (wxml->QueryUnsignedAttribute("health", &health) != XML_NO_ERROR)
- entity.assign<Health>(health, health);
}
// hill creation
@@ -330,6 +376,8 @@ void WorldSystem::load(const std::string& file)
wxml = wxml->NextSiblingElement();
}
+
+ game::events.emit<BGMToggleEvent>();
}
/*