Prep for walls, not done
This commit is contained in:
11
src/dawnrose/scene/components/CMakeLists.txt
Normal file
11
src/dawnrose/scene/components/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
GameCamera.cpp
|
||||
PlayerController.cpp
|
||||
)
|
29
src/dawnrose/scene/components/GameCamera.cpp
Normal file
29
src/dawnrose/scene/components/GameCamera.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "GameCamera.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
GameCamera::GameCamera(SceneItem *i) : SceneItemComponent(i) {}
|
||||
|
||||
std::vector<SceneItemComponent*> GameCamera::getDependencies() {
|
||||
return {
|
||||
(this->camera = item->getComponent<Camera>())
|
||||
};
|
||||
}
|
||||
|
||||
void GameCamera::onStart() {
|
||||
assertNotNull(this->camera);
|
||||
assertNotNull(this->player);
|
||||
|
||||
useEvent([&](float_t delta) {
|
||||
glm::vec3 cameraTarget = player->transform->getLocalPosition();
|
||||
camera->transform->lookAt(
|
||||
cameraTarget + glm::vec3(0, 10.0f, 1.5f),
|
||||
cameraTarget
|
||||
);
|
||||
}, getScene()->eventSceneUpdate);
|
||||
}
|
22
src/dawnrose/scene/components/GameCamera.hpp
Normal file
22
src/dawnrose/scene/components/GameCamera.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/components/display/Camera.hpp"
|
||||
#include "scene/components/PlayerController.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class GameCamera : public SceneItemComponent {
|
||||
protected:
|
||||
Camera *camera = nullptr;
|
||||
|
||||
public:
|
||||
PlayerController *player = nullptr;
|
||||
|
||||
GameCamera(SceneItem *item);
|
||||
std::vector<SceneItemComponent*> getDependencies() override;
|
||||
void onStart() override;
|
||||
};
|
||||
}
|
38
src/dawnrose/scene/components/PlayerController.cpp
Normal file
38
src/dawnrose/scene/components/PlayerController.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "PlayerController.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
PlayerController::PlayerController(SceneItem *item) : SceneItemComponent(item) {
|
||||
|
||||
}
|
||||
|
||||
void PlayerController::onStart() {
|
||||
useEvent([&](float_t delta){
|
||||
auto camera = getScene()->findComponent<Camera>();
|
||||
assertNotNull(camera);
|
||||
|
||||
// Friction
|
||||
velocity -= velocity * friction * delta;
|
||||
|
||||
// Movement
|
||||
auto inputMove = getGame()->inputManager.getAxis2D(
|
||||
INPUT_BIND_NEGATIVE_X, INPUT_BIND_POSITIVE_X,
|
||||
INPUT_BIND_NEGATIVE_Y, INPUT_BIND_POSITIVE_Y
|
||||
) * delta * moveSpeed;
|
||||
|
||||
if(inputMove.x != 0 || inputMove.y != 0) {
|
||||
float_t angle = atan2(inputMove.y, inputMove.x);
|
||||
glm::vec3 movement = glm::vec3(cos(angle), 0, sin(angle));
|
||||
velocity += movement * delta * moveSpeed;
|
||||
}
|
||||
|
||||
// Move / Update
|
||||
transform->setLocalPosition(transform->getLocalPosition() + (velocity * delta));
|
||||
}, getScene()->eventSceneUpdate);
|
||||
}
|
20
src/dawnrose/scene/components/PlayerController.hpp
Normal file
20
src/dawnrose/scene/components/PlayerController.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneItemComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class PlayerController : public SceneItemComponent {
|
||||
public:
|
||||
glm::vec3 velocity;
|
||||
float_t moveSpeed = 40.0f;
|
||||
float_t friction = 12.0f;
|
||||
|
||||
PlayerController(SceneItem *item);
|
||||
|
||||
void onStart() override;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user