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 "model/metamodel/object.h"
00035 #include "model/metamodel/abstractvisual.h"
00036 
00037 #include "location.h"
00038 
00039 
00040 namespace FIFE {
00041 
00042     class Layer;
00043     class Action;
00044     class Instance;
00045     class ActionInfo;
00046     class SayInfo;
00047     class TimeProvider;
00048 
00049     class InstanceActionListener {
00050     public:
00051         virtual ~InstanceActionListener() {};
00052         virtual void onInstanceActionFinished(Instance* instance, Action* action) = 0;
00053     };
00054 
00055     enum InstanceChangeType {
00056         ICHANGE_NO_CHANGES = 0x0000,
00057         ICHANGE_LOC = 0x0001,
00058         ICHANGE_FACING_LOC = 0x0002,
00059         ICHANGE_SPEED = 0x0004,
00060         ICHANGE_ACTION = 0x0008,
00061         ICHANGE_TIME_MULTIPLIER = 0x0010,
00062         ICHANGE_SAYTEXT = 0x0020,
00063         ICHANGE_ROTATION = 0x0040,  // NOTE! does not currently get updated onInstanceChange unless some other activity is performed
00064     };
00065     typedef unsigned int InstanceChangeInfo;
00066 
00067     class InstanceChangeListener {
00068     public:
00069         virtual ~InstanceChangeListener() {};
00070         virtual void onInstanceChanged(Instance* instance, InstanceChangeInfo info) = 0;
00071     };
00072 
00073 
00074     class InstanceDeleteListener {
00075     public:
00076         virtual ~InstanceDeleteListener() {};
00077         virtual void onInstanceDeleted(Instance* instance) =0;
00078     };
00079 
00083     class Instance : public ResourceClass, public InstanceDeleteListener {
00084     public:
00085 
00090         Instance(Object* object, const Location& location, const std::string& identifier="");
00091 
00094         virtual ~Instance();
00095 
00098         const std::string& getId() { return m_id; }
00099 
00102         void setId(const std::string& identifier="");
00103 
00106         Object* getObject() { return m_object; }
00107 
00111         void setLocation(const Location& loc);
00112 
00117         Location getLocation() const { return m_location; }
00118 
00122         Location& getLocationRef() { return m_location; }
00123 
00130         Location getTargetLocation() const;
00131 
00135         void setFacingLocation(const Location& loc);
00136 
00141         Location getFacingLocation();
00142 
00145         void setRotation(int rotation);
00146 
00149         int getRotation() const { return m_rotation; }
00150 
00157         Location& getFacingLocationRef();
00158 
00162         void addActionListener(InstanceActionListener* listener);
00163 
00167         void removeActionListener(InstanceActionListener* listener);
00168 
00172         void addChangeListener(InstanceChangeListener* listener);
00173 
00177         void removeChangeListener(InstanceChangeListener* listener);
00178 
00182         void addDeleteListener(InstanceDeleteListener* listener);
00183 
00187         void removeDeleteListener(InstanceDeleteListener* listener);
00188 
00193         Action* getCurrentAction() const;
00194 
00199         double getMovementSpeed() const;
00200 
00205         unsigned int getActionRuntime();
00206 
00212         void setActionRuntime(unsigned int time_offset);
00213 
00220         void move(const std::string& action_name, const Location& target, const double speed);
00221 
00227         void act(const std::string& action_name, const Location& direction, bool repeating=false);
00228 
00233         void say(const std::string& text, unsigned int duration=0);
00234 
00241         void follow(const std::string& action_name, Instance* leader, const double speed);
00242 
00245         const std::string* getSayText() const;
00246 
00252         InstanceChangeInfo update();
00253 
00256         void setVisual(AbstractVisual* visual) { m_visual = visual; }
00257 
00260         template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
00261 
00264         void setTimeMultiplier(float multip);
00265 
00268         float getTimeMultiplier();
00269 
00272         float getTotalTimeMultiplier();
00273 
00277         unsigned int getRuntime();
00278 
00282         void refresh();
00283 
00286         inline InstanceChangeInfo getChangeInfo();
00287 
00290         void onInstanceDeleted(Instance* instance);
00291 
00292     private:
00293         std::string m_id;
00294 
00295         // The rotation offset of this instance. This is in addition to possible camera rotation and
00296         // intended for setting, for example, a rotation of a tile.
00297         int m_rotation;
00298 
00307         class InstanceActivity {
00308         public:
00309             InstanceActivity(Instance& source);
00310             ~InstanceActivity();
00311 
00312             // ----- Fields related to change tracking -----
00313             // updates cached variables, marks changes
00314             void update(Instance& source);
00315             // location on previous round
00316             Location m_location;
00317             // facing location on previous round
00318             Location m_facinglocation;
00319             // action on previous round. @NOTE: might become invalid, only used for address comparison
00320             Action* m_action;
00321             // speed on previous round
00322             double m_speed;
00323             // time multiplier on previous round
00324             float m_timemultiplier;
00325             // say text on previous round
00326             std::string m_saytxt;
00327             // listeners for changes
00328             std::vector<InstanceChangeListener*> m_changelisteners;
00329 
00330             // ----- Fields related to generic activity -----
00331             // listeners for action related events
00332             std::vector<InstanceActionListener*> m_actionlisteners;
00333             // action information, allocated when actions are bind
00334             ActionInfo* m_actioninfo;
00335             // text to say + duration, allocated when something is said
00336             SayInfo* m_sayinfo;
00337             // time scaler for this instance
00338             TimeProvider* m_timeprovider;
00339         };
00340         InstanceActivity* m_activity;
00341         // bitmask stating current changes
00342         InstanceChangeInfo m_changeinfo;
00343         // listeners for deletion of the instance
00344         std::vector<InstanceDeleteListener*> m_deletelisteners;
00345 
00346         // object where instantiated from
00347         Object* m_object;
00348         // current location
00349         Location m_location;
00350         // current facing location. Just a pointer to save space e.g. on tiles
00351         Location* m_facinglocation;
00352         // instance visualization
00353         AbstractVisual* m_visual;
00354 
00355         Instance(const Instance&);
00356         Instance& operator=(const Instance&);
00357         // Finalize current action
00358         void finalizeAction();
00359         // Initialize action for use
00360         void initializeAction(const std::string& action_name);
00361         // Moves instance. Returns true if finished
00362         bool process_movement();
00363         // Calculates movement based current location and speed
00364         void calcMovement();
00365         // rebinds time provider based on new location
00366         void bindTimeProvider();
00367         // called when instance has been changed. Causes instance to create InstanceActivity
00368         void initializeChanges();
00369     };
00370 
00371     inline InstanceChangeInfo Instance::getChangeInfo() {
00372         if (m_activity) {
00373             return m_changeinfo;
00374         }
00375         return ICHANGE_NO_CHANGES;
00376     }
00377 } // FIFE
00378 
00379 #endif