Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed car behaviour #39

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions TrafficTactician/CarComponent.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions TrafficTactician/CarComponent.h

This file was deleted.

28 changes: 16 additions & 12 deletions TrafficTactician/ControllerComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "ControllerComponent.h"
#include "GameObject.h"
#include "RouteComponent.h"
#include "CameraInputHandler.h"
#include <iostream>
#include <memory>
Expand All @@ -20,28 +19,33 @@ bool ControllerComponent::checkPose() const
return cameraInputHandler::getInputPose() == correctPose;
}

void ControllerComponent::timerCallback() const
void ControllerComponent::changeCarState(RouteComponent::RouteState state) const
{
// If pose was wrong, points --.
if (!checkPose())
{
scene->data.points--;
SoundHandler::getInstance().playSoundSnippet("sounds/points_minus.wav");
return;
}

// If pose was right, points ++.
if (gameObject->getComponent<RouteComponent>().has_value())
{
gameObject->getComponent<RouteComponent>().value()->crossed = true;
gameObject->getComponent<RouteComponent>().value()->state = RouteComponent::RouteState::Moving;
gameObject->getComponent<RouteComponent>().value()->state = state;
}
else
{
LOG(ERROR) << "Error: Can not update UI when no RouteComponent can be found." << std::endl;
throw std::exception("Error: Can not update UI when no RouteComponent can be found.");
}
}

void ControllerComponent::timerCallback() const
{
// If pose was wrong, points --.
if (!checkPose())
{
scene->data.points--;
SoundHandler::getInstance().playSoundSnippet("sounds/points_minus.wav");
return;
}


// If pose was wrong, points ++.
changeCarState(RouteComponent::RouteState::Moving);
timer->toggleTimer(false);
scene->data.points++;
SoundHandler::getInstance().playSoundSnippet("sounds/points_plus.wav");
Expand Down
3 changes: 3 additions & 0 deletions TrafficTactician/ControllerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "CameraInputHandler.h"
#include "Scene.h"
#include "Timer.h"
#include "RouteComponent.h"


class ControllerComponent : public Component
{
Expand All @@ -17,6 +19,7 @@ class ControllerComponent : public Component
~ControllerComponent() = default;

bool checkPose() const;
void changeCarState(RouteComponent::RouteState state) const;
void timerCallback() const;

virtual void update(float deltaTime) override;
Expand Down
16 changes: 9 additions & 7 deletions TrafficTactician/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,30 @@ void Scene::initRouteCache()
glm::vec3(8, 0.000000, 0.600000)
};

// TODO: Make sure the car drives on the other lane!
routeCache[POSE_MOVE_LEFT] = {
glm::vec3(-0.5, 0.000000, -6.864396),
glm::vec3(-0.5, 0.000000, -1.55),
glm::vec3(-0.662630, 0.000000, 0.600000),
glm::vec3(-8, 0.000000, 0.600000)
glm::vec3(-0.662630, 0.000000, -0.600000),
glm::vec3(-8, 0.000000, -0.600000)
};

routeCache[POSE_MOVE_FORWARD] = {
glm::vec3(-0.5, 0.000000, -6.864396),
glm::vec3(-0.5, 0.000000, -1.55),
glm::vec3(-0.5, 0.000000, 0.600000),
glm::vec3(-0.5, 0.000000, 6.000000)
};

// TODO: Add route and support for POSE_STOP
routeCache[POSE_STOP] = {
glm::vec3(-0.5, 0.000000, -6.864396),
glm::vec3(-0.5, 0.000000, -1.55),
glm::vec3(-0.5, 0.000000, 6.0),
};
}

// Creates car with random pose.
std::shared_ptr<GameObject> Scene::createCar()
{
return createCar(static_cast<Pose>(rand() % (POSE_OTHER - 1))); // POSE_OTHER is the last enum value, however it is not one you should require the user to do. This is important!
return createCar(static_cast<Pose>(rand() % (POSE_OTHER))); // POSE_OTHER is the last enum value, however it is not one you should require the user to do. This is important!
}

