Converted to ECS, physics fix
This commit is contained in:
87
src/gamedata/WaterModule.cpp
Normal file
87
src/gamedata/WaterModule.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <Ogre.h>
|
||||
#include "GameData.h"
|
||||
#include "Components.h"
|
||||
#include "WaterModule.h"
|
||||
namespace ECS
|
||||
{
|
||||
static const uint32_t WATER_MASK = 0xF00;
|
||||
WaterModule::WaterModule(flecs::world &ecs)
|
||||
{
|
||||
ecs.module<WaterModule>();
|
||||
ecs.component<WaterSurface>().add(flecs::Singleton);
|
||||
ecs.component<WaterBody>().add(flecs::Singleton);
|
||||
ecs.set<WaterSurface>({ nullptr, nullptr });
|
||||
ecs.set<WaterBody>({ nullptr });
|
||||
ecs.system<const EngineData, const Camera, WaterSurface>("UpdateWater")
|
||||
.kind(flecs::OnUpdate)
|
||||
.each([](const EngineData &eng, const Camera &camera,
|
||||
WaterSurface &water) {
|
||||
float delta = eng.delta;
|
||||
if (!water.mWaterEnt || !water.mWaterNode) {
|
||||
water.mWaterNode =
|
||||
eng.mScnMgr->getRootSceneNode()
|
||||
->createChildSceneNode("Water");
|
||||
|
||||
/*
|
||||
auto mat = Ogre::MaterialManager::getSingleton()
|
||||
.getByName("Water/Above");
|
||||
mat->load();
|
||||
mat->setReceiveShadows(false);
|
||||
auto mat2 =
|
||||
Ogre::MaterialManager::getSingleton()
|
||||
.getByName("Water/Below");
|
||||
mat2->load();
|
||||
mat2->setReceiveShadows(false);
|
||||
*/
|
||||
Ogre::Vector3 cameraPosition =
|
||||
camera.mCameraNode->getPosition();
|
||||
water.mWaterEnt = eng.mScnMgr->createEntity(
|
||||
"Ocean", "sea.glb");
|
||||
water.mWaterEnt->setVisibilityFlags(WATER_MASK);
|
||||
water.mWaterEnt->setCastShadows(true);
|
||||
// water.mWaterEnt->setMaterialName("Water/Above");
|
||||
// water.mWaterEnt->setMaterial(mat);
|
||||
water.mWaterNode->attachObject(water.mWaterEnt);
|
||||
// mDynWorld->attachCollisionObject(
|
||||
// mWaterBody, water_ent, 1, 0x7FFFFFFF);
|
||||
}
|
||||
Ogre::Vector3 mCameraPos =
|
||||
camera.mCameraNode->_getDerivedPosition();
|
||||
Ogre::Vector3 waterPos =
|
||||
water.mWaterNode->_getDerivedPosition();
|
||||
mCameraPos.y = 0;
|
||||
waterPos.y = 0;
|
||||
Ogre::Vector3 d = mCameraPos - waterPos;
|
||||
// Ogre::Vector3 waterPosition = mCameraPos;
|
||||
// mWaterNode->setPosition(waterPosition);
|
||||
if (d.squaredLength() < 100.0f * 100.0f)
|
||||
water.mWaterNode->translate(d * 3.0f * delta);
|
||||
else
|
||||
water.mWaterNode->translate(d);
|
||||
});
|
||||
ecs.system<const EngineData, const WaterSurface, WaterBody>(
|
||||
"UpdateWaterBody")
|
||||
.kind(flecs::OnUpdate)
|
||||
.each([](const EngineData &eng, const WaterSurface &water,
|
||||
WaterBody &body) {
|
||||
if (!body.mWaterBody) {
|
||||
body.mWaterBody = new btGhostObject;
|
||||
btBoxShape *boxShape = new btBoxShape(
|
||||
btVector3(1000, 1000, 1000));
|
||||
btCompoundShape *shape = new btCompoundShape;
|
||||
shape->addChildShape(
|
||||
btTransform(btQuaternion(),
|
||||
btVector3(0, -1000, 0)),
|
||||
boxShape);
|
||||
body.mWaterBody->setCollisionShape(shape);
|
||||
body.mWaterBody->setCollisionFlags(
|
||||
body.mWaterBody->getCollisionFlags() |
|
||||
btCollisionObject::CF_NO_CONTACT_RESPONSE |
|
||||
btCollisionObject::CF_STATIC_OBJECT);
|
||||
eng.mWorld->attachCollisionObject(
|
||||
body.mWaterBody, water.mWaterEnt, 16,
|
||||
0x7fffffff & ~2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user