|
mkRPG
|
Definition of the base class GameObject, and some inherited classes. More...
#include <QtGui>#include <assert.h>#include <algorithm>Go to the source code of this file.
Data Structures | |
| class | Parameter |
| The Parameter class represents a parameter with a valid domain of value. More... | |
| class | Event |
| The Event class describes the. More... | |
| class | Order |
| The Order class. More... | |
| class | Action |
| The Action class describes a game Action, which if the basal part to enable animated games. More... | |
| class | GameObject |
| The GameObject class is the base class for every part of games. More... | |
| class | InheritableObject |
| The InheritableObject class is the base class for every objects that can inherit attributes from an other InheritableObject. More... | |
| class | GameObjectType |
| The GameObjectType class represents types of gameobjects that can be inherited and will be used in TypedObject. More... | |
Defines | |
| #define | UNUSED(p) |
| #define | TypeName(Type) virtual QString typeName() const{return QObject::tr(#Type);} |
| #define | C(Macro, init, Init, body,...) Macro(init##body, Init##body, ##__VA_ARGS__) |
| #define | C0(Macro, init, Init, body) Macro(init##body, Init##body) |
| #define | C1(Macro, init, Init, body, arg) Macro(init##body, Init##body, arg) |
| #define | ProtectFlag(flag) reserved.insert(QString(#flag)); |
| #define | SetFlag(flag, value) setFlag(#flag,value) |
| #define | FlagGetter(flag, Flag) inline bool is##Flag() const{return getFlag(#flag);} |
| #define | FlagSetter(flag, Flag) inline void set##Flag(bool flag){SetFlag(flag,flag); touch();} |
| #define | Flag(flag, Flag) FlagGetter(flag, Flag) FlagSetter(flag, Flag) |
| #define | ProtectParam(param) reserved.insert(QString(#param)); |
| #define | SetParam(param, value) setParam(#param,value) |
| #define | ParamGetter(param) inline int param() const{return getParam(#param);} |
| #define | ParamSetter(param, Param) inline void set##Param(int param##Value){SetParam(param,param##Value); touch();} |
| #define | ParamMGetter(param, Param, M) inline int param##M() const {return getParam##M(#param);} |
| #define | ParamMSetter(param, Param, M) void set##Param##M(int param##M) {setParam##M(#param, param##M);} |
| #define | ParamMin(param, Param) ParamMGetter(param, Param, Min) ParamMSetter(param, Param, Min) |
| #define | ParamMax(param, Param) ParamMGetter(param, Param, Max) ParamMSetter(param, Param, Max) |
| #define | ParamDom(param, Param) ParamMin(param,Param) ParamMax(param,Param) |
| #define | Param(param, Param) ParamGetter(param) ParamSetter(param, Param) ParamDom(param,Param) |
| #define | AttrGetter(attr, Attr, Type) inline Type* attr() const{return a##Attr;} |
| #define | AttrFree(Attr) if(a##Attr) a##Attr->removeReference(); |
| #define | AttrLink(Attr) if(a##Attr) a##Attr->addReference(); |
| #define | AttrSetter(attr, Attr, Type) inline void set##Attr(Type* new##Attr){AttrFree(Attr); a##Attr = new##Attr; AttrLink(Attr); touch();} |
| #define | AttrDef(Attr, Type) private: Type* a##Attr = nullptr; public: |
| #define | Attr(attr, Attr, Type) AttrDef(Attr, Type) AttrGetter(attr,Attr,Type) AttrSetter(attr, Attr, Type) |
| #define | AttrT(type, Type) Attr(type, Type, Type) |
Typedefs | |
| typedef QList< QPair< QString, QList< QString > > > | HierarchicalAttr |
Functions | |
| bool | cleverComp (const QString &na, const QString &nb) |
| bool | isValidName (const QString &n) |
Definition of the base class GameObject, and some inherited classes.
## The objects structure
### Inheritance and parent
Each class which is part of game representation has to inherit from GameObject. It gives the class standard params and flags mechanisms that are used for edition purposes.
Each instance must have a parent (except the Game one) which will keep an eye on his children. If the parent cannot be given at the construction time (case or array)s, the init function must then be called as soon as possible.
### Reference count
### Objects destructors
A default implementation of GameObject destructor is provided. It destroy every children the instance has. This avoid fastidious destructor implementation.
## The Macro System
To add conveniently attributes and flags to GameObject subclassed objects, a set of macro is provided.
### Name conventions
For a attribute named attr, the following conventions are observed :
attr() is the getter methodsetAttr() is the setter methodaAttr is the name of the attribut (if any)A specific convention is applied for flags (boolean attributes) :
### Macros
To define a new attribute, a global macro can be used in the class declaration. The provided basic implementations keep the object edition synchronization.
If a cleverer process is needed, custom getter or setter can be implemented, and the getter and setter macros can be used separately to define the obvious methods
**Provided macros**
Attribute type | Complete declaration | Getter | Setter -----------------------------|-----------------------|-------------|------------ Flags (bool) | Flag |FlagGetter |FlagSetter Parameters (int) | Param |ParamGetter |ParamSetter GameObject based Attributes | Attr |AttrGetter |AttrSetter
**The case of attributes**
An additionnal AttrT macro is provided, that deduce a default name from the type.
### Name tools
To make the definition easier and avoid the name repetition that is introduced by the name convention, a C macro is provided to construct the names with lower and upper initial letter from theses letter and the end of the name.
| #define Attr | ( | attr, | |
| Attr, | |||
| Type | |||
| ) | AttrDef(Attr, Type) AttrGetter(attr,Attr,Type) AttrSetter(attr, Attr, Type) |
The Attr macro defines a new <aAttr> named attribute of type Type, with its generic getter and setter methods.
With respect to the name convention, this macro needs the parameter's name with lower and upper initial letter case.
Example
{.cpp}
Attr(parent,Parent, GameObject)
-->
private:
GameObject *aParent;
public:
inline GameObject* parent() const{return aParent;}
inline void setParent(GameObject* parentObject){aParent = parentObject; touch();}
The AttrDef macro defines a private attribute name <aAttr>.
Attr > must exist.Example
{.cpp}
Attr(Parent,GameObject)
-->
private:
GameObject *aParent;
public:
The AttrFree macro decrease the number of references of the GameObject attribut attr. It should be used before the deletion/modifcation of the pointer.
| #define AttrGetter | ( | attr, | |
| Attr, | |||
| Type | |||
| ) | inline Type* attr() const{return a##Attr;} |
The AttrGetter macro defines a generic getter method for the attribute named attr of type Type.
With respect to the name convention, this macro needs the attribute's name with lower and upper initial letter case.
Example
{.cpp}
AttrGetter(parent,Parent,GameObject)
--> inline GameObject* parent() const{return aParent;}
The AttrLink macro increase the number of references of the GameObject attribut attr. It should be used before the saving of a pointer as an attribute.
| #define AttrSetter | ( | attr, | |
| Attr, | |||
| Type | |||
| ) | inline void set##Attr(Type* new##Attr){AttrFree(Attr); a##Attr = new##Attr; AttrLink(Attr); touch();} |
The AttrSetter macro defines a generic setter method for the attribute named attr of type Type.
With respect to the name convention, this macro needs the attribute's name with lower and upper initial letter case.
Example
{.cpp}
AttrSetter(parent,Parent,GameObject)
--> inline void setParent(GameObject* &parentObject){aParent = parentObject; touch();}
The AttrT macro defines a new attribute of type Type, named after the type name, with its generic getter and setter methods.
With respect to the name convention, this macro needs the parameter's type with lower and upper initial letter case.
Example
{.cpp}
AttrT(cellType,CellType)
-->
private:
CellType *aCellType;
public:
inline CellType* cellType() const{return aCellType;}
inline void setCellType(CellType* cellTypeObject){aCellType = cellTypeObject; touch();}
| #define C | ( | Macro, | |
| init, | |||
| Init, | |||
| body, | |||
| ... | |||
| ) | Macro(init##body, Init##body, ##__VA_ARGS__) |
The C macro calls the Macro argument with argument tokens formed by the concatenation of init and body, and Init and body.
This enables to call a macro with the same argument, with the initial letter in lower and upper case.
A custom number of arguments can be added after the body one.
Example
{.cpp}
C(Param,w,W,idth)
--> Param(width, Width)
C(Attr, p,P,arent, GameObject)
--> Attr(parent, Parent, GameObject)
| #define C0 | ( | Macro, | |
| init, | |||
| Init, | |||
| body | |||
| ) | Macro(init##body, Init##body) |
| #define C1 | ( | Macro, | |
| init, | |||
| Init, | |||
| body, | |||
| arg | |||
| ) | Macro(init##body, Init##body, arg) |
The C1 macro is equivalent to the C macro, with one additional argument.
This macro is provided to avoid the use of variadic arguments that are currently not totally supported by some IDE. Example
{.cpp}
C(Attr, p,P,arent, GameObject)
--> Attr(parent, Parent, GameObject)
| #define Flag | ( | flag, | |
| Flag | |||
| ) | FlagGetter(flag, Flag) FlagSetter(flag, Flag) |
The Flag macro defines generic getter and setter methods for the flag named flag.
With respect to the name convention, this macro needs the flag's name with lower and upper initial letter case.
Example
{.cpp}
Flag(visible,Visible)
--> inline bool isVisible() const{return aFlags["visible"];}
inline void setVisible(bool visible){aFlags["visible"] = visible; touch()}
| #define FlagGetter | ( | flag, | |
| Flag | |||
| ) | inline bool is##Flag() const{return getFlag(#flag);} |
The FlagGetter macro defines a generic getter method for the flag named flag.
With respect to the name convention, this macro needs the flag's name with lower and upper initial letter case.
flag named flag really exist. To avoid runtime access error, it is strongly advice to initialize the flag in the object's contructor, using the setter method or the SetFlag macro.Example
{.cpp}
FlagGetter(visible,Visible)
--> inline bool isVisible() const{return aFlags["visible"];}
| #define FlagSetter | ( | flag, | |
| Flag | |||
| ) | inline void set##Flag(bool flag){SetFlag(flag,flag); touch();} |
The FlagSetter macro defines a generic setter method for the flag named flag.
With respect to the name convention, this macro needs the flag's name with lower and upper initial letter case.
Example
{.cpp}
FlagSetter(visible,Visible)
--> inline void setVisible(bool visible){aFlags["visible"] = visible; touch()}
| #define Param | ( | param, | |
| Param | |||
| ) | ParamGetter(param) ParamSetter(param, Param) ParamDom(param,Param) |
The Param macro defines generic getter and setter methods for the parameter named param.
With respect to the name convention, this macro needs the parameter's name with lower and upper initial letter case.
Example
{.cpp}
Param(width,Width)
--> inline int width() const{return aParams["width"];}
inline void setWidth(int widthValue){aParams["width"] = widthValue; touch();}
| #define ParamGetter | ( | param | ) | inline int param() const{return getParam(#param);} |
The ParamGetter macro defines a generic getter method for the parameter named param.
param named parameter really exist. To avoid runtime access error, it is strongly advice to initialize the parameter in the object's contructor, using the setter method or the SetParam macro.Example
{.cpp}
ParamGetter(width)
--> inline int width() const{return aParams["width"];}
| #define ParamSetter | ( | param, | |
| Param | |||
| ) | inline void set##Param(int param##Value){SetParam(param,param##Value); touch();} |
The ParamSetter macro defines a generic setter method for the parameter named param.
With respect to the name convention, this macro needs the parameter's name with lower and upper initial letter case.
Example
{.cpp}
ParamSetter(width,Width)
--> inline void setWidth(bool widthValue){aParams["width"] = widthValue; touch();}
| #define ProtectFlag | ( | flag | ) | reserved.insert(QString(#flag)); |
The ProtectFlag macro registers the flag named flag as protected ie it cannot be modified directly in a flag editor (it will not appear).
| #define ProtectParam | ( | param | ) | reserved.insert(QString(#param)); |
The ProtectParam macro registers the flag named param as protected ie it cannot be modified directly in a param editor (it will not appear).
| #define SetFlag | ( | flag, | |
| value | |||
| ) | setFlag(#flag,value) |
Conveniant macro to set a flag directly.
This is usefull in custom setters, to avoid call loops.
Example
{.cpp}
SetFlag(visible, false)
--> aFlags["visible"] = false
| #define SetParam | ( | param, | |
| value | |||
| ) | setParam(#param,value) |
This macro declares the type name that will be visible to the user.
It must be use in a GameObject inherited class public scope.
| #define UNUSED | ( | p | ) |
Usefull macro to avoid "Unused parameter" warning.
| typedef QList<QPair<QString,QList<QString> > > HierarchicalAttr |
Convenient type that represent the attributes (flags/parameters) organized by the oldest object that defines them.
| bool cleverComp | ( | const QString & | na, |
| const QString & | nb | ||
| ) |
Compares to string classing name_10 after name_2.
| bool isValidName | ( | const QString & | n | ) |
Returns true if the string n contains only letters, digits or "_".
1.7.6.1