]> git.localhorst.tv Git - l2e.git/commitdiff
extracted map loading/unloading
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 12:54:55 +0000 (14:54 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 12:54:55 +0000 (14:54 +0200)
src/main.cpp
src/map/Map.cpp
src/map/Map.h
src/map/MapState.cpp
src/map/MapState.h

index 2a49ad585f50c09a8d5e487dc9bba6adf5b06079..c4bc0bfb378d76ab9cba18f990a3814d362dc7f3 100644 (file)
@@ -543,6 +543,7 @@ int main(int argc, char **argv) {
                mapMonster.SetAnimation(&mapMonsterAnimation);
                mapMonster.Position() = Vector<float>(64, 32);
                mapMonster.SetOrientation(Entity::ORIENTATION_SOUTH);
+               map1.SetEntities(&mapMonster, 1);
 
                InitScreen screen(width, height);
 
@@ -562,15 +563,8 @@ int main(int argc, char **argv) {
                } else {
                        MapState *mapState(new MapState(&map1));
 
-                       mapState->AddEntity(&mapMaxim);
-                       mapState->AddEntity(&mapSelan);
-                       mapState->AddEntity(&mapGuy);
-                       mapState->AddEntity(&mapDekar);
-
                        mapState->ControlEntity(&mapMaxim);
                        mapState->SetWalkingSpeed(walkSpeed);
-
-                       mapState->AddEntity(&mapMonster);
                        mapMonster.StartAnimation(*mapState);
 
                        state = mapState;
index b87cd03cb5674a2333f3b78315d1e4c03e01cf5d..d7c0b32e49c77c6fa1aa08927a7cbe74ff317243 100644 (file)
@@ -23,6 +23,8 @@ Map::Map()
 , numAreas(0)
 , triggers(0)
 , numTriggers(0)
+, entities(0)
+, numEntities(0)
 , width(0) {
 
 }
index a1b9cebd593c5dd9f809f8fb1d1edb061b9b0b56..8c2804d0b3641a5493aa4cbac83243cc6b97ae27 100644 (file)
@@ -29,6 +29,9 @@ public:
        Trigger *TriggerAt(const geometry::Vector<int> &);
        geometry::Vector<int> TileCoordinates(const geometry::Vector<int> &) const;
 
+       Entity **EntitiesBegin() { return &entities; }
+       Entity **EntitiesEnd() { return (&entities) + numEntities; }
+
        void Render(SDL_Surface *dest, const geometry::Vector<int> &offset) const;
        void RenderDebug(SDL_Surface *dest, const geometry::Vector<int> &offset) const;
 
@@ -37,6 +40,7 @@ public:
        void SetTileset(const graphics::Sprite *t) { tileset = t; }
        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; }
        void SetWidth(int w) { width = w; }
 
 private:
@@ -45,6 +49,8 @@ private:
        int numAreas;
        Trigger *triggers;
        int numTriggers;
+       Entity *entities;
+       int numEntities;
        int width;
 
 };
index 8c5ef6bcf1c53ec612c3ba86d043b003127563b8..7e6d7328d2756bb533527cf41efc25a9a2b6f703 100644 (file)
@@ -37,6 +37,7 @@ MapState::MapState(Map *map)
 
 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
        camera.Resize(screen->w, screen->h);
+       LoadMap(map);
 }
 
 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
@@ -287,17 +288,28 @@ void MapState::StopFollowers(Entity &e) {
 
 
 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
+       UnloadMap();
        Vector<int> position(coordinates * map->Tileset()->Size());
-       entities.clear();
        for (Entity *e(controlled); e; e = e->Follower()) {
                e->Position() = position;
                e->SetOrientation(controlled->GetOrientation());
-               entities.push_back(e);
        }
-       map = newMap;
+       LoadMap(newMap);
        skipLock = true;
 }
 
+void MapState::UnloadMap() {
+       entities.clear();
+}
+
+void MapState::LoadMap(Map *m) {
+       map = m;
+       entities.insert(entities.end(), m->EntitiesBegin(), m->EntitiesEnd());
+       for (Entity *e(controlled); e; e = e->Follower()) {
+               entities.push_back(e);
+       }
+}
+
 
 void MapState::Render(SDL_Surface *screen) {
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
index ccdb51e7fef0c07477640387a4e828f7dcf182ee..0e49100e3b305b6e458123af8060986fd4b76958 100644 (file)
@@ -47,6 +47,9 @@ public:
 private:
        static bool ZCompare(const Entity *lhs, const Entity *rhs);
 
+       void UnloadMap();
+       void LoadMap(Map *);
+
        bool CheckBlocking() const;
 
        void OnTileLock();