From: Daniel Karbach Date: Sun, 7 Oct 2012 12:54:55 +0000 (+0200) Subject: extracted map loading/unloading X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=7fb774ec1df3d550cd8a6805bdc69c11ad36e498;p=l2e.git extracted map loading/unloading --- diff --git a/src/main.cpp b/src/main.cpp index 2a49ad5..c4bc0bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -543,6 +543,7 @@ int main(int argc, char **argv) { mapMonster.SetAnimation(&mapMonsterAnimation); mapMonster.Position() = Vector(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; diff --git a/src/map/Map.cpp b/src/map/Map.cpp index b87cd03..d7c0b32 100644 --- a/src/map/Map.cpp +++ b/src/map/Map.cpp @@ -23,6 +23,8 @@ Map::Map() , numAreas(0) , triggers(0) , numTriggers(0) +, entities(0) +, numEntities(0) , width(0) { } diff --git a/src/map/Map.h b/src/map/Map.h index a1b9ceb..8c2804d 100644 --- a/src/map/Map.h +++ b/src/map/Map.h @@ -29,6 +29,9 @@ public: Trigger *TriggerAt(const geometry::Vector &); geometry::Vector TileCoordinates(const geometry::Vector &) const; + Entity **EntitiesBegin() { return &entities; } + Entity **EntitiesEnd() { return (&entities) + numEntities; } + void Render(SDL_Surface *dest, const geometry::Vector &offset) const; void RenderDebug(SDL_Surface *dest, const geometry::Vector &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; }; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 8c5ef6b..7e6d732 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -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 &coordinates) { + UnloadMap(); Vector 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)); diff --git a/src/map/MapState.h b/src/map/MapState.h index ccdb51e..0e49100 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -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();