2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 18:09:54 +08:00
|
|
|
#include "TerrainTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-04-09 12:23:47 +08:00
|
|
|
|
2015-04-03 17:50:08 +08:00
|
|
|
TerrainTests::TerrainTests()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2015-04-03 17:50:08 +08:00
|
|
|
ADD_TEST_CASE(TerrainSimple);
|
|
|
|
ADD_TEST_CASE(TerrainWalkThru);
|
2015-08-03 14:02:46 +08:00
|
|
|
ADD_TEST_CASE(TerrainWithLightMap);
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 17:50:08 +08:00
|
|
|
Vec3 camera_offset(0, 45, 60);
|
|
|
|
#define PLAYER_HEIGHT 0
|
2015-01-23 18:09:54 +08:00
|
|
|
|
|
|
|
TerrainSimple::TerrainSimple()
|
|
|
|
{
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// use custom camera
|
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 800);
|
2015-01-30 15:46:39 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
2021-12-28 16:06:23 +08:00
|
|
|
_camera->setPosition3D(Vec3(-1, 1.6f, 4));
|
2015-01-30 15:46:39 +08:00
|
|
|
addChild(_camera);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Terrain::DetailMap r("TerrainTest/dirt.jpg"), g("TerrainTest/Grass2.jpg"), b("TerrainTest/road.jpg"),
|
|
|
|
a("TerrainTest/GreenSkin.jpg");
|
2015-01-30 15:46:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Terrain::TerrainData data("TerrainTest/heightmap16.jpg", "TerrainTest/alphamap.png", r, g, b, a);
|
2015-03-30 18:25:22 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT);
|
|
|
|
_terrain->setLODDistance(3.2f, 6.4f, 9.6f);
|
2015-01-30 15:46:39 +08:00
|
|
|
_terrain->setMaxDetailMapAmount(4);
|
|
|
|
addChild(_terrain);
|
|
|
|
_terrain->setCameraMask(2);
|
2015-04-01 18:24:05 +08:00
|
|
|
_terrain->setDrawWire(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(TerrainSimple::onTouchesMoved, this);
|
2015-01-30 15:46:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2021-12-28 16:06:23 +08:00
|
|
|
// add Particle3D for test blend
|
2019-03-06 17:03:16 +08:00
|
|
|
auto rootps = PUParticleSystem3D::create("Particle3D/scripts/mp_torch.pu");
|
|
|
|
rootps->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
rootps->startParticleSystem();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-03-06 17:03:16 +08:00
|
|
|
this->addChild(rootps, 0, 0);
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string TerrainSimple::title() const
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2015-03-30 18:25:22 +08:00
|
|
|
return "Terrain with skirt";
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string TerrainSimple::subtitle() const
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
return "Drag to walkThru";
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void TerrainSimple::onTouchesMoved(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2015-01-30 15:46:39 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
float delta = Director::getInstance()->getDeltaTime();
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
2015-01-30 15:46:39 +08:00
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
2021-12-28 16:06:23 +08:00
|
|
|
Point newPos = PreviousLocation - location;
|
2015-01-30 15:46:39 +08:00
|
|
|
|
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraDir.y = 0;
|
2015-01-30 15:46:39 +08:00
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraRightDir.y = 0;
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
cameraPos += cameraDir * newPos.y * 0.5 * delta;
|
|
|
|
cameraPos += cameraRightDir * newPos.x * 0.5 * delta;
|
|
|
|
_camera->setPosition3D(cameraPos);
|
2015-01-30 15:46:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string TerrainWalkThru::title() const
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2015-04-08 15:13:24 +08:00
|
|
|
return "Player walk around in terrain";
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string TerrainWalkThru::subtitle() const
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2015-04-08 15:13:24 +08:00
|
|
|
return "touch to move";
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TerrainWalkThru::TerrainWalkThru()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchesBegan = AX_CALLBACK_2(TerrainWalkThru::onTouchesBegan, this);
|
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(TerrainWalkThru::onTouchesEnd, this);
|
2015-01-23 18:09:54 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// use custom camera
|
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 200);
|
2015-01-23 18:09:54 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
addChild(_camera);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Terrain::DetailMap r("TerrainTest/dirt.jpg"), g("TerrainTest/Grass2.jpg", 10), b("TerrainTest/road.jpg"),
|
|
|
|
a("TerrainTest/GreenSkin.jpg", 20);
|
2015-01-23 18:09:54 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Terrain::TerrainData data("TerrainTest/heightmap16.jpg", "TerrainTest/alphamap.png", r, g, b, a, Size(32, 32),
|
|
|
|
40.0f, 2);
|
|
|
|
_terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT);
|
2015-03-27 10:47:52 +08:00
|
|
|
_terrain->setMaxDetailMapAmount(4);
|
2015-01-23 18:09:54 +08:00
|
|
|
_terrain->setCameraMask(2);
|
|
|
|
_terrain->setDrawWire(false);
|
|
|
|
|
2015-04-01 18:24:05 +08:00
|
|
|
_terrain->setSkirtHeightRatio(3);
|
2021-12-28 16:06:23 +08:00
|
|
|
_terrain->setLODDistance(64, 128, 192);
|
2022-07-05 14:48:46 +08:00
|
|
|
_player = Player::create("MeshRendererTest/girl.c3b", _camera, _terrain);
|
2015-01-23 18:09:54 +08:00
|
|
|
_player->setCameraMask(2);
|
2015-05-29 06:00:49 +08:00
|
|
|
_player->setScale(0.08f);
|
2021-12-28 16:06:23 +08:00
|
|
|
_player->setPositionY(_terrain->getHeight(_player->getPositionX(), _player->getPositionZ()) + PLAYER_HEIGHT);
|
2019-02-27 17:16:25 +08:00
|
|
|
|
2019-03-06 17:03:16 +08:00
|
|
|
// add Particle3D for test blend
|
|
|
|
auto rootps = PUParticleSystem3D::create("Particle3D/scripts/mp_torch.pu");
|
|
|
|
rootps->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
rootps->setScale(30.0f);
|
|
|
|
rootps->startParticleSystem();
|
|
|
|
_player->addChild(rootps);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-11 12:02:32 +08:00
|
|
|
// add BillBoard for test blend
|
|
|
|
auto billboard = BillBoard::create("Images/btn-play-normal.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
billboard->setPosition3D(Vec3(0, 180, 0));
|
2015-05-11 12:02:32 +08:00
|
|
|
billboard->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
_player->addChild(billboard);
|
2015-01-27 15:18:59 +08:00
|
|
|
|
2022-07-05 14:48:46 +08:00
|
|
|
auto animation = Animation3D::create("MeshRendererTest/girl.c3b", "Take 001");
|
2015-01-27 15:18:59 +08:00
|
|
|
if (animation)
|
|
|
|
{
|
|
|
|
auto animate = Animate3D::create(animation);
|
|
|
|
_player->runAction(RepeatForever::create(animate));
|
|
|
|
}
|
2015-01-23 18:09:54 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_camera->setPosition3D(_player->getPosition3D() + camera_offset);
|
|
|
|
_camera->setRotation3D(Vec3(-45, 0, 0));
|
2015-01-23 18:09:54 +08:00
|
|
|
|
|
|
|
addChild(_player);
|
|
|
|
addChild(_terrain);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void TerrainWalkThru::onTouchesBegan(const std::vector<axis::Touch*>& touches, axis::Event* event) {}
|
2015-01-23 18:09:54 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void TerrainWalkThru::onTouchesEnd(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto touch = touches[0];
|
2015-01-23 18:09:54 +08:00
|
|
|
auto location = touch->getLocationInView();
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_camera)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_player)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
Vec3 dir = farP - nearP;
|
|
|
|
dir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
Vec3 collisionPoint(-999, -999, -999);
|
2015-06-18 12:20:44 +08:00
|
|
|
bool isInTerrain = _terrain->getIntersectionPoint(Ray(nearP, dir), collisionPoint);
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!isInTerrain)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_player->idle();
|
|
|
|
return;
|
2015-06-18 12:20:44 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
dir = collisionPoint - _player->getPosition3D();
|
2015-01-27 15:18:59 +08:00
|
|
|
dir.y = 0;
|
|
|
|
dir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
_player->_headingAngle = -1 * acos(dir.dot(Vec3(0, 0, -1)));
|
|
|
|
dir.cross(dir, Vec3(0, 0, -1), &_player->_headingAxis);
|
|
|
|
_player->_targetPos = collisionPoint;
|
2015-02-02 16:14:13 +08:00
|
|
|
_player->forward();
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
bool Player::isDone() const
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::update(float dt)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto player = (MeshRenderer*)this;
|
2015-01-23 18:09:54 +08:00
|
|
|
switch (_playerState)
|
|
|
|
{
|
|
|
|
case PLAYER_STATE_IDLE:
|
|
|
|
break;
|
|
|
|
case PLAYER_STATE_FORWARD:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
Vec3 curPos = player->getPosition3D();
|
|
|
|
Vec3 newFaceDir = _targetPos - curPos;
|
|
|
|
newFaceDir.y = 0.0f;
|
|
|
|
newFaceDir.normalize();
|
|
|
|
Vec3 offset = newFaceDir * 25.0f * dt;
|
|
|
|
curPos += offset;
|
|
|
|
player->setPosition3D(curPos);
|
|
|
|
}
|
|
|
|
break;
|
2015-01-23 18:09:54 +08:00
|
|
|
case PLAYER_STATE_BACKWARD:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
Vec3 forward_vec;
|
|
|
|
player->getNodeToWorldTransform().getForwardVector(&forward_vec);
|
|
|
|
forward_vec.normalize();
|
|
|
|
player->setPosition3D(player->getPosition3D() - forward_vec * 15 * dt);
|
|
|
|
}
|
|
|
|
break;
|
2015-01-23 18:09:54 +08:00
|
|
|
case PLAYER_STATE_LEFT:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
player->setRotation3D(player->getRotation3D() + Vec3(0, 25 * dt, 0));
|
|
|
|
}
|
|
|
|
break;
|
2015-01-23 18:09:54 +08:00
|
|
|
case PLAYER_STATE_RIGHT:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
player->setRotation3D(player->getRotation3D() + Vec3(0, -25 * dt, 0));
|
|
|
|
}
|
|
|
|
break;
|
2015-01-23 18:09:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-05-26 17:51:26 +08:00
|
|
|
// transform player position to world coord
|
2021-12-28 16:06:23 +08:00
|
|
|
auto playerPos = player->getPosition3D();
|
2015-05-26 17:51:26 +08:00
|
|
|
auto playerModelMat = player->getParent()->getNodeToWorldTransform();
|
|
|
|
playerModelMat.transformPoint(&playerPos);
|
2015-01-27 15:18:59 +08:00
|
|
|
Vec3 Normal;
|
2021-12-28 16:06:23 +08:00
|
|
|
float player_h = _terrain->getHeight(playerPos.x, playerPos.z, &Normal);
|
|
|
|
if (Normal.isZero()) // check the player whether is out of the terrain
|
2015-06-18 10:17:46 +08:00
|
|
|
{
|
|
|
|
player_h = playerPos.y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player_h += PLAYER_HEIGHT;
|
|
|
|
}
|
|
|
|
player->setPositionY(player_h);
|
2015-01-27 15:18:59 +08:00
|
|
|
Quaternion q2;
|
2021-12-28 16:06:23 +08:00
|
|
|
q2.createFromAxisAngle(Vec3(0, 1, 0), (float)-M_PI, &q2);
|
|
|
|
|
2015-01-27 15:18:59 +08:00
|
|
|
Quaternion headingQ;
|
2021-12-28 16:06:23 +08:00
|
|
|
headingQ.createFromAxisAngle(_headingAxis, _headingAngle, &headingQ);
|
|
|
|
player->setRotationQuat(headingQ * q2);
|
|
|
|
auto vec_offset = Vec4(camera_offset.x, camera_offset.y, camera_offset.z, 1);
|
|
|
|
vec_offset = player->getNodeToWorldTransform() * vec_offset;
|
|
|
|
_cam->setPosition3D(player->getPosition3D() + camera_offset);
|
2015-01-23 18:09:54 +08:00
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::turnLeft()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
_playerState = PLAYER_STATE_LEFT;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::turnRight()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
_playerState = PLAYER_STATE_RIGHT;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::idle()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
_playerState = PLAYER_STATE_IDLE;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::forward()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
_playerState = PLAYER_STATE_FORWARD;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::backward()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
|
|
|
_playerState = PLAYER_STATE_BACKWARD;
|
|
|
|
}
|
|
|
|
|
2015-02-02 16:14:13 +08:00
|
|
|
void Player::updateState()
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
auto player = (MeshRenderer*)this;
|
2015-01-23 18:09:54 +08:00
|
|
|
switch (_playerState)
|
|
|
|
{
|
|
|
|
case PLAYER_STATE_FORWARD:
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
|
|
|
Vec2 player_pos = Vec2(player->getPositionX(), player->getPositionZ());
|
|
|
|
Vec2 targetPos = Vec2(_targetPos.x, _targetPos.z);
|
|
|
|
auto dist = player_pos.distance(targetPos);
|
|
|
|
if (dist < 1)
|
2015-01-23 18:09:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_playerState = PLAYER_STATE_IDLE;
|
2015-01-23 18:09:54 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-01-23 18:09:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 16:14:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Player* Player::create(const char* file, Camera* cam, Terrain* terrain)
|
2015-02-02 16:14:13 +08:00
|
|
|
{
|
|
|
|
//
|
2022-07-05 14:48:46 +08:00
|
|
|
auto player = new Player();
|
|
|
|
if (player->initWithFile(file))
|
2015-02-02 16:14:13 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
player->_headingAngle = 0;
|
|
|
|
player->_playerState = PLAYER_STATE_IDLE;
|
|
|
|
player->_cam = cam;
|
|
|
|
player->_terrain = terrain;
|
|
|
|
player->autorelease();
|
|
|
|
player->scheduleUpdate();
|
|
|
|
return player;
|
2015-02-02 16:14:13 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(player);
|
2015-02-02 16:14:13 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-03 14:02:46 +08:00
|
|
|
|
|
|
|
TerrainWithLightMap::TerrainWithLightMap()
|
|
|
|
{
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// use custom camera
|
|
|
|
_camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1f, 800);
|
2015-08-03 14:02:46 +08:00
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
2021-12-28 16:06:23 +08:00
|
|
|
_camera->setPosition3D(Vec3(-1, 1.6f, 4));
|
2015-08-03 14:02:46 +08:00
|
|
|
addChild(_camera);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
Terrain::DetailMap r("TerrainTest/dirt.jpg"), g("TerrainTest/Grass2.jpg"), b("TerrainTest/road.jpg"),
|
|
|
|
a("TerrainTest/GreenSkin.jpg");
|
|
|
|
|
|
|
|
Terrain::TerrainData data("TerrainTest/heightmap16.jpg", "TerrainTest/alphamap.png", r, g, b, a);
|
|
|
|
|
|
|
|
_terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT);
|
|
|
|
_terrain->setLODDistance(3.2f, 6.4f, 9.6f);
|
2015-08-03 14:02:46 +08:00
|
|
|
_terrain->setMaxDetailMapAmount(4);
|
|
|
|
addChild(_terrain);
|
|
|
|
_terrain->setCameraMask(2);
|
|
|
|
_terrain->setDrawWire(false);
|
|
|
|
_terrain->setLightMap("TerrainTest/Lightmap.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(TerrainWithLightMap::onTouchesMoved, this);
|
2015-08-03 14:02:46 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
|
|
|
std::string TerrainWithLightMap::title() const
|
|
|
|
{
|
|
|
|
return "Terrain using light map";
|
|
|
|
}
|
|
|
|
std::string TerrainWithLightMap::subtitle() const
|
|
|
|
{
|
|
|
|
return "Drag to walkThru";
|
|
|
|
}
|
2022-07-11 17:50:21 +08:00
|
|
|
void TerrainWithLightMap::onTouchesMoved(const std::vector<axis::Touch*>& touches, axis::Event* event)
|
2015-08-03 14:02:46 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
float delta = Director::getInstance()->getDeltaTime();
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocation();
|
2015-08-03 14:02:46 +08:00
|
|
|
auto PreviousLocation = touch->getPreviousLocation();
|
2021-12-28 16:06:23 +08:00
|
|
|
Point newPos = PreviousLocation - location;
|
|
|
|
|
2015-08-03 14:02:46 +08:00
|
|
|
Vec3 cameraDir;
|
|
|
|
Vec3 cameraRightDir;
|
|
|
|
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
|
|
|
|
cameraDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraDir.y = 0;
|
2015-08-03 14:02:46 +08:00
|
|
|
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
|
|
|
|
cameraRightDir.normalize();
|
2021-12-28 16:06:23 +08:00
|
|
|
cameraRightDir.y = 0;
|
|
|
|
Vec3 cameraPos = _camera->getPosition3D();
|
|
|
|
cameraPos += cameraDir * newPos.y * 0.5 * delta;
|
|
|
|
cameraPos += cameraRightDir * newPos.x * 0.5 * delta;
|
2015-08-03 14:02:46 +08:00
|
|
|
_camera->setPosition3D(cameraPos);
|
|
|
|
}
|