FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
instance.h
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 
42 namespace 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 
85  class Instance : public FifeClass, public InstanceDeleteListener {
86  public:
87 
92  Instance(Object* object, const Location& location, const std::string& identifier="");
93 
96  virtual ~Instance();
97 
100  const std::string& getId() { return m_id; }
101 
104  void setId(const std::string& identifier="");
105 
108  Object* getObject() { return m_object; }
109 
113  void setLocation(const Location& loc);
114 
119  Location getLocation() const { return m_location; }
120 
124  Location& getLocationRef() { return m_location; }
125 
132  Location getTargetLocation() const;
133 
137  void setFacingLocation(const Location& loc);
138 
143  Location getFacingLocation();
144 
147  void setRotation(int rotation);
148 
151  int getRotation() const { return m_rotation; }
152 
159  Location& getFacingLocationRef();
160 
163  void setBlocking(bool blocking);
164 
167  bool isBlocking() const;
168 
171  void setOverrideBlocking(bool overblock) { m_override_blocking = overblock; }
172 
175  bool isOverrideBlocking() const { return m_override_blocking; }
176 
180  void addActionListener(InstanceActionListener* listener);
181 
185  void removeActionListener(InstanceActionListener* listener);
186 
190  void addChangeListener(InstanceChangeListener* listener);
191 
195  void removeChangeListener(InstanceChangeListener* listener);
196 
200  void addDeleteListener(InstanceDeleteListener* listener);
201 
205  void removeDeleteListener(InstanceDeleteListener* listener);
206 
211  Action* getCurrentAction() const;
212 
217  double getMovementSpeed() const;
218 
223  unsigned int getActionRuntime();
224 
230  void setActionRuntime(unsigned int time_offset);
231 
238  void move(const std::string& action_name, const Location& target, const double speed);
239 
245  void act(const std::string& action_name, const Location& direction, bool repeating=false);
246 
251  void say(const std::string& text, unsigned int duration=0);
252 
259  void follow(const std::string& action_name, Instance* leader, const double speed);
260 
263  const std::string* getSayText() const;
264 
270  InstanceChangeInfo update();
271 
274  bool isActive() const;
275 
278  void setVisual(AbstractVisual* visual) { m_visual = visual; }
279 
282  template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
283 
286  void setTimeMultiplier(float multip);
287 
290  float getTimeMultiplier();
291 
294  float getTotalTimeMultiplier();
295 
299  unsigned int getRuntime();
300 
304  void refresh();
305 
308  inline InstanceChangeInfo getChangeInfo();
309 
312  void onInstanceDeleted(Instance* instance);
313 
314  private:
315  std::string m_id;
316 
317  // The rotation offset of this instance. This is in addition to possible camera rotation and
318  // intended for setting, for example, a rotation of a tile.
319  int m_rotation;
320 
329  class InstanceActivity {
330  public:
331  InstanceActivity(Instance& source);
332  ~InstanceActivity();
333 
334  // ----- Fields related to change tracking -----
335  // updates cached variables, marks changes
336  void update(Instance& source);
337  // location on previous round
338  Location m_location;
339  // rotation on previous round
340  int m_rotation;
341  // facing location on previous round
342  Location m_facinglocation;
343  // action on previous round. @NOTE: might become invalid, only used for address comparison
344  Action* m_action;
345  // speed on previous round
346  double m_speed;
347  // time multiplier on previous round
348  float m_timemultiplier;
349  // say text on previous round
350  std::string m_saytxt;
351  // listeners for changes
352  std::vector<InstanceChangeListener*> m_changelisteners;
353 
354  // ----- Fields related to generic activity -----
355  // listeners for action related events
356  std::vector<InstanceActionListener*> m_actionlisteners;
357  // action information, allocated when actions are bind
358  ActionInfo* m_actioninfo;
359  // text to say + duration, allocated when something is said
360  SayInfo* m_sayinfo;
361  // time scaler for this instance
362  TimeProvider* m_timeprovider;
363  };
364  InstanceActivity* m_activity;
365  // bitmask stating current changes
366  InstanceChangeInfo m_changeinfo;
367  // listeners for deletion of the instance
368  std::vector<InstanceDeleteListener*> m_deletelisteners;
369 
370  // object where instantiated from
371  Object* m_object;
372  // current location
373  Location m_location;
374  // current facing location. Just a pointer to save space e.g. on tiles
375  Location* m_facinglocation;
376  // instance visualization
377  AbstractVisual* m_visual;
378  // instance blocking info
379  bool m_blocking;
380  // allow to override the blocking property
381  bool m_override_blocking;
382 
383  Instance(const Instance&);
384  Instance& operator=(const Instance&);
385  // Finalize current action
386  void finalizeAction();
387  // Initialize action for use
388  void initializeAction(const std::string& action_name);
389  // Moves instance. Returns true if finished
390  bool process_movement();
391  // Calculates movement based current location and speed
392  void calcMovement();
393  // rebinds time provider based on new location
394  void bindTimeProvider();
395  // called when instance has been changed. Causes instance to create InstanceActivity
396  void initializeChanges();
397  };
398 
399  inline InstanceChangeInfo Instance::getChangeInfo() {
400  if (m_activity) {
401  return m_changeinfo;
402  }
403  return ICHANGE_NO_CHANGES;
404  }
405 } // FIFE
406 
407 #endif