// Create a car object with the given pose.
Expand All @@ -103,6 +105,7 @@ std::shared_ptr<GameObject> Scene::createCar(Pose pose)
carObject->addComponent(std::make_shared<RouteComponent>(speed, route));
carObject->addComponent(std::make_shared<ControllerComponent>(pose, this));

SoundHandler::getInstance().playSoundSnippet("sounds/car/Car_Acceleration_2.wav");
return carObject;
}

Expand Down Expand Up @@ -151,7 +154,6 @@ void Scene::update(float deltaTime)
LOG(INFO) << "Spawned new car in scene." << std::endl;

updateVisualCueTexture();
SoundHandler::getInstance().playSoundSnippet("sounds/car/Car_Acceleration_2.wav");
} else
{
const std::shared_ptr<GameObject> carGameObject = currentCarObject.lock();
Expand Down
2 changes: 1 addition & 1 deletion TrafficTactician/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Timer
float rolloverTime = 0.0f;
float soundPassTime = 0.0f;

Timer(const std::function<void()>& callback = nullptr, float rolloverTime = 5.0f);
Timer(const std::function<void()>& callback = nullptr, float rolloverTime = 3.0f);
~Timer() = default;

void setCallback(const std::function<void()>& cb);
Expand Down
17 changes: 2 additions & 15 deletions TrafficTactician/TrafficTactician.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ double lastFrameTime = 0;
std::array clearColor = { 0.3f, 0.4f, 0.6f, 1.0f };

int runApp() {
srand(5); // Set seed for rand() calls.
// TODO: create a hash function for the random number generator to make a reproducible test
srand((unsigned int)time(NULL)); // Set seed to for rand() calls.

if (GetGraphicSettings().mxaaEnabled) glfwWindowHint(GLFW_SAMPLES, 4); // Multisample anti-aliasing.

Expand Down Expand Up @@ -174,20 +175,6 @@ void updateImGui() {
const std::shared_ptr<GameObject> car = sim->scene->currentCarObject.lock();
ImGui::Text("CarPosition: %f, %f, %f", car->position.x, car->position.y, car->position.z);
ImGui::SliderAngle("CarRotation:", &car->rotation.y);
bool continueRoute = false;
if (ImGui::Checkbox("Continue route", &continueRoute)) {

if (car->getComponent<RouteComponent>().has_value())
{
car->getComponent<RouteComponent>().value()->state = RouteComponent::RouteState::Moving;
car->getComponent<RouteComponent>().value()->crossed = true;
}
else
{
LOG(ERROR) << "Error: Can not update UI when no RouteComponent can be found." << std::endl;
throw std::exception("Error: Can not update UI when no RouteComponent can be found.");
}
}

if (car->getComponent<ControllerComponent>().has_value()) {
const auto correctPose = car->getComponent<ControllerComponent>().value()->correctPose;
Expand Down
3 changes: 1 addition & 2 deletions TrafficTactician/TrafficTactician.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ $(ProjectDir)json2hpp.exe "dnnsettings" "settings/dnnSettings.json" "dnnSettings
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CarComponent.cpp" />
<ClCompile Include="components\GameObject.cpp" />
<ClCompile Include="ControllerComponent.cpp" />
<ClCompile Include="easylogging++.cpp" />
Expand Down Expand Up @@ -307,10 +306,10 @@ $(ProjectDir)json2hpp.exe "dnnsettings" "settings/dnnSettings.json" "dnnSettings
<ClCompile Include="WorldComponent.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CarComponent.h" />
<ClInclude Include="components\Component.h" />
<ClInclude Include="components\DrawComponent.h" />
<ClInclude Include="components\GameObject.h" />
<ClInclude Include="ControllerComponent.h" />
<ClInclude Include="dnnMathSettings.h" />
<ClInclude Include="dnnSettings.h" />
<ClInclude Include="easylogging++.h" />
Expand Down
2 changes: 1 addition & 1 deletion TrafficTactician/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Size=200,200
Collapsed=0

[Window][Status]
Pos=0,0
Size=1600,100
Collapsed=0

Expand All @@ -33,7 +34,6 @@ Size=142,48
Collapsed=0

[Window][MainMenu]
Pos=0,0
Size=600,168
Collapsed=0

Expand Down