Debian Codegraph @

Sources/Jazz2/Actors/Multiplayer/RemotablePlayer.cpp

raw
1 #include "RemotablePlayer.h"
2
3 #if defined(WITH_MULTIPLAYER)
4
5 #include "../../Multiplayer/MpLevelHandler.h"
6
7 #include "../../../nCine/Base/Clock.h"
8
9 namespace Jazz2::Actors::Multiplayer
10 {
11 RemotablePlayer::RemotablePlayer(std::shared_ptr<PeerDescriptor> peerDesc)
12 : ChangingWeaponFromServer(false), RespawnPending(false), _warpPending(false)
13 {
14 _peerDesc = std::move(peerDesc);
15 _peerDesc->Player = this;
16 }
17
18 Task<bool> RemotablePlayer::OnActivatedAsync(const ActorActivationDetails& details)
19 {
20 async_return async_await Player::OnActivatedAsync(details);
21 }
22
23 bool RemotablePlayer::OnPerish(ActorBase* collider)
24 {
25 return Player::OnPerish(collider);
26 }
27
28 void RemotablePlayer::OnUpdate(float timeMult)
29 {
30 Player::OnUpdate(timeMult);
31
32 if (_levelExiting != LevelExitingState::None) {
33 OnLevelChanging(nullptr, ExitType::None);
34 }
35 }
36
37 void RemotablePlayer::OnWaterSplash(Vector2f pos, bool inwards)
38 {
39 // Already created and broadcasted by the server
40 }
41
42 bool RemotablePlayer::FireCurrentWeapon(WeaponType weaponType)
43 {
44 if (_weaponCooldown > 0.0f) {
45 return (weaponType != WeaponType::TNT);
46 }
47
48 return true;
49 }
50
51 void RemotablePlayer::SetCurrentWeapon(WeaponType weaponType, SetCurrentWeaponReason reason)
52 {
53 if (_currentWeapon == weaponType) {
54 return;
55 }
56
57 Player::SetCurrentWeapon(weaponType, reason);
58
59 if (!ChangingWeaponFromServer) {
60 static_cast<Jazz2::Multiplayer::MpLevelHandler*>(_levelHandler)->HandlePlayerWeaponChanged(this, reason);
61 }
62 }
63
64 void RemotablePlayer::WarpIn(ExitType exitType)
65 {
66 if (exitType != (ExitType)0xFF) {
67 OnLevelChanging(this, exitType);
68 return;
69 }
70
71 EndDamagingMove();
72 SetState(ActorState::IsInvulnerable, true);
73 SetState(ActorState::ApplyGravitation, false);
74
75 SetAnimation(_currentAnimation->State & ~(AnimState::Uppercut | AnimState::Buttstomp));
76
77 _speed.X = 0.0f;
78 _speed.Y = 0.0f;
79 _externalForce.X = 0.0f;
80 _externalForce.Y = 0.0f;
81 _internalForceY = 0.0f;
82 _fireFramesLeft = 0.0f;
83 _copterFramesLeft = 0.0f;
84 _pushFramesLeft = 0.0f;
85 _warpPending = true;
86
87 // For warping from the water
88 _renderer.setRotation(0.0f);
89
90 PlayPlayerSfx("WarpIn"_s);
91
92 SetPlayerTransition(_isFreefall ? AnimState::TransitionWarpInFreefall : AnimState::TransitionWarpIn, false, true, SpecialMoveType::None, [this]() {
93 if (_warpPending) {
94 _renderer.setDrawEnabled(false);
95 }
96 });
97 }
98
99 void RemotablePlayer::MoveRemotely(Vector2f pos, Vector2f speed)
100 {
101 Vector2f posPrev = _pos;
102 MoveInstantly(pos, MoveType::Absolute | MoveType::Force);
103 _speed = speed;
104
105 if (_warpPending) {
106 _warpPending = false;
107 _trailLastPos = _pos;
108 PlayPlayerSfx("WarpOut"_s);
109
110 _levelHandler->HandlePlayerWarped(this, posPrev, WarpFlags::Default);
111
112 _renderer.setDrawEnabled(true);
113 _isFreefall |= CanFreefall();
114 SetPlayerTransition(_isFreefall ? AnimState::TransitionWarpOutFreefall : AnimState::TransitionWarpOut, false, true, SpecialMoveType::None, [this]() {
115 SetState(ActorState::IsInvulnerable, false);
116 SetState(ActorState::ApplyGravitation, true);
117 _controllable = true;
118 });
119 } else {
120 _levelHandler->HandlePlayerWarped(this, posPrev, WarpFlags::Fast);
121 }
122 }
123
124 bool RemotablePlayer::Respawn(Vector2f pos)
125 {
126 bool success = MpPlayer::Respawn(pos);
127 if (!success) {
128 // Player didn't have enough time to die completely, respawn it when HandlePlayerDied() will be called
129 RespawnPos = pos;
130 RespawnPending = true;
131 return false;
132 }
133
134 return true;
135 }
136 }
137
138 #endif