*/
Solid(float w = 0.0f, float h = 0.0f): width(w), height(h) {offset = 0.0f; passable = true; }
//Solid(float w = 0.0f, float h = 0.0f, vec2 offset = 0.0f): width(w), height(h), offset(offset) {passable = true; }
-
+
void Passable(bool v) {passable = v;}
bool Passable(void) {return passable;}
};
struct SpriteData {
-
- SpriteData(std::string path, vec2 offset):
+
+ SpriteData(std::string path, vec2 offset):
offset(offset) {
pic = Texture::loadTexture(path);
size = Texture::imageDim(path);
}
-
+
GLuint pic;
vec2 offset;
vec2 size;
vec2 getSpriteSize() {
vec2 st; /** the start location of the sprite */
vec2 dim; /** how wide the sprite is */
-
+
if (sprite.size()) {
st.x = sprite[0].second.x;
st.y = sprite[0].second.y;
int rindex;
};
+
+
+// movement styles
+
+/**
+ * Causes the entity to hop around.
+ */
+struct Hop {}; // TODO require wander, for range?
+
+/**
+ * Causes the entity to wander about.
+ */
+struct Wander {
+ Wander(float ix = 0, float r = 0)
+ : initialX(ix), range(r), countdown(0) {}
+
+ float initialX;
+ float range;
+ int countdown;
+};
+
+
/**
* SYSTEMS
*/
ev.subscribe<BGMToggleEvent>(*this);
}
- inline XMLDocument* getXML(void)
+ inline XMLDocument* getXML(void)
{ return &xmlDoc; }
inline float getWidth(void) const
void addHole(const unsigned int& start, const unsigned int& end);
void addHill(const ivec2& peak, const unsigned int& width);
- bool save(const std::string& file);
+ bool save(void);
void load(const std::string& file);
};
Texture::freeTextures();
// close up the game stuff
-// currentWorld->save();
+ game::engine.getSystem<WorldSystem>()->save();
game::engine.getSystem<WindowSystem>()->die();
{
(void)ev;
en.each<Position, Direction>([dt](entityx::Entity entity, Position &position, Direction &direction) {
- (void)entity;
position.x += direction.x * dt;
position.y += direction.y * dt;
+
+ if (entity.has_component<Sprite>()) {
+ auto& fl = entity.component<Sprite>()->faceLeft;
+ if (direction.x != 0)
+ fl = (direction.x < 0);
+ }
+
+ if (entity.has_component<Wander>()) {
+ auto& countdown = entity.component<Wander>()->countdown;
+
+ if (countdown > 0) {
+ countdown--;
+ } else {
+ countdown = 5000 + randGet() % 10 * 100;
+ direction.x = (randGet() % 3 - 1) * 0.02f;
+ }
+ }
});
}
(void)ev;
en.each<Direction, Physics>([dt](entityx::Entity entity, Direction &direction, Physics &physics) {
(void)entity;
- // TODO GET GRAVITY FROM WOLRD
+ // TODO GET GRAVITY FROM WORLD
direction.y += physics.g * dt;
});
}
(void)ev;
(void)dt;
}
-
auto kc = kde.keycode;
auto& loc = *player.component<Position>().get();
auto& vel = *player.component<Direction>().get();
- auto& faceLeft = player.component<Sprite>().get()->faceLeft;
if ((kc == SDLK_SPACE) && game::canJump && ((vel.y > -0.01) & (vel.y < 0.01))) {
loc.y += HLINES(2);
} else if (kc == getControl(1)) {
if (!ui::fadeEnable) {
- moveLeft = faceLeft = true;
+ moveLeft = true;
moveRight = false;
worldSystem.goWorldLeft(loc);
}
} else if (kc == getControl(2)) {
if (!ui::fadeEnable) {
- moveLeft = faceLeft = false;
+ moveLeft = false;
moveRight = true;
worldSystem.goWorldRight(loc, *player.component<Solid>().get());
static Color ambient;
-bool WorldSystem::save(const std::string& s)
+bool WorldSystem::save(void)
{
- (void)s;
- /*for (const auto &e : entity)
- e->saveToXML();
+ std::ofstream save (xmlFolder + currentXMLFile + ".dat");
- currentXMLDoc.SaveFile((s.empty() ? currentXML : xmlFolder + s).c_str(), false);*/
+ // signature?
+ save << "831998 ";
+
+ game::entities.each<Position>([&](entityx::Entity entity, Position& pos) {
+ // save position
+ save << "p " << pos.x << ' ' << pos.y << ' ';
+
+ // save dialog, if exists
+ if (entity.has_component<Dialog>())
+ save << "d " << entity.component<Dialog>()->index << ' ';
+ });
+
+ save.close();
return false;
}
entity.assign<Dialog>((wxml->BoolAttribute("hasDialog") ? 0 : 9999));
} else if (tname == "Grounded") {
entity.assign<Grounded>();
+ } else if (tname == "Wander") {
+ entity.assign<Wander>();
}
abcd = abcd->NextSiblingElement();
wxml = wxml->NextSiblingElement();
}
+ // attempt to load data
+ std::ifstream save (xmlFolder + currentXMLFile + ".dat");
+ if (save.good()) {
+ // check signature
+ int signature;
+ save >> signature;
+ if (signature != 831998)
+ UserError("Save file signature is invalid... (delete it)");
+
+ char id;
+ save >> id;
+
+ entityx::ComponentHandle<Position> pos;
+ for (entityx::Entity entity : game::entities.entities_with_components(pos)) {
+ save >> pos->x >> pos->y;
+ save >> id;
+
+ while (id != 'p') {
+ switch (id) {
+ case 'd':
+ save >> entity.component<Dialog>()->index;
+ break;
+ }
+
+ save >> id;
+ }
+ }
+
+ save.close();
+ }
+
game::events.emit<BGMToggleEvent>();
}
void WorldSystem::goWorldRight(Position& p, Solid &d)
{
- if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(15))) {
+ if (!(world.toRight.empty()) && (p.x + d.width > world.startX * -1 - HLINES(5))) {
ui::toggleBlack();
ui::waitForCover();
auto file = world.toRight;
load(file);
- //game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(15));
+ game::engine.getSystem<PlayerSystem>()->setX(world.startX + HLINES(10));
ui::toggleBlack();
}
}
ui::toggleBlack();
ui::waitForCover();
load(world.toLeft);
- //game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15));
+ game::engine.getSystem<PlayerSystem>()->setX(world.startX * -1 - HLINES(15));
ui::toggleBlack();
}
}
<Physics />
<Name value="Daddy" />
<Dialog />
+ <Wander />
</npc>
<structure>