From b508308115f7c81a046969712a1046eabf6d587c Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Wed, 11 Jan 2012 04:07:35 +0200 Subject: [PATCH] [Add] World Manager. [Fix] Added newlines in end of files. --- Win32/Unuk/LibUnuk/LibUnuk.vcproj | 8 +++++ src/Unuk/Game.cpp | 19 ++++------- src/Unuk/Game.h | 2 -- src/libUnuk/ButtonGroup.cpp | 2 +- src/libUnuk/ButtonGroup.h | 2 +- src/libUnuk/Makefile | 2 +- src/libUnuk/Map.cpp | 20 ++++++++++++ src/libUnuk/Map.h | 5 +++ src/libUnuk/WorldManager.cpp | 54 +++++++++++++++++++++++++++++++ src/libUnuk/WorldManager.h | 20 ++++++++++++ 10 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 src/libUnuk/WorldManager.cpp create mode 100644 src/libUnuk/WorldManager.h diff --git a/Win32/Unuk/LibUnuk/LibUnuk.vcproj b/Win32/Unuk/LibUnuk/LibUnuk.vcproj index f03682c..1a1eaa1 100644 --- a/Win32/Unuk/LibUnuk/LibUnuk.vcproj +++ b/Win32/Unuk/LibUnuk/LibUnuk.vcproj @@ -349,6 +349,14 @@ RelativePath="..\..\..\src\libUnuk\Timer.h" > + + + + diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp index 592fec4..0ae06ae 100644 --- a/src/Unuk/Game.cpp +++ b/src/Unuk/Game.cpp @@ -1,9 +1,10 @@ #include "Game.h" +#include "../libUnuk/NPC.h" +#include "../libUnuk/WorldManager.h" Game::Game(void) { Debug::logger->message("Creating characters.."); _player = new Player(&_map); - _npc = new NPC(&_map); _runGameReturnValue = gameMainMenu; } @@ -12,16 +13,12 @@ Game::~Game(void) { Debug::logger->message("\n----- Cleaning Up ------"); // cleaning _player up caused a nice seg fault. I'll look later. //delete _player; - delete _npc; } gameNavVal_t Game::Run(const string savegameIDArg) { _player->SetXY(50, 50); _player->LoadSprites("../Data/Media/Images/Characters/template.png", 40, 45); - _npc->SetXY(300, 300); - _npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); - LoadSavegame(savegameIDArg); int fps = 0; @@ -73,9 +70,9 @@ gameNavVal_t Game::Run(const string savegameIDArg) { } updateTimer.Pause(); - if (!stillRunning) { - break; - } + if (!stillRunning) { + break; + } renderTimer.Start(); Render(); @@ -107,7 +104,7 @@ gameNavVal_t Game::Run(const string savegameIDArg) { _playerHealth.SetTextBlended(playerHealth.str(), vsmall, COLOUR_BLACK); npcHealth.str(""); - npcHealth << "NPC Health: " << _npc->GetHealth(); + npcHealth << "NPC Health: " << _map.GetWorld().GetNPC(0)->GetHealth(); _npcHealth.SetTextBlended(npcHealth.str(), vsmall, COLOUR_BLACK); } } @@ -175,8 +172,8 @@ void Game::HandleInput(void) { void Game::UpdateGame(void) { if(_ingameMenu.GetStatus() == false) { + _map.Update(); _player->Update(); - _npc->Update(); } else { // :D } @@ -186,9 +183,7 @@ void Game::Render(void) { // SDL_FillRect(screen, NULL, 0); // You might want to clear the buffer! --konom if(_ingameMenu.GetStatus() == false) { _map.Render(); - _player->Render(); - _npc->Render(); if(debugEnabled) { _gameRenderTime.RenderLiteral(); diff --git a/src/Unuk/Game.h b/src/Unuk/Game.h index 6cd21b6..b9a4523 100644 --- a/src/Unuk/Game.h +++ b/src/Unuk/Game.h @@ -14,7 +14,6 @@ #include "../libUnuk/IngameMenu.h" #include "../libUnuk/Map.h" #include "../libUnuk/Timer.h" -#include "../libUnuk/NPC.h" #include "../libUnuk/Debug.h" #include "../libUnuk/Text.h" #include "../libUnuk/MemClass.h" @@ -58,7 +57,6 @@ private: Map _map; Player* _player; - NPC* _npc; }; #endif diff --git a/src/libUnuk/ButtonGroup.cpp b/src/libUnuk/ButtonGroup.cpp index 715b9f9..4bdfd02 100644 --- a/src/libUnuk/ButtonGroup.cpp +++ b/src/libUnuk/ButtonGroup.cpp @@ -98,4 +98,4 @@ Button* ButtonGroup::GetButton(int index) { buttonsIndex++; } return NULL; -} \ No newline at end of file +} diff --git a/src/libUnuk/ButtonGroup.h b/src/libUnuk/ButtonGroup.h index faf1f1b..0769083 100644 --- a/src/libUnuk/ButtonGroup.h +++ b/src/libUnuk/ButtonGroup.h @@ -32,4 +32,4 @@ private: int _selectedButton; }; -#endif \ No newline at end of file +#endif diff --git a/src/libUnuk/Makefile b/src/libUnuk/Makefile index d21a69e..2a9af10 100644 --- a/src/libUnuk/Makefile +++ b/src/libUnuk/Makefile @@ -3,7 +3,7 @@ CFLAGS = -ansi -Wall -g LDADD = -lGL -lGLU -lSDL -lSDL_ttf -lSDL_gfx -lSDL_image -ltinyxml objects = ApplySurface.o Button.o ButtonToggle.o ButtonGroup.o Character.o Collision.o \ Debug.o Font.o FPS.o ImageLoader.o IngameMenu.o Input.o MainMenu.o \ - Map.o MapElement.o MapEntities.o MemManager.o NPC.o ParticleEmitter.o \ + WorldManager.o Map.o MapElement.o MapEntities.o MemManager.o NPC.o ParticleEmitter.o \ Rect.o Text.o Texture.o TextureManager.o Timer.o \ AStar.o \ diff --git a/src/libUnuk/Map.cpp b/src/libUnuk/Map.cpp index 5d760f5..f60b367 100644 --- a/src/libUnuk/Map.cpp +++ b/src/libUnuk/Map.cpp @@ -1,4 +1,5 @@ #include "Map.h" +#include "NPC.h" Map::Map(void) { @@ -126,9 +127,26 @@ void Map::Load(const string filename) { levelHeight = y * TILE_HEIGHT; //character->Load(filename); + + NPC* npc = new NPC(this); + + npc->SetXY(300, 300); + npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); + _world.AddNPC(npc); + + npc = new NPC(this); + npc->SetXY(150, 350); + npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); + _world.AddNPC(npc); + + npc = new NPC(this); + npc->SetXY(100, 250); + npc->LoadSprites("../Data/Media/Images/Characters/template.png", 40,45); + _world.AddNPC(npc); } void Map::Update(void) { + _world.Update(); // Update the map so we can render when camera moves. } @@ -167,6 +185,8 @@ void Map::Render(void) { _tile[i][j].Render(); } } + + _world.Render(); } void Map::Unload(void) { diff --git a/src/libUnuk/Map.h b/src/libUnuk/Map.h index c09570b..7391e55 100644 --- a/src/libUnuk/Map.h +++ b/src/libUnuk/Map.h @@ -14,6 +14,7 @@ #include "ApplySurface.h" #include "MapTile.h" #include "Debug.h" +#include "WorldManager.h" using namespace std; //class CharacterManager; @@ -45,6 +46,8 @@ public: string GetCurrentMap(void); + WorldManager& GetWorld(void) { return _world; } + private: void Unload(void); @@ -57,6 +60,8 @@ private: TextureManager _tileTextures; TextureManager _entityTextures; + + WorldManager _world; }; #endif diff --git a/src/libUnuk/WorldManager.cpp b/src/libUnuk/WorldManager.cpp new file mode 100644 index 0000000..af23a6b --- /dev/null +++ b/src/libUnuk/WorldManager.cpp @@ -0,0 +1,54 @@ +#include "WorldManager.h" +#include "NPC.h" + +WorldManager::WorldManager(void) { +} + +WorldManager::~WorldManager(void) { + for(std::list::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { + NPC* npc = (*i); + delete npc; + } +} + +void WorldManager::Update(void) { + for(std::list::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { + NPC* npc = (*i); + npc->Update(); + } +} + +void WorldManager::Render(void) { + for(std::list::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { + NPC* npc = (*i); + npc->Render(); + } +} + +void WorldManager::AddNPC(NPC* npc) { + _npcs.push_back(npc); +} + +void WorldManager::RemoveNPC(int index) { + int npcsIndex = 0; + for(std::list::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { + NPC* npc = (*i); + if(npcsIndex == index) { + _npcs.erase(i); + delete npc; + } + npcsIndex++; + } +} + +NPC* WorldManager::GetNPC(int index) { + int npcsIndex = 0; + for(std::list::iterator i = _npcs.begin(); i != _npcs.end(); ++i) { + NPC* npc = (*i); + if(npcsIndex == index) { + return npc; + } + npcsIndex++; + } + return NULL; +} diff --git a/src/libUnuk/WorldManager.h b/src/libUnuk/WorldManager.h new file mode 100644 index 0000000..466571c --- /dev/null +++ b/src/libUnuk/WorldManager.h @@ -0,0 +1,20 @@ +#pragma once +#include + +class NPC; + +class WorldManager { +public: + WorldManager(void); + ~WorldManager(void); + + void Update(void); + void Render(void); + + void AddNPC(NPC* npc); + void RemoveNPC(int index); + NPC* GetNPC(int index); + +private: + std::list _npcs; +};