]> git.localhorst.tv Git - l2e.git/commitdiff
closed the gap between battle and map state (yay)
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Oct 2012 20:43:37 +0000 (22:43 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Oct 2012 20:43:37 +0000 (22:43 +0200)
also introduces a GameConfig struct that should hold all global game data

21 files changed:
Debug/src/common/subdir.mk
Release/src/common/subdir.mk
src/battle/BattleState.cpp
src/battle/BattleState.h
src/battle/Resources.cpp
src/battle/Resources.h
src/common/GameConfig.cpp [new file with mode: 0644]
src/common/GameConfig.h [new file with mode: 0644]
src/common/GameState.h
src/common/fwd.h
src/main.cpp
src/map/Area.cpp
src/map/Area.h
src/map/Entity.cpp
src/map/Entity.h
src/map/Map.cpp
src/map/Map.h
src/map/MapState.cpp
src/map/MapState.h
src/map/Tile.cpp
src/map/Tile.h

index 78f304e6a0469de97ced971803e7a932f64417df..e0e6bd3e3b2b4718b9ed2a1048210497167a6b39 100644 (file)
@@ -4,6 +4,7 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/common/GameConfig.cpp \
 ../src/common/GameState.cpp \
 ../src/common/Hero.cpp \
 ../src/common/Ikari.cpp \
@@ -14,6 +15,7 @@ CPP_SRCS += \
 ../src/common/TargetingMode.cpp 
 
 OBJS += \
+./src/common/GameConfig.o \
 ./src/common/GameState.o \
 ./src/common/Hero.o \
 ./src/common/Ikari.o \
@@ -24,6 +26,7 @@ OBJS += \
 ./src/common/TargetingMode.o 
 
 CPP_DEPS += \
+./src/common/GameConfig.d \
 ./src/common/GameState.d \
 ./src/common/Hero.d \
 ./src/common/Ikari.d \
index 910092d940afffb27209b14d61b2a4111f943f55..cc64cb3866efa83e5b99f40152965aeb11d0a706 100644 (file)
@@ -4,6 +4,7 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/common/GameConfig.cpp \
 ../src/common/GameState.cpp \
 ../src/common/Hero.cpp \
 ../src/common/Ikari.cpp \
@@ -14,6 +15,7 @@ CPP_SRCS += \
 ../src/common/TargetingMode.cpp 
 
 OBJS += \
+./src/common/GameConfig.o \
 ./src/common/GameState.o \
 ./src/common/Hero.o \
 ./src/common/Ikari.o \
@@ -24,6 +26,7 @@ OBJS += \
 ./src/common/TargetingMode.o 
 
 CPP_DEPS += \
+./src/common/GameConfig.d \
 ./src/common/GameState.d \
 ./src/common/Hero.d \
 ./src/common/Ikari.d \
index d309502301f98213036ff0ca11a6b6e9fd799f54..dcd824e32243e3a51504ca1a9b286ad49dda4094 100644 (file)
@@ -12,6 +12,7 @@
 #include "states/PerformAttacks.h"
 #include "../app/Application.h"
 #include "../app/Input.h"
+#include "../common/GameState.h"
 #include "../common/Ikari.h"
 #include "../common/Inventory.h"
 #include "../common/Item.h"
@@ -115,7 +116,7 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) {
 }
 
 void BattleState::LoadInventory() {
-       const Inventory &inv(*res->inventory);
+       const Inventory &inv(game->state->inventory);
        itemMenu.Clear();
        itemMenu.Reserve(inv.MaxItems());
        for (int i(0); i < inv.MaxItems(); ++i) {
index e002b7c026d8c33816d5bfadf5f213b9606fadf3..60c24bc87aef33faf4537d961d82ea8cf05bc199 100644 (file)
@@ -18,6 +18,7 @@
 #include "SmallHeroTag.h"
 #include "../app/fwd.h"
 #include "../app/State.h"
+#include "../common/GameConfig.h"
 #include "../common/fwd.h"
 #include "../common/Stats.h"
 #include "../geometry/Vector.h"
@@ -35,11 +36,12 @@ class BattleState
 : public app::State {
 
 public:
-       BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
-       : background(background)
-       , monstersLayout(&monstersLayout)
-       , heroesLayout(&heroesLayout)
-       , res(res)
+       BattleState(common::GameConfig *game, SDL_Surface *background, const PartyLayout *monstersLayout)
+       : game(game)
+       , background(background)
+       , monstersLayout(monstersLayout)
+       , heroesLayout(game->heroesLayout)
+       , res(game->battleResources)
        , attackTypeMenu(res->attackIcons)
        , moveMenu(res->moveIcons)
        , numHeroes(0)
@@ -47,7 +49,7 @@ public:
        , attackCursor(-1)
        , expReward(0)
        , goldReward(0)
-       , ranAway(false) { assert(background && res); }
+       , ranAway(false) { assert(background && monstersLayout && game); }
 
 public:
        void AddMonster(const Monster &);
@@ -143,6 +145,7 @@ private:
        Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
 
 private:
+       common::GameConfig *game;
        SDL_Surface *background;
        const PartyLayout *monstersLayout;
        const PartyLayout *heroesLayout;
index bb0c0adfbc272919ed239149e5cd406f6e40d071..3320c9f6379901dbe494d25febe95cb2ba708912 100644 (file)
@@ -47,7 +47,6 @@ Resources::Resources()
 
 , spellMenuHeadline("")
 , spellMenuProperties(0)
-, inventory(0)
 , itemMenuHeadline("")
 , itemMenuProperties(0)
 , ikariMenuHeadline("")
index 5ef036fd715010553e3e4e7090a0f44c3b67a5ce..051879c36bfb88004a33bfdd66b911b9a74e825a 100644 (file)
@@ -52,7 +52,6 @@ struct Resources {
        const char *spellMenuHeadline;
        graphics::MenuProperties *spellMenuProperties;
 
-       common::Inventory *inventory;
        const char *itemMenuHeadline;
        graphics::MenuProperties *itemMenuProperties;
 
diff --git a/src/common/GameConfig.cpp b/src/common/GameConfig.cpp
new file mode 100644 (file)
index 0000000..b35e991
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * GameConfig.cpp
+ *
+ *  Created on: Oct 9, 2012
+ *      Author: holy
+ */
+
+#include "GameConfig.h"
+
+namespace common {
+
+GameConfig::GameConfig()
+: state(0)
+, battleResources(0)
+, heroesLayout(0) {
+
+}
+
+}
diff --git a/src/common/GameConfig.h b/src/common/GameConfig.h
new file mode 100644 (file)
index 0000000..16b0142
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * GameConfig.h
+ *
+ *  Created on: Oct 9, 2012
+ *      Author: holy
+ */
+
+#ifndef COMMON_GAMECONFIG_H_
+#define COMMON_GAMECONFIG_H_
+
+#include "fwd.h"
+#include "../battle/fwd.h"
+
+namespace common {
+
+struct GameConfig {
+
+       GameConfig();
+
+       GameState *state;
+
+       battle::Resources *battleResources;
+       battle::PartyLayout *heroesLayout;
+
+};
+
+}
+
+#endif /* COMMON_GAMECONFIG_H_ */
index f1edf484e474327363b65f64324ce8452d7da98d..fca50c1f846138168edd40cba990363ad49b6ea8 100644 (file)
@@ -9,6 +9,7 @@
 #define COMMON_GAMESTATE_H_
 
 #include "Hero.h"
+#include "Inventory.h"
 
 #include <SDL.h>
 
@@ -21,6 +22,8 @@ struct GameState {
        Hero heroes[7];
        Hero *party[4];
 
+       Inventory inventory;
+
        Uint32 money;
 
 };
index 3f7e46f85a87eed57913182958bb583140340c0f..053307e4f9db84ed2f049a9c214f349394cec6ac 100644 (file)
@@ -10,6 +10,7 @@
 
 namespace common {
 
+struct GameConfig;
 struct GameState;
 class Hero;
 class HeroGroup;
index 793a76fa6f8b1c9e0b33690f2f772f28cfc4fc06..8029a64c42d9ecb570c776990c562b1b56724203 100644 (file)
@@ -13,6 +13,7 @@
 #include "battle/Monster.h"
 #include "battle/PartyLayout.h"
 #include "battle/Resources.h"
+#include "common/GameConfig.h"
 #include "common/GameState.h"
 #include "common/Hero.h"
 #include "common/Ikari.h"
@@ -58,6 +59,7 @@ using app::Input;
 using battle::BattleState;
 using battle::Monster;
 using battle::PartyLayout;
+using common::GameConfig;
 using common::GameState;
 using common::Hero;
 using common::Ikari;
@@ -194,15 +196,17 @@ int main(int argc, char **argv) {
                gameState.party[2] = &gameState.heroes[2];
                gameState.party[3] = &gameState.heroes[3];
 
+               GameConfig gameConfig;
+               gameConfig.state = &gameState;
+               gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
+               gameConfig.battleResources = caster.GetBattleResources("battleResources");
+
                // temporary test data
                SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
                PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
-               PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
 
                Monster monster(*caster.GetMonster("lizard"));
 
-               battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
-
                gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
                Spell *strongSpell(caster.GetSpell("strongSpell"));
                gameState.heroes[0].AddSpell(strongSpell);
@@ -221,14 +225,12 @@ int main(int argc, char **argv) {
                gameState.heroes[0].AddSpell(valorSpell);
                gameState.heroes[1].AddSpell(valorSpell);
 
-               Inventory inventory;
-               inventory.Add(caster.GetItem("antidoteItem"), 9);
-               inventory.Add(caster.GetItem("magicJarItem"), 4);
-               inventory.Add(caster.GetItem("hiPotionItem"), 4);
-               inventory.Add(caster.GetItem("powerPotionItem"), 4);
-               inventory.Add(caster.GetItem("escapeItem"), 2);
-               inventory.Add(caster.GetItem("sleepBallItem"), 1);
-               battleRes->inventory = &inventory;
+               gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
+               gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
+               gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
+               gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
+               gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
+               gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
 
                gameState.heroes[0].SetWeapon(caster.GetItem("zircoSwordItem"));
                gameState.heroes[0].SetArmor(caster.GetItem("zirconArmorItem"));
@@ -424,6 +426,7 @@ int main(int argc, char **argv) {
                map1.SetTileset(&tileset);
                map1.SetTriggers(triggers1, 1);
                map1.SetWidth(2);
+               map1.SetBattleBackground(bg);
 
                Tile tiles3[64];
 
@@ -511,6 +514,7 @@ int main(int argc, char **argv) {
                map2.SetTileset(&tileset);
                map2.SetTriggers(triggers2, 1);
                map2.SetWidth(1);
+               map2.SetBattleBackground(bg);
 
                triggers1[0].map = &map2;
                triggers1[0].target = Vector<int>(6, 2);
@@ -539,6 +543,8 @@ int main(int argc, char **argv) {
                mapMonster.SetAnimation(&mapMonsterAnimation);
                mapMonster.Position() = Vector<float>(64, 32);
                mapMonster.SetOrientation(Entity::ORIENTATION_SOUTH);
+               mapMonster.SetPartyLayout(&monstersLayout);
+               mapMonster.SetMonsters(&monster, 1);
                map1.SetEntities(&mapMonster, 1);
 
                InitScreen screen(width, height);
@@ -546,7 +552,7 @@ int main(int argc, char **argv) {
                app::State *state(0);
 
                if (battle) {
-                       BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
+                       BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
                        battleState->AddMonster(monster);
                        battleState->AddMonster(monster);
                        battleState->AddMonster(monster);
@@ -557,7 +563,7 @@ int main(int argc, char **argv) {
                        battleState->AddHero(gameState.heroes[3]);
                        state = battleState;
                } else {
-                       MapState *mapState(new MapState(&map1));
+                       MapState *mapState(new MapState(&gameConfig, &map1));
 
                        mapState->ControlEntity(&gameState.heroes[0].MapEntity());
                        mapState->SetWalkingSpeed(walkSpeed);
index 9c066d646f021a0b8d0d644bb6806fbb5f98622d..985dea2d5462f4aae5ecba7663d6c79651f635d3 100644 (file)
@@ -18,13 +18,23 @@ using geometry::Vector;
 namespace map {
 
 Area::Area()
-: tiles(0)
+: battlebg(0)
+, tiles(0)
 , numTiles(0)
 , width(0) {
 
 }
 
 
+Tile *Area::TileAt(const geometry::Vector<int> &offset) {
+       int tileIndex(offset.Y() * width + offset.X());
+       if (tileIndex < numTiles) {
+               return tiles +tileIndex;
+       } else {
+               return 0;
+       }
+}
+
 const Tile *Area::TileAt(const geometry::Vector<int> &offset) const {
        int tileIndex(offset.Y() * width + offset.X());
        if (tileIndex < numTiles) {
index d9cc5d0d8784fc49d411a4ebb972a1913763af94..a044c8ba01a010866d617e4aa32ede2eeb8e4693 100644 (file)
@@ -26,18 +26,22 @@ public:
        int Width() const { return width; }
        int Height() const { return numTiles / width + (numTiles % width ? 1 : 0); }
        geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
+       Tile *TileAt(const geometry::Vector<int> &);
        const Tile *TileAt(const geometry::Vector<int> &) const;
 
+       SDL_Surface *BattleBackground() { return battlebg; }
+
        void Render(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector<int> &offset) const;
        void RenderDebug(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector<int> &offset) const;
 
 // temporary setters
 public:
-       void SetTiles(const Tile *t, int num) { tiles = t; numTiles = num; }
+       void SetTiles(Tile *t, int num) { tiles = t; numTiles = num; }
        void SetWidth(int w) { width = w; }
 
 private:
-       const Tile *tiles;
+       SDL_Surface *battlebg;
+       Tile *tiles;
        int numTiles;
        int width;
 
index c951f66df81fddbc5fdb8b6732a0829cf11b5911..01a8e9c384012a81ce15d5fc872c4342f4fab893 100644 (file)
@@ -18,6 +18,9 @@ namespace map {
 Entity::Entity()
 : follower(0)
 , animation(0)
+, partyLayout(0)
+, monsters(0)
+, numMonsters(0)
 , orientation(ORIENTATION_NORTH)
 , speed(0)
 , flags(0) {
@@ -118,6 +121,8 @@ void Entity::CreateTypeDescription() {
        Entity e;
 
        int animationId(TypeDescription::GetTypeId("Animation"));
+       int monsterId(TypeDescription::GetTypeId("Monster"));
+       int partyLayoutId(TypeDescription::GetTypeId("PartyLayout"));
        int vectorId(TypeDescription::GetTypeId("Vector"));
 
        TypeDescription &td(TypeDescription::CreateOrGet("Entity"));
@@ -126,6 +131,8 @@ void Entity::CreateTypeDescription() {
        td.SetSize(sizeof(Entity));
 
        td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), animationId).SetReferenced());
+       td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), partyLayoutId).SetReferenced());
+       td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), monsterId).SetReferenced().SetAggregate());
        td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), vectorId));
 }
 
index 3ddc9ccef49f5340680882be332ae216cbb1c259..13ecacac627eb021ee9f35ede96db8b739e6ea8f 100644 (file)
@@ -8,6 +8,8 @@
 #ifndef MAP_ENTITY_H_
 #define MAP_ENTITY_H_
 
+#include "../battle/fwd.h"
+#include "../battle/Monster.h"
 #include "../geometry/Vector.h"
 #include "../graphics/fwd.h"
 #include "../graphics/Animation.h"
@@ -56,10 +58,14 @@ public:
 
        void SetFlags(int f) { flags = f; }
        bool Blocking() const { return !(flags & FLAG_NONBLOCKING); }
-       bool Hostile() const {
-               // NOTE: this is a stub for testing!
-               return Blocking();
-       }
+       bool Hostile() const { return partyLayout && numMonsters > 0; }
+
+       void SetPartyLayout(battle::PartyLayout *l) { partyLayout = l; }
+       battle::PartyLayout *PartyLayout() { return partyLayout; }
+
+       void SetMonsters(battle::Monster *m, int num) { monsters = m; numMonsters = num; }
+       battle::Monster *MonstersBegin() { return monsters; }
+       battle::Monster *MonstersEnd() { return monsters + numMonsters; }
 
        Entity *Follower() { return follower; }
        const Entity *Follower() const { return follower; }
@@ -82,6 +88,9 @@ private:
 private:
        Entity *follower;
        const graphics::Animation *animation;
+       battle::PartyLayout *partyLayout;
+       battle::Monster *monsters;
+       int numMonsters;
        graphics::AnimationRunner runner;
        geometry::Vector<int> spriteOffset;
        geometry::Vector<float> position;
index fc664c1d7d4e26c0060176bf0283002abfbbe5a6..a9e11df7e9064627cb4388b0696e78eada8c92b8 100644 (file)
@@ -8,6 +8,7 @@
 #include "Map.h"
 
 #include "Area.h"
+#include "Tile.h"
 #include "Trigger.h"
 #include "../graphics/Sprite.h"
 
@@ -19,6 +20,7 @@ namespace map {
 
 Map::Map()
 : tileset(0)
+, battlebg(0)
 , areas(0)
 , numAreas(0)
 , triggers(0)
@@ -30,6 +32,18 @@ Map::Map()
 }
 
 
+Area *Map::AreaAt(const Vector<int> &offset) {
+       if (numAreas > 0) {
+               Vector<int> coords(TileCoordinates(offset));
+               Vector<int> areaOffset(coords / areas[0].Size());
+               int areaIndex(areaOffset.Index(width));
+               if (areaIndex < numAreas) {
+                       return areas + areaIndex;
+               }
+       }
+       return 0;
+}
+
 const Area *Map::AreaAt(const Vector<int> &offset) const {
        if (numAreas > 0) {
                Vector<int> coords(TileCoordinates(offset));
@@ -42,6 +56,16 @@ const Area *Map::AreaAt(const Vector<int> &offset) const {
        return 0;
 }
 
+Tile *Map::TileAt(const Vector<int> &offset) {
+       Area *area(AreaAt(offset));
+       if (area) {
+               Vector<int> tileOffset(TileCoordinates(offset) % area->Size());
+               return area->TileAt(tileOffset);
+       } else {
+               return 0;
+       }
+}
+
 const Tile *Map::TileAt(const Vector<int> &offset) const {
        const Area *area(AreaAt(offset));
        if (area) {
@@ -63,6 +87,18 @@ Trigger *Map::TriggerAt(const geometry::Vector<int> &offset) {
        return 0;
 }
 
+SDL_Surface *Map::BattleBackgroundAt(const geometry::Vector<int> &position) {
+       Tile *tile(TileAt(position));
+       if (tile && tile->BattleBackground()) {
+               return tile->BattleBackground();
+       }
+       Area *area(AreaAt(position));
+       if (area && area->BattleBackground()) {
+               return area->BattleBackground();
+       }
+       return battlebg;
+}
+
 Vector<int> Map::TileCoordinates(const Vector<int> &position) const {
        return position / tileset->Size();
 }
index ab0728b765ba8ec363c20d27f3a7e0b9e8e70454..5eacc7b9b08e15ba2c3d4968debc6793a97811b0 100644 (file)
@@ -24,9 +24,12 @@ public:
 
 public:
        const graphics::Sprite *Tileset() const { return tileset; }
+       Area *AreaAt(const geometry::Vector<int> &);
        const Area *AreaAt(const geometry::Vector<int> &) const;
+       Tile *TileAt(const geometry::Vector<int> &);
        const Tile *TileAt(const geometry::Vector<int> &) const;
        Trigger *TriggerAt(const geometry::Vector<int> &);
+       SDL_Surface *BattleBackgroundAt(const geometry::Vector<int> &);
        geometry::Vector<int> TileCoordinates(const geometry::Vector<int> &) const;
 
        Entity **EntitiesBegin() { return &entities; }
@@ -38,6 +41,7 @@ public:
 // temporary setters
 public:
        void SetTileset(const graphics::Sprite *t) { tileset = t; }
+       void SetBattleBackground(SDL_Surface *bg) { battlebg = bg; }
        void SetAreas(Area *a, int num) { areas = a; numAreas = num; }
        void SetTriggers(Trigger *t, int num) { triggers = t; numTriggers = num; }
        void SetEntities(Entity *e, int num) { entities = e; numEntities = num; }
@@ -45,6 +49,7 @@ public:
 
 private:
        const graphics::Sprite *tileset;
+       SDL_Surface *battlebg;
        Area *areas;
        int numAreas;
        Trigger *triggers;
index ce14c2905982e04efcdce3c9edee11534b4a0931..773190e88952b604511fb4a249f94d751035c4a2 100644 (file)
 #include "Trigger.h"
 #include "../app/Application.h"
 #include "../app/Input.h"
+#include "../battle/BattleState.h"
+#include "../common/GameConfig.h"
+#include "../common/GameState.h"
 #include "../graphics/ColorFade.h"
 
 #include <algorithm>
 
 using app::Application;
 using app::Input;
+using battle::BattleState;
+using common::GameConfig;
 using geometry::Vector;
 using graphics::ColorFade;
 
 namespace map {
 
-MapState::MapState(Map *map)
-: ctrl(0)
+MapState::MapState(GameConfig *g, Map *map)
+: game(g)
+, ctrl(0)
 , map(map)
 , controlled(0)
 , tempTarget(20, 20)
@@ -95,16 +101,19 @@ void MapState::OnTileLock() {
        if (moveTimer.Running() && !moveTimer.JustHit()) return;
 
        Vector<int> nowLock(controlled->Position());
+       bool event(false);
        if (nowLock != lastLock) {
-               OnGridLock();
+               event = OnGridLock();
                afterLock = true;
                moveTimer.Clear();
        } else if (moveTimer.JustHit()) {
-               OnGridLock();
+               event = OnGridLock();
                afterLock = true;
        }
 
-       // TODO: halt all activity if lock caused a state/map transition
+       if (event) {
+               return;
+       }
 
        if (nextDirection >= 0) {
                bool blocked(CheckBlocking());
@@ -193,13 +202,13 @@ bool MapState::CheckBlocking() const {
        return false;
 }
 
-void MapState::OnGridLock() {
+bool MapState::OnGridLock() {
        if (skipLock) {
                skipLock = false;
+               return false;
        } else {
                LockEntities();
-               CheckMonster();
-               CheckTrigger();
+               return CheckMonster() || CheckTrigger();
        }
 }
 
@@ -213,7 +222,7 @@ void MapState::LockEntities() {
        }
 }
 
-void MapState::CheckMonster() {
+bool MapState::CheckMonster() {
        Vector<int> coords(map->TileCoordinates(controlled->Position()));
        Vector<int> neighbor[4];
        neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
@@ -225,17 +234,38 @@ void MapState::CheckMonster() {
                for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
                        if ((*e)->Hostile() && map->TileCoordinates((*e)->Position()) == neighbor[i]) {
                                // TODO: check for turn advantage, see #26
-                               // TODO: remove entity, push battle state and transition and halt all other activity
+                               // TODO: other transition
+                               BattleState *battleState(new BattleState(game, map->BattleBackgroundAt((*e)->Position()), (*e)->PartyLayout()));
+                               for (int i(0); i < 4; ++i) {
+                                       if (game->state->party[i]) {
+                                               battleState->AddHero(*game->state->party[i]);
+                                       }
+                               }
+                               for (battle::Monster *monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
+                                       battleState->AddMonster(*monster);
+                               }
+
+                               ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
+                               fadeIn->SetLeadInTime(500);
+                               ColorFade *fadeOut(new ColorFade(this, 0, 500));
+                               fadeOut->SetLeadOutTime(500);
+
+                               ctrl->PushState(fadeIn);
+                               ctrl->PushState(battleState);
+                               ctrl->PushState(fadeOut);
+                               // TODO: move entity erase to happen after the transition or battle
+                               entities.erase(e);
+                               return true;
                                // needed information here:
-                               //  - battle background (from tile?)
+                               //  - battle background (from tile/area/map)
                                //  - monsters + layout (from entity)
-                               //  - battle resources (from global resources)
                        }
                }
        }
+       return false;
 }
 
-void MapState::CheckTrigger() {
+bool MapState::CheckTrigger() {
        Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
        if (trigger) {
                // TODO: run trigger script
@@ -245,9 +275,10 @@ void MapState::CheckTrigger() {
                        ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
                        fadeOut->SetLeadOutTime(500);
                        ctrl->PushState(fadeOut);
+                       return true;
                }
        }
-
+       return false;
 }
 
 void MapState::OnMove(bool realMove) {
index dc352f69522636e5f2248076f6cddb59bea2a804..a6d292b78d757b4b694e587ca29716f2e79333fd 100644 (file)
@@ -11,6 +11,7 @@
 #include "Entity.h"
 #include "fwd.h"
 #include "../app/State.h"
+#include "../common/fwd.h"
 #include "../geometry/Vector.h"
 #include "../graphics/Camera.h"
 
@@ -22,7 +23,7 @@ class MapState
 : public app::State {
 
 public:
-       explicit MapState(Map *);
+       explicit MapState(common::GameConfig *, Map *);
        virtual ~MapState() { }
 
 public:
@@ -53,17 +54,18 @@ private:
        bool CheckBlocking() const;
 
        void OnTileLock();
-       void OnGridLock();
+       bool OnGridLock();
        void OnMove(bool);
 
        void UpdateFollower(Entity &);
        void StopFollowers(Entity &);
 
        void LockEntities();
-       void CheckMonster();
-       void CheckTrigger();
+       bool CheckMonster();
+       bool CheckTrigger();
 
 private:
+       common::GameConfig *game;
        app::Application *ctrl;
        Map *map;
        Entity *controlled;
index 7b77fb1feccdb93a6f260c0adad725a7acf9b2f7..303c9e556eba5fcf1836eca8b7aa1ac4b05a4fb2 100644 (file)
@@ -10,7 +10,8 @@
 namespace map {
 
 Tile::Tile()
-: flags(0) {
+: battlebg(0)
+, flags(0) {
 
 }
 
index 88db623ed0cdb5e8f24e2949f924ac3dc697b06c..06b3b1175b62ae315c5c5c3054e7b4e7eb051bc4 100644 (file)
@@ -28,6 +28,8 @@ public:
                BLOCK_WEST = 0x08,
        };
 
+       SDL_Surface *BattleBackground() { return battlebg; }
+
        const geometry::Vector<int> &Offset() const { return offset; }
 
        bool BlocksNorth() const { return flags & BLOCK_NORTH; }
@@ -41,6 +43,7 @@ public:
        Tile &SetFlags(Uint32 f) { flags = f; return *this; }
 
 private:
+       SDL_Surface *battlebg;
        geometry::Vector<int> offset;
        Uint32 flags;