comparison engine/core/model/structures/instance.h @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef FIFE_INSTANCE_H
23 #define FIFE_INSTANCE_H
24
25 // Standard C++ library includes
26 #include <vector>
27
28 // 3rd party library includes
29
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "model/metamodel/object.h"
35 #include "model/metamodel/abstractvisual.h"
36
37 #include "location.h"
38
39
40 namespace FIFE {
41
42 class Layer;
43 class Action;
44 class Instance;
45 class ActionInfo;
46 class SayInfo;
47 class TimeProvider;
48
49 class InstanceActionListener {
50 public:
51 virtual ~InstanceActionListener() {};
52 virtual void onInstanceActionFinished(Instance* instance, Action* action) = 0;
53 };
54
55 enum InstanceChangeType {
56 ICHANGE_NO_CHANGES = 0x0000,
57 ICHANGE_LOC = 0x0001,
58 ICHANGE_FACING_LOC = 0x0002,
59 ICHANGE_SPEED = 0x0004,
60 ICHANGE_ACTION = 0x0008,
61 ICHANGE_TIME_MULTIPLIER = 0x0010,
62 ICHANGE_SAYTEXT = 0x0020,
63 ICHANGE_ROTATION = 0x0040, // NOTE! does not currently get updated onInstanceChange unless some other activity is performed
64 };
65 typedef unsigned int InstanceChangeInfo;
66
67 class InstanceChangeListener {
68 public:
69 virtual ~InstanceChangeListener() {};
70 virtual void onInstanceChanged(Instance* instance, InstanceChangeInfo info) = 0;
71 };
72
73 /**
74 * An Instance is an "instantiation" of an Object at a Location.
75 */
76 class Instance : public ResourceClass {
77 public:
78
79 /** Constructor
80 * Instances are created by calling addInstance from layer, thus
81 * this method should really be called only by layer or test code
82 */
83 Instance(Object* object, const Location& location, const std::string& identifier="");
84
85 /** Destructor
86 */
87 virtual ~Instance();
88
89 /** Get the identifier for this instance; possibly null.
90 */
91 const std::string& getId() { return m_id; }
92
93 /** Gets object where this instance is instantiated from
94 */
95 Object* getObject() { return m_object; }
96
97 /** Sets location of the instance
98 * @param loc new location
99 */
100 void setLocation(const Location& loc);
101
102 /** Gets current location of instance
103 * @note does not return const Location&, since swig wont be const correct
104 * @return current location
105 */
106 Location getLocation() const { return m_location; }
107
108 /** Gets reference of current location of instance
109 * @return reference to current location
110 */
111 Location& getLocationRef() { return m_location; }
112
113 /** Gets movement target in case instance is moving. In case not, returns current location
114 * To move target location, call move-method
115 * @see move
116 * @note does not return const Location&, since swig wont be const correct
117 * @return Movement target location
118 */
119 Location getTargetLocation() const;
120
121 /** Sets the direction where instance is heading. Useful e.g. with static
122 * instances which don't "move" or "act"
123 */
124 void setFacingLocation(const Location& loc);
125
126 /** Returns the direction where instance is heading
127 * @note does not return const Location&, since swig wont be const correct
128 * @return the direction of instance.
129 */
130 Location getFacingLocation();
131
132 /** Set the rotation offset of this instance
133 */
134 void setRotation(int rotation);
135
136 /** Get the rotation offset of this instance
137 */
138 int getRotation() const { return m_rotation; }
139
140 /** Returns reference to the direction where instance is heading
141 * Note: if instance didn't previously hadn't defined facing location
142 * (e.g. by movement or setFacingLocation), method creates the location
143 * thus increasing memory consumption.
144 * @return reference to the direction of instance.
145 */
146 Location& getFacingLocationRef();
147
148 /** Adds new instance action listener
149 * @param listener to add
150 */
151 void addActionListener(InstanceActionListener* listener);
152
153 /** Removes associated instance action listener
154 * @param listener to remove
155 */
156 void removeActionListener(InstanceActionListener* listener);
157
158 /** Adds new instance change listener
159 * @param listener to add
160 */
161 void addChangeListener(InstanceChangeListener* listener);
162
163 /** Removes associated instance change listener
164 * @param listener to remove
165 */
166 void removeChangeListener(InstanceChangeListener* listener);
167
168 /** Gets the currently active action. This is owned by
169 * the instance's object, so don't delete it!
170 * @return current action, NULL in case there is none
171 */
172 Action* getCurrentAction() const;
173
174 /** Gets the speed in case instance is moving
175 * otherwise returns 0
176 * @return instance speed. Value 1 means distance 1 in layer coordinates / second
177 */
178 double getMovementSpeed() const;
179
180 /** Gets the time in milliseconds how long action has been active
181 * In case there is no current action, returns -1
182 * @return action runtime
183 */
184 int getActionRuntime() const;
185
186 /** Performs given named action to the instance. While performing the action
187 * moves instance to given target with given speed
188 * @param action_name name of the action
189 * @param target place where to move this instance
190 * @param speed speed used for movement. Units = distance 1 in layer coordinates per second
191 */
192 void move(const std::string& action_name, const Location& target, const double speed);
193
194 /** Performs given named action to the instance. Performs no movement
195 * @param action_name name of the action
196 * @param direction coordinates for cell towards instance is heading to when performing the action
197 * @param repeating in case true, keeps repeating this action
198 */
199 void act(const std::string& action_name, const Location& direction, bool repeating=false);
200
201 /** Causes instance to "say" given text (shown on screen next to the instance)
202 * @param text text to say. If "" given, clear the text
203 * @param duration duration to show the text (in ms). If 0, shows forever
204 */
205 void say(const std::string& text, unsigned int duration=0);
206
207 /** Performs given named action to the instance. While performing the action
208 * follows given isntance with given speed
209 * @param action_name name of the action
210 * @param leader followed instance
211 * @param speed speed used for movement. Units = distance 1 in layer coordinates per second
212 */
213 void follow(const std::string& action_name, Instance* leader, const double speed);
214
215 /** Returns pointer to currently set saytext. In case no text is set, returns NULL
216 */
217 const std::string* getSayText() const;
218
219 /** Updates the instance related to the current action
220 * @param curticks current tick count of the system
221 * @note call this only once in engine update cycle, so that tracking between
222 * current position and previous position keeps in sync.
223 * @returns marked changes
224 */
225 InstanceChangeInfo update(unsigned int curticks=0);
226
227 /** Sets visualization to be used. Transfers ownership.
228 */
229 void setVisual(AbstractVisual* visual) { m_visual = visual; }
230
231 /** Gets used visualization
232 */
233 template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
234
235 /** Sets speed for the map. See Model::setTimeMultiplier.
236 */
237 void setTimeMultiplier(float multip);
238
239 /** Gets instance speed. @see setTimeMultiplier.
240 */
241 float getTimeMultiplier();
242
243 /** Gets instance speed, considering also model and map speeds. @see setTimeMultiplier.
244 */
245 float getTotalTimeMultiplier();
246
247 /** Refreshes instance e.g. in case location is updated directly (not via setLocation)
248 * In this case e.g. instance's master time provider is changed, so it needs to be updated
249 */
250 void refresh();
251
252 /** Returns a bitmask of changes since previous update
253 */
254 inline InstanceChangeInfo getChangeInfo();
255
256 private:
257 std::string m_id;
258
259 // The rotation offset of this instance. This is in addition to possible camera rotation and
260 // intended for setting, for example, a rotation of a tile.
261 int m_rotation;
262
263 /** InstanceActivity gets allocated in case there is some runtime
264 * activity related to the instance. Keeping activity related variables
265 * in separate class keeps memory consumption lower e.g. for large tile
266 * areas.
267 * Class also keeps track of changes since the previous update call.
268 * With this bookkeeping, it is possible to optimize several spots in
269 * the engine, basically only reacting to changes instead of polling.
270 */
271 class InstanceActivity {
272 public:
273 InstanceActivity(Instance& source);
274 ~InstanceActivity();
275
276 // ----- Fields related to change tracking -----
277 // updates cached variables, marks changes
278 void update(Instance& source);
279 // location on previous round
280 Location m_location;
281 // facing location on previous round
282 Location m_facinglocation;
283 // action on previous round. @NOTE: might become invalid, only used for address comparison
284 Action* m_action;
285 // speed on previous round
286 double m_speed;
287 // time multiplier on previous round
288 float m_timemultiplier;
289 // say text on previous round
290 std::string m_saytxt;
291 // listeners for changes
292 std::vector<InstanceChangeListener*> m_changelisteners;
293
294 // ----- Fields related to generic activity -----
295 // listeners for action related events
296 std::vector<InstanceActionListener*> m_actionlisteners;
297 // action information, allocated when actions are bind
298 ActionInfo* m_actioninfo;
299 // text to say + duration, allocated when something is said
300 SayInfo* m_sayinfo;
301 // time scaler for this instance
302 TimeProvider* m_timeprovider;
303 };
304 InstanceActivity* m_activity;
305 // bitmask stating current changes
306 InstanceChangeInfo m_changeinfo;
307
308 // object where instantiated from
309 Object* m_object;
310 // current location
311 Location m_location;
312 // current facing location. Just a pointer to save space e.g. on tiles
313 Location* m_facinglocation;
314 // instance visualization
315 AbstractVisual* m_visual;
316
317 Instance(const Instance&);
318 Instance& operator=(const Instance&);
319 // Finalize current action
320 void finalizeAction();
321 // Initialize action for use
322 void initalizeAction(const std::string& action_name);
323 // Moves instance. Returns true if finished
324 bool process_movement();
325 // Calculates movement based current location and speed
326 void calcMovement();
327 // rebinds time provider based on new location
328 void bindTimeProvider();
329 // called when instance has been changed. Causes instance to create InstanceActivity
330 void initializeChanges();
331 };
332
333 inline InstanceChangeInfo Instance::getChangeInfo() {
334 if (m_activity) {
335 return m_changeinfo;
336 }
337 return ICHANGE_NO_CHANGES;
338 }
339 } // FIFE
340
341 #endif