Prep for walls, not done

This commit is contained in:
2023-03-16 20:24:43 -07:00
parent 646f8b057a
commit c6391f1111
8 changed files with 142 additions and 35 deletions

View File

@ -14,4 +14,5 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(game)
add_subdirectory(save)
add_subdirectory(save)
add_subdirectory(scene)

View File

@ -0,0 +1,7 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(components)

View 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
)

View 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);
}

View 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;
};
}

View 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);
}

View 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;
};
}

View File

@ -5,57 +5,36 @@
#pragma once
#include "scene/Scene.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "scene/components/ui/UILabel.hpp"
#include "scene/components/ui/UIImage.hpp"
#include "display/font/BitmapFont.hpp"
#include "scene/components/PlayerController.hpp"
#include "scene/components/GameCamera.hpp"
#include "scene/components/physics/3d/CubeCollider.hpp"
#include "scene/components/GameCamera.hpp"
namespace Dawn {
class HelloWorldScene : public Scene {
protected:
Camera *camera;
SimpleSpinningCubePrefab *cube;
UICanvas *canvas;
UILabel *label;
UIImage *image;
BitmapFont font;
void stage() override {
camera = Camera::create(this);
camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0));
auto gameCamera = camera->item->addComponent<GameCamera>();
cube = SimpleSpinningCubePrefab::create(this);
auto playerItem = this->createSceneItem();
auto player = playerItem->addComponent<PlayerController>();
auto hitbox = playerItem->addComponent<CubeCollider>();
canvas = UICanvas::create(this);
auto wall = this->createSceneItem();
auto wallHitbox = wall->addComponent<CubeCollider>();
wall->transform.setLocalPosition(glm::vec3(-10, 0, 0));
wallHitbox->min = -(wallHitbox->max = glm::vec3(1, 1, 5));
auto imageItem = this->createSceneItem();
image = imageItem->addComponent<UIImage>();
image->color = COLOR_BLACK;
imageItem->transform.setParent(canvas->transform);
auto labelItem = this->createSceneItem();
label = labelItem->addComponent<UILabel>();
labelItem->transform.setParent(canvas->transform);
auto assMan = &this->game->assetManager;
auto texture = assMan->get<TextureAsset>("testbitmap_texture");
auto tileset = assMan->get<TilesetAsset>("testbitmap_tileset");
this->font.texture = &texture->texture;
this->font.tileset = &tileset->tileset;
label->text = "Hello World, how are you today? I hope you are doing well. I really like the fact I can ramble in my text for once.";
label->font = &font;
label->maxWidth = 220;
image->alignment = glm::vec4(0, 0, label->getContentWidth(), label->getContentHeight());
gameCamera->player = player;
}
std::vector<Asset*> getRequiredAssets() override {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets;
vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
assets.push_back(assMan->get<TextureAsset>("testbitmap_texture"));
assets.push_back(assMan->get<TilesetAsset>("testbitmap_tileset"));
return assets;
}