This commit is contained in:
2023-01-06 21:01:06 -08:00
parent d13984dddc
commit d6b7895cab
21 changed files with 190 additions and 190 deletions

View File

@ -8,26 +8,28 @@
#include "scene/Scene.hpp"
namespace Dawn {
template<class T>
template<class T, typename J, class P = T>
class Prefab {
public:
/**
* Returns the list of assets required for this prefab.
*
* @param man Asset Manasger for getting required assets from.
* @return List of required assets this prefab.
*/
static std::vector<Asset*> getRequiredAssets() {
return T::prefabAssets();
static std::vector<Asset*> getRequiredAssets(AssetManager *man) {
return P::prefabAssets(man);
}
/**
* Creates a new instance of this asset.
* Creates a new instance of this prefabricated asset.
*
* @param scene Scene this item belongs to.
* @param context Custom context that this prefab needs to initialize.
* @return The instance of the created prefab.
*/
static T * create(Scene *scene, UICanvas *canvas) {
return T::prefabCreate(scene, canvas);
static T * create(J *context) {
assertNotNull(context);
return P::prefabCreate(context);
}
};
}

View File

@ -11,17 +11,27 @@ namespace Dawn {
template<class T>
class SceneItemPrefab :
public SceneItem,
public Prefab<T>
public Prefab<T, Scene>
{
protected:
public:
static T * prefabCreate(Scene *scene, UICanvas *canvas) {
/**
* Creates an instance of this prefab for the given scene.
*
* @param scene Scene that this prefab is going to be added to.
* @return The created prefab instance.
*/
static T * prefabCreate(Scene *scene) {
T *item = scene->createSceneItemOfType<T>();
item->prefabInit();
item->prefabInit(&scene->game->assetManager);
return item;
}
/**
* Constructor for this SceneItemPrefab.
*
* @param scene Scene that this prefab belongs to.
* @param id ID of this scene item.
*/
SceneItemPrefab(Scene *scene, sceneitemid_t id) :
SceneItem(scene, id)
{
@ -32,6 +42,6 @@ namespace Dawn {
* Virtual method called by the subclass for initialization after the
* prefab has been created.
*/
virtual void prefabInit() = 0;
virtual void prefabInit(AssetManager *man) = 0;
};
}

View File

@ -5,19 +5,23 @@
#pragma once
#include "Prefab.hpp"
#include "ui/UIComponent.hpp"
namespace Dawn {
template<class T>
template<class T, class P>
class UIPrefab :
public Prefab<T>
public Prefab<T, UICanvas, P>
{
public:
static T * prefabCreate(Scene *scene) {
}
static void apply(T *item) {
assertNotNull(item);
P::prefabApply(&item->getGame()->assetManager , item);
}
static T * prefabCreate(UICanvas *canvas) {
T * item = canvas->addElement<T>();
P::apply(item);
return item;
}
};
}

View File

@ -1,39 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefab/SceneItemPrefab.hpp"
#include "scene/components/Components.hpp"
#include "display/mesh/CubeMesh.hpp"
namespace Dawn {
class SimpleCubePrefab : public SceneItemPrefab<SimpleCubePrefab> {
public:
MeshRenderer *renderer = nullptr;
MeshHost *host = nullptr;
Material *material = nullptr;
static std::vector<Asset*> prefabAssets() {
return std::vector<Asset*>();
}
SimpleCubePrefab(Scene *scene, sceneitemid_t id) :
SceneItemPrefab(scene, id)
{
}
void prefabInit() override {
this->renderer = this->addComponent<MeshRenderer>();
this->host = this->addComponent<MeshHost>();
this->material = this->addComponent<Material>();
this->host->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(&this->host->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);
std::cout << "Preab Init" << std::endl;
}
};
}

View File

@ -10,7 +10,7 @@
using namespace Dawn;
UICanvas * UICanvas::createCanvas(Scene *scene) {
UICanvas * UICanvas::create(Scene *scene) {
auto item = scene->createSceneItem();
return item->addComponent<UICanvas>();
}

View File

@ -32,7 +32,7 @@ namespace Dawn {
* @param scene Scene to create the UI Canvas for.
* @return Created UI Canvas.
*/
static UICanvas * createCanvas(Scene *scene);
static UICanvas * create(Scene *scene);
//
std::vector<UIComponent*> children;

View File

@ -19,4 +19,15 @@ namespace Dawn {
++it;
}
}
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> append) {
assertNotNull(list);
auto it = append.begin();
while(it != append.end()) {
list->push_back(*it);
++it;
}
}
}

View File

@ -22,7 +22,6 @@ void VisualNovelManager::onStart() {
this->fader = this->uiCanvas->findElement<VisualNovelFader>();
assertNotNull(this->textBox);
assertNotNull(this->fader);
this->getScene()->eventSceneUnpausedUpdate.addListener(this, &VisualNovelManager::onUnpausedUpdate);

View File

@ -9,7 +9,7 @@ using namespace Dawn;
SimpleVisualNovelBackground * SimpleVisualNovelBackground::create(Scene *s) {
auto item = s->createSceneItem();
item->addComponent<MeshRenderer>();
// item->addComponent<MeshRenderer>();
item->addComponent<MeshHost>();
item->addComponent<Material>();
auto background = item->addComponent<SimpleVisualNovelBackground>();

View File

@ -14,23 +14,26 @@ SimpleVNScene::SimpleVNScene(DawnGame *game) : Scene(game) {
std::vector<Asset*> SimpleVNScene::getRequiredAssets() {
auto assMan = &this->game->assetManager;
// return PokerGameTextbox::getAssets(assMan);
std::vector<Asset*> assets;
vectorAppend(&assets, VisualNovelTextboxPrefab::getRequiredAssets(assMan));
return std::vector<Asset*>();
}
void SimpleVNScene::stage() {
auto assMan = &this->game->assetManager;
// Camera
this->camera = Camera::create(this);
this->camera->transform->lookAtPixelPerfect(
glm::vec3(0, 0, 0),
glm::vec3(0, 0, 0),
1.0f,
this->camera->getRenderTarget()->getHeight(),
camera->fov
);
// UI
this->canvas = UICanvas::createCanvas(this);
// this->textbox = Prefab::create<VisualNovelTextboxPrefab>(this)->getComponent<VisualNovelTextbox>();
this->canvas = UICanvas::create(this);
this->textbox = VisualNovelTextboxPrefab::create(this->canvas);
this->background = SimpleVisualNovelBackground::create(this);
// VN Manager

View File

@ -13,6 +13,7 @@
#include "visualnovel/events/VisualNovelPauseEvent.hpp"
#include "visualnovel/events/VisualNovelFadeEvent.hpp"
#include "visualnovel/events/VisualNovelChangeSimpleBackgroundEvent.hpp"
#include "prefabs/ui/VisualNovelTextboxPrefab.hpp"
namespace Dawn {
class SimpleVNScene : public Scene {

View File

@ -17,7 +17,6 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(game)
add_subdirectory(prefabs)
add_subdirectory(ui)
add_subdirectory(visualnovel)
add_subdirectory(save)

View File

@ -5,7 +5,7 @@
#include "DawnGame.hpp"
#include "scenes/Scene_1_1.hpp"
#include "scenes/PrefabTestingScene.hpp"
#include "scenes/TestScene.hpp"
using namespace Dawn;
@ -23,7 +23,7 @@ int32_t DawnGame::init() {
this->localeManager.init();
this->renderManager.init();
this->scene = new PrefabTestingScene(this);
this->scene = new TestScene(this);
return DAWN_GAME_INIT_RESULT_SUCCESS;
}

View File

@ -1,10 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
# target_sources(${DAWN_TARGET_NAME}
# PRIVATE
# VNPlayer.cpp
# )

View File

@ -1,52 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "asset/AssetManager.hpp"
#include "poker/PokerPlayer.hpp"
#include "scene/components/Components.hpp"
#include "visualnovel/components/VisualNovelCharacter.hpp"
#include "display/animation/TiledSpriteAnimation.hpp"
namespace Dawn {
class VNPenny {
public:
static std::vector<Asset*> getAssets(AssetManager *assMan) {
return std::vector<Asset*>{
assMan->get<TextureAsset>("texture_penny"),
assMan->get<TilesetAsset>("tileset_penny")
};
}
static SceneItem * create(Scene *scene) {
auto item = scene->createSceneItem();
auto textureAsset = scene->game->assetManager.get<TextureAsset>("texture_penny");
auto tilesetAsset = scene->game->assetManager.get<TilesetAsset>("tileset_penny");
auto meshRenderer = item->addComponent<MeshRenderer>();
auto material = item->addComponent<Material>();
auto meshHost = item->addComponent<MeshHost>();
auto tiledSprite = item->addComponent<TiledSprite>();
auto animation = item->addComponent<AnimationController>();
auto pokerPlayer = item->addComponent<PokerPlayer>();
auto vnCharacter = item->addComponent<VisualNovelCharacter>();
vnCharacter->nameKey = "character.penny.name";
auto param = material->getShader()->getParameterByName("u_Text");
material->textureValues[param] = &textureAsset->texture;
tiledSprite->setTilesetAndSize(&tilesetAsset->tileset);
auto anim = new TiledSpriteAnimation(tiledSprite);
anim->addSequentialKeyframes(0.1f, 0, 22);
anim->loop = true;
animation->animation = anim;
return item;
}
};
}

View File

@ -0,0 +1,53 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefab/SceneItemPrefab.hpp"
#include "asset/AssetManager.hpp"
#include "poker/PokerPlayer.hpp"
#include "scene/components/Components.hpp"
#include "visualnovel/components/VisualNovelCharacter.hpp"
#include "display/animation/TiledSpriteAnimation.hpp"
namespace Dawn {
class PennyPrefab : public SceneItemPrefab<PennyPrefab> {
public:
static std::vector<Asset*> prefabAssets(AssetManager *assMan) {
return std::vector<Asset*>{
assMan->get<TextureAsset>("texture_penny"),
assMan->get<TilesetAsset>("tileset_penny")
};
}
PennyPrefab(Scene *scene, sceneitemid_t id) : SceneItemPrefab(scene, id){}
void prefabInit(AssetManager *man) override {
auto textureAsset = man->get<TextureAsset>("texture_penny");
auto tilesetAsset = man->get<TilesetAsset>("tileset_penny");
auto meshRenderer = this->addComponent<MeshRenderer>();
auto material = this->addComponent<Material>();
auto meshHost = this->addComponent<MeshHost>();
auto tiledSprite = this->addComponent<TiledSprite>();
auto animation = this->addComponent<AnimationController>();
auto pokerPlayer = this->addComponent<PokerPlayer>();
auto vnCharacter = this->addComponent<VisualNovelCharacter>();
vnCharacter->nameKey = "character.penny.name";
auto param = material->getShader()->getParameterByName("u_Text");
material->textureValues[param] = &textureAsset->texture;
tiledSprite->setTilesetAndSize(&tilesetAsset->tileset);
tiledSprite->setTile(0);
this->transform.setLocalPosition(glm::vec3(0, 0, 0));
// auto anim = new TiledSpriteAnimation(tiledSprite);
// anim->addSequentialKeyframes(0.1f, 0, 22);
// anim->loop = true;
// animation->animation = anim;
}
};
}

View File

@ -0,0 +1,25 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefab/UIPrefab.hpp"
#include "ui/UIBorder.hpp"
namespace Dawn {
class UIBorderPrefab : public UIPrefab<UIBorder, UIBorderPrefab> {
public:
static std::vector<Asset*> prefabAssets(AssetManager *man) {
std::vector<Asset*> assets;
assets.push_back(man->get<TextureAsset>("texture_test"));
return assets;
}
static void prefabApply(AssetManager *man, UIBorder *border) {
auto text = man->get<TextureAsset>("texture_test");
border->texture = &text->texture;
border->setBorderSize(glm::vec2(16, 16));
}
};
}

View File

@ -0,0 +1,34 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefabs/ui/UIBorderPrefab.hpp"
#include "visualnovel/ui/VisualNovelTextbox.hpp"
namespace Dawn {
class VisualNovelTextboxPrefab :
public UIPrefab<VisualNovelTextbox, VisualNovelTextboxPrefab>
{
public:
static std::vector<Asset*> prefabAssets(AssetManager *man) {
std::vector<Asset*> assets;
assets.push_back(man->get<TrueTypeAsset>("truetype_ark"));
return assets;
}
static void prefabApply(AssetManager *man, VisualNovelTextbox *textbox) {
auto assetFont = man->get<TrueTypeAsset>("truetype_ark");
UIBorderPrefab::apply(&textbox->border);
textbox->setFont(&assetFont->font);
textbox->setFontSize(40);
textbox->setLabelPadding(glm::vec2(10, 8));
textbox->setTransform(
UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_END,
glm::vec4(0, 238, 0, 0),
0.0f
);
}
};
}

View File

@ -14,7 +14,7 @@ PokerVNScene::PokerVNScene(DawnGame *game) : SimpleVNScene(game) {
std::vector<Asset*> PokerVNScene::getRequiredAssets() {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets, l;
vectorAppend(&assets, &(l = SimpleVNScene::getRequiredAssets()));
vectorAppend(&assets,SimpleVNScene::getRequiredAssets());
vectorAppend(&assets, &(l = PokerPlayerDisplay::getAssets(assMan)));
return assets;
}
@ -29,9 +29,9 @@ void PokerVNScene::vnStage() {
int32_t i = 0;
while(it != this->pokerPlayers.end()) {
auto player = *it;
auto uiPlayer = canvas->addElement<PokerPlayerDisplay>();
uiPlayer->setTransform(UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START, glm::vec4(i * 220, 0, 220, 200), 0);
uiPlayer->setPlayer(player);
// auto uiPlayer = canvas->addElement<PokerPlayerDisplay>();
// uiPlayer->setTransform(UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START, glm::vec4(i * 220, 0, 220, 200), 0);
// uiPlayer->setPlayer(player);
++it;
++i;
}

View File

@ -1,32 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
#include "prefabs/display/SimpleCubePrefab.hpp"
namespace Dawn {
class PrefabTestingScene : public Scene {
protected:
SimpleCubePrefab *cube = nullptr;
Camera *camera = nullptr;
public:
PrefabTestingScene(DawnGame *game) : Scene(game) {
}
std::vector<Asset*> getRequiredAssets() override {
return std::vector<Asset*>();
}
void stage() override {
camera = Camera::create(this);
camera->transform->lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0));
cube = SimpleCubePrefab::create(this);
}
};
}

View File

@ -5,21 +5,21 @@
#pragma once
#include "PokerVNScene.hpp"
#include "prefabs/VNPenny.hpp"
#include "prefabs/characters/PennyPrefab.hpp"
namespace Dawn {
class TestScene : public PokerVNScene {
protected:
SceneItem *penny;
SceneItem *julie;
SceneItem *sammy;
SceneItem *lucy;
PennyPrefab *penny;
PennyPrefab *julie;
PennyPrefab *sammy;
PennyPrefab *lucy;
void vnStage() override {
penny = VNPenny::create(this);
julie = VNPenny::create(this);
sammy = VNPenny::create(this);
lucy = VNPenny::create(this);
penny = PennyPrefab::create(this);
julie = PennyPrefab::create(this);
sammy = PennyPrefab::create(this);
lucy = PennyPrefab::create(this);
PokerVNScene::vnStage();
}
@ -39,27 +39,19 @@ namespace Dawn {
auto start = new VisualNovelChangeSimpleBackgroundEvent(
vnManager, &texture->texture
);
start
->then(new VisualNovelTextboxEvent(vnManager, penny->getComponent<VisualNovelCharacter>(), "scene.1.1"))
// ->then(new PokerNewGameEvent(vnManager))
->then(new VisualNovelTextboxEvent(vnManager, penny->getComponent<VisualNovelCharacter>(), "undefined"))
// ->then(new PokerInitialEvent(vnManager))
;
start->then(new VisualNovelTextboxEvent(vnManager, penny->getComponent<VisualNovelCharacter>(), "scene.1.1"));
return start;
}
public:
TestScene(DawnGame *game) : PokerVNScene(game) {
}
TestScene(DawnGame *game) : PokerVNScene(game) {}
std::vector<Asset*> getRequiredAssets() override {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets;
vectorAppend(&assets, &PokerVNScene::getRequiredAssets());
vectorAppend(&assets, &VNPenny::getAssets(assMan));
vectorAppend(&assets, PokerVNScene::getRequiredAssets());
vectorAppend(&assets, PennyPrefab::getRequiredAssets(assMan));
assets.push_back(assMan->get<TextureAsset>("texture_tavern_night"));
return assets;
}