#include "TestLevel.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "lwe/Timer.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include TestLevel::TestLevel() : count(0), cooldown(0.0f) { } TestLevel::TestLevel(const ehs::Str_8& id) : 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; } void TestLevel::SetupResources(lwe::GpuInterface* inf) { lwe::GpuFontAtlas* arial_24 = new lwe::GpuFontAtlas("resources/fonts/Arial_24.ehf", inf); AddResource(arial_24); lwe::GpuModel* vampire = new lwe::GpuModel("resources/models/Vampire.ehm", inf); AddResource(vampire); 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)); AddResource(new lwe::GpuImg("resources/textures/Character_Specular.png", inf, lwe::IMG_ASPECT_COLOR)); lwe::GpuMesh* portrait = new lwe::GpuMesh(inf, ehs::portrait); //portrait->Calculate(); AddResource(portrait); lwe::GpuMesh* portraitGUI = new lwe::GpuMesh(inf, ehs::portraitGui); AddResource(portraitGUI); AddResource(new lwe::GpuModel("resources/models/Cube.ehm", inf)); AddResource(new lwe::GpuModel("resources/models/Sphere.ehm", inf)); lwe::GpuCubeMap* cm = new lwe::GpuCubeMap(inf, "resources/textures/skybox", "Skybox"); AddResource(cm); AddResource(new ehs::Audio("resources/audio/sample.wav", ehs::DataType::FLOAT)); } void TestLevel::Setup(lwe::GpuInterface* inf) { 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 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 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); } */ lwe::AudioSystem *audioSys = new lwe::AudioSystem(); lwe::AudioSource src("Song", "sample"); src.Play(); audioSys->AddSource(src); AddSystem(audioSys); AddSystem(new lwe::SkyboxSystem()); AddSystem(new lwe::RigidBodySystem(true)); AddSystem(new lwe::CameraSystem()); 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}); AddResource(new lwe::GpuFontAtlas("resources/fonts/Arial_24.ehf", inf)); 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}); deltaTime->SetPosition({0.0f, 24.0f, 0.0f}); stats->AddChild(deltaTime); lwe::LabelGui* gbg = new lwe::LabelGui("Garbage", "Arial_24", "Garbage: 0"); gbg->SetColor({1.0f}); gbg->SetPosition({0.0f, 48.0f, 0.0f}); stats->AddChild(gbg); lwe::LabelGui* playback = new lwe::LabelGui("Playback", "Arial_24", "Playback: 0:0 / 0:0"); playback->SetColor({1.0f}); playback->SetPosition({0.0f, 72.0f, 0.0f}); stats->AddChild(playback); lwe::LabelGui* volume = new lwe::LabelGui("Volume", "Arial_24", "Volume: 0"); volume->SetColor({1.0f}); volume->SetPosition({0.0f, 96.0f, 0.0f}); stats->AddChild(volume); guiSys->AddGui(stats); ehs::Serializer args(ehs::Endianness::LE); args.Write(stats); args.SetOffset(0); timerSys->Add({0, 1.0f, args, [](lwe::Timer* timer, ehs::Serializer& args) { lwe::TimerSystem* sys = timer->GetParent(); lwe::Level* lvl = sys->GetParent(); lwe::GameLoop* gl = lvl->GetParent(); lwe::CollectionGui* stats = args.Read(); args.SetOffset(0); lwe::LabelGui* deltaTime = (lwe::LabelGui*)stats->GetChild("DeltaTime"); deltaTime->SetText("Delta Time: " + ehs::Str_8::FromNum(gl->GetRawDeltaTime())); lwe::LabelGui* gbg = (lwe::LabelGui*)stats->GetChild("Garbage"); gbg->SetText("Garbage: " + ehs::Str_8::FromNum(ehs::GC::Size())); }}); /* lwe::PanelGui* testPanel = new lwe::PanelGui("TestPanel"); testPanel->SetPosition({300.0f, 200.0f, 0.0f}); testPanel->SetScale({400.0f, 400.0f}); guiSys->AddGui(testPanel); 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); */ lwe::Entity player("Player"); //player->AddComponent(new lwe::Networked(true, false, lwe::EndpointOwner::ENDPOINT)); player.AddComponent(new lwe::Point3D("Head", 1.0f, 1.0f)); player.AddComponent(new lwe::PlyController("Head")); //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); AddTemplate((lwe::Entity&&)player); 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)); 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); AddTemplate((lwe::Entity&&)plyPuppet); CreateEntity("Player", "Player"); lwe::Entity skyboxEnt("Skybox"); lwe::Skybox* skybox = new lwe::Skybox("Skybox", "Skybox"); skyboxEnt.AddComponent(skybox); AddEntity((lwe::Entity&&)skyboxEnt); lwe::Entity ent("Main"); ent.SetScale({0.25f, 0.25f, 0.25f}); ent.SetPos({0.0f, -1.0f, 10.0f}); //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); as->EnableAutoDelete(false); ent.AddComponent(as); */ lwe::Health* entHealth = new lwe::Health(100, 100); ent.AddComponent(entHealth); 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) { lwe::Entity* aOwner = a->GetEntity(); lwe::Health* health = (lwe::Health*)aOwner->GetComponent("Health"); health->TakeDamage(10); lwe::Entity* bOwner = b->GetEntity(); bOwner->Delete(); if (health->GetHealth() <= 0) aOwner->Delete(); }); ent.AddComponent(entCollider); /* 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"); ent.AddComponent(ph); AddEntity((lwe::Entity&&)ent); lwe::Entity sun("Sun"); sun.SetPos({-5.0f, 0.0f, 10.0f}); lwe::AmbientPointLight* sunLight = new lwe::AmbientPointLight("Light"); sunLight->SetDiffuse({1.0f, 0.77254901960784313725490196078431f, 0.56078431372549019607843137254902f}); sun.AddComponent(sunLight); AddEntity((lwe::Entity&&)sun); lwe::Entity emergency("Emergency"); emergency.SetPos({5.0f, 0.0f, 10.0f}); lwe::PointLight* pl = new lwe::PointLight("Light"); pl->SetColor({1.0f, 0.0f, 0.0f}); emergency.AddComponent(pl); AddEntity((lwe::Entity&&)emergency); lwe::Entity testEnt("Test"); lwe::Billboard* testBb = new lwe::Billboard("TestBB", "Character_Diffuse"); testEnt.AddComponent(testBb); AddEntity((lwe::Entity&&)testEnt); lwe::Entity bullet("Bullet"); bullet.SetScale({0.1f, 0.1f, 0.1f}); bullet.AddComponent(new lwe::Projectile(25)); lwe::OBB3D* bulletCollider = new lwe::OBB3D("Collider", 1.0f, 1.0f); bulletCollider->SetScale(0.1f); bullet.AddComponent(bulletCollider); lwe::SolidColor* bulletMdl = new lwe::SolidColor("Mdl", {1.0f, 1.0f, 0.0f, 1.0f}, "Cube", ""); bullet.AddComponent(bulletMdl); AddTemplate((lwe::Entity&&)bullet); } 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() + "\"."); } */ } void TestLevel::OnUpdate(lwe::RenderWindow* win, ehs::Input* input, const float delta) { lwe::GameLoop* gl = GetParent(); if (!gl) return; 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())); const ehs::InputHandler* ih = win->GetInputHandler(); if (!ih) return; const ehs::Mouse* mouse = (ehs::Mouse*)ih->GetDeviceByType(EHS_HID_MOUSE); if (!mouse) return; const ehs::Keyboard* keyboard = (ehs::Keyboard*)ih->GetDeviceByType(EHS_HID_KEYBOARD); if (!keyboard) return; lwe::Entity* ent = GetEntity("Main"); if (!ent) return; //ent->SetRot(ent->GetRot() + ehs::Vec3_f(50.0f) * delta); lwe::Entity* ply = GetEntity("Player"); if (ply && win->IsCursorConstrained() && !win->IsCursorVisible()) { 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()); if (cooldown <= 0.0f && mouse->IsDown(ehs::Mouse::LMB)) // Left Mouse Button { /* lwe::AudioSource* gunshot = new lwe::AudioSource("Gunshot_" + ehs::Str_8::FromNum(count), "Gunshot"); gunshot->SetVolume(1.0f); ply->AddComponent(gunshot); */ lwe::Entity* bullet = CreateEntity("Bullet", "Bullet_" + ehs::Str_8::FromNum(count++)); bullet->SetPos(ply->GetPos() + cam->GetTransform().GetForward() * 2.0f); bullet->SetScale({0.1f, 0.1f, 0.1f}); bullet->SetRot(ply->GetRot()); lwe::OBB3D* rb = (lwe::OBB3D*)bullet->GetComponent("OBB3D", "Collider"); rb->SetVelocity(cam->GetTransform().GetForward() * 10.0f); 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; ehs::Audio* audio = (ehs::Audio*)GetResource("Audio", source->GetAudioHashId()); if (!audio) return; lwe::LabelGui* playback = (lwe::LabelGui*)stats->GetChild("Playback"); ehs::UInt_64 elapsed = source->GetFrameOffset() / audio->GetSampleRate(); ehs::UInt_64 duration = audio->GetFrameCount() / audio->GetSampleRate(); 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)); if (keyboard->IsJustReleased(ehs::Keyboard::Left)) source->SetVolume(source->GetVolume() - 0.1f); if (keyboard->IsJustReleased(ehs::Keyboard::Right)) source->SetVolume(source->GetVolume() + 0.1f); lwe::LabelGui* volume = (lwe::LabelGui*)stats->GetChild("Volume"); volume->SetText("Volume: " + ehs::Str_8::FromNum(source->GetVolume())); if (keyboard->IsJustReleased(ehs::Keyboard::P)) { if (source->IsPlaying()) source->Pause(); else source->Play(); } if (keyboard->IsJustReleased(ehs::Keyboard::Backspace)) source->Reset(); }