godot-lportal/lroom_manager.cpp

324 lines
7.3 KiB
C++
Raw Normal View History

// Copyright (c) 2019 Lawnjelly
// 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 "lroom_manager.h"
#include "lportal.h"
#include "lroom.h"
2019-09-12 12:07:43 +02:00
#include "core/engine.h"
2019-09-12 16:51:33 +02:00
#include "scene/3d/camera.h"
LRoomManager::LRoomManager()
{
2019-09-12 12:07:43 +02:00
m_room_curr = 0;
2019-09-12 16:51:33 +02:00
m_cameraID = 0;
}
2019-09-12 16:51:33 +02:00
void LRoomManager::set_camera(Node * pCam)
{
m_cameraID = 0;
if (!pCam)
return;
Camera * pCamera = Object::cast_to<Camera>(pCam);
if (!pCamera)
{
WARN_PRINT("Not a camera");
return;
}
m_cameraID = pCam->get_instance_id();
}
// convert empties and meshes to rooms and portals
2019-09-12 12:07:43 +02:00
void LRoomManager::convert()
{
2019-09-12 12:07:43 +02:00
print_line("running convert");
Convert_Rooms();
Convert_Portals();
2019-09-12 12:07:43 +02:00
Find_Rooms();
}
void LRoomManager::Find_Rooms()
{
print_line ("Find_Rooms");
m_room_IDs.clear();
// first find all room empties and convert to LRooms
for (int n=0; n<get_child_count(); n++)
{
Node * pChild = get_child(n);
// don't want to handle already converted rooms
LRoom * pRoom = Object::cast_to<LRoom>(pChild);
if (pRoom)
2019-09-12 16:51:33 +02:00
{
pRoom->m_LocalRoomID = m_room_IDs.size();
2019-09-12 12:07:43 +02:00
m_room_IDs.push_back(pRoom->get_instance_id());
2019-09-12 16:51:33 +02:00
}
2019-09-12 12:07:43 +02:00
}
m_room_curr = 0;
// just set current room to first room
if (m_room_IDs.size())
{
m_room_curr = m_room_IDs[0];
print_line("first room ID is " + itos(m_room_curr));
}
2019-09-12 16:51:33 +02:00
// make sure bitfield is right size for number of rooms
m_BF_visible_rooms.Create(m_room_IDs.size());
}
void LRoomManager::Convert_Rooms()
{
2019-09-12 12:07:43 +02:00
print_line("Convert_Rooms");
bool bConvertedOne = true;
// instead of recursive routine
while (bConvertedOne)
{
bConvertedOne = false;
// first find all room empties and convert to LRooms
for (int n=0; n<get_child_count(); n++)
{
Node * pChild = get_child(n);
// don't want to handle already converted rooms
LRoom * pRoom = Object::cast_to<LRoom>(pChild);
if (pRoom)
continue;
2019-09-12 12:07:43 +02:00
Spatial * pSpatialChild = Object::cast_to<Spatial>(pChild);
if (!pSpatialChild)
continue;
if (LPortal::NameStartsWith(pSpatialChild, "room_"))
{
2019-09-12 12:07:43 +02:00
if (Convert_Room(pSpatialChild))
bConvertedOne = true;
}
if (bConvertedOne)
break;
}
}
}
void LRoomManager::Convert_Portals()
{
for (int pass=0; pass<3; pass++)
{
2019-09-12 12:07:43 +02:00
print_line("Convert_Portals pass " + itos(pass));
// first find all room empties and convert to LRooms
for (int n=0; n<get_child_count(); n++)
{
Node * pChild = get_child(n);
// don't want to handle already converted rooms
LRoom * pRoom = Object::cast_to<LRoom>(pChild);
if (pRoom)
{
switch (pass)
{
case 0:
pRoom->DetectPortalMeshes();
break;
case 1:
pRoom->MakePortalsTwoWay();
break;
case 2:
pRoom->MakePortalQuickList();
break;
}
}
}
}
}
2019-09-12 12:07:43 +02:00
bool LRoomManager::Convert_Room(Spatial * pNode)
{
// get the room part of the name
String szFullName = pNode->get_name();
2019-09-12 12:07:43 +02:00
String szRoom = LPortal::FindNameAfter(pNode, "room_");
print_line("Convert_Room : " + szFullName);
// create a new LRoom to exchange the children over to, and delete the original empty
LRoom * pNew = memnew(LRoom);
pNew->set_name(szRoom);
add_child(pNew);
2019-09-12 12:07:43 +02:00
// make the transform of the L room match the original spatial
pNew->set_transform(pNode->get_transform());
int nChildren = pNode->get_child_count();
for (int n=0; n<nChildren; n++)
{
// reverse count
int c = nChildren - n - 1;
Node * pChild = pNode->get_child(c);
pNode->remove_child(pChild);
// add the child to the new node
pNew->add_child(pChild);
}
// all moved .. now finally delete the empty
remove_child(pNode);
pNode->queue_delete();
return true;
}
2019-09-12 12:07:43 +02:00
void LRoomManager::FrameUpdate()
{
2019-09-12 16:51:33 +02:00
if (Engine::get_singleton()->is_editor_hint())
{
WARN_PRINT_ONCE("LRoomManager::FrameUpdate should not be called in editor");
return;
}
2019-09-12 12:07:43 +02:00
// if not started
if (!m_room_curr)
return;
// determine visibility
Object *pObj = ObjectDB::get_instance(m_room_curr);
if (!pObj)
return;
LRoom * pRoom = Object::cast_to<LRoom>(pObj);
if (!pRoom)
{
WARN_PRINT_ONCE("LRoomManager::FrameUpdate : curr room is not an LRoom");
print_line("curr room is not an LRoom");
m_room_curr = 0;
return;
}
2019-09-12 16:51:33 +02:00
m_BF_visible_rooms.Blank();
2019-09-12 12:07:43 +02:00
LCamera cam;
cam.m_ptPos = Vector3(0, 0, 0);
cam.m_ptDir = Vector3 (-1, 0, 0);
// reset the pool for another frame
m_Pool.Reset();
unsigned int pool_member = m_Pool.Request();
assert (pool_member != -1);
LVector<Plane> &planes = m_Pool.Get(pool_member);
planes.clear();
2019-09-12 12:07:43 +02:00
2019-09-12 16:51:33 +02:00
// get the camera desired and make into lcamera
if (m_cameraID)
{
Object *pObj = ObjectDB::get_instance(m_cameraID);
2019-09-12 12:07:43 +02:00
2019-09-12 16:51:33 +02:00
Camera * pCamera = Object::cast_to<Camera>(pObj);
if (pCamera)
{
Transform tr = pCamera->get_global_transform();
cam.m_ptPos = tr.origin;
cam.m_ptDir = tr.basis.get_row(2); // or possibly get_axis .. z is what we want
2019-09-12 12:07:43 +02:00
planes.copy_from(pCamera->get_frustum());
2019-09-12 16:51:33 +02:00
}
}
pRoom->DetermineVisibility_Recursive(*this, 0, cam, planes, m_BF_visible_rooms);
2019-09-12 16:51:33 +02:00
// finally hide all the rooms that are currently visible but not in the visible bitfield as having been hit
// NOTE this could be more efficient
for (int n=0; n<m_room_IDs.size(); n++)
{
Object *pObj = ObjectDB::get_instance(m_room_IDs[n]);
LRoom * pRoom = Object::cast_to<LRoom>(pObj);
if (pRoom)
{
if (!m_BF_visible_rooms.GetBit(n))
{
pRoom->hide();
}
}
}
2019-09-12 12:07:43 +02:00
// only do once for now
2019-09-12 16:51:33 +02:00
// m_room_curr = 0;
2019-09-12 12:07:43 +02:00
}
void LRoomManager::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
// bool bVisible = is_visible_in_tree();
// ChangeFlags(SF_INVISIBLE, bVisible == false);
// SetProcessing();
if (!Engine::get_singleton()->is_editor_hint())
set_process_internal(true);
2019-09-12 16:51:33 +02:00
else
set_process_internal(false);
2019-09-12 12:07:43 +02:00
// // we can't translate string name of Target to a node until we are in the tree
// ResolveTargetPath();
} break;
// case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
// FixedUpdate();
// } break;
case NOTIFICATION_INTERNAL_PROCESS: {
FrameUpdate();
} break;
// case NOTIFICATION_VISIBILITY_CHANGED: {
// bool bVisible = is_visible_in_tree();
// ChangeFlags(SF_INVISIBLE, bVisible == false);
// SetProcessing();
//// if (bVisible)
//// print_line("now visible");
//// else
//// print_line("now hidden");
// } break;
}
}
void LRoomManager::_bind_methods()
{
2019-09-12 12:07:43 +02:00
ClassDB::bind_method(D_METHOD("convert"), &LRoomManager::convert);
2019-09-12 16:51:33 +02:00
ClassDB::bind_method(D_METHOD("set_camera"), &LRoomManager::set_camera);
}