File: PinballPaddle.cpp

package info (click to toggle)
jazz2-native 3.5.0-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (95 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "PinballPaddle.h"
#include "../../ILevelHandler.h"
#include "../Player.h"

namespace Jazz2::Actors::Solid
{
	PinballPaddle::PinballPaddle()
		: _cooldown(0.0f)
	{
	}

	void PinballPaddle::Preload(const ActorActivationDetails& details)
	{
		PreloadMetadataAsync("Object/PinballPaddle"_s);
	}

	Task<bool> PinballPaddle::OnActivatedAsync(const ActorActivationDetails& details)
	{
		bool facingLeft = (details.Params[0] != 0);

		SetState(ActorState::CollideWithTileset | ActorState::IsSolidObject | ActorState::ApplyGravitation, false);

		async_await RequestMetadataAsync("Object/PinballPaddle"_s);

		SetFacingLeft(facingLeft);
		SetAnimation(AnimState::Idle);

		async_return true;
	}

	void PinballPaddle::OnUpdate(float timeMult)
	{
		if (_frozenTimeLeft > 0.0f) {
			return;
		}

		if (_cooldown <= 0.0f) {
			_levelHandler->FindCollisionActorsByRadius(_pos.X, _pos.Y, 30.0f, [this](ActorBase* actor) {
				if (auto* player = runtime_cast<Player>(actor)) {
					Vector2f playerPos = player->GetPos();
					if (playerPos.Y > _pos.Y - 26.0f && playerPos.Y < _pos.Y + 10.0f) {
						_cooldown = 10.0f;

						SetTransition(AnimState::TransitionActivate, false);
						PlaySfx("Hit"_s, 0.6f, 0.4f);

						float mult = (playerPos.X - _pos.X) / _currentAnimation->Base->FrameDimensions.X;
						if (IsFacingLeft()) {
							mult = 1.0f - mult;
						}
						mult = std::clamp(0.2f + mult * 1.6f, 0.4f, 1.0f);

						float forceY = 1.9f * mult;
						if (!_levelHandler->IsReforged()) {
							forceY *= 0.85f;
						}

						player->_speed.X = 0.0f;
						player->_speed.Y = (_levelHandler->IsReforged() ? -1.0f : -0.7f);

						if (player->_activeModifier == Player::Modifier::None) {
							if (player->_copterFramesLeft > 1.0f) {
								player->_copterFramesLeft = 1.0f;
							}

							player->_externalForce.Y -= forceY;
							player->_externalForceCooldown = 10.0f;
							player->_controllable = true;
							player->SetState(ActorState::CanJump, false);
							player->EndDamagingMove();
						}

						// TODO: Check this
						player->AddScore(500);
					}
				}
				return true;
			});
		} else {
			_cooldown -= timeMult;
		}
	}

	void PinballPaddle::OnUpdateHitbox()
	{
		if (_currentAnimation != nullptr) {
			AABBInner = AABBf(
				_pos.X - _currentAnimation->Base->FrameDimensions.X * (IsFacingLeft() ? 0.7f : 0.3f),
				_pos.Y - _currentAnimation->Base->FrameDimensions.Y * 0.1f,
				_pos.X + _currentAnimation->Base->FrameDimensions.X * (IsFacingLeft() ? 0.3f : 0.7f),
				_pos.Y + _currentAnimation->Base->FrameDimensions.Y * 0.3f
			);
		}
	}
}