diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-10-14 07:39:38 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-10-14 07:39:38 -0400 |
commit | 9d590665caa9f282e4a7365b10e1ba8a85e384d9 (patch) | |
tree | 508ff160936cf9a2bed170f93e8392147715121b /themes | |
parent | f6fe40e125a99bd3ea47c476f6a95b6e820a51aa (diff) |
Leaves can now fall off of the tree
Diffstat (limited to 'themes')
-rw-r--r-- | themes/LeafParticleRender.cpp | 46 | ||||
-rw-r--r-- | themes/LeafParticleRender.h | 14 | ||||
-rwxr-xr-x | themes/dist/output.wasm | bin | 54718 -> 55929 bytes |
3 files changed, 57 insertions, 3 deletions
diff --git a/themes/LeafParticleRender.cpp b/themes/LeafParticleRender.cpp index df2a453..c006c06 100644 --- a/themes/LeafParticleRender.cpp +++ b/themes/LeafParticleRender.cpp @@ -3,11 +3,13 @@ #include "mathlib.h" #include "TreeShape.h" #include "types.h" +#include <math.h> const int32 verticesPerLeaf = 6; const float32 leafRadius = 3.f; +const int32 fallChanceMax = 100; -inline void createLeaf(Renderer2dVertex* vertices, Vector2 position, Vector4 color) { +inline void updateLeaf(Renderer2dVertex* vertices, Vector2 position, Vector4 color) { Vector2 bottomLeft = Vector2(-leafRadius, -leafRadius) + position; Vector2 bottomRight = Vector2(leafRadius, -leafRadius) + position; Vector2 topLeft = Vector2(-leafRadius, leafRadius) + position; @@ -34,6 +36,7 @@ void LeafParticleRender::load(Renderer2d *renderer, TreeShapeLoadResult* lr) { int32 randomBranch = randomIntBetween(0, lr->numBranches); int32 randomVertex = randomIntBetween(0, 6); // TODO: Manually entering num vertices per branch. updateData[leafIdx].vertexToFollow = &lr->updateData[randomBranch].vertices[randomVertex]; + updateData[leafIdx].fallChance = randomIntBetween(0, fallChanceMax); updateData[leafIdx].color = Vector4(randomFloatBetween(0.3, 0.9), randomFloatBetween(0.1, 0.6), 0, 1); updateData[leafIdx].vertexPtr = &vertices[leafIdx * verticesPerLeaf]; } @@ -58,10 +61,47 @@ void LeafParticleRender::load(Renderer2d *renderer, TreeShapeLoadResult* lr) { } void LeafParticleRender::update(float32 dtSeconds) { + elapsedTimeSeconds += dtSeconds; + + // Every time the fallIntervalSeconds passes, we remove one leaf + // from the tree and send it barrelling towards the earth. + int32 fallRoll; + bool didGenerateFall = false; + if (elapsedTimeSeconds >= fallIntervalSeconds) { + fallRoll = randomIntBetween(0, fallChanceMax); + didGenerateFall = true; + elapsedTimeSeconds = 0; + } + for (int32 leafIdx = 0; leafIdx < numLeaves; leafIdx++) { auto updateDataItem = &updateData[leafIdx]; - createLeaf(updateDataItem->vertexPtr, updateDataItem->vertexToFollow->position, updateDataItem->color); + if (didGenerateFall) { + if (!updateDataItem->isFalling && updateDataItem->fallChance == fallRoll) { + updateDataItem->isFalling = true; + updateDataItem->fallPosition = updateDataItem->vertexToFollow->position; + updateDataItem->fallVerticalVelocity = -randomFloatBetween(15.f, 25.f); + updateDataItem->fallHorizontalFrequency = randomFloatBetween(3.f, 5.f); + } + } + + if (updateDataItem->onGround) { + + } + else if (updateDataItem->isFalling) { + updateDataItem->timeFallingSeconds += dtSeconds; + const float32 xPosUpdate = cosf(updateDataItem->fallHorizontalFrequency * updateDataItem->timeFallingSeconds); + updateDataItem->fallPosition.x += xPosUpdate; + updateDataItem->fallPosition.y += updateDataItem->fallVerticalVelocity * dtSeconds; + if (updateDataItem->fallPosition.y <= 25.f) { // TODO: Hardcoded ground for now + updateDataItem->fallPosition.y = 25.f; + updateDataItem->onGround = true; + } + updateLeaf(updateDataItem->vertexPtr, updateDataItem->fallPosition, updateDataItem->color); + } + else { + updateLeaf(updateDataItem->vertexPtr, updateDataItem->vertexToFollow->position, updateDataItem->color); + } } } @@ -80,4 +120,6 @@ void LeafParticleRender::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); delete [] vertices; + + elapsedTimeSeconds = 0; }
\ No newline at end of file diff --git a/themes/LeafParticleRender.h b/themes/LeafParticleRender.h index c305363..0627322 100644 --- a/themes/LeafParticleRender.h +++ b/themes/LeafParticleRender.h @@ -15,11 +15,23 @@ struct LeafParticleUpdateData { Renderer2dVertex* vertexToFollow = NULL; Vector4 color = Vector4(1.f, 0.f, 0.f, 0.f); float32 scale = 1.f; - Vector2 fallDirection; + + float32 timeFallingSeconds = 0.f; + bool isFalling = false; + int32 fallChance = -1; + Vector2 fallPosition; + float32 fallVerticalVelocity; + float32 fallHorizontalFrequency; + + bool onGround = false; + Renderer2dVertex* vertexPtr = NULL; }; struct LeafParticleRender { + float32 elapsedTimeSeconds = 0.5; + float32 fallIntervalSeconds = 1.f; + // Update data int32 numLeaves = 0; diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm Binary files differindex 32331e3..d341f4f 100755 --- a/themes/dist/output.wasm +++ b/themes/dist/output.wasm |