| 1 |
#include "AmbientBubbles.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../../Tiles/TileMap.h" |
| 4 |
|
| 5 |
#include "../../../nCine/Base/Random.h" |
| 6 |
|
| 7 |
namespace Jazz2::Actors::Environment |
| 8 |
{ |
| 9 |
AmbientBubbles::AmbientBubbles() |
| 10 |
: _cooldown(0.0f), _bubblesLeft(0), _delay(0.0f) |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
void AmbientBubbles::Preload(const ActorActivationDetails& details) |
| 15 |
{ |
| 16 |
PreloadMetadataAsync("Common/AmbientBubbles"_s); |
| 17 |
} |
| 18 |
|
| 19 |
Task<bool> AmbientBubbles::OnActivatedAsync(const ActorActivationDetails& details) |
| 20 |
{ |
| 21 |
_speed = details.Params[0]; |
| 22 |
|
| 23 |
SetState(ActorState::ForceDisableCollisions, true); |
| 24 |
SetState(ActorState::CanBeFrozen | ActorState::CollideWithTileset | ActorState::CollideWithOtherActors | ActorState::ApplyGravitation, false); |
| 25 |
|
| 26 |
async_await RequestMetadataAsync("Common/AmbientBubbles"_s); |
| 27 |
|
| 28 |
async_return true; |
| 29 |
} |
| 30 |
|
| 31 |
void AmbientBubbles::OnUpdate(float timeMult) |
| 32 |
{ |
| 33 |
_cooldown -= timeMult; |
| 34 |
if (_cooldown <= 0.0f) { |
| 35 |
SpawnBubbles(_bubblesLeft); |
| 36 |
_bubblesLeft = _speed; |
| 37 |
_cooldown = BaseTime; |
| 38 |
} else if (_bubblesLeft > 0) { |
| 39 |
_delay -= timeMult; |
| 40 |
if (_delay <= 0.0f) { |
| 41 |
SpawnBubbles(1); |
| 42 |
_bubblesLeft--; |
| 43 |
_delay = BaseTime / _speed; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
void AmbientBubbles::SpawnBubbles(std::int32_t count) |
| 49 |
{ |
| 50 |
if (count <= 0) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
auto tilemap = _levelHandler->TileMap(); |
| 55 |
if (tilemap != nullptr) { |
| 56 |
auto* res = _metadata->FindAnimation(AnimState::Default); // AmbientBubbles |
| 57 |
if (res != nullptr && res->Base->TextureDiffuse != nullptr) { |
| 58 |
Vector2i texSize = res->Base->TextureDiffuse->GetSize(); |
| 59 |
Vector2i size = res->Base->FrameDimensions; |
| 60 |
Vector2i frameConf = res->Base->FrameConfiguration; |
| 61 |
|
| 62 |
for (int i = 0; i < count; i++) { |
| 63 |
float scale = Random().NextFloat(0.3f, 1.0f); |
| 64 |
float speedX = Random().NextFloat(-0.5f, 0.5f) * scale; |
| 65 |
float speedY = Random().NextFloat(-3.0f, -2.0f) * scale; |
| 66 |
float accel = Random().NextFloat(-0.008f, -0.001f) * scale; |
| 67 |
int frame = res->FrameOffset + Random().Next(0, res->FrameCount); |
| 68 |
|
| 69 |
Tiles::TileMap::DestructibleDebris debris = { }; |
| 70 |
debris.Pos = _pos; |
| 71 |
debris.Depth = _renderer.layer(); |
| 72 |
debris.Size = Vector2f((float)size.X, (float)size.Y); |
| 73 |
debris.Speed = Vector2f(speedX, speedY); |
| 74 |
debris.Acceleration = Vector2f(0.0f, accel); |
| 75 |
|
| 76 |
debris.Scale = scale; |
| 77 |
debris.Alpha = 0.9f; |
| 78 |
debris.AlphaSpeed = -0.009f; |
| 79 |
|
| 80 |
debris.Time = 110.0f; |
| 81 |
|
| 82 |
debris.TexScaleX = (size.X / float(texSize.X)); |
| 83 |
debris.TexBiasX = ((float)(frame % frameConf.X) / frameConf.X); |
| 84 |
debris.TexScaleY = (size.Y / float(texSize.Y)); |
| 85 |
debris.TexBiasY = ((float)(frame / frameConf.X) / frameConf.Y); |
| 86 |
|
| 87 |
debris.DiffuseTexture = res->Base->TextureDiffuse.get(); |
| 88 |
|
| 89 |
tilemap->CreateDebris(debris); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |