00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef GAME_SIMULATION_H
00028 #define GAME_SIMULATION_H
00029
00030 #include <PVLE/Export.h>
00031 #include <PVLE/Util/Assert.h>
00032 #include <PVLE/Util/Callback.h>
00033 #include <PVLE/Util/Util.h>
00034 #include <osg/observer_ptr>
00035
00036 class Simulation;
00037
00038
00040 class PVLE_EXPORT SimulationCallback : public Util::Callback<SimulationCallback> {
00041 public:
00042 virtual void simulationChanged(Simulation *) =0;
00043 };
00044
00045
00046
00050 class PVLE_EXPORT Simulation {
00051 public:
00052 Simulation();
00053 ~Simulation();
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 inline void setBasePhyStep(double step) {
00064 ASSERT(step>0);
00065
00066
00067 basePhyStep = step;
00068 if (pCB.valid()) pCB->simulationChanged(this);
00069 }
00070
00071 inline UINT getBasePhyStep() const { return basePhyStep; }
00072
00074 inline double getCurPhyStep() const { return basePhyStep * phySpeedStep; }
00076 inline double getCurPhyNbStepsPerSec() const { return phySpeedNum / basePhyStep; }
00077
00082 inline double getPhySpeedNum() const { return phySpeedNum; }
00083 inline void setPhySpeedNum(double factor) {
00084 if (factor<=0) {
00085 pause(true);
00086 return;
00087 }
00088 phySpeedNum = factor;
00089 if (pCB.valid()) pCB->simulationChanged(this);
00090 }
00091
00097 inline double getPhySpeedStep() const { return phySpeedStep; }
00098 inline void setPhySpeedStep(double factor) {
00099 if (factor<=0) {
00100 pause(true);
00101 return;
00102 }
00103 phySpeedStep = factor;
00104 if (pCB.valid()) pCB->simulationChanged(this);
00105 }
00106
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 inline bool isPaused() const { return paused; }
00119 inline void pause(bool set = true) { paused = set; if (pCB.valid()) pCB->simulationChanged(this); }
00120 inline void togglePause() { paused = !paused; if (pCB.valid()) pCB->simulationChanged(this); }
00121
00123 osg::observer_ptr<SimulationCallback> pCB;
00124
00125 void addOrSetCallback(SimulationCallback * _pCB) {
00126 if (pCB.valid()) pCB->addNestedCallback(_pCB);
00127 else pCB = _pCB;
00128 }
00129
00130 protected:
00131
00132 double basePhyStep;
00133 double phySpeedNum;
00134 double phySpeedStep;
00135 bool paused;
00136 };
00137
00138
00139
00140
00141 #include <PVLE/Util/Singleton.h>
00142 #include <boost/cast.hpp>
00143
00145 class SimulationHolder : public Util::Singleton<SimulationHolder> {
00146 public:
00147 Simulation * get() { return pObj; }
00148
00150 template<class T>
00151 T * get() { return boost::polymorphic_downcast<T *>(pObj); }
00152
00156 void set(Simulation * pObj) { this->pObj = pObj; }
00157
00158 protected:
00159 Simulation * pObj;
00160 };
00161
00162
00163 #endif // GAME_SIMULATION_H