FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
instance.h
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_INSTANCE_H
00023 #define FIFE_INSTANCE_H
00024 
00025 // Standard C++ library includes
00026 #include <vector>
00027 
00028 // 3rd party library includes
00029 
00030 // FIFE includes
00031 // These includes are split up in two parts, separated by one empty line
00032 // First block: files included from the FIFE root src directory
00033 // Second block: files included from the same folder
00034 #include "util/base/fifeclass.h"
00035 
00036 #include "model/metamodel/object.h"
00037 #include "model/metamodel/abstractvisual.h"
00038 
00039 #include "location.h"
00040 
00041 
00042 namespace FIFE {
00043 
00044     class Layer;
00045     class Action;
00046     class Instance;
00047     class ActionInfo;
00048     class SayInfo;
00049     class TimeProvider;
00050 
00051     class InstanceActionListener {
00052     public:
00053         virtual ~InstanceActionListener() {};
00054         virtual void onInstanceActionFinished(Instance* instance, Action* action) = 0;
00055     };
00056 
00057     enum InstanceChangeType {
00058         ICHANGE_NO_CHANGES = 0x0000,
00059         ICHANGE_LOC = 0x0001,
00060         ICHANGE_FACING_LOC = 0x0002,
00061         ICHANGE_SPEED = 0x0004,
00062         ICHANGE_ACTION = 0x0008,
00063         ICHANGE_TIME_MULTIPLIER = 0x0010,
00064         ICHANGE_SAYTEXT = 0x0020,
00065         ICHANGE_ROTATION = 0x0040,
00066     };
00067     typedef unsigned int InstanceChangeInfo;
00068 
00069     class InstanceChangeListener {
00070     public:
00071         virtual ~InstanceChangeListener() {};
00072         virtual void onInstanceChanged(Instance* instance, InstanceChangeInfo info) = 0;
00073     };
00074 
00075 
00076     class InstanceDeleteListener {
00077     public:
00078         virtual ~InstanceDeleteListener() {};
00079         virtual void onInstanceDeleted(Instance* instance) =0;
00080     };
00081 
00085     class Instance : public FifeClass, public InstanceDeleteListener {
00086     public:
00087 
00092         Instance(Object* object, const Location& location, const std::string& identifier="");
00093 
00096         virtual ~Instance();
00097 
00100         const std::string& getId() { return m_id; }
00101 
00104         void setId(const std::string& identifier="");
00105 
00108         Object* getObject() { return m_object; }
00109 
00113         void setLocation(const Location& loc);
00114 
00119         Location getLocation() const { return m_location; }
00120 
00124         Location& getLocationRef() { return m_location; }
00125 
00132         Location getTargetLocation() const;
00133 
00137         void setFacingLocation(const Location& loc);
00138 
00143         Location getFacingLocation();
00144 
00147         void setRotation(int rotation);
00148 
00151         int getRotation() const { return m_rotation; }
00152 
00159         Location& getFacingLocationRef();
00160 
00163         void setBlocking(bool blocking);
00164 
00167         bool isBlocking() const;
00168 
00171         void setOverrideBlocking(bool overblock) { m_override_blocking = overblock; }
00172 
00175         bool isOverrideBlocking() const { return m_override_blocking; }
00176 
00180         void addActionListener(InstanceActionListener* listener);
00181 
00185         void removeActionListener(InstanceActionListener* listener);
00186 
00190         void addChangeListener(InstanceChangeListener* listener);
00191 
00195         void removeChangeListener(InstanceChangeListener* listener);
00196 
00200         void addDeleteListener(InstanceDeleteListener* listener);
00201 
00205         void removeDeleteListener(InstanceDeleteListener* listener);
00206 
00211         Action* getCurrentAction() const;
00212 
00217         double getMovementSpeed() const;
00218 
00223         unsigned int getActionRuntime();
00224 
00230         void setActionRuntime(unsigned int time_offset);
00231 
00238         void move(const std::string& action_name, const Location& target, const double speed);
00239 
00245         void act(const std::string& action_name, const Location& direction, bool repeating=false);
00246 
00251         void say(const std::string& text, unsigned int duration=0);
00252 
00259         void follow(const std::string& action_name, Instance* leader, const double speed);
00260 
00263         const std::string* getSayText() const;
00264 
00270         InstanceChangeInfo update();
00271 
00274         bool isActive() const;
00275 
00278         void setVisual(AbstractVisual* visual) { m_visual = visual; }
00279 
00282         template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
00283 
00286         void setTimeMultiplier(float multip);
00287 
00290         float getTimeMultiplier();
00291 
00294         float getTotalTimeMultiplier();
00295 
00299         unsigned int getRuntime();
00300 
00304         void refresh();
00305 
00308         inline InstanceChangeInfo getChangeInfo();
00309 
00312         void onInstanceDeleted(Instance* instance);
00313 
00314     private:
00315         std::string m_id;
00316 
00317         // The rotation offset of this instance. This is in addition to possible camera rotation and
00318         // intended for setting, for example, a rotation of a tile.
00319         int m_rotation;
00320 
00329         class InstanceActivity {
00330         public:
00331             InstanceActivity(Instance& source);
00332             ~InstanceActivity();
00333 
00334             // ----- Fields related to change tracking -----
00335             // updates cached variables, marks changes
00336             void update(Instance& source);
00337             // location on previous round
00338             Location m_location;
00339             // rotation on previous round
00340             int m_rotation;
00341             // facing location on previous round
00342             Location m_facinglocation;
00343             // action on previous round. @NOTE: might become invalid, only used for address comparison
00344             Action* m_action;
00345             // speed on previous round
00346             double m_speed;
00347             // time multiplier on previous round
00348             float m_timemultiplier;
00349             // say text on previous round
00350             std::string m_saytxt;
00351             // listeners for changes
00352             std::vector<InstanceChangeListener*> m_changelisteners;
00353 
00354             // ----- Fields related to generic activity -----
00355             // listeners for action related events
00356             std::vector<InstanceActionListener*> m_actionlisteners;
00357             // action information, allocated when actions are bind
00358             ActionInfo* m_actioninfo;
00359             // text to say + duration, allocated when something is said
00360             SayInfo* m_sayinfo;
00361             // time scaler for this instance
00362             TimeProvider* m_timeprovider;
00363         };
00364         InstanceActivity* m_activity;
00365         // bitmask stating current changes
00366         InstanceChangeInfo m_changeinfo;
00367         // listeners for deletion of the instance
00368         std::vector<InstanceDeleteListener*> m_deletelisteners;
00369 
00370         // object where instantiated from
00371         Object* m_object;
00372         // current location
00373         Location m_location;
00374         // current facing location. Just a pointer to save space e.g. on tiles
00375         Location* m_facinglocation;
00376         // instance visualization
00377         AbstractVisual* m_visual;
00378         // instance blocking info
00379         bool m_blocking;
00380         // allow to override the blocking property
00381         bool m_override_blocking;
00382 
00383         Instance(const Instance&);
00384         Instance& operator=(const Instance&);
00385         // Finalize current action
00386         void finalizeAction();
00387         // Initialize action for use
00388         void initializeAction(const std::string& action_name);
00389         // Moves instance. Returns true if finished
00390         bool process_movement();
00391         // Calculates movement based current location and speed
00392         void calcMovement();
00393         // rebinds time provider based on new location
00394         void bindTimeProvider();
00395         // called when instance has been changed. Causes instance to create InstanceActivity
00396         void initializeChanges();
00397     };
00398 
00399     inline InstanceChangeInfo Instance::getChangeInfo() {
00400         if (m_activity) {
00401             return m_changeinfo;
00402         }
00403         return ICHANGE_NO_CHANGES;
00404     }
00405 } // FIFE
00406 
00407 #endif