From 3a8e247d08c44419e632f575d2c6a919ae42a236 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 12 Oct 2012 21:25:18 +0200 Subject: [PATCH] added directional triggers these are useful e.g. for implementing doors refs #18 --- src/main.cpp | 2 ++ src/map/Map.cpp | 32 ++++++++++++++++++++++++++------ src/map/MapState.cpp | 40 ++++++++++++++++++++++++++-------------- src/map/MapState.h | 5 ++++- src/map/Trigger.cpp | 4 +++- src/map/Trigger.h | 12 ++++++++++++ 6 files changed, 73 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 26a8573..beef2fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -417,6 +417,7 @@ int main(int argc, char **argv) { Trigger triggers1[1]; triggers1[0].SetTilePosition(Vector(8, 3)); + triggers1[0].SetType(Trigger::TYPE_NORTH); SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png")); Sprite tileset(tilesetImg, tileSize, tileSize); @@ -508,6 +509,7 @@ int main(int argc, char **argv) { Trigger triggers2[1]; triggers2[0].SetTilePosition(Vector(6, 2)); + triggers2[0].SetType(Trigger::TYPE_SOUTH); Map map2; map2.SetAreas(areas2, 1); diff --git a/src/map/Map.cpp b/src/map/Map.cpp index a9e11df..be39ec3 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -11,6 +11,7 @@ #include "Tile.h" #include "Trigger.h" #include "../graphics/Sprite.h" +#include "../sdl/utility.h" #include @@ -122,12 +123,31 @@ void Map::RenderDebug(SDL_Surface *dest, const Vector &inOffset) const { } for (int i(0); i < numTriggers; ++i) { Vector offset((triggers[i].TilePosition() * tileset->Size()) + inOffset); - SDL_Rect destRect; - destRect.x = offset.X() + (tileset->Width() / 4); - destRect.y = offset.Y() + (tileset->Height() / 4); - destRect.w = tileset->Width() / 2; - destRect.h = tileset->Height() / 2; - SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + switch (triggers[i].GetType()) { + case Trigger::TYPE_NORTH: + sdl::HorizontalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + break; + case Trigger::TYPE_EAST: + sdl::VerticalLine(dest, offset + Vector(tileset->Width() * 3 / 4, tileset->Height() / 4), tileset->Height() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + break; + case Trigger::TYPE_SOUTH: + sdl::HorizontalLine(dest, offset + Vector(tileset->Width() / 4, tileset->Height() * 3 / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + break; + case Trigger::TYPE_WEST: + sdl::VerticalLine(dest, offset + (tileset->Size() / 4), tileset->Width() / 2, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + break; + case Trigger::TYPE_CONTACT: { + SDL_Rect destRect; + destRect.x = offset.X() + (tileset->Width() / 4); + destRect.y = offset.Y() + (tileset->Height() / 4); + destRect.w = tileset->Width() / 2; + destRect.h = tileset->Height() / 2; + SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0xFF)); + } + break; + case Trigger::TYPE_BUTTON: + break; + } } } diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index c1b3451..a716f95 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -265,7 +265,7 @@ bool MapState::OnGridLock() { return false; } else { LockEntities(); - return CheckMonster() || CheckTrigger(); + return CheckMonster() || CheckLockTrigger(); } } @@ -322,23 +322,17 @@ bool MapState::CheckMonster() { return false; } -bool MapState::CheckTrigger() { +bool MapState::CheckLockTrigger() { Trigger *trigger(map->TriggerAt(Vector(controlled->Position()))); - if (trigger) { - // TODO: run trigger script - if (trigger->map) { - ctrl->PushState(new ColorFade(this, 0, 500, true)); - ctrl->PushState(new TransitionState(this, trigger->map, trigger->target)); - ColorFade *fadeOut(new ColorFade(this, 0, 500, false)); - fadeOut->SetLeadOutTime(500); - ctrl->PushState(fadeOut); - return true; - } - } - return false; + if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false; + RunTrigger(*trigger); + return true; } void MapState::OnMove(bool realMove) { + if (CheckMoveTrigger()) { + return; + } // TODO: evaluate monster movements if (realMove) { UpdateFollower(*controlled); @@ -347,6 +341,24 @@ void MapState::OnMove(bool realMove) { } } +bool MapState::CheckMoveTrigger() { + Trigger *trigger(map->TriggerAt(Vector(controlled->Position()))); + if (!trigger || int(trigger->GetType()) != nextDirection) return false; + RunTrigger(*trigger); + return true; +} + +void MapState::RunTrigger(Trigger &trigger) { + // TODO: run trigger script + if (trigger.map) { + ctrl->PushState(new ColorFade(this, 0, 500, true)); + ctrl->PushState(new TransitionState(this, trigger.map, trigger.target)); + ColorFade *fadeOut(new ColorFade(this, 0, 500, false)); + fadeOut->SetLeadOutTime(500); + ctrl->PushState(fadeOut); + } +} + void MapState::UpdateFollower(Entity &e) { if (!e.Follower()) return; diff --git a/src/map/MapState.h b/src/map/MapState.h index 0ca5a84..bf19f4d 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -63,7 +63,10 @@ private: void LockEntities(); bool CheckMonster(); - bool CheckTrigger(); + + bool CheckLockTrigger(); + bool CheckMoveTrigger(); + void RunTrigger(Trigger &); private: common::GameConfig *game; diff --git a/src/map/Trigger.cpp b/src/map/Trigger.cpp index 4f370d9..3b91511 100644 --- a/src/map/Trigger.cpp +++ b/src/map/Trigger.cpp @@ -9,7 +9,9 @@ namespace map { -Trigger::Trigger() { +Trigger::Trigger() +: type(TYPE_CONTACT) +, map(0) { } diff --git a/src/map/Trigger.h b/src/map/Trigger.h index 47f933b..222f813 100644 --- a/src/map/Trigger.h +++ b/src/map/Trigger.h @@ -8,6 +8,7 @@ #ifndef MAP_TRIGGER_H_ #define MAP_TRIGGER_H_ +#include "Entity.h" #include "fwd.h" #include "../geometry/Vector.h" @@ -19,15 +20,26 @@ public: Trigger(); ~Trigger() { } + enum Type { + TYPE_NORTH = Entity::ORIENTATION_NORTH, + TYPE_EAST = Entity::ORIENTATION_EAST, + TYPE_SOUTH = Entity::ORIENTATION_SOUTH, + TYPE_WEST = Entity::ORIENTATION_WEST, + TYPE_CONTACT, + }; + public: const geometry::Vector &TilePosition() const { return tilePosition; } + Type GetType() const { return type; } // temporary setters public: void SetTilePosition(const geometry::Vector &p) { tilePosition = p; } + void SetType(Type t) { type = t; } private: geometry::Vector tilePosition; + Type type; // temporary members until scripting is implemented public: -- 2.39.2