source: branches/active/0.3.3dev/engine/core/model/structures/instance.h @ 3517

Revision 3517, 13.2 KB checked in by helios2000, 4 years ago (diff)
  • Instances now inherit blocking property from objects. Modified the ObjectEdit? Plugin, so you can use it to change the object blocking or the Instance blocking individually. Note: Object blocking changes, overwrite the instance blocking property. fixes514
  • Property svn:eol-style set to native
Line 
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                 *
7 *   modify it under the terms of the GNU Lesser General Public            *
8 *   License as published by the Free Software Foundation; either          *
9 *   version 2.1 of the License, or (at your option) any later version.    *
10 *                                                                         *
11 *   This library 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 GNU     *
14 *   Lesser General Public License for more details.                       *
15 *                                                                         *
16 *   You should have received a copy of the GNU Lesser General Public      *
17 *   License along with this library; if not, write to the                 *
18 *   Free Software Foundation, Inc.,                                       *
19 *   51 Franklin Street, 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 "util/base/fifeclass.h"
35
36#include "model/metamodel/object.h"
37#include "model/metamodel/abstractvisual.h"
38
39#include "location.h"
40
41
42namespace FIFE {
43
44   class Layer;
45   class Action;
46   class Instance;
47   class ActionInfo;
48   class SayInfo;
49   class TimeProvider;
50
51   class InstanceActionListener {
52   public:
53      virtual ~InstanceActionListener() {};
54      virtual void onInstanceActionFinished(Instance* instance, Action* action) = 0;
55   };
56
57   enum InstanceChangeType {
58      ICHANGE_NO_CHANGES = 0x0000,
59      ICHANGE_LOC = 0x0001,
60      ICHANGE_FACING_LOC = 0x0002,
61      ICHANGE_SPEED = 0x0004,
62      ICHANGE_ACTION = 0x0008,
63      ICHANGE_TIME_MULTIPLIER = 0x0010,
64      ICHANGE_SAYTEXT = 0x0020,
65      ICHANGE_ROTATION = 0x0040,
66   };
67   typedef unsigned int InstanceChangeInfo;
68
69   class InstanceChangeListener {
70   public:
71      virtual ~InstanceChangeListener() {};
72      virtual void onInstanceChanged(Instance* instance, InstanceChangeInfo info) = 0;
73   };
74
75
76   class InstanceDeleteListener {
77   public:
78      virtual ~InstanceDeleteListener() {};
79      virtual void onInstanceDeleted(Instance* instance) =0;
80   };
81
82   /**
83    *  An Instance is an "instantiation" of an Object at a Location.
84    */
85   class Instance : public FifeClass, public InstanceDeleteListener {
86   public:
87
88      /** Constructor
89       * Instances are created by calling addInstance from layer, thus
90       * this method should really be called only by layer or test code
91       */
92      Instance(Object* object, const Location& location, const std::string& identifier="");
93
94      /** Destructor
95       */
96      virtual ~Instance();
97
98      /** Get the identifier for this instance; possibly null.
99       */
100      const std::string& getId() { return m_id; }
101
102      /** Set the identifier for this instance.
103       */
104      void setId(const std::string& identifier="");
105
106      /** Gets object where this instance is instantiated from
107       */
108      Object* getObject() { return m_object; }
109
110      /** Sets location of the instance
111       *  @param loc new location
112       */
113      void setLocation(const Location& loc);
114
115      /** Gets current location of instance
116       *  @note does not return const Location&, since swig wont be const correct
117       *  @return current location
118       */
119      Location getLocation() const { return m_location; }
120
121      /** Gets reference of current location of instance
122       *  @return reference to current location
123       */
124      Location& getLocationRef() { return m_location; }
125
126      /** Gets movement target in case instance is moving. In case not, returns current location
127       *  To move target location, call move-method
128       *  @see move
129       *  @note does not return const Location&, since swig wont be const correct
130       *  @return Movement target location
131       */
132      Location getTargetLocation() const;
133
134      /** Sets the direction where instance is heading. Useful e.g. with static
135       * instances which don't "move" or "act"
136       */
137      void setFacingLocation(const Location& loc);
138
139      /** Returns the direction where instance is heading
140      *  @note does not return const Location&, since swig wont be const correct
141       * @return the direction of instance.
142       */
143      Location getFacingLocation();
144
145      /** Set the rotation offset of this instance
146       */
147      void setRotation(int rotation);
148
149      /** Get the rotation offset of this instance
150       */
151      int getRotation() const { return m_rotation; }
152
153      /** Returns reference to the direction where instance is heading
154       * Note: if instance didn't previously hadn't defined facing location
155       * (e.g. by movement or setFacingLocation), method creates the location
156       * thus increasing memory consumption.
157       * @return reference to the direction of instance.
158       */
159      Location& getFacingLocationRef();
160
161      /** Sets if instance blocks movement
162       */
163      void setBlocking(bool blocking);
164
165      /** Gets if instance blocks movement
166       */
167      bool isBlocking() const;
168
169      /** Adds new instance action listener
170       * @param listener to add
171       */
172      void addActionListener(InstanceActionListener* listener);
173
174      /** Removes associated instance action listener
175       * @param listener to remove
176       */
177      void removeActionListener(InstanceActionListener* listener);
178
179      /** Adds new instance change listener
180       * @param listener to add
181       */
182      void addChangeListener(InstanceChangeListener* listener);
183
184      /** Removes associated instance change listener
185       * @param listener to remove
186       */
187      void removeChangeListener(InstanceChangeListener* listener);
188
189      /** Adds new instance delete listener
190       * @param listener to add
191       */
192      void addDeleteListener(InstanceDeleteListener* listener);
193
194      /** Removes associated instance delete listener
195       * @param listener to remove
196       */
197      void removeDeleteListener(InstanceDeleteListener* listener);
198
199      /** Gets the currently active action. This is owned by
200       *  the instance's object, so don't delete it!
201       * @return current action, NULL in case there is none
202       */
203      Action* getCurrentAction() const;
204
205      /** Gets the speed in case instance is moving
206       *  otherwise returns 0
207       * @return instance speed. Value 1 means distance 1 in layer coordinates / second
208       */
209      double getMovementSpeed() const;
210
211      /** Gets the time in milliseconds how long action has been active
212       *  In case there is no current action, returns -1
213       * @return action runtime
214       */
215      unsigned int getActionRuntime();
216
217      /** Sets the time in milliseconds how long an action has been active
218      *  This was requested in Ticket #373.  This way the state
219      *  of the action can be saved and restored at a later time
220      *  @parm The action time offset that should be applied
221      */
222      void setActionRuntime(unsigned int time_offset);
223
224      /** Performs given named action to the instance. While performing the action
225       *  moves instance to given target with given speed
226       *  @param action_name name of the action
227       *  @param target place where to move this instance
228       *  @param speed speed used for movement. Units = distance 1 in layer coordinates per second
229       */
230      void move(const std::string& action_name, const Location& target, const double speed);
231
232      /** Performs given named action to the instance. Performs no movement
233       *  @param action_name name of the action
234       *  @param direction coordinates for cell towards instance is heading to when performing the action
235       *  @param repeating in case true, keeps repeating this action
236       */
237      void act(const std::string& action_name, const Location& direction, bool repeating=false);
238
239      /** Causes instance to "say" given text (shown on screen next to the instance)
240       *  @param text text to say. If "" given, clear the text
241       *  @param duration duration to show the text (in ms). If 0, shows forever
242       */
243      void say(const std::string& text, unsigned int duration=0);
244
245      /** Performs given named action to the instance. While performing the action
246       *  follows given isntance with given speed
247       *  @param action_name name of the action
248       *  @param leader followed instance
249       *  @param speed speed used for movement. Units = distance 1 in layer coordinates per second
250       */
251      void follow(const std::string& action_name, Instance* leader, const double speed);
252
253      /** Returns pointer to currently set saytext. In case no text is set, returns NULL
254       */
255      const std::string* getSayText() const;
256
257      /** Updates the instance related to the current action
258       * @note call this only once in engine update cycle, so that tracking between
259       *  current position and previous position keeps in sync.
260       * @returns marked changes
261       */
262      InstanceChangeInfo update();
263
264      /** If this returns true, the instance needs to be updated
265       */
266      bool isActive() const;
267
268      /** Sets visualization to be used. Transfers ownership.
269       */
270      void setVisual(AbstractVisual* visual) { m_visual = visual; }
271
272      /** Gets used visualization
273       */
274      template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
275
276      /** Sets speed for the map. See Model::setTimeMultiplier.
277      */
278      void setTimeMultiplier(float multip);
279
280      /** Gets instance speed. @see setTimeMultiplier.
281      */
282      float getTimeMultiplier();
283
284      /** Gets instance speed, considering also model and map speeds. @see setTimeMultiplier.
285      */
286      float getTotalTimeMultiplier();
287
288      /** Gets the scaled runtime in milliseconds
289       * @return runtime
290       */
291      unsigned int getRuntime();
292
293      /** Refreshes instance e.g. in case location is updated directly (not via setLocation)
294       * In this case e.g. instance's master time provider is changed, so it needs to be updated
295       */
296      void refresh();
297
298      /** Returns a bitmask of changes since previous update
299       */
300      inline InstanceChangeInfo getChangeInfo();
301
302      /** callback so other instances we depend on can notify us if they go away
303      */
304      void onInstanceDeleted(Instance* instance);
305
306   private:
307      std::string m_id;
308
309      // The rotation offset of this instance. This is in addition to possible camera rotation and
310      // intended for setting, for example, a rotation of a tile.
311      int m_rotation;
312
313      /** InstanceActivity gets allocated in case there is some runtime
314       * activity related to the instance. Keeping activity related variables
315       * in separate class keeps memory consumption lower e.g. for large tile
316       * areas.
317       * Class also keeps track of changes since the previous update call.
318       * With this bookkeeping, it is possible to optimize several spots in
319       * the engine, basically only reacting to changes instead of polling.
320       */
321      class InstanceActivity {
322      public:
323         InstanceActivity(Instance& source);
324         ~InstanceActivity();
325
326         // ----- Fields related to change tracking -----
327         // updates cached variables, marks changes
328         void update(Instance& source);
329         // location on previous round
330         Location m_location;
331         // rotation on previous round
332         int m_rotation;
333         // facing location on previous round
334         Location m_facinglocation;
335         // action on previous round. @NOTE: might become invalid, only used for address comparison
336         Action* m_action;
337         // speed on previous round
338         double m_speed;
339         // time multiplier on previous round
340         float m_timemultiplier;
341         // say text on previous round
342         std::string m_saytxt;
343         // listeners for changes
344         std::vector<InstanceChangeListener*> m_changelisteners;
345
346         // ----- Fields related to generic activity -----
347         // listeners for action related events
348         std::vector<InstanceActionListener*> m_actionlisteners;
349         // action information, allocated when actions are bind
350         ActionInfo* m_actioninfo;
351         // text to say + duration, allocated when something is said
352         SayInfo* m_sayinfo;
353         // time scaler for this instance
354         TimeProvider* m_timeprovider;
355      };
356      InstanceActivity* m_activity;
357      // bitmask stating current changes
358      InstanceChangeInfo m_changeinfo;
359      // listeners for deletion of the instance
360      std::vector<InstanceDeleteListener*> m_deletelisteners;
361
362      // object where instantiated from
363      Object* m_object;
364      // current location
365      Location m_location;
366      // current facing location. Just a pointer to save space e.g. on tiles
367      Location* m_facinglocation;
368      // instance visualization
369      AbstractVisual* m_visual;
370      // instance blocking info
371      bool m_blocking;
372
373      Instance(const Instance&);
374      Instance& operator=(const Instance&);
375      // Finalize current action
376      void finalizeAction();
377      // Initialize action for use
378      void initializeAction(const std::string& action_name);
379      // Moves instance. Returns true if finished
380      bool process_movement();
381      // Calculates movement based current location and speed
382      void calcMovement();
383      // rebinds time provider based on new location
384      void bindTimeProvider();
385      // called when instance has been changed. Causes instance to create InstanceActivity
386      void initializeChanges();
387   };
388
389   inline InstanceChangeInfo Instance::getChangeInfo() {
390      if (m_activity) {
391         return m_changeinfo;
392      }
393      return ICHANGE_NO_CHANGES;
394   }
395} // FIFE
396
397#endif
Note: See TracBrowser for help on using the repository browser.