| 1 |
#include "Moth.h" |
| 2 |
#include "../../ILevelHandler.h" |
| 3 |
#include "../../Events/EventMap.h" |
| 4 |
#include "../Player.h" |
| 5 |
|
| 6 |
#include "../../../nCine/Base/Random.h" |
| 7 |
|
| 8 |
namespace Jazz2::Actors::Environment |
| 9 |
{ |
| 10 |
Moth::Moth() |
| 11 |
: _timer(0.0f), _direction(0) |
| 12 |
{ |
| 13 |
} |
| 14 |
|
| 15 |
Task<bool> Moth::OnActivatedAsync(const ActorActivationDetails& details) |
| 16 |
{ |
| 17 |
SetState(ActorState::CanBeFrozen, false); |
| 18 |
_renderer.setLayer(_renderer.layer() + 20); |
| 19 |
|
| 20 |
uint8_t theme = details.Params[0]; |
| 21 |
|
| 22 |
async_await RequestMetadataAsync("Object/Moth"_s); |
| 23 |
SetAnimation((AnimState)theme); |
| 24 |
|
| 25 |
_renderer.AnimPaused = true; |
| 26 |
|
| 27 |
async_return true; |
| 28 |
} |
| 29 |
|
| 30 |
void Moth::OnUpdate(float timeMult) |
| 31 |
{ |
| 32 |
ActorBase::OnUpdate(timeMult); |
| 33 |
|
| 34 |
if (_timer > 0.0f) { |
| 35 |
if (GetState(ActorState::CanJump)) { |
| 36 |
_timer = 0.0f; |
| 37 |
} else { |
| 38 |
_timer -= timeMult; |
| 39 |
|
| 40 |
_externalForce.X = lerpByTime(_externalForce.X, sinf((100.0f - _timer) / 6.0f) * 4.0f * _direction, 0.6f, timeMult); |
| 41 |
_externalForce.Y = lerpByTime(_externalForce.Y, -0.00005f * _timer * _timer, 0.6f, timeMult); |
| 42 |
|
| 43 |
SetFacingLeft(_speed.X < 0.0f); |
| 44 |
} |
| 45 |
} else if (GetState(ActorState::CanJump)) { |
| 46 |
_speed.X = 0.0f; |
| 47 |
_externalForce.Y = 0.0f; |
| 48 |
_externalForce.X = 0.0f; |
| 49 |
|
| 50 |
_renderer.AnimTime = 0.0f; |
| 51 |
_renderer.AnimPaused = true; |
| 52 |
} else if (GetState(ActorState::ApplyGravitation)) { |
| 53 |
_externalForce.Y = _levelHandler->GetGravity() * -0.8f; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
bool Moth::OnHandleCollision(std::shared_ptr<ActorBase> other) |
| 58 |
{ |
| 59 |
if (auto* player = runtime_cast<Player>(other.get())) { |
| 60 |
if (_timer <= 50.0f) { |
| 61 |
_timer = 100.0f - _timer * 0.2f; |
| 62 |
|
| 63 |
SetState(ActorState::CanJump, false); |
| 64 |
|
| 65 |
_direction = (Random().NextBool() ? -1 : 1); |
| 66 |
_speed.X = Random().NextFloat(-1.4f, 0.0f) * _direction; |
| 67 |
_speed.Y = Random().NextFloat(-0.4f, 0.0f); |
| 68 |
|
| 69 |
_renderer.AnimPaused = false; |
| 70 |
} |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
void Moth::Preload(const ActorActivationDetails& details) |
| 78 |
{ |
| 79 |
PreloadMetadataAsync("Object/Moth"_s); |
| 80 |
} |
| 81 |
} |