Permalink
Browse files

Added basic StateManager

  • Loading branch information...
1 parent 1c66d02 commit 3d4b7c592fbe76f218d492e383570d894609f4df @johanekholm johanekholm committed Jan 1, 2012
Showing with 103 additions and 0 deletions.
  1. +2 −0 CMakeLists.txt
  2. +2 −0 Classes/CentralControl.cpp
  3. +3 −0 Classes/SceneLoader.cpp
  4. +68 −0 Classes/StateManager.cpp
  5. +28 −0 Classes/StateManager.h
View
@@ -31,6 +31,7 @@ SET( SOURCES
Classes/ScriptManager.cpp
Classes/ShapeImage.cpp
Classes/Sound.cpp
+ Classes/StateManager.cpp
Classes/StringImage.cpp
Classes/TextureCatalog.cpp
Classes/TextureMap.cpp
@@ -68,6 +69,7 @@ SET( HEADERS
Classes/ScriptManager.h
Classes/ShapeImage.h
Classes/Sound.h
+ Classes/StateManager.h
Classes/StringImage.h
Classes/TextureCatalog.h
Classes/TextureMap.h
@@ -23,6 +23,7 @@
#include "ViewController.h"
#include "ViewControllerManager.h"
#include "SceneLoader.h"
+#include "StateManager.h"
#include "toolkit.h"
#include "geometry.h"
@@ -76,6 +77,7 @@ CentralControl::CentralControl() {
this->switchMode(ControlMode::ADVENTURE);
//this->switchMode(ControlMode::BATTLE);
+
//SceneLoader::instance()->switchToMainMenu();
//this->switchMode(ControlMode::MENU);
}
View
@@ -20,6 +20,7 @@
#include "UnitFactory.h"
#include "MenuView.h"
#include "ScriptManager.h"
+#include "StateManager.h"
SceneLoader* SceneLoader::_instance = 0;
@@ -91,6 +92,8 @@ void SceneLoader::loadBattleScene(const std::string& mapName, int enemyPartyType
ScriptManager::instance()->add(ScriptedAction::build(ScriptedActionNS::END_BATTLE, ModelEventNS::PARTY_WIPEOUT));
Sound::instance()->play("music1");
+
+ StateManager::save("state.txt");
}
void SceneLoader::loadAdventureScene() {
View
@@ -0,0 +1,68 @@
+/*
+ * StateManager.cpp
+ *
+ * Created by Johan Ekholm
+ *
+ */
+
+#include "StateManager.h"
+#include "ModelManager.h"
+#include "UnitModel.h"
+#include "json.h"
+#include <string>
+#include <vector>
+#include <iostream>
+
+
+void StateManager::createState(Json::Value& root) {
+ Json::Value unitsNode = root["units"];
+ Json::Value* unitState;
+ std::vector<UnitModel*> units;
+ ModelManager* modelManager = ModelManager::instance();
+ Json::FastWriter writer;
+
+ units = modelManager->getAllUnits();
+ DEBUGLOG("Iterating: %i", units.size());
+ for (std::vector<UnitModel*>::iterator it = units.begin(); it != units.end(); ++it) {
+ DEBUGLOG("Iterating over units");
+ if (*it != 0) {
+ unitState = new Json::Value();
+ (*it)->serialize(*unitState);
+ DEBUGLOG("serialized: %s", writer.write(*unitState).c_str());
+ unitsNode.append(*unitState);
+ }
+ }
+
+}
+
+void StateManager::loadStateFromFile(Json::Value& root, const std::string& filename) {
+ // To-Do: implement load from file
+}
+
+void StateManager::saveStateToFile(Json::Value& root, const std::string& filename) {
+ // To-Do: implement save to file
+
+ Json::FastWriter writer;
+ std::string jsonString;
+
+ jsonString = writer.write(root);
+ DEBUGLOG("State saved with data: %s", jsonString.c_str());
+}
+
+void StateManager::recreateFromState(Json::Value& root) {
+ // To-Do: implement loading objects
+}
+
+void StateManager::load(const std::string& filename) {
+ Json::Value state;
+
+ loadStateFromFile(state, filename);
+ recreateFromState(state);
+}
+
+void StateManager::save(const std::string& filename) {
+ Json::Value state;
+
+ createState(state);
+ saveStateToFile(state, filename);
+}
View
@@ -0,0 +1,28 @@
+/*
+ * StateManager.h
+ *
+ * Created by Johan Ekholm
+ *
+ */
+
+#ifndef STATEMANAGER_H
+#define STATEMANAGER_H
+
+#include "json-forwards.h"
+#include <vector>
+#include <string>
+
+class StateManager {
+
+ StateManager() {}
+ static void createState(Json::Value& root);
+ static void loadStateFromFile(Json::Value& root, const std::string& filename);
+ static void saveStateToFile(Json::Value& root, const std::string& filename);
+ static void recreateFromState(Json::Value& root);
+
+public:
+ static void load(const std::string& filename);
+ static void save(const std::string& filename);
+};
+
+#endif

0 comments on commit 3d4b7c5

Please sign in to comment.