diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-10-23 09:50:55 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-10-23 09:50:55 -0400 |
commit | ac9c187740797c5f8105b052bc057d274edc4be1 (patch) | |
tree | aa03267c14044ff2a4ea25d72954f409b7d9b637 /themes | |
parent | 5ee0e7a8dbd647b79073beee88e82870e181b363 (diff) |
(mkosarek) Better reemerging
Diffstat (limited to 'themes')
-rw-r--r-- | themes/LeafParticleRender.cpp | 74 | ||||
-rw-r--r-- | themes/LeafParticleRender.h | 14 | ||||
-rw-r--r-- | themes/TreeShape.cpp | 5 | ||||
-rw-r--r-- | themes/TreeShape.h | 1 | ||||
-rwxr-xr-x | themes/dist/output.wasm | bin | 57898 -> 58861 bytes |
5 files changed, 63 insertions, 31 deletions
diff --git a/themes/LeafParticleRender.cpp b/themes/LeafParticleRender.cpp index fd35888..2fc89ac 100644 --- a/themes/LeafParticleRender.cpp +++ b/themes/LeafParticleRender.cpp @@ -9,11 +9,12 @@ const int32 verticesPerLeaf = 6; const float32 leafRadius = 3.f; const int32 fallChanceMax = 100; -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; - Vector2 topRight = Vector2(leafRadius, leafRadius) + position; +inline void updateLeaf(Renderer2dVertex* vertices, Vector2 position, Vector4 color, float32 scale) { + float32 radius = scale * leafRadius; + Vector2 bottomLeft = Vector2(-radius, -radius) + position; + Vector2 bottomRight = Vector2(radius, -radius) + position; + Vector2 topLeft = Vector2(-radius, radius) + position; + Vector2 topRight = Vector2(radius, radius) + position; vertices[0] = { bottomLeft, color }; vertices[1] = { bottomRight, color }; @@ -25,7 +26,7 @@ inline void updateLeaf(Renderer2dVertex* vertices, Vector2 position, Vector4 col void LeafParticleRender::load(Renderer2d *renderer, TreeShapeLoadResult* lr) { LeafParticleLoadData ld; - ld.numLeaves = 640; + ld.numLeaves = 256; numLeaves = ld.numLeaves; numVertices = ld.numLeaves * verticesPerLeaf; @@ -39,6 +40,7 @@ void LeafParticleRender::load(Renderer2d *renderer, TreeShapeLoadResult* lr) { 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]; + updateData[leafIdx].resetTime = randomFloatBetween(4.f, 6.f); } useShader(renderer->shader); @@ -77,42 +79,64 @@ void LeafParticleRender::update(float32 dtSeconds) { auto updateDataItem = &updateData[leafIdx]; if (didGenerateFall) { - if (!updateDataItem->isFalling && updateDataItem->fallChance == fallRoll) { - updateDataItem->isFalling = true; + if (updateDataItem->state == LeafParticleState::OnTree && updateDataItem->fallChance == fallRoll) { + updateDataItem->state = LeafParticleState::Falling; updateDataItem->fallPosition = updateDataItem->vertexToFollow->position; updateDataItem->fallVerticalVelocity = -randomFloatBetween(15.f, 25.f); updateDataItem->fallHorizontalFrequency = randomFloatBetween(3.f, 5.f); } } - if (updateDataItem->onGround) { - updateDataItem->timeFallingSeconds += dtSeconds; + switch (updateDataItem->state) { + case (LeafParticleState::Remerging): { + updateDataItem->timeElapsedSeconds += dtSeconds; - if (updateDataItem->timeFallingSeconds >= updateDataItem->resetTime) { - updateDataItem->onGround = false; - updateDataItem->isFalling = false; + if (updateDataItem->timeElapsedSeconds >= updateDataItem->resetTime) { + updateDataItem->timeElapsedSeconds = 0.f; + updateDataItem->state = LeafParticleState::OnTree; updateDataItem->color.w = 1.f; + updateDataItem->scale = 1.f; } else { - updateDataItem->color.w = 1.f - (updateDataItem->timeFallingSeconds / updateDataItem->resetTime); - updateLeaf(updateDataItem->vertexPtr, updateDataItem->fallPosition, updateDataItem->color); + updateDataItem->color.w = (updateDataItem->timeElapsedSeconds / updateDataItem->resetTime); + updateDataItem->scale = (updateDataItem->timeElapsedSeconds / updateDataItem->resetTime); } + + updateLeaf(updateDataItem->vertexPtr, updateDataItem->vertexToFollow->position, updateDataItem->color, updateDataItem->scale); + break; } - else if (updateDataItem->isFalling) { - updateDataItem->timeFallingSeconds += dtSeconds; - const float32 xPosUpdate = cosf(updateDataItem->fallHorizontalFrequency * updateDataItem->timeFallingSeconds); + case (LeafParticleState::OnGround): { + updateDataItem->timeElapsedSeconds += dtSeconds; + + if (updateDataItem->timeElapsedSeconds >= updateDataItem->resetTime) { + updateDataItem->timeElapsedSeconds = 0.f; + updateDataItem->color.w = 0.f; + updateDataItem->state = LeafParticleState::Remerging; + } + else { + updateDataItem->color.w = 1.f - (updateDataItem->timeElapsedSeconds / updateDataItem->resetTime); + updateLeaf(updateDataItem->vertexPtr, updateDataItem->fallPosition, updateDataItem->color, updateDataItem->scale); + } + break; + } + case (LeafParticleState::Falling): { + updateDataItem->timeElapsedSeconds += dtSeconds; + const float32 xPosUpdate = cosf(updateDataItem->fallHorizontalFrequency * updateDataItem->timeElapsedSeconds); 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; - updateDataItem->timeFallingSeconds = 0; + if (updateDataItem->fallPosition.y <= 50.f) { // TODO: Hardcoded ground for now + updateDataItem->fallPosition.y = 50.f; + updateDataItem->state = LeafParticleState::OnGround; + updateDataItem->timeElapsedSeconds = 0; updateDataItem->resetTime = randomFloatBetween(2.f, 5.f); // TODO: Hardcoded reset interval } - updateLeaf(updateDataItem->vertexPtr, updateDataItem->fallPosition, updateDataItem->color); + updateLeaf(updateDataItem->vertexPtr, updateDataItem->fallPosition, updateDataItem->color, updateDataItem->scale); + break; + } + case (LeafParticleState::OnTree): { + updateLeaf(updateDataItem->vertexPtr, updateDataItem->vertexToFollow->position, updateDataItem->color, updateDataItem->scale); + break; } - else { - updateLeaf(updateDataItem->vertexPtr, updateDataItem->vertexToFollow->position, updateDataItem->color); } } } diff --git a/themes/LeafParticleRender.h b/themes/LeafParticleRender.h index 9f5d5f4..28c9bc1 100644 --- a/themes/LeafParticleRender.h +++ b/themes/LeafParticleRender.h @@ -10,20 +10,26 @@ struct LeafParticleLoadData { int numLeaves = 48; }; +enum LeafParticleState { + OnTree, + Falling, + OnGround, + Remerging +}; + struct LeafParticleUpdateData { - bool canFall = false; + LeafParticleState state = LeafParticleState::Remerging; + Renderer2dVertex* vertexToFollow = NULL; Vector4 color = Vector4(1.f, 0.f, 0.f, 0.f); float32 scale = 1.f; - float32 timeFallingSeconds = 0.f; - bool isFalling = false; + float32 timeElapsedSeconds = 0.f; int32 fallChance = -1; Vector2 fallPosition; float32 fallVerticalVelocity; float32 fallHorizontalFrequency; - bool onGround = false; float32 resetTime = 0.f; Renderer2dVertex* vertexPtr = NULL; diff --git a/themes/TreeShape.cpp b/themes/TreeShape.cpp index 49f6a99..797ceca 100644 --- a/themes/TreeShape.cpp +++ b/themes/TreeShape.cpp @@ -95,6 +95,7 @@ void TreeShape::createBranch(TreeLoadData* ld, TreeBranchLoadData* generationDat TreeBranchUpdateData* branchUpdateData = &updateData[*branchIndex]; branchUpdateData->tier = branchLevel; + branchUpdateData->periodOffset = randomFloatBetween(0.f, 2.f * PI); branchUpdateData->period = randomFloatBetween(3.f, 5.f); branchUpdateData->amplitude = randomFloatBetween(0.01f, 0.05f); branchUpdateData->branchToFollow = parent; @@ -155,8 +156,8 @@ void TreeShape::update(float32 dtSeconds) { int32 startParentIndex = bIdx * 6; - branchUpdataData->currentOffset.x = branchUpdataData->amplitude * sinf(branchUpdataData->period * timeElapsedSeconds); - branchUpdataData->currentOffset.y = branchUpdataData->amplitude * sinf(branchUpdataData->period * timeElapsedSeconds); + branchUpdataData->currentOffset.x = branchUpdataData->amplitude * cosf(branchUpdataData->periodOffset + branchUpdataData->period * timeElapsedSeconds); + branchUpdataData->currentOffset.y = branchUpdataData->amplitude * sinf(branchUpdataData->periodOffset + branchUpdataData->period * timeElapsedSeconds); if (branchUpdataData->branchToFollow != NULL) { branchUpdataData->currentOffset += branchUpdataData->branchToFollow->currentOffset; diff --git a/themes/TreeShape.h b/themes/TreeShape.h index 987295a..490b848 100644 --- a/themes/TreeShape.h +++ b/themes/TreeShape.h @@ -32,6 +32,7 @@ struct TreeBranchLoadData { struct TreeBranchUpdateData { int32 tier = 0; + float32 periodOffset = 0; float32 period = 0; float32 amplitude = 0; Vector2 currentOffset; diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm Binary files differindex 8319928..dff138e 100755 --- a/themes/dist/output.wasm +++ b/themes/dist/output.wasm |