TechDemo/Levels/TestLevel.cpp

575 lines
18 KiB
C++
Raw Normal View History

2023-11-14 00:32:05 -08:00
#include "TestLevel.h"
2024-07-05 13:17:24 -07:00
#include <ehs/GC.h>
2024-01-26 09:01:01 -08:00
#include <ehs/io/Console.h>
#include <ehs/io/File.h>
#include <ehs/io/socket/DNS.h>
#include <ehs/io/audio/Audio.h>
#include <ehs/io/hid/Keyboard.h>
#include <ehs/io/hid/Mouse.h>
#include <lwe/GameLoop.h>
#include <lwe/RenderWindow.h>
#include <lwe/gpu/GpuModel.h>
#include <lwe/gpu/GpuFontAtlas.h>
#include <lwe/gpu/GpuImg.h>
#include <lwe/gpu/GpuCubeMap.h>
#include <lwe/systems/TimerSystem.h>
#include <lwe/systems/GuiSystem.h>
#include <lwe/systems/RigidBodySystem.h>
#include <lwe/systems/GuiSystem.h>
#include <lwe/systems/AudioSystem.h>
#include <lwe/systems/CameraSystem.h>
#include <lwe/systems/SkyboxSystem.h>
#include <lwe/systems/LightSystem.h>
#include <lwe/systems/SolidColorSystem.h>
#include <lwe/systems/NetSystem.h>
#include <lwe/systems/PhongSystem.h>
#include <lwe/coms/Camera.h>
#include <lwe/coms/PlyController.h>
#include <lwe/coms/Skybox.h>
#include <lwe/coms/AmbientPointLight.h>
#include <lwe/coms/PointLight.h>
#include <lwe/coms/SolidColor.h>
#include <lwe/coms/Phong.h>
#include "lwe/Timer.h"
#include <lwe/coms/AudioSource3D.h>
#include <lwe/coms/AABB2D.h>
#include <lwe/coms/Circle.h>
#include <lwe/coms/OBB3D.h>
#include <lwe/coms/Projectile.h>
#include <lwe/coms/Health.h>
#include <lwe/coms/Networked.h>
#include <lwe/gui/LabelGui.h>
#include <lwe/gui/SolidBoxGui.h>
#include <lwe/gui/ButtonGui.h>
#include <lwe/gui/CheckBoxGui.h>
#include <lwe/gui/TextFieldGui.h>
#include <lwe/gui/ImgGui.h>
#include <lwe/gui/PanelGui.h>
#include <lwe/gui/CollectionGui.h>
#include <lwe/gui/WindowGui.h>
#include <lwe/systems/BillboardSystem.h>
#include <lwe/coms/Billboard.h>
2023-11-14 00:32:05 -08:00
TestLevel::TestLevel()
: count(0), cooldown(0.0f)
{
}
2024-01-26 09:01:01 -08:00
TestLevel::TestLevel(const ehs::Str_8& id)
2023-11-14 00:32:05 -08:00
: Level(id), count(0), cooldown(0.0f)
{
}
TestLevel::TestLevel(const TestLevel& level)
: Level(level), count(0), cooldown(0.0f)
{
}
TestLevel& TestLevel::operator=(const TestLevel& level)
{
if (this == &level)
return *this;
Level::operator=(level);
count = 0;
cooldown = 0.0f;
return *this;
}
2024-01-26 09:01:01 -08:00
void TestLevel::SetupResources(lwe::GpuInterface* inf)
2023-11-14 00:32:05 -08:00
{
2024-01-26 09:01:01 -08:00
lwe::GpuFontAtlas* arial_24 = new lwe::GpuFontAtlas("resources/fonts/Arial_24.ehf", inf);
2023-11-14 00:32:05 -08:00
AddResource(arial_24);
2024-01-26 09:01:01 -08:00
lwe::GpuModel* vampire = new lwe::GpuModel("resources/models/Vampire.ehm", inf);
2023-11-14 00:32:05 -08:00
AddResource(vampire);
2024-07-05 13:17:24 -07:00
2024-01-26 09:01:01 -08:00
AddResource(new lwe::GpuImg("resources/textures/Character_Diffuse.png", inf, lwe::IMG_ASPECT_COLOR));
AddResource(new lwe::GpuImg("resources/textures/Character_Normal.png", inf, lwe::IMG_ASPECT_COLOR));
2024-02-20 23:07:28 -08:00
AddResource(new lwe::GpuImg("resources/textures/Character_Specular.png", inf, lwe::IMG_ASPECT_COLOR));
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
lwe::GpuMesh* portrait = new lwe::GpuMesh(inf, ehs::portrait);
2023-11-14 00:32:05 -08:00
//portrait->Calculate();
AddResource(portrait);
2024-01-26 09:01:01 -08:00
lwe::GpuMesh* portraitGUI = new lwe::GpuMesh(inf, ehs::portraitGui);
2023-11-14 00:32:05 -08:00
AddResource(portraitGUI);
2024-02-20 23:07:28 -08:00
AddResource(new lwe::GpuModel("resources/models/Cube.ehm", inf));
AddResource(new lwe::GpuModel("resources/models/Sphere.ehm", inf));
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::GpuCubeMap* cm = new lwe::GpuCubeMap(inf, "resources/textures/skybox", "Skybox");
2023-11-14 00:32:05 -08:00
AddResource(cm);
2024-02-20 23:07:28 -08:00
AddResource(new ehs::Audio("resources/audio/sample.wav", ehs::DataType::FLOAT));
2023-11-14 00:32:05 -08:00
}
2024-02-20 23:07:28 -08:00
void TestLevel::Setup(lwe::GpuInterface* inf)
2023-11-14 00:32:05 -08:00
{
lwe::GameLoop* gl = (lwe::GameLoop*)GetParent();
if (!gl)
return;
/*
lwe::File configFile("Config.json", lwe::Mode::READ, lwe::Disposition::OPEN);
configFile.Initialize();
lwe::Json config(configFile.ReadStr(configFile.Size()), 0);
configFile.UnInitialize();
if (gl->GetWindow())
{
lwe::NetSystem* ns = new lwe::NetSystem(ehc::Disposition::ENDPOINT, *(lwe::JsonStr*)config.RetrieveValue("client.username"), 0);
ns->GetSocket()->SetAddressType(lwe::AddrType::IPV6);
ns->GetSocket()->EnableDropPackets(true);
ns->GetSocket()->SetMaxTimeout(*(lwe::JsonNum*)config.RetrieveValue("server_client.timeout"));
ns->GetSocket()->SetResendRate(*(lwe::JsonNum*)config.RetrieveValue("server_client.resendRate"));
AddSystem(ns);
}
else
{
lwe::NetSystem* ns = new lwe::NetSystem(ehc::Disposition::SERVICE, *(lwe::JsonStr*)config.RetrieveValue("server.identifier"), (lwe::UInt_32)*(lwe::JsonNum*)config.RetrieveValue("server.maxPlayers"));
ns->GetSocket()->SetAddressType(lwe::AddrType::IPV6);
ns->GetSocket()->EnableDropPackets(true);
ns->GetSocket()->SetMaxTimeout(*(lwe::JsonNum*)config.RetrieveValue("server_client.timeout"));
ns->GetSocket()->SetResendRate(*(lwe::JsonNum*)config.RetrieveValue("server_client.resendRate"));
ns->GetSocket()->SetActiveCb([](ehc::Socket* sock, ehc::Endpoint* end){
lwe::Level* lvl = (lwe::Level*)sock->GetParent("Level");
if (!lvl)
return;
lwe::Entity* ply = lvl->CreateEntity("Puppet", "Ply-" + end->GetId());
lwe::Networked* net = (lwe::Networked*)ply->GetComponent("Networked", "");
net->SetOwner(end->GetHashId());
lwe::Serializer puppetPayload(lwe::Endianness::LE);
puppetPayload.Write(lwe::Str_8::Hash_64("Puppet"));
puppetPayload.WriteStr(ply->GetId());
puppetPayload.Write(end->GetHashId());
puppetPayload.WriteVec3(ply->GetPos());
puppetPayload.WriteVec3(ply->GetRot());
puppetPayload.WriteVec3(ply->GetScale());
lwe::Serializer playerPayload(lwe::Endianness::LE);
playerPayload.Write(lwe::Str_8::Hash_64("Player"));
playerPayload.WriteStr(ply->GetId());
playerPayload.Write(end->GetHashId());
playerPayload.WriteVec3(ply->GetPos());
playerPayload.WriteVec3(ply->GetRot());
playerPayload.WriteVec3(ply->GetScale());
lwe::Array<ehc::Endpoint*> endpoints = sock->GetEndpoints(ehc::Disposition::ENDPOINT, ehc::Status::ACTIVE);
for (lwe::UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i] == end)
endpoints[i]->Send(false, true, true, "LWE", "CreateEnt", playerPayload);
else
{
lwe::Serializer otherPuppet(lwe::Endianness::LE);
otherPuppet.Write(lwe::Str_8::Hash_64("Puppet"));
otherPuppet.WriteStr("Ply-" + endpoints[i]->GetId());
playerPayload.Write(endpoints[i]->GetHashId());
otherPuppet.WriteVec3(ply->GetPos());
otherPuppet.WriteVec3(ply->GetRot());
otherPuppet.WriteVec3(ply->GetScale());
end->Send(false, true, true, "LWE", "CreateEnt", otherPuppet);
endpoints[i]->Send(false, true, true, "LWE", "CreateEnt", puppetPayload);
}
}
});
ns->GetSocket()->SetDisconnectedCb([](ehc::Socket* sock, ehc::Endpoint* end){
Level* lvl = (Level*)sock->GetParent("Level");
if (!lvl)
return;
lwe::Entity* ply = lvl->GetEntity("Ply-" + end->GetId());
if (!ply)
return;
lwe::Serializer payload(lwe::Endianness::LE);
payload.Write(ply->GetHashId());
lwe::Array<ehc::Endpoint*> endpoints = sock->GetEndpoints(ehc::Disposition::ENDPOINT, ehc::Status::ACTIVE);
for (lwe::UInt_64 i = 0; i < endpoints.Size(); ++i)
{
if (endpoints[i] == end)
continue;
endpoints[i]->Send(false, true, true, "LWE", "DeleteEnt", payload);
}
ply->Delete();
});
AddSystem(ns);
}
*/
2023-12-05 16:02:42 -08:00
//AddSystem(new lwe::AudioSystem());
2023-11-14 00:32:05 -08:00
AddSystem(new lwe::SkyboxSystem());
2023-12-05 16:02:42 -08:00
AddSystem(new lwe::RigidBodySystem(true));
AddSystem(new lwe::CameraSystem());
2023-11-14 00:32:05 -08:00
AddSystem(new lwe::PhongSystem());
AddSystem(new lwe::SolidColorSystem());
AddSystem(new lwe::BillboardSystem());
lwe::LightSystem* ls = new lwe::LightSystem(true);
ls->SetAmbient({0.25f, 0.25f, 0.25f});
AddSystem(ls);
lwe::TimerSystem* timerSys = new lwe::TimerSystem();
AddSystem(timerSys);
lwe::GuiSystem* guiSys = new lwe::GuiSystem();
AddSystem(guiSys);
lwe::CollectionGui* stats = new lwe::CollectionGui("Stats");
stats->SetScale({250.0f, 127.0f});
2024-01-26 09:01:01 -08:00
stats->AddResource(new lwe::GpuFontAtlas("resources/fonts/Arial_24.ehf", inf));
2023-11-14 00:32:05 -08:00
lwe::SolidBoxGui* bckgrd = new lwe::SolidBoxGui("Background");
bckgrd->SetScale({250.0f, 127.0f});
bckgrd->SetColor({0.0f, 0.0f, 0.0f, 0.5f});
stats->AddChild(bckgrd);
lwe::LabelGui* fps = new lwe::LabelGui("FPS", "Arial_24", "FPS: 0");
fps->SetColor({1.0f});
stats->AddChild(fps);
lwe::LabelGui* deltaTime = new lwe::LabelGui("DeltaTime", "Arial_24", "Delta Time: 0");
deltaTime->SetColor({1.0f});
2024-01-26 09:01:01 -08:00
deltaTime->SetPosition({0.0f, 24.0f, 0.0f});
2023-11-14 00:32:05 -08:00
stats->AddChild(deltaTime);
lwe::LabelGui* gbg = new lwe::LabelGui("Garbage", "Arial_24", "Garbage: 0");
gbg->SetColor({1.0f});
2024-01-26 09:01:01 -08:00
gbg->SetPosition({0.0f, 48.0f, 0.0f});
2023-11-14 00:32:05 -08:00
stats->AddChild(gbg);
lwe::LabelGui* playback = new lwe::LabelGui("Playback", "Arial_24", "Playback: 0:0 / 0:0");
playback->SetColor({1.0f});
2024-01-26 09:01:01 -08:00
playback->SetPosition({0.0f, 72.0f, 0.0f});
2023-11-14 00:32:05 -08:00
stats->AddChild(playback);
lwe::LabelGui* volume = new lwe::LabelGui("Volume", "Arial_24", "Volume: 0");
volume->SetColor({1.0f});
2024-01-26 09:01:01 -08:00
volume->SetPosition({0.0f, 96.0f, 0.0f});
2023-11-14 00:32:05 -08:00
stats->AddChild(volume);
guiSys->AddGui(stats);
2024-01-26 09:01:01 -08:00
ehs::Serializer<ehs::UInt_64> args(ehs::Endianness::LE);
2023-11-14 00:32:05 -08:00
args.Write(stats);
args.SetOffset(0);
2024-01-26 09:01:01 -08:00
timerSys->Add({0, 1.0f, args, [](lwe::Timer* timer, ehs::Serializer<ehs::UInt_64>& args)
2023-11-14 00:32:05 -08:00
{
2024-02-20 23:07:28 -08:00
lwe::TimerSystem* sys = timer->GetParent();
lwe::Level* lvl = sys->GetParent();
lwe::GameLoop* gl = lvl->GetParent();
2023-11-14 00:32:05 -08:00
lwe::CollectionGui* stats = args.Read<lwe::CollectionGui*>();
args.SetOffset(0);
lwe::LabelGui* deltaTime = (lwe::LabelGui*)stats->GetChild("DeltaTime");
2024-01-26 09:01:01 -08:00
deltaTime->SetText("Delta Time: " + ehs::Str_8::FromNum(gl->GetRawDeltaTime()));
2023-11-14 00:32:05 -08:00
lwe::LabelGui* gbg = (lwe::LabelGui*)stats->GetChild("Garbage");
2024-07-05 13:17:24 -07:00
gbg->SetText("Garbage: " + ehs::Str_8::FromNum(ehs::GC::Size()));
2023-11-14 00:32:05 -08:00
}});
2024-01-26 09:01:01 -08:00
/*
2023-12-05 16:02:42 -08:00
lwe::PanelGui* testPanel = new lwe::PanelGui("TestPanel");
2024-01-26 09:01:01 -08:00
testPanel->SetPosition({300.0f, 200.0f, 0.0f});
2023-12-05 16:02:42 -08:00
testPanel->SetScale({400.0f, 400.0f});
guiSys->AddGui(testPanel);
2024-01-26 09:01:01 -08:00
lwe::WindowGui* testWin = new lwe::WindowGui("TestWindow", "Hello world!");
testWin->SetPosition({300.0f, 200.0f, 0.0f});
testWin->SetScale({400.0f, 400.0f});
guiSys->AddGui(testWin);
2024-02-20 23:07:28 -08:00
*/
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity player("Player");
2023-11-14 00:32:05 -08:00
//player->AddComponent(new lwe::Networked(true, false, lwe::EndpointOwner::ENDPOINT));
2024-02-20 23:07:28 -08:00
player.AddComponent(new lwe::Point3D("Head", 1.0f, 1.0f));
player.AddComponent(new lwe::PlyController("Head"));
2023-11-14 00:32:05 -08:00
//player->AddComponent(new lwe::Health(100, 100));
lwe::SolidColor* plyMdl = new lwe::SolidColor("Cube", {0.0f, 0.0f, 1.0f}, "Cube", "");
plyMdl->SetDiffused(true);
plyMdl->SetScale({0.5f});
plyMdl->SetPos({0.0f, 0.0f, -0.5f});
//player->AddComponent(plyMdl);
2024-01-26 09:01:01 -08:00
AddTemplate((lwe::Entity&&)player);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity plyPuppet("Puppet");
plyPuppet.AddComponent(new lwe::OBB3D("Head", 1.0f, 1.0f));
plyPuppet.AddComponent(new lwe::Camera("Head"));
plyPuppet.AddComponent(new lwe::Health(100, 100));
2023-11-14 00:32:05 -08:00
lwe::SolidColor* puppetMdl = new lwe::SolidColor("Cube", {0.0f, 0.0f, 1.0f}, "Cube", "");
puppetMdl->SetDiffused(true);
puppetMdl->SetScale({0.5f});
puppetMdl->SetPos({0.0f, 0.0f, -0.5f});
//plyPuppet->AddComponent(puppetMdl);
2024-01-26 09:01:01 -08:00
AddTemplate((lwe::Entity&&)plyPuppet);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
CreateEntity("Player", "Player");
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity skyboxEnt("Skybox");
2023-11-14 00:32:05 -08:00
lwe::Skybox* skybox = new lwe::Skybox("Skybox", "Skybox");
2024-02-20 23:07:28 -08:00
skyboxEnt.AddComponent(skybox);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddEntity((lwe::Entity&&)skyboxEnt);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity ent("Main");
ent.SetScale({0.25f, 0.25f, 0.25f});
2024-07-24 00:51:03 -07:00
ent.SetPos({0.0f, -1.0f, 10.0f});
2023-11-14 00:32:05 -08:00
//ent->SetRot({-90.0f, 180.0f, 0.0f});
//ent->AddComponent(new lwe::Networked(false, false, lwe::EndpointOwner::SERVICE));
lwe::AudioSource3D* as = new lwe::AudioSource3D("Song", "sample", 20.0f);
as->Pause();
as->EnableLoop(true);
2024-02-20 23:07:28 -08:00
as->EnableAutoDelete(false);
2023-11-14 00:32:05 -08:00
//as->EnablePanning(false);
//as->EnableAttenuation(false);
2024-02-20 23:07:28 -08:00
ent.AddComponent(as);
2023-11-14 00:32:05 -08:00
2023-12-05 16:02:42 -08:00
lwe::Health* entHealth = new lwe::Health(100, 100);
2024-02-20 23:07:28 -08:00
ent.AddComponent(entHealth);
2023-12-05 16:02:42 -08:00
lwe::OBB3D* entCollider = new lwe::OBB3D("Collider", 1.0f, 1.0f);
entCollider->SetScale({1.0f, 2.0f, 1.0f});
entCollider->SetPos({0.0f, 1.1f, 0.0f});
entCollider->SetCollidedCb([](lwe::RigidBody* a, lwe::RigidBody* b)
{
2024-01-26 09:01:01 -08:00
lwe::Entity* aOwner = a->GetEntity();
2023-12-05 16:02:42 -08:00
lwe::Health* health = (lwe::Health*)aOwner->GetComponent("Health");
health->TakeDamage(10);
2024-01-26 09:01:01 -08:00
lwe::Entity* bOwner = b->GetEntity();
2023-12-05 16:02:42 -08:00
bOwner->Delete();
if (health->GetHealth() <= 0)
aOwner->Delete();
});
2024-02-20 23:07:28 -08:00
ent.AddComponent(entCollider);
2023-11-14 00:32:05 -08:00
/*
lwe::AudioSource* as2 = new lwe::AudioSource("Song2", "sample");
as2->Play();
ent->AddComponent(as2);
*/
lwe::Phong* ph = new lwe::Phong("Cube", "Vampire", "", "Character_Diffuse", "Character_Specular", "Character_Normal");
ph->SetAnimation("Test");
2024-02-20 23:07:28 -08:00
ent.AddComponent(ph);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddEntity((lwe::Entity&&)ent);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
lwe::Entity sun("Sun");
2024-07-24 00:51:03 -07:00
sun.SetPos({-5.0f, 0.0f, 10.0f});
2023-11-14 00:32:05 -08:00
lwe::AmbientPointLight* sunLight = new lwe::AmbientPointLight("Light");
sunLight->SetDiffuse({1.0f, 0.77254901960784313725490196078431f, 0.56078431372549019607843137254902f});
2024-01-26 09:01:01 -08:00
sun.AddComponent(sunLight);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddEntity((lwe::Entity&&)sun);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity emergency("Emergency");
2024-07-24 00:51:03 -07:00
emergency.SetPos({5.0f, 0.0f, 10.0f});
2023-11-14 00:32:05 -08:00
lwe::PointLight* pl = new lwe::PointLight("Light");
pl->SetColor({1.0f, 0.0f, 0.0f});
2024-02-20 23:07:28 -08:00
emergency.AddComponent(pl);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddEntity((lwe::Entity&&)emergency);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity testEnt("Test");
2023-11-14 00:32:05 -08:00
lwe::Billboard* testBb = new lwe::Billboard("TestBB", "Character_Diffuse");
2024-02-20 23:07:28 -08:00
testEnt.AddComponent(testBb);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddEntity((lwe::Entity&&)testEnt);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::Entity bullet("Bullet");
bullet.SetScale({0.1f, 0.1f, 0.1f});
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
bullet.AddComponent(new lwe::Projectile(25));
2023-12-05 16:02:42 -08:00
lwe::OBB3D* bulletCollider = new lwe::OBB3D("Collider", 1.0f, 1.0f);
bulletCollider->SetScale(0.1f);
2024-02-20 23:07:28 -08:00
bullet.AddComponent(bulletCollider);
2023-11-14 00:32:05 -08:00
lwe::SolidColor* bulletMdl = new lwe::SolidColor("Mdl", {1.0f, 1.0f, 0.0f, 1.0f}, "Cube", "");
2024-02-20 23:07:28 -08:00
bullet.AddComponent(bulletMdl);
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
AddTemplate((lwe::Entity&&)bullet);
2023-11-14 00:32:05 -08:00
}
void TestLevel::PostInitialize(lwe::GpuCmdBuffer* cmdBuffer)
{
lwe::GameLoop* gl = (lwe::GameLoop*)GetParent();
if (!gl)
return;
/*
lwe::NetSystem* ns = (lwe::NetSystem*)GetSystem("NetSystem");
if (!ns)
return;
lwe::File configFile("Config.json", lwe::Mode::READ, lwe::Disposition::OPEN);
configFile.Initialize();
lwe::Json config(configFile.ReadStr(configFile.Size()), 0);
configFile.UnInitialize();
if (gl->GetWindow())
{
lwe::Str_8 address = lwe::DNS::Resolve(lwe::AddrType::IPV6, *(lwe::JsonStr*)config.RetrieveValue("client.connectAddress"));
lwe::UInt_16 port = (lwe::UInt_16)*(lwe::JsonNum*) config.RetrieveValue("client.connectPort");
ns->GetSocket()->Connect(address, port);
EHS_LOG("Info", "TechDemo", 0, "Connecting to server, standby.");
}
else
{
lwe::Str_8 addr = ((lwe::JsonStr*)config.RetrieveValue("server.address"))->value;
if (addr.Size())
addr = lwe::DNS::Resolve(lwe::AddrType::IPV6, addr);
lwe::UInt_16 port = (lwe::UInt_16) *(lwe::JsonNum*) config.RetrieveValue("server.port");
ns->GetSocket()->Bind(addr, port);
EHS_LOG("Info", "TechDemo", 0, "Server successfully initialized with the id, \"" + ns->GetSocket()->GetId() + "\".");
}
*/
}
2024-01-26 09:01:01 -08:00
void TestLevel::OnUpdate(lwe::RenderWindow* win, ehs::Input* input, const float delta)
2023-11-14 00:32:05 -08:00
{
2024-01-26 09:01:01 -08:00
lwe::GameLoop* gl = GetParent();
2023-11-14 00:32:05 -08:00
if (!gl)
return;
2024-07-24 00:51:03 -07:00
lwe::GuiSystem* guiSys = (lwe::GuiSystem*)GetSystem("GuiSystem");
if (!guiSys)
return;
lwe::CollectionGui* stats = (lwe::CollectionGui*)guiSys->GetGui("Stats");
if (!stats)
return;
lwe::LabelGui* fps = (lwe::LabelGui*)stats->GetChild("FPS");
fps->SetText("FPS: " + ehs::Str_8::FromNum(gl->GetTPS()));
2024-01-26 09:01:01 -08:00
const ehs::InputHandler* ih = win->GetInputHandler();
2023-12-05 16:02:42 -08:00
if (!ih)
return;
2024-01-26 09:01:01 -08:00
const ehs::Mouse* mouse = (ehs::Mouse*)ih->GetDeviceByType(EHS_HID_MOUSE);
2023-12-05 16:02:42 -08:00
if (!mouse)
2023-11-14 00:32:05 -08:00
return;
2024-01-26 09:01:01 -08:00
const ehs::Keyboard* keyboard = (ehs::Keyboard*)ih->GetDeviceByType(EHS_HID_KEYBOARD);
2023-12-05 16:02:42 -08:00
if (!keyboard)
return;
2023-11-14 00:32:05 -08:00
lwe::Entity* ent = GetEntity("Main");
2023-12-05 16:02:42 -08:00
if (!ent)
return;
2024-07-05 13:17:24 -07:00
//ent->SetRot(ent->GetRot() + ehs::Vec3_f(50.0f) * delta);
2023-11-14 00:32:05 -08:00
lwe::Entity* ply = GetEntity("Player");
2023-12-05 16:02:42 -08:00
if (ply && win->IsCursorConstrained() && !win->IsCursorVisible())
2023-11-14 00:32:05 -08:00
{
lwe::PlyController* cam = (lwe::PlyController*)ply->GetComponent("PlyController", "Head");
if (cam)
{
lwe::CameraSystem* camSys = (lwe::CameraSystem*)GetSystem("CameraSystem");
if (camSys)
camSys->SetPrimary(ply->GetHashId(), cam->GetHashId());
2024-01-26 09:01:01 -08:00
if (cooldown <= 0.0f && mouse->IsDown(ehs::Mouse::LMB)) // Left Mouse Button
2023-11-14 00:32:05 -08:00
{
2024-01-26 09:01:01 -08:00
lwe::AudioSource* gunshot = new lwe::AudioSource("Gunshot_" + ehs::Str_8::FromNum(count), "Gunshot");
2023-11-14 00:32:05 -08:00
gunshot->SetVolume(1.0f);
ply->AddComponent(gunshot);
2024-01-26 09:01:01 -08:00
lwe::Entity* bullet = CreateEntity("Bullet", "Bullet_" + ehs::Str_8::FromNum(count++));
2023-11-14 00:32:05 -08:00
bullet->SetPos(ply->GetPos() + cam->GetTransform().GetForward() * 2.0f);
bullet->SetScale({0.1f, 0.1f, 0.1f});
bullet->SetRot(ply->GetRot());
2023-12-05 16:02:42 -08:00
lwe::OBB3D* rb = (lwe::OBB3D*)bullet->GetComponent("OBB3D", "Collider");
rb->SetVelocity(cam->GetTransform().GetForward() * 10.0f);
2023-11-14 00:32:05 -08:00
cooldown = 0.15f;
}
}
}
if (cooldown > 0.0f)
{
if (cooldown - delta < 0.0f)
cooldown = 0.0f;
else
cooldown -= delta;
}
lwe::AudioSource* source = (lwe::AudioSource*)ent->GetComponent("AudioSource3D", "Song");
if (!source)
return;
2024-02-20 23:07:28 -08:00
ehs::Audio* audio = (ehs::Audio*)GetResource("Audio", source->GetAudioHashId());
2023-11-14 00:32:05 -08:00
if (!audio)
return;
lwe::LabelGui* playback = (lwe::LabelGui*)stats->GetChild("Playback");
2024-01-26 09:01:01 -08:00
ehs::UInt_64 elapsed = source->GetFrameOffset() / audio->GetSampleRate();
ehs::UInt_64 duration = audio->GetFrameCount() / audio->GetSampleRate();
2023-11-14 00:32:05 -08:00
2024-01-26 09:01:01 -08:00
playback->SetText("Playback: " + ehs::Str_8::FromNum(elapsed / 60) + ":" + ehs::Str_8::FromNum(elapsed % 60) +
" / " + ehs::Str_8::FromNum(duration / 60) + ":" + ehs::Str_8::FromNum(duration % 60));
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
if (keyboard->IsJustReleased(ehs::Keyboard::Left))
source->SetVolume(source->GetVolume() - 0.1f);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
if (keyboard->IsJustReleased(ehs::Keyboard::Right))
source->SetVolume(source->GetVolume() + 0.1f);
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
lwe::LabelGui* volume = (lwe::LabelGui*)stats->GetChild("Volume");
volume->SetText("Volume: " + ehs::Str_8::FromNum(source->GetVolume()));
2023-11-14 00:32:05 -08:00
2024-02-20 23:07:28 -08:00
if (keyboard->IsJustReleased(ehs::Keyboard::P))
{
if (source->IsPlaying())
source->Pause();
else
source->Play();
2023-12-05 16:02:42 -08:00
}
2024-02-20 23:07:28 -08:00
if (keyboard->IsJustReleased(ehs::Keyboard::Backspace))
source->Reset();
2023-11-14 00:32:05 -08:00
}