Path.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef _PATH_H_
  2. #define _PATH_H_
  3. #include "2d/sceneobject/SceneObject.h"
  4. class Path;
  5. class PathObject
  6. {
  7. private:
  8. friend class Path;
  9. void setCurrNode(S32 node);
  10. void setNextNode(S32 node);
  11. void setPrevNode(S32 node);
  12. Path* mPath;
  13. SimObjectPtr<SceneObject> mObj;
  14. SimObjectId mObjId;
  15. F32 mMaxSpeed;
  16. bool mOrient;
  17. F32 mMaxForce;
  18. F32 mAngOff;
  19. S32 mCurrNode;
  20. S32 mPrevNode;
  21. S32 mNextNode;
  22. bool mLoop;
  23. bool mSnapToNode;
  24. S32 mLoopCount;
  25. S32 mMaxLoop;
  26. public:
  27. PathObject();
  28. ~PathObject() {};
  29. };
  30. class Path : public SceneObject
  31. {
  32. typedef SceneObject Parent;
  33. public:
  34. struct Node
  35. {
  36. Node(Vector2 pos, F32 dst, F32 wght)
  37. {
  38. position = pos;
  39. distance = dst;
  40. weight = wght;
  41. };
  42. Vector2 position;
  43. F32 distance;
  44. F32 weight;
  45. };
  46. Path();
  47. ~Path();
  48. virtual void onDeleteNotify(SimObject* object);
  49. static void initPersistFields();
  50. virtual void preIntegrate(const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats);
  51. virtual void integrateObject(const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats);
  52. S32 addNode(Vector2 pos, F32 distance, F32 weight);
  53. S32 getNodeCount() const { return mNodes.size(); }
  54. inline Node& getNode(S32 index)
  55. {
  56. if (isValidNode(index)) return mNodes[index];
  57. return mNodes[0];
  58. }
  59. inline bool isValidNode(S32 index)
  60. {
  61. if (mNodes.empty()) return false;
  62. if((index >= 0) && (index < mNodes.size())) return true;
  63. return false;
  64. }
  65. void attachObject(SceneObject* object, F32 speed, F32 force, bool orientToPath, F32 angleOff, bool snapToNode, S32 startNode, bool loop, S32 maxLoop);
  66. void detachObject(SceneObject* object);
  67. S32 getAttachedObjectCount() { return mObjs.size(); }
  68. SceneObject* getPathObject(U32 index) { if (index < mObjs.size()) return mObjs[index]->mObj; return NULL; }
  69. inline PathObject* getAttachedObject(const SceneObject* obj)
  70. {
  71. if (obj == NULL)
  72. return NULL;
  73. Vector<PathObject*>::iterator i;
  74. for (i = mObjs.begin(); i != mObjs.end(); i++)
  75. if ((*i)->mObj == obj) return *i;
  76. return NULL;
  77. }
  78. DECLARE_CONOBJECT(Path);
  79. private:
  80. void moveObject(PathObject& obj);
  81. Vector2 truncate(Vector2 vec, F32 max);
  82. Vector2 seek(Vector2 target, Vector2 objPos, F32 max, Vector2 curr, F32 slowRad);
  83. Vector<PathObject*> mObjs;
  84. Vector<Node> mNodes;
  85. };
  86. #endif