From 423ad110c5bdc4bf4387f0baa6583c3d25f00f30 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Tue, 19 Jul 2022 17:16:57 +0700 Subject: [PATCH] refactor --- CMakeLists.txt | 4 +-- engine/utils/CameraController.cpp | 43 ------------------------------- engine/utils/CameraController.h | 26 ------------------- engine/utils/ObjectController.cpp | 41 +++++++++++++++++++++++++++++ engine/utils/ObjectController.h | 24 +++++++++++++++++ 5 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 engine/utils/CameraController.cpp delete mode 100644 engine/utils/CameraController.h create mode 100644 engine/utils/ObjectController.cpp create mode 100644 engine/utils/ObjectController.h diff --git a/CMakeLists.txt b/CMakeLists.txt index efda2fd..cb32e37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,8 +66,8 @@ add_executable(${CMAKE_PROJECT_NAME} engine/io/Mouse.h engine/io/SoundController.cpp engine/io/SoundController.h - engine/utils/CameraController.cpp - engine/utils/CameraController.h + engine/utils/ObjectController.cpp + engine/utils/ObjectController.h engine/animation/Animation.h engine/animation/Timeline.cpp engine/animation/Timeline.h diff --git a/engine/utils/CameraController.cpp b/engine/utils/CameraController.cpp deleted file mode 100644 index 116ef18..0000000 --- a/engine/utils/CameraController.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// -// Created by Иван Ильин on 22.01.2022. -// - -#include "CameraController.h" -#include "Time.h" -#include "../math/Vec2D.h" - -CameraController::CameraController(std::shared_ptr camera, - std::shared_ptr keyboard, - std::shared_ptr mouse) : - _camera(std::move(camera)), - _keyboard(std::move(keyboard)), - _mouse(std::move(mouse)){ -} - -void CameraController::update() { - // Left and right - if (Keyboard::isKeyPressed(sf::Keyboard::A)) - _camera->translate(_camera->left()*Time::deltaTime()*5.0); - - if (Keyboard::isKeyPressed(sf::Keyboard::D)) - _camera->translate(-_camera->left()*Time::deltaTime()*5.0); - - // Forward and backward - if (Keyboard::isKeyPressed(sf::Keyboard::W)) - _camera->translate(_camera->lookAt()*Time::deltaTime()*5.0); - - if (Keyboard::isKeyPressed(sf::Keyboard::S)) - _camera->translate(-_camera->lookAt()*Time::deltaTime()*5.0); - - if (Keyboard::isKeyPressed(sf::Keyboard::LShift)) - _camera->translate(Vec3D{0.0, -Time::deltaTime()*5.0, 0}); - - if (Keyboard::isKeyPressed(sf::Keyboard::Space)) - _camera->translate(Vec3D{0.0, Time::deltaTime()*5.0, 0}); - - // Mouse movement - Vec2D disp = _mouse->getMouseDisplacement(); - - _camera->rotate(Vec3D{0, -disp.x()/1000.0, 0}); - _camera->rotateLeft(disp.y()/1000.0); -} \ No newline at end of file diff --git a/engine/utils/CameraController.h b/engine/utils/CameraController.h deleted file mode 100644 index e0fbe8b..0000000 --- a/engine/utils/CameraController.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Иван Ильин on 22.01.2022. -// - -#ifndef SHOOTER_CAMERACONTROLLER_H -#define SHOOTER_CAMERACONTROLLER_H - -#include "../Camera.h" -#include "../io/Keyboard.h" -#include "../io/Mouse.h" - -class CameraController { -private: - std::shared_ptr _camera; - std::shared_ptr _keyboard; - std::shared_ptr _mouse; - -public: - CameraController(std::shared_ptr camera, - std::shared_ptr keyboard, - std::shared_ptr mouse); - - void update(); -}; - -#endif //SHOOTER_CAMERACONTROLLER_H diff --git a/engine/utils/ObjectController.cpp b/engine/utils/ObjectController.cpp new file mode 100644 index 0000000..d9ca9ac --- /dev/null +++ b/engine/utils/ObjectController.cpp @@ -0,0 +1,41 @@ +// +// Created by Иван Ильин on 22.01.2022. +// + +#include "ObjectController.h" +#include "Time.h" +#include "../math/Vec2D.h" + +ObjectController::ObjectController(std::shared_ptr object, + std::shared_ptr mouse) : + _object(std::move(object)), + _mouse(std::move(mouse)){ +} + +void ObjectController::update() { + // Left and right + if (Keyboard::isKeyPressed(sf::Keyboard::A)) + _object->translate(_object->left()*Time::deltaTime()*5.0); + + if (Keyboard::isKeyPressed(sf::Keyboard::D)) + _object->translate(-_object->left()*Time::deltaTime()*5.0); + + // Forward and backward + if (Keyboard::isKeyPressed(sf::Keyboard::W)) + _object->translate(_object->lookAt()*Time::deltaTime()*5.0); + + if (Keyboard::isKeyPressed(sf::Keyboard::S)) + _object->translate(-_object->lookAt()*Time::deltaTime()*5.0); + + if (Keyboard::isKeyPressed(sf::Keyboard::LShift)) + _object->translate(Vec3D{0.0, -Time::deltaTime()*5.0, 0}); + + if (Keyboard::isKeyPressed(sf::Keyboard::Space)) + _object->translate(Vec3D{0.0, Time::deltaTime()*5.0, 0}); + + // Mouse movement + Vec2D disp = _mouse->getMouseDisplacement(); + + _object->rotate(Vec3D{0, -disp.x()/1000.0, 0}); + _object->rotateLeft(disp.y()/1000.0); +} \ No newline at end of file diff --git a/engine/utils/ObjectController.h b/engine/utils/ObjectController.h new file mode 100644 index 0000000..6371bd6 --- /dev/null +++ b/engine/utils/ObjectController.h @@ -0,0 +1,24 @@ +// +// Created by Иван Ильин on 22.01.2022. +// + +#ifndef SHOOTER_OBJECTCONTROLLER_H +#define SHOOTER_OBJECTCONTROLLER_H + +#include "../Object.h" +#include "../io/Keyboard.h" +#include "../io/Mouse.h" + +class ObjectController { +private: + std::shared_ptr _object; + std::shared_ptr _mouse; + +public: + ObjectController(std::shared_ptr object, + std::shared_ptr mouse); + + void update(); +}; + +#endif //SHOOTER_OBJECTCONTROLLER_H