File: PinballBumper.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 (120 lines) | stat: -rw-r--r-- 3,398 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "PinballBumper.h"
#include "../../ILevelHandler.h"
#include "../Player.h"

namespace Jazz2::Actors::Solid
{
	PinballBumper::PinballBumper()
		: _cooldown(0.0f), _lightIntensity(0.0f), _lightBrightness(0.0f)
	{
	}

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

	Task<bool> PinballBumper::OnActivatedAsync(const ActorActivationDetails& details)
	{
		std::uint8_t theme = details.Params[0];

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

		async_await RequestMetadataAsync("Object/PinballBumper"_s);

		SetAnimation((AnimState)theme);

		async_return true;
	}

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

		if (_cooldown <= 0.0f) {
			_levelHandler->FindCollisionActorsByRadius(_pos.X, _pos.Y, 16.0f, [this, timeMult](ActorBase* actor) {
				if (auto* player = runtime_cast<Player>(actor)) {
					_cooldown = 16.0f;

					SetTransition(_currentAnimation->State | (AnimState)0x200, true);
					PlaySfx("Hit"_s, 0.8f);

					constexpr float forceMult = 12.0f;
					Vector2f force = (player->GetPos() - _pos).Normalize() * forceMult;
					if (!_levelHandler->IsReforged()) {
						force.Y *= 0.9f;
					}

					// Move the player back
					player->MoveInstantly(-player->_speed * timeMult, MoveType::Relative);

					// Reset speed if the force acts on the other side
					if (force.X < 0.0f) {
						if (player->_speed.X > 0.0f) player->_speed.X = 0.0f;
						if (player->_externalForce.X > 0.0f) player->_externalForce.X = 0.0f;
					} else if (force.X > 0.0f) {
						if (player->_speed.X < 0.0f) player->_speed.X = 0.0f;
						if (player->_externalForce.X < 0.0f) player->_externalForce.X = 0.0f;
					}
					if (force.Y < 0.0f) {
						if (player->_speed.Y > 0.0f) player->_speed.Y = 0.0f;
						if (player->_externalForce.Y > 0.0f) player->_externalForce.Y = 0.0f;
					} else if (force.Y > 0.0f) {
						if (player->_speed.Y < 0.0f) player->_speed.Y = 0.0f;
						if (player->_externalForce.Y < 0.0f) player->_externalForce.Y = 0.0f;
					}

					player->_speed.X += force.X * 0.4f;
					player->_speed.Y += force.Y * 0.4f;

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

						player->_externalForce.X += force.X * 0.04f;
						player->_externalForce.Y += force.Y * 0.04f;
						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;
		}

		if (_lightIntensity > 0.0f) {
			_lightIntensity -= timeMult * 0.01f;
			if (_lightIntensity < 0.0f) {
				_lightIntensity = 0.0f;
			}
		}
		if (_lightBrightness > 0.0f) {
			_lightBrightness -= timeMult * 0.02f;

			if (_lightIntensity < 0.0f) {
				_lightIntensity = 0.0f;
			}
		}
	}

	void PinballBumper::OnEmitLights(SmallVectorImpl<LightEmitter>& lights)
	{
		if (_lightIntensity > 0.0f) {
			auto& light = lights.emplace_back();
			light.Pos = _pos;
			light.Intensity = _lightIntensity;
			light.Brightness = _lightBrightness;
			light.RadiusNear = 24.0f;
			light.RadiusFar = 60.0f;
		}
	}
}