152 lines
4.1 KiB
C++
152 lines
4.1 KiB
C++
#include <iostream>
|
|
#include <Ogre.h>
|
|
#include <OgreMeshManager.h>
|
|
#include <pugixml.hpp>
|
|
#include "loader.h"
|
|
#include "Components.h"
|
|
#include "GameData.h"
|
|
#include "BoatModule.h"
|
|
|
|
namespace ECS
|
|
{
|
|
|
|
BoatModule::BoatModule(flecs::world &ecs)
|
|
{
|
|
ecs.module<BoatModule>();
|
|
ecs.component<BoatBase>();
|
|
ecs.component<BoatBody>();
|
|
ecs.system<const EngineData, BoatType, Body2Entity>("CreateBoat")
|
|
.kind(flecs::OnUpdate)
|
|
.without<BoatBase>()
|
|
.without<BoatBody>()
|
|
.each([](flecs::entity e, const EngineData &eng, BoatType &type,
|
|
Body2Entity &b2e) {
|
|
BoatBase &boat = e.ensure<BoatBase>();
|
|
if (type.resourceName.find(".glb") !=
|
|
std::string::npos) {
|
|
boat.mEnt = ECS::get<EngineData>()
|
|
.mScnMgr->createEntity(
|
|
type.resourceName);
|
|
boat.mNode = eng.mScnMgr->getRootSceneNode()
|
|
->createChildSceneNode(
|
|
type.position,
|
|
type.orientation);
|
|
boat.mNode->attachObject(boat.mEnt);
|
|
|
|
BoatBody &body = e.ensure<BoatBody>();
|
|
body.body =
|
|
ECS::get<EngineData>()
|
|
.mWorld->addRigidBody(
|
|
0, boat.mEnt,
|
|
Ogre::Bullet::CT_TRIMESH,
|
|
nullptr, 2, 0x7fffffff);
|
|
b2e.entities[body.body] = e;
|
|
} else if (type.resourceName.find(".scene") !=
|
|
std::string::npos) {
|
|
boat.mNode =
|
|
ECS::get<EngineData>()
|
|
.mScnMgr->getRootSceneNode()
|
|
->createChildSceneNode(
|
|
type.position,
|
|
type.orientation);
|
|
Ogre::SceneNode *attachment =
|
|
eng.mScnMgr->getRootSceneNode()
|
|
->createChildSceneNode();
|
|
Ogre::DataStreamPtr scene =
|
|
Ogre::ResourceGroupManager::getSingleton()
|
|
.openResource(
|
|
type.resourceName,
|
|
Ogre::ResourceGroupManager::
|
|
AUTODETECT_RESOURCE_GROUP_NAME);
|
|
SceneLoader loader;
|
|
loader.load(scene, "General", attachment, e);
|
|
// attachment->loadChildren(type.resourceName);
|
|
std::vector<Ogre::Node *> v =
|
|
attachment->getChildren();
|
|
int i;
|
|
OgreAssert(v.size() == 1, "Bad nodes count");
|
|
Ogre::Any any =
|
|
static_cast<Ogre::SceneNode *>(v[0])
|
|
->getUserObjectBindings()
|
|
.getUserAny("type");
|
|
OgreAssert(any.has_value(),
|
|
"bas node type costom prop");
|
|
Ogre::String obj_type =
|
|
Ogre::any_cast<Ogre::String>(any);
|
|
std::cout << "type: " << obj_type << std::endl;
|
|
OgreAssert(obj_type == "boat", "not a boat");
|
|
boat.mNode =
|
|
static_cast<Ogre::SceneNode *>(v[0]);
|
|
boat.mNode->_setDerivedPosition(type.position);
|
|
boat.mNode->_setDerivedOrientation(
|
|
type.orientation);
|
|
boat.mEnt = static_cast<Ogre::Entity *>(
|
|
boat.mNode->getAttachedObject(
|
|
std::to_string((int)e.raw_id()) +
|
|
"/boat"));
|
|
BoatBody &body = e.ensure<BoatBody>();
|
|
body.body =
|
|
ECS::get<EngineData>()
|
|
.mWorld->addRigidBody(
|
|
0, boat.mEnt,
|
|
Ogre::Bullet::CT_BOX,
|
|
nullptr, 2, 0x7fffffff);
|
|
b2e.entities[body.body] = e;
|
|
std::vector<Ogre::Node *> slots =
|
|
boat.mNode->getChildren();
|
|
for (i = 0; i < slots.size(); i++) {
|
|
Ogre::Any any =
|
|
static_cast<Ogre::SceneNode *>(
|
|
slots[i])
|
|
->getUserObjectBindings()
|
|
.getUserAny("type");
|
|
if (!any.has_value())
|
|
continue;
|
|
Ogre::String obj_type =
|
|
Ogre::any_cast<Ogre::String>(
|
|
any);
|
|
std::cout << "child type: " << obj_type
|
|
<< std::endl;
|
|
}
|
|
if (slots.size() > 0) {
|
|
ObjectSlots &vs =
|
|
e.ensure<ObjectSlots>();
|
|
for (i = 0; i < slots.size(); i++) {
|
|
Ogre::Any any =
|
|
static_cast<Ogre::SceneNode
|
|
*>(
|
|
slots[i])
|
|
->getUserObjectBindings()
|
|
.getUserAny(
|
|
"type");
|
|
if (!any.has_value())
|
|
continue;
|
|
Ogre::String obj_type =
|
|
Ogre::any_cast<
|
|
Ogre::String>(
|
|
any);
|
|
any = static_cast<
|
|
Ogre::SceneNode *>(
|
|
slots[i])
|
|
->getUserObjectBindings()
|
|
.getUserAny(
|
|
"name");
|
|
if (!any.has_value())
|
|
continue;
|
|
Ogre::String obj_name =
|
|
Ogre::any_cast<
|
|
Ogre::String>(
|
|
any);
|
|
vs.slots[obj_name] = {
|
|
obj_type,
|
|
static_cast<Ogre::SceneNode
|
|
*>(
|
|
slots[i])
|
|
};
|
|
}
|
|
}
|
|
}
|
|
e.modified<BoatBody>();
|
|
});
|
|
}
|
|
} |