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 /** Definition of script actions.
24  * @file    HCScriptAction.h
25  * @author  Juan Carlos Seijo P�rez
26  * @date    03/07/2004
27  * @version 0.0.1 - 03/07/2004 - Primera versi�n.
28  */
29 
30 #ifndef _HCSCRIPTACTION_INCLUDED
31 #define _HCSCRIPTACTION_INCLUDED
32 
33 #include <JLib/Util/JTypes.h>
34 #include <JLib/Util/JApp.h>
35 #include <JLib/Util/JTextFile.h>
36 #include <JLib/Util/JUtil.h>
37 #include <JLib/Graphics/JFont.h>
38 #include <HCCharacter.h>
39 #include <HCText.h>
40 
41 class HCApp;
42 
43 /** Types of action.
44  */
45 enum HCScriptActionType
46 {
47 	HCSAT_NOP = 0,                        /**< No operation. */
48 	HCSAT_MOVE,                           /**< Moves a character. */
49 	HCSAT_DIALOG,                         /**< Makes a character to talk. */
50 	HCSAT_NARRATIVE,                      /**< Shows a narrative text. */
51 	HCSAT_SOUND,                          /**< Plays a sound. */
52 	HCSAT_WAIT,                           /**< Waits. */
53 };
54 
55 /** Basic action.
56  */
57 class HCScriptAction
58 {
59  public:
60 	HCScriptActionType type;              /**< Type of this action. */
61 	bool finished;                        /**< Indicates whether it has finished or not. */
62 
63 	/** Creates a script action.
64 	 */
type(_type)65 	HCScriptAction(HCScriptActionType _type = HCSAT_NOP) : type(_type), finished(false)
66 	{}
67 
68 	/** Checks if it has finished.
69 	 * @return <b>true<b> if it has finished, <b>false</b> otherwise.
70 	 */
Finished()71 	bool Finished() {return finished;}
72 
73 	/** Loads the action from the file. If the method encounters '[ MOVE ball 2 4 5 ]', it will
74 	 * create and return a new HCScriptActionMove* (MOVE) casted to a HCScriptAction* of
75 	 * the third (2) ball enemy of the level to move left (4) five cells (5).
76 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
77 	 * @return A new action corresponding to the one found or 0 if an error occurred.
78 	 */
79 	static HCScriptAction * Load(JTextFile &f);
80 
81 	/** Returns the appropiate character based upon its name and index.
82 	 * @param  name Character name. Must be one of "main", "enemy" or "guest".
83 	 * @index  index Index of the character in the array.
84 	 */
85 	HCCharacter * Character(const char *name, s32 index);
86 
87 	/** Skips this action. Must be implemented in the children classes, if possible.
88 	 */
Skip()89   virtual void Skip() {}
90 
91 	/** Updates the action.
92 	 * @return 0 if it didn't need update, 1 otherwise.
93 	 */
94 	virtual s32 Update();
95 
96 	/** Prepares the action for execution. The block calls this method when its time
97 	 * to execute the action. The action prepares its data to be executed (init timers, etc.).
98 	 */
Current()99 	virtual void Current() {}
100 
101 	/** Destroys the object.
102 	 */
~HCScriptAction()103 	virtual ~HCScriptAction() {}
104 };
105 
106 /** For unifying the actions' hierarchy.
107  */
108 typedef HCScriptAction HCScriptActionNop;
109 
110 class HCScriptActionMove : public HCScriptAction
111 {
112  protected:
113 	HCCharacter *character;               /**< Character to move. */
114 	s32 direction;                        /**< Direction to follow. */
115 	s32 totalAmount;                      /**< Amount of cells to displace. */
116 	s32 curAmount;                        /**< Current amount of cells. */
117 	s32 orgRow;                           /**< Original row. */
118 	s32 orgCol;                           /**< Original column. */
119 	s32 lastRow;                          /**< Last visited row. */
120 	s32 lastCol;                          /**< Last visited column. */
121 
122  public:
123 	/** Creates an empty Move Action. Init must be called in order to use it.
124 	 */
HCScriptActionMove()125 	HCScriptActionMove() : HCScriptAction(HCSAT_MOVE), character(0)
126 	{}
127 
128 	/** Initializes this action.
129 	 * @param  _character Character to move.
130 	 * @param  dir Movement direction (one of 2, 4, 6 or 8).
131 	 * @param  amount Movement amount in cells.
132 	 * @return <b>true</b> if initialization succeeded, <b>false</b> otherwise.
133 	 */
134 	bool Init(HCCharacter *_character, s32 dir, s32 amount);
135 
136 	/** Loads the action from the file.
137 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
138 	 * @return <b>true</b> if loading succeeded, <b>false</b> otherwise.
139 	 */
140 	bool Load(JTextFile &f);
141 
142 	/** Updates the action.
143 	 * @return 0 if it didn't need update, 1 otherwise.
144 	 */
145 	virtual s32 Update();
146 
147 	/** Sets the character state.
148 	 */
149 	virtual void Current();
150 
151 	/** Destroys the object.
152 	 */
~HCScriptActionMove()153 	virtual ~HCScriptActionMove() {}
154 };
155 
156 class HCScriptActionDialog : public HCScriptAction
157 {
158  protected:
159 	HCCharacter *character;               /**< Character who talks. */
160 	HCText dialog;                        /**< Dialog to show. */
161 
162  public:
163 	/** Creates an empty Dialog Action. Init must be called in order to use it.
164 	 */
HCScriptActionDialog()165 	HCScriptActionDialog() : HCScriptAction(HCSAT_DIALOG), character(0)
166 	{}
167 
168 	/** Initializes this action.
169 	 * @param  _character Character to talk.
170 	 * @param  text Text of the dialog.
171 	 * @param  theme Theme to use.
172 	 * @param  font Font to use.
173 	 * @param  align Alignment of text.
174 	 * @param  left Whether the peak must face to the left or to the right.
175 	 * @param  subtype Subtype within the theme.
176 	 * @param  r Color red component.
177 	 * @param  g Color green component.
178 	 * @param  b Color blue component.
179 	 * @return <b>true</b> if initialization succeeded, <b>false</b> otherwise.
180 	 */
181 	bool Init(HCCharacter *_character,
182 						const char *text,
183 						HCTheme *theme,
184 						JFont *font,
185 						JFontAlign align,
186 						bool left,
187 						s32 subtype,
188 						u8 r, u8 g, u8 b);
189 
190 	/** Loads the action from the file.
191 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
192 	 * @return <b>true</b> if loading succeeded, <b>false</b> otherwise.
193 	 */
194 	bool Load(JTextFile &f);
195 
196 	/** Updates the action.
197 	 * @return 0 if it didn't need update, 1 otherwise.
198 	 */
199 	virtual s32 Update();
200 
201 	/** Skips this action.
202 	 */
Skip()203   virtual void Skip() {dialog.Skip();}
204 
205 	/** Sets the character's dialog and inits the timer.
206 	 */
207 	virtual void Current();
208 
209 	/** Destroys the object.
210 	 */
~HCScriptActionDialog()211 	virtual ~HCScriptActionDialog() {}
212 };
213 
214 class HCScriptActionNarrative : public HCScriptAction
215 {
216  protected:
217 	HCText narrative;                        /**< Narrative to show. */
218 
219  public:
220 	/** Creates an empty Narrative Action. Init must be called in order to use it.
221 	 */
HCScriptActionNarrative()222 	HCScriptActionNarrative() : HCScriptAction(HCSAT_NARRATIVE)
223 	{}
224 
225 	/** Initializes this action.
226 	 * @param  s32 Frame alignment within screen.
227 	 * @param  text Text of the narrative.
228 	 * @param  theme Theme to use.
229 	 * @param  font Font to use.
230 	 * @param  align Alignment of text.
231 	 * @param  left Whether the peak must face to the left or to the right.
232 	 * @param  subtype Subtype within the theme.
233 	 * @param  r Color red component.
234 	 * @param  g Color green component.
235 	 * @param  b Color blue component.
236 	 * @return <b>true</b> if initialization succeeded, <b>false</b> otherwise.
237 	 */
238 	bool Init(s32 alignment,
239 						const char *text,
240 						HCTheme *theme,
241 						JFont *font,
242 						JFontAlign align,
243 						s32 subtype,
244 						u8 r, u8 g, u8 b);
245 
246 	/** Loads the action from the file.
247 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
248 	 * @return <b>true</b> if loading succeeded, <b>false</b> otherwise.
249 	 */
250 	bool Load(JTextFile &f);
251 
252 	/** Updates the action.
253 	 * @return 0 if it didn't need update, 1 otherwise.
254 	 */
255 	virtual s32 Update();
256 
257 	/** Prepares for execution.
258 	 */
259 	virtual void Current();
260 
261 	/** Destroys the object.
262 	 */
~HCScriptActionNarrative()263 	virtual ~HCScriptActionNarrative() {}
264 };
265 
266 class HCScriptActionSound : public HCScriptAction
267 {
268  protected:
269 	JChunk sound;                         /**< Sound to play. */
270 	s32 numLoops;                         /**< Number of loops, -1 means play forever, 0 means once, 1 means 2 times, and so on. */
271 
272  public:
273 	/** Creates an empty Sound Action. Init must be called in order to use it.
274 	 */
HCScriptActionSound()275 	HCScriptActionSound() : HCScriptAction(HCSAT_SOUND)
276 	{}
277 
278 	/** Initializes this action.
279 	 * @param  filename File name of the sound.
280 	 * @param  loops Number of loops, -1 means forever.
281 	 * @param  waitToEnd Must end to considere it is finished?
282 	 * @return <b>true</b> if initialization succeeded, <b>false</b> otherwise.
283 	 */
284 	bool Init(const char *filename, s32 loops, bool waitToEnd);
285 
286 	/** Loads the action from the file.
287 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
288 	 * @return <b>true</b> if loading succeeded, <b>false</b> otherwise.
289 	 */
290 	bool Load(JTextFile &f);
291 
292 	/** Updates the action.
293 	 * @return 0 if it didn't need update, 1 otherwise.
294 	 */
295 	virtual s32 Update();
296 
297 	/** Prepares the action for execution. The block calls this method when its time
298 	 * to execute the action. The action prepares its data to be executed (init timers, etc.).
299 	 */
300 	virtual void Current();
301 
302 	/** Destroys the object.
303 	 */
~HCScriptActionSound()304 	virtual ~HCScriptActionSound() {}
305 };
306 
307 class HCScriptActionWait : public HCScriptAction
308 {
309  protected:
310 	JTimer timer;                         /**< Timer. */
311 	s32 ms;                               /**< Millisecond to wait. */
312 
313  public:
314 	/** Creates an empty Wait Action. Init must be called in order to use it.
315 	 */
HCScriptActionWait()316 	HCScriptActionWait() : HCScriptAction(HCSAT_WAIT)
317 	{}
318 
319 	/** Initializes this action.
320 	 * @param  loops Number of milliseconds to wait.
321 	 * @return <b>true</b> if initialization succeeded, <b>false</b> otherwise.
322 	 */
323 	bool Init(s32 millis = 1000);
324 
325 	/** Loads the action from the file.
326 	 * @param  f File Opened an positioned in the begin of the action (just after character '[').
327 	 * @return <b>true</b> if loading succeeded, <b>false</b> otherwise.
328 	 */
329 	bool Load(JTextFile &f);
330 
331 	/** Updates the action.
332 	 * @return 0 if it didn't need update, 1 otherwise.
333 	 */
334 	virtual s32 Update();
335 
336 	/** Prepares the action for execution. The block calls this method when its time
337 	 * to execute the action. The action prepares its data to be executed (init timers, etc.).
338 	 */
339 	virtual void Current();
340 
341 	/** Destroys the object.
342 	 */
~HCScriptActionWait()343 	virtual ~HCScriptActionWait() {}
344 };
345 
346 #endif // _HCSCRIPTACTION_INCLUDED
347