2015-05-29 16:45:33 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +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
|
|
|
|
2022-07-09 01:23:11 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "navmesh/CCNavMeshObstacle.h"
|
|
|
|
#if CC_USE_NAVMESH
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "navmesh/CCNavMesh.h"
|
|
|
|
# include "2d/CCNode.h"
|
|
|
|
# include "2d/CCScene.h"
|
|
|
|
# include "recast/DetourTileCache.h"
|
2015-05-29 16:45:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-29 16:45:33 +08:00
|
|
|
|
|
|
|
NavMeshObstacle* NavMeshObstacle::create(float radius, float height)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ref = new NavMeshObstacle();
|
|
|
|
if (ref->initWith(radius, height))
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
ref->autorelease();
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ref);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view NavMeshObstacle::getNavMeshObstacleComponentName()
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
static std::string comName = "___NavMeshObstacleComponent___";
|
|
|
|
return comName;
|
|
|
|
}
|
|
|
|
|
|
|
|
NavMeshObstacle::NavMeshObstacle()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _radius(0.0f), _height(0.0f), _syncFlag(NODE_AND_NODE), _obstacleID(-1), _tileCache(nullptr)
|
|
|
|
{}
|
2015-05-29 16:45:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::NavMeshObstacle::~NavMeshObstacle() {}
|
2015-05-29 16:45:33 +08:00
|
|
|
|
|
|
|
bool NavMeshObstacle::initWith(float radius, float height)
|
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
_height = height;
|
|
|
|
setName(getNavMeshObstacleComponentName());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::removeFrom(dtTileCache* /*tileCache*/)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
_tileCache->removeObstacle(_obstacleID);
|
2021-12-25 10:04:45 +08:00
|
|
|
_tileCache = nullptr;
|
2015-05-29 16:45:33 +08:00
|
|
|
_obstacleID = -1;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::addTo(dtTileCache* tileCache)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
_tileCache = tileCache;
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 mat = _owner->getNodeToWorldTransform();
|
2015-05-29 16:45:33 +08:00
|
|
|
_tileCache->addObstacle(&mat.m[12], _radius, _height, &_obstacleID);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::onExit()
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_obstacleID == -1)
|
|
|
|
return;
|
2015-05-29 16:45:33 +08:00
|
|
|
Component::onExit();
|
|
|
|
auto scene = _owner->getScene();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (scene && scene->getNavMesh())
|
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
scene->getNavMesh()->removeNavMeshObstacle(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::onEnter()
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_obstacleID != -1)
|
|
|
|
return;
|
2015-05-29 16:45:33 +08:00
|
|
|
Component::onEnter();
|
|
|
|
auto scene = _owner->getScene();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (scene && scene->getNavMesh())
|
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
scene->getNavMesh()->addNavMeshObstacle(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::postUpdate(float /*delta*/)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
if ((_syncFlag & OBSTACLE_TO_NODE) != 0)
|
|
|
|
syncToNode();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::preUpdate(float /*delta*/)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
if ((_syncFlag & NODE_TO_OBSTACLE) != 0)
|
|
|
|
syncToObstacle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshObstacle::syncToNode()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tileCache)
|
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
auto obstacle = _tileCache->getObstacleByRef(_obstacleID);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (obstacle && obstacle->type == DT_OBSTACLE_CYLINDER)
|
|
|
|
{
|
2020-11-14 13:32:51 +08:00
|
|
|
auto& cylinder = obstacle->cylinder;
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 localPos = Vec3(cylinder.pos[0], cylinder.pos[1], cylinder.pos[2]);
|
2015-06-01 17:38:35 +08:00
|
|
|
if (_owner->getParent())
|
|
|
|
_owner->getParent()->getWorldToNodeTransform().transformPoint(localPos, &localPos);
|
|
|
|
_owner->setPosition3D(localPos);
|
2020-11-14 13:32:51 +08:00
|
|
|
_radius = cylinder.radius;
|
|
|
|
_height = cylinder.height;
|
2015-06-01 17:38:35 +08:00
|
|
|
}
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::setRadius(float radius)
|
2015-06-01 17:38:35 +08:00
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void axis::NavMeshObstacle::setHeight(float height)
|
2015-06-01 17:38:35 +08:00
|
|
|
{
|
|
|
|
_height = height;
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:45:33 +08:00
|
|
|
void NavMeshObstacle::syncToObstacle()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tileCache)
|
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
auto obstacle = _tileCache->getObstacleByRef(_obstacleID);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (obstacle && obstacle->type == DT_OBSTACLE_CYLINDER)
|
|
|
|
{
|
|
|
|
Mat4 mat = _owner->getNodeToWorldTransform();
|
2020-11-14 13:32:51 +08:00
|
|
|
auto& cylinder = obstacle->cylinder;
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((mat.m[12] != cylinder.pos[0] && mat.m[13] != cylinder.pos[1] && mat.m[14] != cylinder.pos[2]) ||
|
|
|
|
cylinder.radius != _radius || cylinder.height != _height)
|
|
|
|
{
|
2015-06-01 17:38:35 +08:00
|
|
|
_tileCache->removeObstacle(_obstacleID);
|
|
|
|
_tileCache->addObstacle(&mat.m[12], _radius, _height, &_obstacleID);
|
|
|
|
}
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +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
|