2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2019-11-23 20:27:39 +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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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 "2d/CCScene.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_PHYSICS
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "physics/CCPhysicsWorld.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "physics3d/CCPhysics3DWorld.h"
|
|
|
|
# include "physics3d/CCPhysics3DComponent.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_NAVMESH
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "navmesh/CCNavMesh.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Scene::Scene()
|
|
|
|
: _event(_director->getEventDispatcher()->addCustomEventListener(
|
|
|
|
Director::EVENT_PROJECTION_CHANGED,
|
|
|
|
std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1)))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
_event->retain();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_ignoreAnchorPointForPosition = true;
|
|
|
|
setAnchorPoint(Vec2(0.5f, 0.5f));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scene::~Scene()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
|
|
|
AX_SAFE_RELEASE(_physics3DWorld);
|
|
|
|
AX_SAFE_RELEASE(_physics3dDebugCamera);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_NAVMESH
|
|
|
|
AX_SAFE_RELEASE(_navMesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->getEventDispatcher()->removeEventListener(_event);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_event);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_PHYSICS
|
2019-11-23 20:27:39 +08:00
|
|
|
delete _physicsWorld;
|
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->releaseAllChildrenRecursive(this);
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_NAVMESH
|
2019-11-23 20:27:39 +08:00
|
|
|
void Scene::setNavMesh(NavMesh* navMesh)
|
|
|
|
{
|
|
|
|
if (_navMesh != navMesh)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(navMesh);
|
|
|
|
AX_SAFE_RELEASE(_navMesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
_navMesh = navMesh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool Scene::init()
|
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
auto size = _director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithSize(size);
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
bool Scene::initWithSize(const Vec2& size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
initDefaultCamera();
|
2019-11-23 20:27:39 +08:00
|
|
|
setContentSize(size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Scene::initDefaultCamera()
|
|
|
|
{
|
|
|
|
if (!_defaultCamera)
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
_defaultCamera = Camera::create();
|
|
|
|
addChild(_defaultCamera);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Scene* Scene::create()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Scene* ret = new Scene();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Scene* Scene::createWithSize(const Vec2& size)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Scene* ret = new Scene();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithSize(size))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Scene::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<Scene | tag = %d>", _tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::onProjectionChanged(EventCustom* /*event*/)
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
2022-08-11 10:52:38 +08:00
|
|
|
_defaultCamera->initDefault();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool camera_cmp(const Camera* a, const Camera* b)
|
|
|
|
{
|
|
|
|
return a->getRenderOrder() < b->getRenderOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<Camera*>& Scene::getCameras()
|
|
|
|
{
|
|
|
|
if (_cameraOrderDirty)
|
|
|
|
{
|
|
|
|
stable_sort(_cameras.begin(), _cameras.end(), camera_cmp);
|
|
|
|
_cameraOrderDirty = false;
|
|
|
|
}
|
|
|
|
return _cameras;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::render(Renderer* renderer, const Mat4& eyeTransform, const Mat4* eyeProjection)
|
|
|
|
{
|
|
|
|
Camera* defaultCamera = nullptr;
|
|
|
|
const auto& transform = getNodeToParentTransform();
|
|
|
|
|
|
|
|
for (const auto& camera : getCameras())
|
|
|
|
{
|
|
|
|
if (!camera->isVisible())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Camera::_visitingCamera = camera;
|
|
|
|
if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
|
|
|
|
{
|
|
|
|
defaultCamera = Camera::_visitingCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are two ways to modify the "default camera" with the eye Transform:
|
|
|
|
// a) modify the "nodeToParentTransform" matrix
|
|
|
|
// b) modify the "additional transform" matrix
|
|
|
|
// both alternatives are correct, if the user manually modifies the camera with a camera->setPosition()
|
|
|
|
// then the "nodeToParent transform" will be lost.
|
|
|
|
// And it is important that the change is "permanent", because the matrix might be used for calculate
|
|
|
|
// culling and other stuff.
|
|
|
|
if (eyeProjection)
|
|
|
|
camera->setAdditionalProjection(*eyeProjection * camera->getProjectionMatrix().getInversed());
|
|
|
|
|
|
|
|
camera->setAdditionalTransform(eyeTransform.getInversed());
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2021-12-25 10:04:45 +08:00
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,
|
|
|
|
Camera::_visitingCamera->getViewProjectionMatrix());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
camera->apply();
|
2021-12-25 10:04:45 +08:00
|
|
|
// clear background with max depth
|
2019-11-23 20:27:39 +08:00
|
|
|
camera->clearBackground();
|
2021-12-25 10:04:45 +08:00
|
|
|
// visit the scene
|
2019-11-23 20:27:39 +08:00
|
|
|
visit(renderer, transform, 0);
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_NAVMESH
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_navMesh && _navMeshDebugCamera == camera)
|
|
|
|
{
|
|
|
|
_navMesh->debugDraw(renderer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
renderer->render();
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// we shouldn't restore the transform matrix since it could be used
|
|
|
|
// from "update" or other parts of the game to calculate culling or something else.
|
|
|
|
// camera->setNodeToParentTransform(eyeCopy);
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_physics3DWorld && _physics3DWorld->isDebugDrawEnabled())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Camera* physics3dDebugCamera = _physics3dDebugCamera != nullptr ? _physics3dDebugCamera : defaultCamera;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (eyeProjection)
|
2021-12-25 10:04:45 +08:00
|
|
|
physics3dDebugCamera->setAdditionalProjection(*eyeProjection *
|
|
|
|
physics3dDebugCamera->getProjectionMatrix().getInversed());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
physics3dDebugCamera->setAdditionalTransform(eyeTransform.getInversed());
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2021-12-25 10:04:45 +08:00
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,
|
|
|
|
physics3dDebugCamera->getViewProjectionMatrix());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
physics3dDebugCamera->apply();
|
|
|
|
physics3dDebugCamera->clearBackground();
|
|
|
|
|
|
|
|
_physics3DWorld->debugDraw(renderer);
|
|
|
|
renderer->render();
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Camera::_visitingCamera = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::removeAllChildren()
|
|
|
|
{
|
|
|
|
if (_defaultCamera)
|
|
|
|
_defaultCamera->retain();
|
|
|
|
|
|
|
|
Node::removeAllChildren();
|
|
|
|
|
|
|
|
if (_defaultCamera)
|
|
|
|
{
|
|
|
|
addChild(_defaultCamera);
|
|
|
|
_defaultCamera->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
void Scene::setPhysics3DDebugCamera(Camera* camera)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(camera);
|
|
|
|
AX_SAFE_RELEASE(_physics3dDebugCamera);
|
2019-11-23 20:27:39 +08:00
|
|
|
_physics3dDebugCamera = camera;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_NAVMESH
|
2021-12-25 10:04:45 +08:00
|
|
|
void Scene::setNavMeshDebugCamera(Camera* camera)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(camera);
|
|
|
|
AX_SAFE_RELEASE(_navMeshDebugCamera);
|
2019-11-23 20:27:39 +08:00
|
|
|
_navMeshDebugCamera = camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_USE_PHYSICS || (AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION))
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Scene* Scene::createWithPhysics()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Scene* ret = new Scene();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithPhysics())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Scene::initWithPhysics()
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
initDefaultCamera();
|
|
|
|
return initPhysicsWorld();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Scene::initPhysicsWorld()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_USE_PHYSICS
|
2019-11-23 20:27:39 +08:00
|
|
|
_physicsWorld = PhysicsWorld::construct(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool ret = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
this->setContentSize(_director->getWinSize());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DWorldDes info;
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_BREAK_IF(!(_physics3DWorld = Physics3DWorld::create(&info)));
|
2019-11-23 20:27:39 +08:00
|
|
|
_physics3DWorld->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// success
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_USE_PHYSICS || (AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION) || AX_USE_NAVMESH)
|
2019-11-23 20:27:39 +08:00
|
|
|
void Scene::stepPhysicsAndNavigation(float deltaTime)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_USE_PHYSICS
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_physicsWorld && _physicsWorld->isAutoStep())
|
|
|
|
_physicsWorld->update(deltaTime);
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_USE_3D_PHYSICS && AX_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_physics3DWorld)
|
|
|
|
{
|
|
|
|
_physics3DWorld->stepSimulate(deltaTime);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2022-07-16 10:43:05 +08:00
|
|
|
# if AX_USE_NAVMESH
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_navMesh)
|
|
|
|
{
|
|
|
|
_navMesh->update(deltaTime);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|