00001 #ifndef CSACTION_H 00002 #define CSACTION_H 00003 00004 #include <string> 00005 #include <vector> 00006 #include "CSTypes.h" 00007 #include "CSAnimation.h" 00008 #include "CSLoadable.h" 00009 #include "CSLog.h" 00010 00011 class CSActionLoader; 00012 class CSAction; 00013 00014 const int NORTH_ACTION = 0; 00015 const int SOUTH_ACTION = 1; 00016 const int WEST_ACTION = 2; 00017 const int EAST_ACTION = 3; 00018 const int STAY_ACTION = 4; 00019 00020 typedef std::vector<CSAction *> CSActions; 00021 00022 //! data classes are used for loading a loadable class 00023 //! first the data is loaded via xml into this "DATA"-class 00024 //! from this data - the actual action is created 00025 //! the data can be reused to create another instance of an 00026 //! action class - though probably not needed... 00027 class CSActionData 00028 { 00029 public: 00030 char *id; //!< id of action, unique 00031 int type; 00032 char *nextAction; //!< (default) nextAction (id) 00033 bool isBreakable; //!< is this action breakable in the midth of phases 00034 unsigned int repeatable; //!< status of repeatable, see animation 00035 00036 unsigned int envoker; //!< from what is this action envoked 00037 unsigned int sizePhases; //!< how many phases? 00038 unsigned int *phases; //!< arrayPointer # sizePhases 00039 StringVector *animationNames; //!< animations filenames (xml) 00040 00041 CSActionData() 00042 { 00043 id = 0; 00044 nextAction = 0; 00045 envoker = 0; 00046 isBreakable = true; 00047 repeatable = 0; 00048 animationNames = 0; 00049 sizePhases = 0; 00050 phases = 0; 00051 } 00052 00053 ~CSActionData() 00054 { 00055 if (id != 0) 00056 { 00057 free (id); 00058 id = 0; 00059 } 00060 if (nextAction != 0) 00061 { 00062 free (nextAction); 00063 nextAction = 0; 00064 } 00065 if (phases != 0) 00066 { 00067 delete []phases; 00068 phases = 0; 00069 } 00070 if (animationNames != 0) 00071 { 00072 for (StringVector::iterator iter = animationNames->begin(); iter != animationNames->end(); iter++) 00073 { 00074 delete *iter; 00075 } 00076 delete animationNames; 00077 animationNames = 0; 00078 } 00079 } 00080 }; 00081 00082 //! the states hold all variable parts needed for printing the 00083 //! action on screen 00084 //! putting these in a state class, enables us to 00085 //! reuse the memory expensive action class! 00086 class ActionState 00087 { 00088 public: 00089 AnimationState mAnimationState; //!< part of each action are animations 00090 //!< so is the state part of this state 00091 00092 bool mForward; //!< in animation sequence going ++ or -- 00093 bool mFinished; //!< if a NOT_REPEATABLE animation, 00094 //!< this is false upon "one round" 00095 CSAnimation *mAnimation; //!< current animation 00096 signed int mCurrentPhase; //!< current "active" phaseNo 00097 unsigned int mCurrentAnimation; //!< current active animation, corresponse to # 00098 ActionState() 00099 { 00100 mForward = true; //!< in animation sequence going ++ or -- 00101 mFinished = false; //!< if a NOT_REPEATABLE animation, 00102 mAnimation = 0; 00103 mCurrentPhase = -1; 00104 mCurrentAnimation = -1; //!< current active animation, corresponse to 00105 } 00106 virtual ~ActionState() {} 00107 }; 00108 00109 /** 00110 This file defines CSAction: 00111 00112 CSAction can only be created thru a loader (CSActionLoader). 00113 CSAction are defined by (now ONLY thru) an xml-File, like: 00114 DTD: 00115 \verbatim 00116 <!ELEMENT ACTION (ID,IS_BREAKABLE,REPEATABLE,(PHASE)+,(ANIMATION)+,ENVOKER,(DEFAULT_NEXT_ACTION)?)> 00117 <!ELEMENT ID (#PCDATA)> 00118 <!ELEMENT IS_BREAKABLE (#PCDATA)> 00119 <!ELEMENT REPEATABLE (#PCDATA)> 00120 <!ELEMENT PHASE (#PCDATA)> 00121 <!ELEMENT ANIMATION (#PCDATA)> 00122 <!ELEMENT ENVOKER (#PCDATA)> 00123 <!ELEMENT DEFAULT_NEXT_ACTION (#PCDATA)> 00124 \endverbatim 00125 Example: 00126 00127 \verbatim 00128 <ACTION> 00129 <ID>PAC_DOWN_ACTION</ID> 00130 <IS_BREAKABLE>&true;</IS_BREAKABLE> 00131 <REPEATABLE>&INVERSE_ONCE;</REPEATABLE> 00132 <PHASE>0</PHASE> 00133 <ANIMATION>animation\\pac_down.xml</ANIMATION> 00134 <ENVOKER>&DOWN;</ENVOKER> 00135 <DEFAULT_NEXT_ACTION>PAC_DOWN_STAY_ACTION</DEFAULT_NEXT_ACTION> 00136 </ACTION> 00137 \endverbatim 00138 <PRE> 00139 ID - supposed a unique identifier 00140 IS_BREAKABLE - if an action can be interrupted by some action 00141 REAPEATABLE - one of: 00142 ACTION_NOT_REPEATABLE 0 00143 ACTION_REPEATABLE 1 00144 ACTION_INVERSE_ONCE 2 00145 ACTION_INVERSE_REPEATABLE 3 00146 ACTION_RANDOM 4 00147 00148 PHASES - many phases, each phase is represented by a number 00149 the number corresponse to the ANIMATION that is 00150 loaded with the ini - file (numbered from top to 00151 bottom. 00152 NOTE: 00153 REAPEATABLE ACTION_INVERSE_ONCE 00154 PHASES 0 00155 PHASES 1 00156 PHASES 2 00157 PHASES 3 00158 PHASES 4 00159 00160 gives the same result as: 00161 REAPEATABLE ACTION_NOT_REPEATABLE 00162 PHASES 0 00163 PHASES 1 00164 PHASES 2 00165 PHASES 3 00166 PHASES 4 00167 PHASES 3 00168 PHASES 2 00169 PHASES 1 00170 PHASES 0 00171 00172 ANIMATION - many animations are possible! 00173 describes the animation to be loaded, 00174 parameter gives the ini-file for a CSAnimation 00175 00176 ENVOKER - what 'envokes' the action, usually for a sprite 00177 this will be something like UP - DOWN 00178 (see definition in message.h) 00179 00180 DEFAULT_NEXT_ACTION - the unique id of another action, that will be envoked 00181 if this action 'ends' 00182 (JUMP - might be an action, but at some stage JUMP WILL end, 00183 than another action will per default be envoked, 00184 like STAND) 00185 This parameter is optional, it overrides (if set), the 00186 DEFAULT_ACTION of a CSSprite. 00187 This default - actions is not loaded with this action. 00188 To have any effect it must also be loaded with the 00189 corresponding CSSprite. 00190 00191 Methods: 00192 unsigned int getMaxX(); 00193 unsigned int getMaxY(); 00194 void setSecPerFrame(float secPerFrame); 00195 00196 ActionState buildState(); 00197 void resetState(ActionState &state); 00198 void startAction(ActionState &state); 00199 void startNextAction(ActionState &state); 00200 bool isBreakable(ActionState &state); 00201 virtual bool next(ActionState &state); // if finished and not repeat -> false, true otherwise 00202 virtual void display(ActionState &state); 00203 00204 // inlines 00205 unsigned int getEnvoker() 00206 bool isAction(char *id); 00207 char *getId() // do not free return pointer! 00208 char *getDefaultAction() // do not free return pointer! 00209 </PRE> 00210 the action class has no instance relevant data (state independant) 00211 thus one instance can be reused 00212 one action can be (re) used severall times 00213 the data used for printing must come from the outside (ActionState) 00214 */ 00215 class CSAction 00216 { 00217 friend CSActionLoader; 00218 private: 00219 bool mIsBreakable; //!< can action be interrupted? 00220 //!< by events 00221 char *mId; //!< id of action (unique) 00222 int mType; 00223 char *mNextAction; //!< id of next (default) action 00224 unsigned int mRepeatable; //!< status of repeatable, see animation 00225 unsigned int mSizePhases; //!< how many phases 00226 unsigned int *mPhases; //!< arrayPointer # mSizePhases 00227 unsigned int mEnvoker; //!< envoker corresponds to subtype of a ActionMessage 00228 //!< see message.h 00229 CSAnimations *mAnimations; //!< all animations this action is made of 00230 00231 //! constructor using "*.xml" file 00232 CSAction(const std::string &filename); 00233 static void loadActionData(const std::string &filename, CSActionData &data); 00234 00235 void initialize(const CSActionData &data); 00236 void initialize(); 00237 00238 public: 00239 CSAction(const CSAction &action); 00240 virtual ~CSAction(); 00241 static const char *CLASS; 00242 virtual std::string getType() {return (std::string) CLASS;} 00243 00244 unsigned int getMaxX(); 00245 unsigned int getMaxY(); 00246 void setSecPerFrame(float secPerFrame); 00247 bool isAction(char *id); 00248 00249 ActionState buildState(); 00250 void resetState(ActionState &state); 00251 void startAction(ActionState &state); 00252 void startNextAction(ActionState &state); 00253 bool isBreakable(ActionState &state); 00254 virtual bool next(ActionState &state); //!< if finished and not repeat -> false, true otherwise 00255 virtual void display(ActionState &state); 00256 void setAlpha(int alpha); 00257 00258 unsigned int getEnvoker() {return mEnvoker;} 00259 char *getId() {return mId;} //!< do not free return pointer! 00260 int getActionType() {return mType;} 00261 char *getDefaultAction() {return mNextAction;} //!< do not free return pointer! 00262 }; 00263 00264 //! factory - class 00265 //! that is used to create CSAction 00266 //! uses xml - data-files, produces "singletons" 00267 class CSActionLoader : public Loadable<CSAction> 00268 { 00269 protected: 00270 virtual CSAction *create(const std::string &filename) 00271 { 00272 return new CSAction(filename); 00273 } 00274 00275 public: 00276 static CSActionLoader INSTANCE; 00277 }; 00278 00279 #endif // CSACTION_H