Skip to content

Commit

Permalink
Can switch into world!
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyNeubauer committed Dec 17, 2019
1 parent ca3580d commit 0cd797e
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 22 deletions.
2 changes: 1 addition & 1 deletion GameDesign/src/GameDesign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ GameDesign::GameDesign()

Parts::Init();
Ships::Init();
PushOverlay(new Hazel::DebugLayer());
//PushOverlay(new Hazel::DebugLayer());

PushLayer(new EditorLayer());
sound = Hazel::AudioManager::Load("assets/audio/computers_have_control.mp3");
Expand Down
31 changes: 24 additions & 7 deletions GameDesign/src/layers/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "GameDesign.h"
#include "ship/Parts.h"

#include "WorldLayer.h"

#include <imgui.h>
#include <Hazel.h>

Expand Down Expand Up @@ -96,25 +98,25 @@ bool EditorLayer::OnMouseMoved(Hazel::MouseMovedEvent* event)
{
partCmp += glm::rotate(glm::vec2(0.0f, partHitbox.y / 2.0f), partRot);
heldCmp.y -= heldHitbox.y / 2.0f;
if (!part->Def->Connections[TOP_INDEX] || !m_HeldPart->Def->Connections[BOTTOM_INDEX]) continue;
if (!part->Connections[TOP_INDEX] || !part->Def->Connections[TOP_INDEX] || !m_HeldPart->Def->Connections[BOTTOM_INDEX]) continue;
}
else if (i == RIGHT_INDEX)
{
partCmp += glm::rotate(glm::vec2(partHitbox.x / 2.0f, 0.0f), partRot);
heldCmp.x -= heldHitbox.x / 2.0f;
if (!part->Def->Connections[RIGHT_INDEX] || !m_HeldPart->Def->Connections[LEFT_INDEX]) continue;
if (!part->Connections[RIGHT_INDEX] || !part->Def->Connections[RIGHT_INDEX] || !m_HeldPart->Def->Connections[LEFT_INDEX]) continue;
}
else if (i == BOTTOM_INDEX)
{
partCmp -= glm::rotate(glm::vec2(0.0f, partHitbox.y / 2.0f), partRot);
heldCmp.y += heldHitbox.y / 2.0f;
if (!part->Def->Connections[BOTTOM_INDEX] || !m_HeldPart->Def->Connections[TOP_INDEX]) continue;
if (!part->Connections[BOTTOM_INDEX] || !part->Def->Connections[BOTTOM_INDEX] || !m_HeldPart->Def->Connections[TOP_INDEX]) continue;
}
else if (i == LEFT_INDEX)
{
partCmp -= glm::rotate(glm::vec2(partHitbox.x / 2.0f, 0.0f), partRot);
heldCmp.x += heldHitbox.x / 2.0f;
if (!part->Def->Connections[LEFT_INDEX] || !m_HeldPart->Def->Connections[RIGHT_INDEX]) continue;
if (!part->Connections[LEFT_INDEX] || !part->Def->Connections[LEFT_INDEX] || !m_HeldPart->Def->Connections[RIGHT_INDEX]) continue;
}
float distance = glm::length(heldCmp - partCmp);
if (bestSide == -1 || distance < bestDistance)
Expand Down Expand Up @@ -211,6 +213,7 @@ bool EditorLayer::OnMousePressed(Hazel::MouseButtonPressedEvent* event)
m_GohstPlace->m_Offset = m_GohstPlace->m_Offset - m_GohstParent->m_Offset;
m_GohstPlace->ParentPart = m_GohstParent;
m_ActiveShip->GetParts().push_back(m_GohstPlace);
m_GohstParent->Connections[m_GohstSide] = false;

//Reset state
m_GohstPlace = nullptr;
Expand All @@ -233,15 +236,16 @@ bool EditorLayer::OnMousePressed(Hazel::MouseButtonPressedEvent* event)

bool EditorLayer::OnMouseReleased(Hazel::MouseButtonReleasedEvent* event)
{
if (m_HeldPart)
if (m_HeldPart && m_GohstPlace)
{
//Attempt to place the part on the body
m_ActiveShip->GetParts().emplace_back(m_GohstPlace);
m_GohstPlace->ParentPart = m_GohstParent;
m_GohstPlace->m_Offset = m_GohstPlace->m_Offset - m_GohstParent->m_Offset;

m_GohstParent->Connections.
m_GohstParent->Connections[m_GohstSide] = false;

m_GohstSide = -1;
m_GohstPlace = nullptr;
m_GohstParent = nullptr;
}
Expand All @@ -256,10 +260,23 @@ void EditorLayer::OnImGuiRender()
if (m_HoveredShop)
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Test");
ImGui::TextUnformatted(m_HoveredShop->Name);
ImGui::EndTooltip();
}

