coeng
changeset 1:b0d8d454c546
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 05 Feb 2015 00:38:59 +0200 |
parents | 14e743b53289 |
children | 4a1c9597f4d3 |
files | src/comp.h src/gameobj.cc src/gameobj.h |
diffstat | 3 files changed, 132 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- a/src/comp.h Wed Feb 04 17:23:35 2015 +0200 1.2 +++ b/src/comp.h Thu Feb 05 00:38:59 2015 +0200 1.3 @@ -1,18 +1,44 @@ 1.4 #ifndef COMP_H_ 1.5 #define COMP_H_ 1.6 1.7 +#include <string> 1.8 #include <vmath/vmath.h> 1.9 1.10 +class GameObject; 1.11 + 1.12 class Component { 1.13 +protected: 1.14 + char *name; 1.15 + GameObject *parent; 1.16 + int upd_prio; // update priority (0: normal) 1.17 + 1.18 public: 1.19 Component() {} 1.20 virtual ~Component() {} 1.21 + 1.22 + const char *get_name() const; 1.23 + 1.24 + virtual void update(); 1.25 + 1.26 + bool operator <(const Component &c) const; // for sorting based on priority 1.27 +}; 1.28 + 1.29 +class CompXForm : public Component { 1.30 +public: 1.31 + Matrix4x4 xform; 1.32 + 1.33 + CompXForm(); 1.34 }; 1.35 1.36 class CompPRS : public Component { 1.37 +private: 1.38 + CompXForm *co_xform; // cached xform component of the parent object 1.39 + 1.40 public: 1.41 Vector3 pos, scale; 1.42 Quaternion rot; 1.43 + 1.44 + void update(); 1.45 }; 1.46 1.47 #endif // COMP_H_
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/gameobj.cc Thu Feb 05 00:38:59 2015 +0200 2.3 @@ -0,0 +1,77 @@ 2.4 +#include <algorithm> 2.5 +#include "gameobj.h" 2.6 + 2.7 +GameObject::GameObject() 2.8 +{ 2.9 + sorted = true; 2.10 +} 2.11 + 2.12 +GameObject::~GameObject() 2.13 +{ 2.14 + for(size_t i=0; i<comp.size(); i++) { 2.15 + delete comp[i]; 2.16 + } 2.17 +} 2.18 + 2.19 +bool GameObject::add_component(Component *c) 2.20 +{ 2.21 + const char *name = c->get_name(); 2.22 + 2.23 + if(comp_by_name.find(name) != comp_by_name.end()) { 2.24 + fprintf(stderr, "component %s already exists in this game object (%p)\n", name, (void*)this); 2.25 + return false; 2.26 + } 2.27 + 2.28 + try { 2.29 + comp.push_back(c); 2.30 + comp_by_name[name] = c; 2.31 + } 2.32 + catch(...) { 2.33 + fprintf(stderr, "failed to add component: %s\n", name); 2.34 + return false; 2.35 + } 2.36 + 2.37 + sorted = false; 2.38 + return true; 2.39 +} 2.40 + 2.41 +bool GameObject::remove_component(Component *c) 2.42 +{ 2.43 + for(size_t i=0; i<comp.size(); i++) { 2.44 + if(comp[i] == c) { 2.45 + comp.erase(comp.begin() + i); 2.46 + comp_by_name.erase(c->get_name()); 2.47 + return true; 2.48 + } 2.49 + } 2.50 + return false; 2.51 +} 2.52 + 2.53 +bool GameObject::delete_component(Component *c) 2.54 +{ 2.55 + if(remove_component(c)) { 2.56 + delete c; 2.57 + return true; 2.58 + } 2.59 + return false; 2.60 +} 2.61 + 2.62 +Component *GameObject::get_component(const char *name) const 2.63 +{ 2.64 + std::map<std::string, Component*>::const_iterator it; 2.65 + if((it = comp_by_name.find(name)) != comp_by_name.end()) { 2.66 + return it->second; 2.67 + } 2.68 + return 0; 2.69 +} 2.70 + 2.71 +void GameObject::update() 2.72 +{ 2.73 + if(!sorted) { 2.74 + std::sort(comp.begin(), comp.end()); 2.75 + } 2.76 + 2.77 + for(size_t i=0; i<comp.size(); i++) { 2.78 + comp[i]->update(); 2.79 + } 2.80 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/gameobj.h Thu Feb 05 00:38:59 2015 +0200 3.3 @@ -0,0 +1,29 @@ 3.4 +#ifndef GAMEOBJECT_H_ 3.5 +#define GAMEOBJECT_H_ 3.6 + 3.7 +#include <vector> 3.8 +#include <map> 3.9 +#include <string> 3.10 +#include "comp.h" 3.11 + 3.12 +class GameObject { 3.13 +private: 3.14 + std::vector<Component*> comp; 3.15 + std::map<std::string, Component*> comp_by_name; 3.16 + 3.17 + bool sorted; 3.18 + 3.19 +public: 3.20 + GameObject(); 3.21 + ~GameObject(); 3.22 + 3.23 + bool add_component(Component *c); // takes ownership 3.24 + bool remove_component(Component *c); 3.25 + bool delete_component(Component *c); 3.26 + 3.27 + Component *get_component(const char *name) const; 3.28 + 3.29 + void update(); 3.30 +}; 3.31 + 3.32 +#endif // GAMEOBJECT_H_