1 /*
2  * Holotz's Castle
3  * Copyright (C) 2004 Juan Carlos Seijo P�rez
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc., 59
17  * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Juan Carlos Seijo P�rez
20  * jacob@mainreactor.net
21  */
22 
23 /** Holotz's Castle character definition file.
24  * @file    HCCharacter.h
25  * @author  Juan Carlos Seijo P�rez
26  * @date    30/04/2004
27  * @version 0.0.1 - 30/04/2004 - First version.
28  */
29 
30 #ifndef _HCCHARACTER_INCLUDED
31 #define _HCCHARACTER_INCLUDED
32 
33 #include <JLib/Util/JTypes.h>
34 #include <JLib/Util/JString.h>
35 #include <JLib/Util/JFile.h>
36 #include <JLib/Graphics/JImageSprite.h>
37 #include <HCTheme.h>
38 #include <HCMap.h>
39 #include <HCRope.h>
40 #include <HCText.h>
41 #include <HCPreferences.h>
42 
43 class HCed;
44 
45 enum HCCharacterState
46 {
47 	HCCS_STOP = 0,                        /**< Stopped state. */
48 	HCCS_RIGHT,                           /**< Walk-right state. */
49 	HCCS_LEFT,                            /**< Walk-left state. */
50 	HCCS_UP,                              /**< Up state. */
51 	HCCS_DOWN,                            /**< Down state. */
52 	HCCS_SLIDE,                           /**< Slide state. */
53 	HCCS_JUMP,                            /**< Jump left state. */
54 	HCCS_JUMPLEFT,                        /**< Jump left state. */
55 	HCCS_JUMPRIGHT,                       /**< Jump right state. */
56 	HCCS_FALL,                            /**< Fall state. */
57 	HCCS_DIE,                             /**< Die state. */
58 	HCCS_HANG,                            /**< Hang state. */
59 	HCCS_COUNT,                           /**< State count. */
60 };
61 
62 // Requested actions for the character
63 #define HCCA_STOP   0x00
64 #define HCCA_UP     0x01
65 #define HCCA_DOWN   0x02
66 #define HCCA_LEFT   0x04
67 #define HCCA_RIGHT  0x08
68 #define HCCA_JUMP   0x10
69 
70 class HCCharacter : public JDrawable
71 {
72 	friend class HCed;
73 
74  protected:
75 	JImageSprite states[HCCS_COUNT];      /**< Table of state drawables. */
76 	HCCharacterState state;               /**< Current state of the character. */
77 	u32 lastAction;                       /**< Last action of the character. */
78 	u32 actions;                          /**< Actions requested for the character. */
79 	HCMap *map;                           /**< Map for this character. */
80 	JVector v;                            /**< Veloccity. */
81 	JVector a;                            /**< Acceleration. */
82 	JVector vMax;                         /**< Maximum veloccity. */
83 	JVector aMax;                         /**< Maximum acceleration. */
84 	JVector vJumpMax;                     /**< Maximum veloccity for jumps. */
85 	JTimer timer;                         /**< Timer for jumps. */
86 
87 	s32 startJumpRow;                     /**< Row at where jump took place. */
88 	s32 maxFallRows;                      /**< Max. number of cells to free fall if not reached floor or a rope yet. */
89 	s32 maxJumpRows;                      /**< Max. number of cells in vertical it can jump. */
90 	s32 maxJumpCols;                      /**< Max. number of cells in horizontal it can jump. */
91 	s32 breakRow;                         /**< Row starting break floor, -1 if none. */
92 	s32 breakCol;                         /**< Column starting break floor. */
93 	s32 hangCheckYOffset;                 /**< Offset within character for hanging cheks. */
94 	s32 hangCheckSize;                    /**< Size of the square side centered at the offset for hanging cheks. */
95 
96 	JString name;                         /**< Name of the character. */
97 	s32 subtype;                          /**< Subtype within the theme. */
98 
99 	HCRope **ropes;                       /**< Ropes to check. */
100 	s32 numRopes;                         /**< Number of ropes to check. */
101 	HCRope *trackRope;                    /**< Rope to track when hanging. */
102 
103 	HCText *dialog;                       /**< Dialog to say. */
104 
105  public:
106 
107 	/** Creates the character. Init must be called before starting to use it.
108 	 */
109 	HCCharacter();
110 
111 	/** Initializes the character.
112 	 * @param  sprites Sprites for this character.
113 	 * @param  _map Map for this character.
114 	 * @param  _ropes Ropes this character must check.
115 	 * @param  nRopes Number of ropes this character must check.
116 	 * @return <b>true</b> if everything goes well, <b>false</b> otherwise.
117 	 */
118 	virtual bool Init(JImageSprite *sprites, HCMap *_map, HCRope **_ropes = 0, s32 nRopes = 0);
119 
120 	/** Draws the character.
121 	 */
122 	virtual void Draw();
123 
124 	/** Updates the character. The general character's update pipeline is:
125 	 * <pre>
126 	 * 1. Update sprite
127 	 * 2. If hanging, do nothing (return)
128 	 * 3. Update vellocity.
129 	 * 4. Update collisions with the map
130 	 * 6. Update their dialog
131 	 * </pre>
132 	 * @return Return value of the associated drawable's update.
133 	 */
134 	virtual s32 Update();
135 
136 	/** Updates the veloccity of the character based upon the character's acceleration and the map's gravity.
137 	 */
138 	virtual void UpdateVeloccity();
139 
140 	/** Updates collisions with the map and modifies the veloccity according to the result of the collision tests.
141 	 */
142 	virtual void UpdateCollisions();
143 
144 	/** Updates this character's speech dialog.
145 	 */
146 	virtual void UpdateDialog();
147 
148 	/** Check if the character must fall upon a broken floor and changes its state if so.
149 	 */
150 	virtual void CheckBrokenFloor();
151 
152 	/** Check if the character must hang of a rope.
153 	 */
154 	virtual void CheckHang();
155 
156 	/** Updates the character in the Stop state.
157 	 * @return Return value of the associated drawable's update.
158 	 */
159 	virtual s32 UpdateStop();
160 
161 	/** Updates the character in the Right state.
162 	 * @return Return value of the associated drawable's update.
163 	 */
164 	virtual s32 UpdateRight();
165 
166 	/** Updates the character in the Left state.
167 	 * @return Return value of the associated drawable's update.
168 	 */
169 	virtual s32 UpdateLeft();
170 
171 	/** Updates the character in the Up state.
172 	 * @return Return value of the associated drawable's update.
173 	 */
174 	virtual s32 UpdateUp();
175 
176 	/** Updates the character in the Down state.
177 	 * @return Return value of the associated drawable's update.
178 	 */
179 	virtual s32 UpdateDown();
180 
181 	/** Updates the character in the Slide state.
182 	 * @return Return value of the associated drawable's update.
183 	 */
184 	virtual s32 UpdateSlide();
185 
186 	/** Updates the character in the Jump state.
187 	 * @return Return value of the associated drawable's update.
188 	 */
189 	virtual s32 UpdateJump();
190 
191 	/** Updates the character in the JumpLeft state.
192 	 * @return Return value of the associated drawable's update.
193 	 */
194 	virtual s32 UpdateJumpLeft();
195 
196 	/** Updates the character in the JumpRight state.
197 	 * @return Return value of the associated drawable's update.
198 	 */
199 	virtual s32 UpdateJumpRight();
200 
201 	/** Updates the character in the Fall state.
202 	 * @return Return value of the associated drawable's update.
203 	 */
204 	virtual s32 UpdateFall();
205 
206 	/** Updates the character in the Die state.
207 	 * @return Return value of the associated drawable's update.
208 	 */
209 	virtual s32 UpdateDie();
210 
211 	/** Updates the character in the Hang state.
212 	 * @return Return value of the associated drawable's update.
213 	 */
214 	virtual s32 UpdateHang();
215 
216 	/** Set this character's position.
217 	 * @param  x New X coordinate.
218 	 * @param  y New Y coordinate.
219 	 */
220 	virtual void Pos(float x, float y);
221 
222 	/** Gets this character's position.
223 	 * @return Character's position.
224 	 */
Pos()225 	virtual const JVector & Pos() {return pos;}
226 
227 	/** Gets the current row.
228 	 * @return The row this character is at.
229 	 */
230 	s32 Row();
231 
232 	/** Gets the current column.
233 	 * @return The column this character is at.
234 	 */
235 	s32 Col();
236 
237 	/** Fix the character position to the actual row and col.
238 	 * @param  col Column to be fixed at.
239 	 * @param  row Row to be fixed at.
240 	 */
241 	void FixPos(s32 col, s32 row);
242 
243 	/** Fix the character's x position to the actual col.
244 	 * @param  col Column to be fixed at.
245 	 */
246 	void FixX(s32 col);
247 
248 	/** Fix the character's y position to the actual row.
249 	 * @param  row Row to be fixed at.
250 	 */
251 	void FixY(s32 row);
252 
253 	/** Changes the state of the character.
254 	 * @param  newState New state for this character.
255 	 */
256 	void State(HCCharacterState newState);
257 
258 	/** Returns the state of the character.
259 	 * @return the state of the character.
260 	 */
State()261 	HCCharacterState State() {return state;}
262 
263 	/** Returns the name of this character.
264 	 */
Name()265 	const JString& Name() {return name;}
266 
267 	/** Sets the name of this character. The name will be the base directory
268 	 * from where to load its resources.
269 	 */
Name(const JString & _name)270 	void Name(const JString &_name) {name = _name;}
271 
272   /** Gets the subtype of this character.
273    * @return Subtype of this character.
274    */
Subtype()275   s32 Subtype() {return subtype;}
276 
277   /** Sets the subtype of this character.
278    * @param newSubtype New type of this character.
279    */
Subtype(s32 newSubtype)280   void Subtype(s32 newSubtype) {subtype = newSubtype;}
281 
282   /** Gets the dialog of this character.
283    * @return Dialog of this character.
284    */
Dialog()285   HCText * Dialog() {return dialog;}
286 
287   /** Sets the dialog for this character.
288    * @param newDialog New type of this character.
289    */
Dialog(HCText * newDialog)290   void Dialog(HCText *newDialog) {dialog = newDialog;}
291 
292 	/** Returns the current image sprite used.
293 	 * @return Current image sprite used.
294 	 */
CurSprite()295 	JImageSprite *CurSprite() {return &states[state];}
296 
297 	/** Tries to put the character in the given state.
298 	 * @param  st State to try to reach.
299 	 * @return <b>true</b> if the state could be changed, <b>false</b> otherwise.
300 	 */
301 	bool Try(HCCharacterState st);
302 
303 	/** Sets the actions this character must perform.
304 	 * @param  act Actions requested as a bitmask.
305 	 * @return <b>true</b> if the state could be changed, <b>false</b> otherwise.
306 	 */
307 	bool Act(u32 act);
308 
309 	/** Returns the maximum veloccity vector for this character.
310 	 */
MaxVeloccity()311 	JVector & MaxVeloccity() {return vMax;}
312 
313 	/** Returns the maximum jump veloccity vector for this character.
314 	 */
JumpMaxVeloccity()315 	JVector & JumpMaxVeloccity() {return vJumpMax;}
316 
317 	/** Returns the veloccity vector for this character.
318 	 */
Veloccity()319 	JVector & Veloccity() {return v;}
320 
321 	/** Returns the acceleration vector for this character.
322 	 */
Acceleration()323 	JVector & Acceleration() {return a;}
324 
325 	/** Returns the maximum jump rows parameter for this character.
326 	 */
MaxJumpRows()327 	s32 MaxJumpRows() {return maxJumpRows;}
328 
329 	/** Sets the maximum jump rows parameter for this character. Computes the
330 	 * maximum jump y veloccity of the character, also.
331 	 */
332 	void MaxJumpRows(s32 newMaxJumpRows);
333 
334 	/** Loads the character.
335 	 * @param file File opened and positioned already.
336 	 * @return 0 if it succeeds, 1 if there is an I/O error, 2 if integrity error.
337 	 */
338 	u32 Load(JRW &file);
339 
340 	/** Saves the character. The file must be opened and positioned already.
341 	 * @param file File opened and positioned already.
342 	 * @return 0 if it succeeds, 1 if there is an I/O error, 2 if integrity error.
343 	 */
344 	u32 Save(JRW &file);
345 
346 	/** Destroys the character.
347 	 */
348 	void Destroy();
349 
350 	/** Destroys the character.
351 	 */
~HCCharacter()352 	virtual ~HCCharacter() {Destroy();}
353 };
354 
355 #endif // _HCCHARACTER_INCLUDED
356