ImGui::Begin("Ship stats");
if (ImGui::Button("Launch!"))
{
Hazel::LayerStack& layerStack = Hazel::Application::Get().GetLayerStack();
layerStack.PushLayer(new WorldLayer(m_ActiveShip));
layerStack.PopLayer(this);
}
static char name[128];
if (ImGui::InputTextWithHint("Ship name", "Saturn V", name, sizeof(name)))
{
m_ActiveShip->GetName() = name;
}
ImGui::End();
}

void EditorLayer::Render()
Expand Down
2 changes: 1 addition & 1 deletion GameDesign/src/layers/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class EditorLayer : public Hazel::Layer
Hazel::Ref<EditorPart> m_HoveredReal;
Hazel::Ref<EditorPart> m_HeldPart;
Hazel::Ref<EditorPart> m_GohstPlace, m_GohstParent;
int gohstSize;
int m_GohstSide;


};
94 changes: 93 additions & 1 deletion GameDesign/src/layers/WorldLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "WorldLayer.h"

#include "ship/Part.h"
#include "ship/Parts.h"

#include <imgui.h>
#include <random>

Expand Down Expand Up @@ -55,7 +58,96 @@ static bool SliderDouble(const char* label, double* v, double v_min, double v_ma

void WorldLayer::OnImGuiRender()
{
ImGui::Begin("Ship");
ImGui::Text("pos {%.2f, %.2f} m", m_Ship->GetCenterOfMass().x, m_Ship->GetCenterOfMass().y);
ImGui::Text("rotation %.2f degrees", glm::degrees(m_Ship->GetRotation()));
ImGui::Text("mass %.2f kg", m_Ship->GetPhsicsBody()->GetMass());
ImGui::Text("speed %.3f m/s", m_Ship->GetPhsicsBody()->GetLinearVelocity().Length());
ImGui::Text("speed %.2f degrees/s", glm::degrees(m_Ship->GetPhsicsBody()->GetAngularVelocity()));

if (ImGui::Button("Break"))
{
int distance = -1;
std::vector<Hazel::Ref<Part>> partsToStage;
for (auto& part : m_Ship->GetParts())
{
int currentDistance = 0;
Part* partPtr = part.get();
while (!partPtr->IsRoot())
{
currentDistance++;
partPtr->Advance(partPtr);
}
//Root part
if (currentDistance == 0) continue;
//Reset for new maxes
if (distance == -1 || currentDistance > distance)
{
partsToStage.clear();
distance = currentDistance;
}

//Add all parts that match the best distance found so far
if (distance == currentDistance)
{
partsToStage.push_back(part);
}
}

for (Hazel::Ref<Part> part : partsToStage)
{
Ship* newShip = m_Ship->Split(*m_World, part.get());
if (newShip) m_World->AddShip(newShip);
}
}

if (ImGui::Button("Stage"))
{
int distance = -1;
std::vector<Hazel::Ref<Part>> partsToStage;
for (auto& part : m_Ship->GetParts())
{
int currentDistance = 0;
Part* partPtr = part.get();
if (!dynamic_cast<DecouplerPart*>(partPtr)) continue;

while (!partPtr->IsRoot())
{
currentDistance++;
partPtr->Advance(partPtr);
}
//Root part
if (currentDistance == 0) continue;
//Reset for new maxes
if (distance == -1 || currentDistance > distance)
{
partsToStage.clear();
distance = currentDistance;
}

//Add all parts that match the best distance found so far
if (distance == currentDistance)
{
partsToStage.push_back(part);
}
}

for (Hazel::Ref<Part> part : partsToStage)
{
DecouplerPart* dec = dynamic_cast<DecouplerPart*>(part.get());
Ship* newShip = dec->Release(*m_World, *m_Ship);
if (newShip) m_World->AddShip(newShip);
}
}

if (ImGui::SliderFloat("Throttle", &m_Ship->GetThrottle(), 0.0f, 1.0f))
{
Ship* ship = m_Ship.get();
m_Ship->ForEachPartIfType<EnginePart>([ship](EnginePart& part) {
part.SetThrottle(ship->GetThrottle());
});
}
ImGui::End();
}


Expand All @@ -74,7 +166,7 @@ void WorldCameraController::Update(Hazel::Timestep ts, Hazel::Camera2D& camera)
HZ_PROFILE_FUNCTION();


camera.m_ZoomVel -= Hazel::Input::GetScrollDelta() * 3.0f;
camera.m_ZoomVel -= Hazel::Input::GetScrollDelta() * 2.0f;

camera.m_Zoom += camera.m_ZoomVel * ts.Seconds();
camera.m_ZoomVel *= (1.0f - ts.Seconds() * 5.0f);
Expand Down
5 changes: 3 additions & 2 deletions GameDesign/src/ship/Ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class EditorShip

void Render(const Hazel::Camera& camera);

std::vector<Hazel::Ref<EditorPart>>& GetParts() { return m_Parts; }
inline std::vector<Hazel::Ref<EditorPart>>& GetParts() { return m_Parts; }
inline std::string& GetName() { return m_Name; }

Hazel::Ref<EditorPart>& GetRoot();

private:
std::vector<Hazel::Ref<EditorPart>> m_Parts;
std::string name = "Un named ship";
std::string m_Name = "";

};

