Testing some event stuff

This commit is contained in:
2023-11-17 22:40:08 -06:00
parent 0c46c328fa
commit 26b5ec2c7f
20 changed files with 260 additions and 30 deletions

View File

@@ -22,6 +22,22 @@ void Scene::stage() {
sceneInitializer(selfReference);
}
void Scene::update() {
if(!hasInitialized) {
hasInitialized = true;
for(auto &item : sceneItems) {
item->init();
}
}
float_t delta = getGame()->timeManager.delta;
if(paused) {
onPausedUpdate.emit(delta);
} else {
onUnpausedUpdate.emit(delta);
}
}
std::shared_ptr<Game> Scene::getGame() {
return game.lock();
}

View File

@@ -13,8 +13,13 @@ namespace Dawn {
std::weak_ptr<Game> game;
const std::function<void(Scene&)> sceneInitializer;
std::vector<std::shared_ptr<SceneItem>> sceneItems;
bool_t paused = false;
bool_t hasInitialized = false;
public:
Event<float_t> onUnpausedUpdate;
Event<float_t> onPausedUpdate;
/**
* Constructs a scene object.
*
@@ -30,6 +35,14 @@ namespace Dawn {
*/
void stage();
/**
* Called by the game every frame that the scene is set as the currently
* active scene. This may not be sequential, for example a scene can be
* active, then switched to a different scene temporarily, and then can be
* switched back to the active one.
*/
void update();
/**
* Returns a copy of the game instance that this scene belongs to.
*

View File

@@ -23,6 +23,13 @@ std::shared_ptr<Scene> SceneItem::getScene() {
return scene.lock();
}
void SceneItem::init() {
auto sharedThis = shared_from_this();
for(auto &component : components) {
component->init(sharedThis);
}
}
SceneItem::~SceneItem() {
std::for_each(
components.begin(),

View File

@@ -29,6 +29,12 @@ namespace Dawn {
*/
SceneItem(const std::weak_ptr<Scene> scene);
/**
* Called when the scene item is supposed to initialize. Should happen
* on the first frame that the scene item is staged.
*/
void init();
/**
* Returns the scene that this scene item belongs to.
*

View File

@@ -43,9 +43,6 @@ namespace Dawn {
this->components.push_back(
static_pointer_cast<SceneComponent>(component)
);
// Init compoonent and return
component->init(sceneItemComponentsSelf());
return component;
}

View File

@@ -64,6 +64,29 @@ void SceneItemTransform::updateWorldTransformFromParent() {
this->updateChildrenWorldTransforms();
}
void SceneItemTransform::updateLocalTransformFromLocalValues() {
this->transformLocal = glm::translate(
glm::mat4(1.0f),
this->localPosition
) * glm::mat4_cast(this->localRotation) * glm::scale(
glm::mat4(1.0f),
this->localScale
);
this->updateWorldTransformFromLocalTransform();
}
void SceneItemTransform::updateWorldTransformFromLocalTransform() {
auto parent = this->getParent();
if(!parent) {
this->transformWorld = this->transformLocal;
} else {
this->transformWorld = (
parent->transformWorld * this->transformLocal
);
}
this->updateChildrenWorldTransforms();
}
std::shared_ptr<SceneItem> SceneItemTransform::getParent() {
return parent.lock();
}
@@ -88,12 +111,39 @@ glm::mat4 SceneItemTransform::getWorldTransform() {
return this->transformWorld;
}
glm::vec3 SceneItemTransform::getLocalPosition() {
return this->localPosition;
}
glm::vec3 SceneItemTransform::getLocalScale() {
return this->localScale;
}
glm::quat SceneItemTransform::getLocalRotation() {
return this->localRotation;
}
void SceneItemTransform::setWorldTransform(const glm::mat4 transform) {
this->transformWorld = transform;
this->updateLocalTransformFromWorldTransform();
this->updateChildrenWorldTransforms();
}
void SceneItemTransform::setLocalPosition(const glm::vec3 position) {
this->localPosition = position;
this->updateLocalTransformFromLocalValues();
}
void SceneItemTransform::setLocalScale(const glm::vec3 scale) {
this->localScale = scale;
this->updateLocalTransformFromLocalValues();
}
void SceneItemTransform::setLocalRotation(const glm::quat rotation) {
this->localRotation = rotation;
this->updateLocalTransformFromLocalValues();
}
bool_t SceneItemTransform::isChildOf(std::shared_ptr<SceneItem> item) {
assertNotNull(item, "Cannot check if child of null item.");
auto parent = this->getParent();

View File

@@ -45,6 +45,16 @@ namespace Dawn {
*/
void updateWorldTransformFromParent();
/**
* Updates the local transform of this item from the local values.
*/
void updateLocalTransformFromLocalValues();
/**
* Updates the world transform from this items local transform.
*/
void updateWorldTransformFromLocalTransform();
public:
SceneItemTransform();
@@ -75,6 +85,27 @@ namespace Dawn {
* @return World transform of this item.
*/
glm::mat4 getWorldTransform();
/**
* Gets the local position of this item (relative to its parent).
*
* @return Local position of this item.
*/
glm::vec3 getLocalPosition();
/**
* Local scale of this item (relative to its parent).
*
* @return Local scale of this item.
*/
glm::vec3 getLocalScale();
/**
* Local rotation of this item (relative to its parent).
*
* @return Local rotation of this item.
*/
glm::quat getLocalRotation();
/**
* Sets the transform of this item within world space (relative to scene
@@ -84,6 +115,27 @@ namespace Dawn {
*/
void setWorldTransform(const glm::mat4 transform);
/**
* Sets the local position of this item (relative to its parent).
*
* @param position Local position of this item.
*/
void setLocalPosition(const glm::vec3 position);
/**
* Sets the local scale of this item (relative to its parent).
*
* @param scale Local scale of this item.
*/
void setLocalScale(const glm::vec3 scale);
/**
* Sets the local rotation of this item (relative to its parent).
*
* @param rotation Local rotation of this item.
*/
void setLocalRotation(const glm::quat rotation);
/**
* Returns whether or not this item is a child of the given item. This
* will traverse up the entire heirarchy to confirm.