123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- #ifndef __CCNAV_MESH_AGENT_H__
- #define __CCNAV_MESH_AGENT_H__
- #include "base/ccConfig.h"
- #if CC_USE_NAVMESH
- #include "2d/CCComponent.h"
- #include "base/CCRef.h"
- #include "math/Vec3.h"
- #include "recast/DetourCrowd/DetourCrowd.h"
- class dtNavMeshQuery;
- NS_CC_BEGIN
- struct CC_DLL NavMeshAgentParam
- {
- NavMeshAgentParam();
- float radius;
- float height;
- float maxAcceleration;
- float maxSpeed;
-
- float collisionQueryRange;
- float pathOptimizationRange;
-
- float separationWeight;
-
- unsigned char updateFlags;
-
-
- unsigned char obstacleAvoidanceType;
-
- unsigned char queryFilterType;
- };
- struct CC_DLL OffMeshLinkData
- {
- Vec3 startPosition;
- Vec3 endPosition;
- };
- class CC_DLL NavMeshAgent : public Component
- {
- friend class NavMesh;
- public:
- enum NavMeshAgentSyncFlag
- {
- NONE = 0,
- NODE_TO_AGENT = 1,
- AGENT_TO_NODE = 2,
- NODE_AND_NODE = NODE_TO_AGENT | AGENT_TO_NODE,
- };
- typedef std::function<void(NavMeshAgent *agent, float totalTimeAfterMove)> MoveCallback;
-
- static NavMeshAgent* create(const NavMeshAgentParam ¶m);
- static const std::string& getNavMeshAgentComponentName();
- virtual void onEnter() override;
- virtual void onExit() override;
-
- void setRadius(float radius);
-
- float getRadius() const;
-
- void setHeight(float height);
-
- float getHeight() const;
-
- void setMaxAcceleration(float maxAcceleration);
-
- float getMaxAcceleration() const;
-
- void setMaxSpeed(float maxSpeed);
-
- float getMaxSpeed() const;
-
- void setSeparationWeight(float weight);
-
- float getSeparationWeight() const;
-
- void setObstacleAvoidanceType(unsigned char type);
-
- unsigned char getObstacleAvoidanceType() const;
-
- Vec3 getCurrentVelocity() const;
-
- void move(const Vec3 &destination, const MoveCallback &callback = nullptr);
-
- void pause();
-
- void resume();
-
- void stop();
-
- void setOrientationRefAxes(const Vec3 &rotRefAxes);
-
- void setAutoOrientation(bool isAuto);
-
- void setAutoTraverseOffMeshLink(bool isAuto);
-
- bool isOnOffMeshLink();
-
- void completeOffMeshLink();
-
- OffMeshLinkData getCurrentOffMeshLinkData();
- void setUserData(void *data) { _userData = data; };
- void* getUserData() const { return _userData; };
-
- void setSyncFlag(const NavMeshAgentSyncFlag &flag) { _syncFlag = flag; }
- NavMeshAgentSyncFlag getSyncFlag() const { return _syncFlag; }
-
- void syncToAgent();
-
- void syncToNode();
-
-
- Vec3 getVelocity() const;
- CC_CONSTRUCTOR_ACCESS:
- NavMeshAgent();
- virtual ~NavMeshAgent();
- private:
- bool initWith(const NavMeshAgentParam ¶m);
- void addTo(dtCrowd *crowed);
- void removeFrom(dtCrowd *crowed);
- void setNavMeshQuery(dtNavMeshQuery *query);
- void preUpdate(float delta);
- void postUpdate(float delta);
- static void convertTodtAgentParam(const NavMeshAgentParam &inParam, dtCrowdAgentParams &outParam);
- private:
- MoveCallback _moveCallback;
- NavMeshAgentParam _param;
- NavMeshAgentSyncFlag _syncFlag;
- Vec3 _origination;
- Vec3 _destination;
- Vec3 _rotRefAxes;
- unsigned char _state;
- bool _needAutoOrientation;
- int _agentID;
- bool _needUpdateAgent;
- bool _needMove;
- float _totalTimeAfterMove;
- void *_userData;
- dtCrowd *_crowd;
- dtNavMeshQuery *_navMeshQuery;
- };
- NS_CC_END
- #endif
- #endif
|