|
mkRPG
|
00001 #ifndef GAME_H 00002 #define GAME_H 00003 00004 #include "object.h" 00005 #include "map.h" 00006 #include "entity.h" 00007 00008 00014 #define DefaultType(type,Type) private: Type *default##Type = new Type(*this); public: Type &type() const{return *default##Type;} 00015 00020 class World; 00027 class DefaultTypes : public GameObject 00028 { 00029 public: 00030 TypeName() 00031 DefaultTypes(World &parent); 00032 bool isEditable() const{return false;} 00033 C0(DefaultType,c,C,ellType) 00034 C0(DefaultType,m,M,apType) 00035 C0(DefaultType,o,O,bjectType) 00036 C0(DefaultType,e,E,ntityType) 00037 }; 00038 00039 00040 00048 template<class Type> 00049 class GameObjectList : public GameObject 00050 { 00051 public: 00052 TypeName() 00053 GameObjectList(const QString &name, GameObject &parent); 00054 bool isEditable() const{return false;} 00055 00056 void addGameObject(Type &go); 00057 void removeGameObject(int id); 00058 QList<Type*> gameObjects(); 00059 Type &gameObject(int id); 00060 private: 00061 QMap<int, Type*> aGameObjects; 00062 }; 00063 00064 00065 template<class Type> 00066 GameObjectList<Type>::GameObjectList(const QString &name, GameObject &parent) : 00067 GameObject(parent), aGameObjects(QMap<int,Type*>()) 00068 { 00069 setName(name); 00070 } 00071 00072 template<class Type> 00073 void GameObjectList<Type>::addGameObject(Type &go){ 00074 aGameObjects[go.ident()] = &go; 00075 go.setParent(this); 00076 } 00077 00078 template<class Type> 00079 void GameObjectList<Type>::removeGameObject(int id){ 00080 assert(aGameObjects.contains(id)); 00081 aGameObjects[id]->setParent(parent()->parent()); 00082 aGameObjects.remove(id); 00083 } 00084 00085 template<class Type> 00086 Type &GameObjectList<Type>::gameObject(int id){ 00087 assert(aGameObjects.contains(id)); 00088 return *aGameObjects.value(id); 00089 } 00090 00091 template<class Type> 00092 QList<Type*> GameObjectList<Type>::gameObjects(){ 00093 return aGameObjects.values(); 00094 } 00095 00096 00097 00098 00099 #define ListDef(Type, Types) private: GameObjectList<Type> *a##Types; public: 00102 #define ListAdd(type, Type, Types) void add##Type(Type *type) const{a##Types->addGameObject(*type);} 00105 #define ListRemove(Type, Types) void remove##Type(int id) const{a##Types->removeGameObject(id);} 00108 #define ListSetter(type, Type, Types) ListAdd(type, Type, Types) ListRemove(Type,Types) 00111 #define ListGetAll(types, Type, Types) QList<Type*> types() const{return a##Types->gameObjects();} 00114 #define ListGetOne(type, Type, Types) Type &type(int id) const{return a##Types->gameObject(id);} 00117 #define ListGetter(type, types, Type, Types) ListGetAll(types, Type, Types) ListGetOne(type, Type, Types) 00120 #define List(type, types, Type, Types) ListDef(Type, Types) ListSetter(type, Type, Types) ListGetter(type, types, Type, Types) 00123 #define ListD(init,Init,body,sg,pl) List(init##body##sg,init##body##pl,Init##body##sg,Init##body##pl) 00126 #define ListInit(Type, Types) a##Types = new GameObjectList<Type>(QObject::tr(#Types),*this); 00129 #define ListInitD(Body, sg,pl) ListInit(Body##sg, Body##pl) 00141 class GameObjectInventory : public GameObject 00142 { 00143 public: 00144 TypeName() 00145 GameObjectInventory(World &parent); 00146 bool isEditable() const{return false;} 00147 //ObjectList(type,Type,) 00148 00149 ListD(m,M,ap,,s) 00150 ListD(o,O,bject,,s) 00151 ListD(e,E,ntit,y,ies) 00152 }; 00153 00154 00155 00156 00157 00161 class World : public GameObject 00162 { 00163 public: 00164 TypeName(World) 00165 World(Game *g, GameObject *aParent); 00166 00167 /*const*/ DefaultTypes &types() /*const*/; 00168 /*const*/ GameObjectInventory &objects() /*const*/; 00169 private: 00170 DefaultTypes *aTypes; 00171 GameObjectInventory *aObjects; 00172 }; 00173 00174 00185 class Game : public GameObject 00186 { 00187 public: 00188 TypeName(Game) 00189 Game(); 00190 ~Game(); 00191 int newIdent(GameObject *obj); 00192 void aboutToDestroy(GameObject *obj); 00193 GameObject *object(int id); 00199 inline World &world(){return *w;} 00200 inline Map* currentMap(){return map;} 00201 void setCurrentMap(Map *m); 00202 00203 //void addImage(Image *im); 00204 00205 QList<QString> actions() const; 00206 Action *action(const QString &a); 00207 QString addAction(QString a, Action *act); 00208 void removeAction(const QString &a); 00209 QString renameAction(const QString &a, const QString &nv); 00210 00211 ListD(i,I,mage,,s) 00212 00213 private: 00214 int idDisp; 00215 QMap<int, GameObject*> objects; 00216 00217 World *w; 00218 Map *map; 00219 QMap<int, Image*> picts; 00220 QMap<int, QString> strings; 00221 QMap<QString, Action*> aActions; 00222 }; 00223 00224 00225 00226 00227 #endif // GAME_H
1.7.6.1