From: Daniel Karbach Date: Wed, 3 Oct 2012 13:09:02 +0000 (+0200) Subject: trigger handling in map state X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c055b357c50dd871b49c38191a9d867ff42ea420;p=l2e.git trigger handling in map state --- diff --git a/src/main.cpp b/src/main.cpp index aa20890..20eb55b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ #include "map/Map.h" #include "map/MapState.h" #include "map/Tile.h" +#include "map/Trigger.h" #include "sdl/InitImage.h" #include "sdl/InitScreen.h" #include "sdl/InitSDL.h" @@ -79,6 +80,7 @@ using map::Entity; using map::Map; using map::MapState; using map::Tile; +using map::Trigger; using sdl::InitImage; using sdl::InitScreen; using sdl::InitSDL; @@ -311,16 +313,20 @@ int main(int argc, char **argv) { tiles[62].SetOffset(Vector(1, 1)); tiles[63].SetOffset(Vector(1, 1)); - Area area; - area.SetTiles(tiles, 64); - area.SetWidth(8); + Area areas[1]; + areas[0].SetTiles(tiles, 64); + areas[0].SetWidth(8); + + Trigger triggers[1]; + triggers[0].SetTilePosition(Vector(2, 0)); SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png")); Sprite tileset(tilesetImg, tileSize, tileSize); Map map; - map.SetAreas(&area, 1); + map.SetAreas(areas, 1); map.SetTileset(&tileset); + map.SetTriggers(triggers, 1); map.SetWidth(1); SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png")); diff --git a/src/map/Map.cpp b/src/map/Map.cpp index 2e05e8b..7712a02 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -8,6 +8,7 @@ #include "Map.h" #include "Area.h" +#include "Trigger.h" #include "../graphics/Sprite.h" #include @@ -20,6 +21,8 @@ Map::Map() : tileset(0) , areas(0) , numAreas(0) +, triggers(0) +, numTriggers(0) , width(0) { } @@ -43,6 +46,17 @@ const Tile &Map::TileAt(const Vector &offset) const { return area.TileAt(tileOffset); } +Trigger *Map::TriggerAt(const geometry::Vector &offset) { + // TODO: add support for multiple triggers on a tile? + Vector tilePosition(offset.X() / tileset->Width(), offset.Y() / tileset->Height()); + for (Trigger *i(triggers); i != triggers + numTriggers; ++i) { + if (i->TilePosition() == tilePosition) { + return i; + } + } + return 0; +} + void Map::Render(SDL_Surface *dest, const Vector &inOffset) const { // TODO: skip invisible areas diff --git a/src/map/Map.h b/src/map/Map.h index 340c76c..dd0185c 100644 --- a/src/map/Map.h +++ b/src/map/Map.h @@ -26,19 +26,23 @@ public: const graphics::Sprite *Tileset() const { return tileset; } const Area &AreaAt(const geometry::Vector &) const; const Tile &TileAt(const geometry::Vector &) const; + Trigger *TriggerAt(const geometry::Vector &); void Render(SDL_Surface *dest, const geometry::Vector &offset) const; // temporary setters public: void SetTileset(const graphics::Sprite *t) { tileset = t; } - void SetAreas(const Area *a, int num) { areas = a; numAreas = num; } + void SetAreas(Area *a, int num) { areas = a; numAreas = num; } + void SetTriggers(Trigger *t, int num) { triggers = t; numTriggers = num; } void SetWidth(int w) { width = w; } private: const graphics::Sprite *tileset; - const Area *areas; + Area *areas; int numAreas; + Trigger *triggers; + int numTriggers; int width; }; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 8d9b278..3be5a45 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -20,7 +20,7 @@ using geometry::Vector; namespace map { -MapState::MapState(const Map *map) +MapState::MapState(Map *map) : map(map) , controlled(0) , tempTarget(20, 20) @@ -120,7 +120,11 @@ void MapState::UpdateWorld(float deltaT) { } void MapState::OnGridLock() { - // TODO: check for adjacent monsters and triggers on the current tile + Trigger *trigger(map->TriggerAt(Vector(controlled->Position()))); + if (trigger) { + // TODO: run trigger + } + // TODO: check for adjacent monsters // TODO: force all entities into their grid positions? } diff --git a/src/map/MapState.h b/src/map/MapState.h index 243325c..2b53069 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -22,7 +22,7 @@ class MapState : public app::State { public: - explicit MapState(const Map *); + explicit MapState(Map *); virtual ~MapState() { } public: @@ -48,7 +48,7 @@ private: void OnMove(); private: - const Map *map; + Map *map; Entity *controlled; geometry::Vector tempTarget; geometry::Vector lastLock;