axmol/core/navmesh/CCNavMeshAgent.h

238 lines
6.9 KiB
C
Raw Normal View History

2015-05-29 16:45:33 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCNAV_MESH_AGENT_H__
#define __CCNAV_MESH_AGENT_H__
#include "base/ccConfig.h"
#if CC_USE_NAVMESH
2021-12-25 10:04:45 +08:00
# include "2d/CCComponent.h"
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
# include "base/CCRef.h"
# include "math/Vec3.h"
# include "recast/DetourCrowd.h"
2015-05-29 16:45:33 +08:00
class dtNavMeshQuery;
NS_AX_BEGIN
2015-05-29 16:45:33 +08:00
/**
* @addtogroup 3d
* @{
*/
struct CC_DLL NavMeshAgentParam
{
NavMeshAgentParam();
2021-12-25 10:04:45 +08:00
float radius; ///< Agent radius. [Limit: >= 0]
float height; ///< Agent height. [Limit: > 0]
float maxAcceleration; ///< Maximum allowed acceleration. [Limit: >= 0]
float maxSpeed; ///< Maximum allowed speed. [Limit: >= 0]
2015-05-29 16:45:33 +08:00
/// Defines how close a collision element must be before it is considered for steering behaviors. [Limits: > 0]
float collisionQueryRange;
2021-12-25 10:04:45 +08:00
float pathOptimizationRange; ///< The path visibility optimization range. [Limit: > 0]
2015-05-29 16:45:33 +08:00
/// How aggressive the agent manager should be at avoiding collisions with this agent. [Limit: >= 0]
2015-05-29 16:45:33 +08:00
float separationWeight;
/// Flags that impact steering behavior. (See: #UpdateFlags)
unsigned char updateFlags;
2021-12-25 10:04:45 +08:00
/// The index of the avoidance configuration to use for the agent.
2015-05-29 16:45:33 +08:00
/// [Limits: 0 <= value <= #DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS]
unsigned char obstacleAvoidanceType;
/// The index of the query filter used by this agent.
unsigned char queryFilterType;
};
struct CC_DLL OffMeshLinkData
{
2021-12-25 10:04:45 +08:00
Vec3 startPosition; // position in local coordinate system.
Vec3 endPosition; // position in local coordinate system.
2015-05-29 16:45:33 +08:00
};
/** @brief NavMeshAgent: The code wrapping of dtCrowdAgent, use component mode. */
class CC_DLL NavMeshAgent : public Component
{
friend class NavMesh;
2021-12-25 10:04:45 +08:00
public:
2015-05-29 16:45:33 +08:00
enum NavMeshAgentSyncFlag
{
2021-12-25 10:04:45 +08:00
NONE = 0,
2015-05-29 16:45:33 +08:00
NODE_TO_AGENT = 1,
AGENT_TO_NODE = 2,
NODE_AND_NODE = NODE_TO_AGENT | AGENT_TO_NODE,
};
2021-12-25 10:04:45 +08:00
typedef std::function<void(NavMeshAgent* agent, float totalTimeAfterMove)> MoveCallback;
2015-05-29 16:45:33 +08:00
/**
Create agent
@param param The parameters of agent.
*/
2021-12-25 10:04:45 +08:00
static NavMeshAgent* create(const NavMeshAgentParam& param);
2021-12-26 23:26:34 +08:00
static std::string_view getNavMeshAgentComponentName();
2015-05-29 16:45:33 +08:00
virtual void onEnter() override;
virtual void onExit() override;
/** set agent radius */
void setRadius(float radius);
/** get agent radius */
float getRadius() const;
/** set agent height */
void setHeight(float height);
/** get agent height */
float getHeight() const;
/** set maximal acceleration of agent*/
void setMaxAcceleration(float maxAcceleration);
/** get maximal acceleration of agent*/
float getMaxAcceleration() const;
/** set maximal speed of agent */
void setMaxSpeed(float maxSpeed);
/** get maximal speed of agent */
float getMaxSpeed() const;
/** set separation weight */
void setSeparationWeight(float weight);
/** get separation weight */
float getSeparationWeight() const;
/** set obstacle avoidance type */
void setObstacleAvoidanceType(unsigned char type);
/** get obstacle avoidance type */
unsigned char getObstacleAvoidanceType() const;
/** get current velocity */
Vec3 getCurrentVelocity() const;
/**
Move agent to a destination
@param destination The position in world coordinate system.
@param callback Use this function can catch movement state.
*/
2021-12-25 10:04:45 +08:00
void move(const Vec3& destination, const MoveCallback& callback = nullptr);
2015-05-29 16:45:33 +08:00
/** pause movement */
2015-05-29 16:45:33 +08:00
void pause();
/** resume movement */
void resume();
/** stop movement */
void stop();
/**
Set the reference axes of agent's orientation
@param rotRefAxes The value of reference axes in local coordinate system.
*/
2021-12-25 10:04:45 +08:00
void setOrientationRefAxes(const Vec3& rotRefAxes);
2015-05-29 16:45:33 +08:00
/**Set automatic Orientation */
void setAutoOrientation(bool isAuto);
/**Set automatic Traverse OffMeshLink */
void setAutoTraverseOffMeshLink(bool isAuto);
/**Check agent arrived OffMeshLink */
bool isOnOffMeshLink();
/**Traverse OffMeshLink manually*/
void completeOffMeshLink();
/**Get current OffMeshLink information*/
OffMeshLinkData getCurrentOffMeshLinkData();
2021-12-25 10:04:45 +08:00
void setUserData(void* data) { _userData = data; };
2015-05-29 16:45:33 +08:00
void* getUserData() const { return _userData; };
/**
2021-12-25 10:04:45 +08:00
* synchronization between node and agent is time consuming, you can skip some synchronization using this function
*/
void setSyncFlag(const NavMeshAgentSyncFlag& flag) { _syncFlag = flag; }
2015-05-29 16:45:33 +08:00
NavMeshAgentSyncFlag getSyncFlag() const { return _syncFlag; }
/** synchronize parameter to agent. */
void syncToAgent();
/** synchronize parameter to node. */
void syncToNode();
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
/** get current velocity */
Vec3 getVelocity() const;
NavMeshAgent();
2015-05-29 16:45:33 +08:00
virtual ~NavMeshAgent();
private:
2021-12-25 10:04:45 +08:00
bool initWith(const NavMeshAgentParam& param);
void addTo(dtCrowd* crowed);
void removeFrom(dtCrowd* crowed);
void setNavMeshQuery(dtNavMeshQuery* query);
2015-05-29 16:45:33 +08:00
void preUpdate(float delta);
void postUpdate(float delta);
2021-12-25 10:04:45 +08:00
static void convertTodtAgentParam(const NavMeshAgentParam& inParam, dtCrowdAgentParams& outParam);
2015-05-29 16:45:33 +08:00
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;
2021-12-25 10:04:45 +08:00
void* _userData;
dtCrowd* _crowd;
dtNavMeshQuery* _navMeshQuery;
2015-05-29 16:45:33 +08:00
};
/** @} */
NS_AX_END
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
#endif // CC_USE_NAVMESH
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
#endif // __CCNAV_MESH_AGENT_H__