#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 "LWE/Timer.h" #include #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 lwe::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::FontAtlas* arial_24 = new lwe::FontAtlas("Resources/Fonts/Arial_24.ehf"); AddResource(arial_24); lwe::Model* vampire = new lwe::Model("Resources/Models/Vampire.ehm"); vampire->Calculate(); AddResource(vampire); AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Diffuse.png", lwe::IMG_ASPECT_COLOR)); AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Normal.png", lwe::IMG_ASPECT_COLOR)); AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Specular.png", lwe::IMG_ASPECT_COLOR)); lwe::Mesh* portrait = new lwe::Mesh("Portrait", lwe::portraitVerts, lwe::portraitIndices); //portrait->Calculate(); AddResource(portrait); lwe::Mesh* portraitGUI = new lwe::Mesh("PortraitGUI", lwe::portraitGuiVerts, lwe::portraitGuiIndices); AddResource(portraitGUI); AddResource(new lwe::Model("Resources/Models/Cube.ehm")); AddResource(new lwe::Model("Resources/Models/Sphere.ehm")); AddResource(new lwe::Model("Resources/Models/PointLight.ehm")); lwe::CubeMap* cm = new lwe::CubeMap("Resources/Textures/Skybox", "Skybox"); AddResource(cm); AddResource(lwe::Audio::FromFile_Heap("Resources/Audio/sample.wav", lwe::DataType::FLOAT)); } void TestLevel::Setup() { 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); } */ //AddSystem(new lwe::AudioSystem()); 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}); stats->AddResource(new lwe::FontAtlas("Resources/Fonts/Arial_24.ehf")); 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}); stats->AddChild(deltaTime); lwe::LabelGui* gbg = new lwe::LabelGui("Garbage", "Arial_24", "Garbage: 0"); gbg->SetColor({1.0f}); gbg->SetPosition({0.0f, 48.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}); stats->AddChild(playback); lwe::LabelGui* volume = new lwe::LabelGui("Volume", "Arial_24", "Volume: 0"); volume->SetColor({1.0f}); volume->SetPosition({0.0f, 96.0f}); stats->AddChild(volume); guiSys->AddGui(stats); lwe::Serializer args(lwe::Endianness::LE); args.Write(stats); args.SetOffset(0); timerSys->Add({0, 1.0f, args, [](lwe::Timer* timer, lwe::Serializer& args) { lwe::GameLoop* gl = (lwe::GameLoop*)timer->GetParent("GameLoop"); lwe::CollectionGui* stats = args.Read(); args.SetOffset(0); lwe::LabelGui* deltaTime = (lwe::LabelGui*)stats->GetChild("DeltaTime"); deltaTime->SetText("Delta Time: " + lwe::Str_8::FromNum(gl->GetRawDeltaTime())); lwe::LabelGui* gbg = (lwe::LabelGui*)stats->GetChild("Garbage"); gbg->SetText("Garbage: " + lwe::Str_8::FromNum(lwe::GarbageCollector::Size())); }}); lwe::PanelGui* testPanel = new lwe::PanelGui("TestPanel"); testPanel->SetPosition({300.0f, 200.0f}); testPanel->SetScale({400.0f, 400.0f}); guiSys->AddGui(testPanel); lwe::Entity* player = new lwe::Entity("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(player); lwe::Entity* plyPuppet = new lwe::Entity("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(plyPuppet); CreateEntity(player->GetHashId(), "Player"); lwe::Entity* skyboxEnt = new lwe::Entity("Skybox"); lwe::Skybox* skybox = new lwe::Skybox("Skybox", "Skybox"); skyboxEnt->AddComponent(skybox); AddEntity(skyboxEnt); lwe::Entity* ent = new lwe::Entity("Main"); ent->SetScale({0.25f, 0.25f, 0.25f}); ent->SetPos({0.0f, -1.0f, 5.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->EnablePanning(false); //as->EnableAttenuation(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 = (lwe::Entity*)a->GetParent(); lwe::Health* health = (lwe::Health*)aOwner->GetComponent("Health"); health->TakeDamage(10); lwe::Entity* bOwner = (lwe::Entity*)b->GetParent(); 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(ent); lwe::Entity* sun = new lwe::Entity("Sun"); sun->SetPos({-5.0f, 0.0f, 5.0f}); lwe::AmbientPointLight* sunLight = new lwe::AmbientPointLight("Light"); sunLight->SetDiffuse({1.0f, 0.77254901960784313725490196078431f, 0.56078431372549019607843137254902f}); sun->AddComponent(sunLight); AddEntity(sun); lwe::Entity* emergency = new lwe::Entity("Emergency"); emergency->SetPos({5.0f, 0.0f, 5.0f}); lwe::PointLight* pl = new lwe::PointLight("Light"); pl->SetColor({1.0f, 0.0f, 0.0f}); emergency->AddComponent(pl); AddEntity(emergency); lwe::Entity* testEnt = new lwe::Entity("Test"); lwe::Billboard* testBb = new lwe::Billboard("TestBB", "Character_Diffuse"); testEnt->AddComponent(testBb); AddEntity(testEnt); lwe::Entity* bullet = new lwe::Entity("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(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, lwe::Input* input, const float delta) { lwe::GameLoop* gl = (lwe::GameLoop*)GetParent("GameLoop"); if (!gl) return; const lwe::InputHandler* ih = win->GetInputHandler(); if (!ih) return; const lwe::Mouse* mouse = (lwe::Mouse*)ih->GetDeviceByType(LWE_HID_MOUSE); if (!mouse) return; const lwe::Keyboard* keyboard = (lwe::Keyboard*)ih->GetDeviceByType(LWE_HID_KEYBOARD); if (!keyboard) return; lwe::Entity* ent = GetEntity("Main"); if (!ent) return; ent->SetRot(ent->GetRot() + lwe::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(lwe::Mouse::LMB)) // Left Mouse Button { lwe::AudioSource* gunshot = new lwe::AudioSource("Gunshot_" + lwe::Str_8::FromNum(count), "Gunshot"); gunshot->SetVolume(1.0f); ply->AddComponent(gunshot); lwe::Entity* bullet = CreateEntity("Bullet", "Bullet_" + lwe::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::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: " + lwe::Str_8::FromNum(gl->GetTPS())); lwe::AudioSource* source = (lwe::AudioSource*)ent->GetComponent("AudioSource3D", "Song"); if (!source) return; lwe::Audio* audio = (lwe::Audio*)GetResource("Audio", source->GetAudioHashId()); if (!audio) return; lwe::LabelGui* playback = (lwe::LabelGui*)stats->GetChild("Playback"); lwe::UInt_64 elapsed = source->GetFrameOffset() / audio->GetSampleRate(); lwe::UInt_64 duration = audio->GetFrameCount() / audio->GetSampleRate(); playback->SetText("Playback: " + lwe::Str_8::FromNum(elapsed / 60) + ":" + lwe::Str_8::FromNum(elapsed % 60) + " / " + lwe::Str_8::FromNum(duration / 60) + ":" + lwe::Str_8::FromNum(duration % 60)); if (win->IsCursorConstrained() && !win->IsCursorVisible()) { if (keyboard->IsJustReleased(lwe::Keyboard::Left)) source->SetVolume(source->GetVolume() - 0.25f); if (keyboard->IsJustReleased(lwe::Keyboard::Right)) source->SetVolume(source->GetVolume() + 0.25f); lwe::LabelGui* volume = (lwe::LabelGui*)stats->GetChild("Volume"); volume->SetText("Volume: " + lwe::Str_8::FromNum(source->GetVolume())); if (keyboard->IsJustReleased(lwe::Keyboard::P)) { if (source->IsPlaying()) source->Pause(); else source->Play(); } if (keyboard->IsJustReleased(lwe::Keyboard::Backspace)) source->Reset(); } }