object.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "util/base/exception.h"
00031
00032 #include "object.h"
00033 #include "action.h"
00034 #include "abstractpather.h"
00035
00036 namespace FIFE {
00037 Object::Object(const std::string& identifier, const std::string& name_space, Object* inherited):
00038 m_id(identifier),
00039 m_namespace(name_space),
00040 m_inherited(inherited),
00041 m_actions(NULL),
00042 m_blocking(false),
00043 m_static(false),
00044 m_pather(NULL),
00045 m_visual(NULL),
00046 m_defaultaction(NULL) {
00047 }
00048
00049 Object::~Object() {
00050 if (m_actions) {
00051 std::map<std::string, Action*>::const_iterator i(m_actions->begin());
00052 while (i != m_actions->end()) {
00053 delete i->second;
00054 ++i;
00055 }
00056 delete m_actions;
00057 }
00058 delete m_visual;
00059 }
00060
00061 Action* Object::createAction(const std::string& identifier, bool is_default) {
00062 if (!m_actions) {
00063 m_actions = new std::map<std::string, Action*>;
00064 }
00065
00066 std::map<std::string, Action*>::const_iterator it = m_actions->begin();
00067 for(; it != m_actions->end(); ++it) {
00068 if(identifier == it->second->getId()) {
00069 throw NameClash(identifier);
00070 }
00071 }
00072
00073 Action* a = getAction(identifier);
00074 if (!a) {
00075 a = new Action(identifier);
00076 (*m_actions)[identifier] = a;
00077 if (is_default || (!m_defaultaction)) {
00078 m_defaultaction = a;
00079 }
00080 }
00081 return a;
00082 }
00083
00084 Action* Object::getAction(const std::string& identifier) const {
00085 std::map<std::string, Action*>::const_iterator i;
00086 if (m_actions) {
00087 i = m_actions->find(identifier);
00088 }
00089 if ((!m_actions) || (i == m_actions->end())) {
00090 if (m_inherited) {
00091 return m_inherited->getAction(identifier);
00092 }
00093 return NULL;
00094 }
00095 return i->second;
00096 }
00097
00098 std::list<std::string> Object::getActionIds() const {
00099 std::list<std::string> action_ids;
00100 action_ids.clear();
00101 if (m_actions) {
00102 std::map<std::string, Action*>::const_iterator actions_it = m_actions->begin();
00103 for(; actions_it != m_actions->end(); ++actions_it) {
00104 action_ids.push_back(actions_it->first);
00105 }
00106 }
00107 return action_ids;
00108 }
00109
00110 void Object::setPather(AbstractPather* pather) {
00111 m_pather = pather;
00112 }
00113
00114 bool Object::isBlocking() const {
00115 if (m_blocking) {
00116 return true;
00117 }
00118 if (m_inherited) {
00119 return m_inherited->isBlocking();
00120 }
00121 return false;
00122 }
00123
00124 bool Object::isStatic() const {
00125 if (m_static) {
00126 return true;
00127 }
00128 if (m_inherited) {
00129 return m_inherited->isStatic();
00130 }
00131 return false;
00132 }
00133
00134 bool Object::operator==(const Object& obj) const {
00135 return m_id == obj.getId() && m_namespace == obj.getNamespace();
00136 }
00137
00138 bool Object::operator!=(const Object& obj) const {
00139 return m_id != obj.getId() || m_namespace != obj.getNamespace();
00140 }
00141
00142 }