Expand Down
1 change: 1 addition & 0 deletions Hazel/src/Hazel/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace Hazel {

while (m_Running)
{
m_LayerStack.Update();
HZ_PROFILE_SCOPE("Application::Run()");
Engine::Update();
Timestep ts = Engine::GetDeltaTime();
Expand Down
1 change: 1 addition & 0 deletions Hazel/src/Hazel/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Hazel {
void PushOverlay(Layer* layer);
void OnEvent(Event* e);
inline Window& GetWindow() { return *m_Window; }
inline LayerStack& GetLayerStack() { return m_LayerStack; }

inline static Application& Get() { return *s_Instance; }

Expand Down
25 changes: 19 additions & 6 deletions Hazel/src/Hazel/Layer/LayerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,44 @@ namespace Hazel {
}
}

void LayerStack::PushLayer(Layer* layer)
void LayerStack::Update()
{
for (Layer* layer : m_ToAddLayer)
PushLayerImpl(layer);
for (Layer* layer : m_ToAddOverlay)
PushOverlayImpl(layer);
for (Layer* layer : m_ToRemoveLayer)
PopLayerImpl(layer);
for (Layer* layer : m_ToRemoveOverlay)
PopOverlayImpl(layer);
m_ToAddLayer.clear(); m_ToAddOverlay.clear(); m_ToRemoveLayer.clear(); m_ToRemoveOverlay.clear();
}

void LayerStack::PushLayerImpl(Layer* layer)
{
m_Layers.emplace(m_Layers.begin() + m_LayerInsertIndex, layer);
layer->OnAttach();
m_LayerInsertIndex++;
}

void LayerStack::PushOverlay(Layer* overlay)
void LayerStack::PushOverlayImpl(Layer* overlay)
{
m_Layers.emplace_back(overlay);
overlay->OnAttach();
}

void LayerStack::PopLayer(Layer* layer)
void LayerStack::PopLayerImpl(Layer* layer)
{
auto it = std::find(m_Layers.begin(), m_Layers.end(), layer);
auto it = std::find(m_Layers.begin(), m_Layers.begin() + m_LayerInsertIndex, layer);
if (it != m_Layers.end())
{
layer->OnDetach();
m_Layers.erase(it);
m_LayerInsertIndex--;
layer->OnDetach();
}
}

void LayerStack::PopOverlay(Layer* overlay)
void LayerStack::PopOverlayImpl(Layer* overlay)
{
auto it = std::find(m_Layers.begin(), m_Layers.end(), overlay);
if (it != m_Layers.end())
Expand Down
22 changes: 18 additions & 4 deletions Hazel/src/Hazel/Layer/LayerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@ namespace Hazel {
LayerStack();
~LayerStack();

void PushLayer(Layer* layer);
void PushOverlay(Layer* overlay);
void PopLayer(Layer* layer);
void PopOverlay(Layer* overlay);
void Update();

inline void PushLayer(Layer* layer) { m_ToAddLayer.emplace_back(layer); }
inline void PushOverlay(Layer* overlay) { m_ToAddOverlay.emplace_back(overlay); }
inline void PopLayer(Layer* layer) { m_ToRemoveLayer.emplace_back(layer); }
inline void PopOverlay(Layer* overlay) { m_ToRemoveOverlay.emplace_back(overlay); }

std::vector<Layer*>::iterator begin() { return m_Layers.begin(); }
std::vector<Layer*>::iterator end() { return m_Layers.end(); }

private:
void PushLayerImpl(Layer* layer);
void PushOverlayImpl(Layer* overlay);
void PopLayerImpl(Layer* layer);
void PopOverlayImpl(Layer* overlay);


private:
std::vector<Layer*> m_Layers;
unsigned int m_LayerInsertIndex = 0;

std::vector<Layer*> m_ToAddLayer, m_ToAddOverlay;
std::vector<Layer*> m_ToRemoveLayer, m_ToRemoveOverlay;

};

}

0 comments on commit 0cd797e

Please sign in to comment.