Commit 3b93dadf authored by Elias Broschin's avatar Elias Broschin
Browse files

game_state_machine done

parent 0cdf0d38
This diff is collapsed.
......@@ -10,7 +10,7 @@ public:
game_state(engine &engine);
virtual ~game_state();
virtual void update() = 0;
virtual void update(float dt) = 0;
virtual void on_exit() = 0;
virtual void on_enter() = 0;
......
......@@ -3,6 +3,9 @@
#include <map>
#include <memory>
#include <util/id.hpp>
#include <game_state_machine/game_state.h>
#include <util/map_helper.h>
namespace gdw {
......@@ -14,8 +17,20 @@ public:
game_state_machine(engine &engine);
~game_state_machine();
void change_state(unsigned long long id);
void update();
template<typename T>
void change_state() {
auto id = type_id<T>();
if(!current_state_ ||
current_state_->id() == id ||
!map_helper::findKey(game_states_, id))
return;
current_state_->on_exit();
current_state_ = game_states_[id].get();
current_state_->on_enter();
}
void update(float dt);
private:
gdw::engine& engine_;
std::map<unsigned long long, std::unique_ptr<game_state>> game_states_;
......
......@@ -10,7 +10,7 @@ public:
menu_state(engine& engine);
~menu_state();
void update();
void update(float dt);
void on_enter();
void on_exit();
};
......
......@@ -10,7 +10,7 @@ public:
play_state(engine& engine);
~play_state();
void update();
void update(float dt);
void on_enter();
void on_exit();
};
......
#ifndef __LIST_HELPER__
#define __LIST_HELPER__
#include <vector>
#include <list>
#include <algorithm>
namespace gdw {
namespace list_helper {
/** Searches for an element in a given vector.
* Returns true, when found.
*/
template<typename T>
bool findElement(std::vector<T> &vector, T elem) {
auto it = std::find(vector.begin(), vector.end(), elem);
return it != vector.end();
}
template<typename T>
bool findElement(std::list<T> &list, T elem) {
auto it = std::find(list.begin(), list.end(), elem);
return it != list.end();
}
/** Searches for an element in a given vector and removes it.
* Does not respect insertion order.
* Returns true, when found and removed, otherwise false.
*/
template<typename T>
bool removeElement(std::vector<T> &vector, const T& elem) {
auto it = std::find(vector.begin(), vector.end(), elem);
if(it == vector.end())
return false;
auto last = vector.end() -1;
if(it != last)
*it = *last;
vector.pop_back();
return true;
}
} //Namespace List
} // Namespace Util
#endif
#ifndef __MAPHELPER__
#define __MAPHELPER__
#include <map>
#include <unordered_map>
#include <list>
namespace gdw {
namespace map_helper {
template<typename T, typename U>
bool findKey(const std::map<T,U> &map, T key) {
auto it = map.find(key);
return it != map.end();
}
template<typename T, typename U>
bool removeKey(std::map<T,U> &map, T key) {
auto it = map.find(key);
if(it == map.end()) return false;
map.erase(it);
return true;
}
template<typename T, typename U>
bool findKey(const std::unordered_map<T,U> &map, T key) {
auto it = map.find(key);
return it != map.end();
}
template<typename T, typename U>
bool removeKey(std::unordered_map<T,U> &map, T key) {
auto it = map.find(key);
if(it == map.end()) return false;
map.erase(it);
return true;
}
template<typename T, typename U>
std::list<T> getKeys(std::unordered_map<T,U> &map) {
std::list<T> list;
if(!map.empty()) {
auto it = map.begin();
while(it != map.end()) {
list.push_back(it->first);
it++;
}
}
return list;
}
template<typename T, typename U>
std::list<T> getKeys(std::map<T,U> &map) {
std::list<T> list;
if(!map.empty()) {
auto it = map.begin();
while(it != map.end()) {
list.push_back(it->first);
it++;
}
}
return list;
}
} //Namespace List
} //Namespace Util
#endif
#ifndef __GDW_STACKTRACE_HPP__
#define __GDW_STACKTRACE_HPP__
//#ifndef __GDW_STACKTRACE_HPP__
//#define __GDW_STACKTRACE_HPP__
#include <string>
#include <stdexcept>
//#include <string>
//#include <stdexcept>
namespace gdw {
//namespace gdw {
extern void initStacktrace(std::string exeName);
// extern void initStacktrace(std::string exeName);
extern bool isStacktraceAvailable();
// extern bool isStacktraceAvailable();
extern std::string genStacktrace(std::size_t framesToSkip=0);
// extern std::string genStacktrace(std::size_t framesToSkip=0);
struct Error : public std::runtime_error {
explicit Error(const std::string& msg)
: std::runtime_error(msg+"\n At "+genStacktrace(1)) {}
};
// struct Error : public std::runtime_error {
// explicit Error(const std::string& msg)
// : std::runtime_error(msg+"\n At "+genStacktrace(1)) {}
// };
}
//}
#endif //__GDW_STACKTRACE_HPP__
\ No newline at end of file
//#endif //__GDW_STACKTRACE_HPP__
......@@ -40,7 +40,7 @@ namespace gdw {
}
void engine::update(float delta_time) {
game_state_machine_->update();
game_state_machine_->update(delta_time);
graphics_system_->begin();
rendering_system_->update(delta_time);
graphics_system_->end(delta_time);
......
#include <game_state_machine/game_state_machine.h>
#include <core/engine.hpp>
#include <game_state_machine/game_state.h>
#include <util/id.hpp>
#include <util/make_unique.hpp>
#include <game_state_machine/play_state.h>
#include <game_state_machine/menu_state.h>
......@@ -20,25 +19,9 @@ game_state_machine::~game_state_machine() {
}
void game_state_machine::change_state(unsigned long long id) {
if(!current_state_) return;
if(current_state_->id() == id) return;
//if(!Util::Map::findKey(states_, type)) return nullptr;
// currentState()->onExit();
// stateStack_.pop();
// stateStack_.push(states_[type].get());
// engine_.uiManager().notifyStateChange(currentState()->type());
// currentState()->onEnter();
// return currentState();
}
void game_state_machine::update() {
void game_state_machine::update(float dt) {
if(current_state_)
current_state_->update();
current_state_->update(dt);
}
} //namespace gdw
......@@ -5,6 +5,8 @@
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <ecs/entity.hpp>
#include <game_state_machine/game_state_machine.h>
#include <game_state_machine/play_state.h>
namespace gdw {
......@@ -17,7 +19,7 @@ menu_state::~menu_state() {}
void menu_state::on_enter() {
}
void menu_state::update() {
void menu_state::update(float dt) {
}
void menu_state::on_exit() {
......
......@@ -5,6 +5,8 @@
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <ecs/entity.hpp>
#include <game_state_machine/game_state_machine.h>
#include <game_state_machine/menu_state.h>
namespace gdw {
......@@ -17,8 +19,7 @@ play_state::~play_state() {}
void play_state::on_enter() {
}
void play_state::update() {
}
void play_state::update(float dt) {}
void play_state::on_exit() {
......
......@@ -7,8 +7,8 @@
#include <util/stacktrace.hpp>
int main(int argc, char* argv[]) {
try {
gdw::initStacktrace(argv[0]);
try {
//gdw::initStacktrace(argv[0]);
gdw::engine e;
e.run();
} catch (std::exception& ex) {
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment