Botcraft 26.3
Loading...
Searching...
No Matches
PhysicsManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <memory>
5#include <thread>
6
8
11
12namespace Botcraft
13{
14 class EntityManager;
15 class InventoryManager;
16 class LocalPlayer;
17 class NetworkManager;
18 class World;
19
20 class Item;
21
23 {
24 public:
25 PhysicsManager() = delete;
27 const std::shared_ptr<InventoryManager>& inventory_manager_,
28 const std::shared_ptr<EntityManager>& entity_manager_,
29 const std::shared_ptr<NetworkManager>& network_manager_,
30 const std::shared_ptr<World>& world_
31 );
33
34 void StartPhysics();
35 void StopPhysics();
36
37 /// @brief Enable/disable auto triggering of sprint when press/unpress/press forward key
38 /// in less than 7 ticks (enabled by default)
39 /// @param b Double tap will start sprinting if b is true
40 void SetDoubleTapCauseSprint(const bool b);
41
42 double GetMsPerTick() const;
43
44 protected:
45 virtual void Handle(ProtocolCraft::ClientboundPlayerPositionPacket& packet) override;
46#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
47 virtual void Handle(ProtocolCraft::ClientboundTickingStatePacket& packet) override;
48#endif
49#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
50 virtual void Handle(ProtocolCraft::ClientboundPlayerRotationPacket& packet) override;
51#endif
52
53 private:
54 void Physics();
55
56 /// @brief Follow minecraft physics related flow in LocalPlayer tick function
57 void PhysicsTick();
58 void UpdateSwimming() const;
59 /// @brief Perform fluid physics on the player, and set in_water/lava boolean accordingly
60 /// @param water If true, will push with water, lava otherwise
61 void FluidPhysics(const bool water);
62 void InputsToCrouch() const;
63 void InputsToSprint() const;
64 void SetSprinting(const bool b) const;
65 void InputsToFly() const;
66 void InputsToJump() const;
67 void JumpFromGround() const;
68 void ApplyInputs(const float strength) const;
69 /// @brief Send position/rotation/on_ground to server
70 void SendPosition();
71
72 /// @brief Check collisions of an AABB with a given movement
73 /// @param aabb AABB to move
74 /// @param movement Movement vector to apply to AABB
75 /// @return New movement vector after collisions
76 Vector3<double> CollideBoundingBox(const AABB& aabb, const Vector3<double>& movement) const;
77
78 void CollideOneAxis(AABB& aabb, Vector3<double>& movement, const unsigned int axis, const std::vector<AABB>& colliders) const;
79
80 bool IsSwimmingAndNotFlying() const;
81 bool IsInClimbable() const;
82 void MovePlayer() const;
83 void ApplyMovement() const;
84
85 void OnUpdateAbilities() const;
86 void CheckInsideBlocks() const;
88
89 private:
90 std::shared_ptr<EntityManager> entity_manager;
91 std::shared_ptr<InventoryManager> inventory_manager;
92 std::shared_ptr<LocalPlayer> player;
93 std::shared_ptr<NetworkManager> network_manager;
94 std::shared_ptr<World> world;
95
96 std::atomic<bool> should_run;
97
99
100 std::thread thread_physics; // Thread running to compute position and send it to the server every tick
101
103
104#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
105 std::atomic<double> ms_per_tick = 50.0;
106#else
107 static constexpr double ms_per_tick = 50.0;
108#endif
109
110 std::atomic<bool> double_tap_cause_sprint;
111
112 };
113} // Botcraft
Vector3< double > CollideBoundingBox(const AABB &aabb, const Vector3< double > &movement) const
Check collisions of an AABB with a given movement.
std::shared_ptr< InventoryManager > inventory_manager
virtual void Handle(ProtocolCraft::ClientboundPlayerPositionPacket &packet) override
void PhysicsTick()
Follow minecraft physics related flow in LocalPlayer tick function.
std::shared_ptr< NetworkManager > network_manager
std::shared_ptr< EntityManager > entity_manager
void FluidPhysics(const bool water)
Perform fluid physics on the player, and set in_water/lava boolean accordingly.
void CollideOneAxis(AABB &aabb, Vector3< double > &movement, const unsigned int axis, const std::vector< AABB > &colliders) const
std::shared_ptr< World > world
void SetDoubleTapCauseSprint(const bool b)
Enable/disable auto triggering of sprint when press/unpress/press forward key in less than 7 ticks (e...
void SendPosition()
Send position/rotation/on_ground to server.
std::atomic< bool > double_tap_cause_sprint
std::atomic< double > ms_per_tick
std::atomic< bool > should_run
Position GetBlockBelowAffectingMovement() const
std::shared_ptr< LocalPlayer > player
void SetSprinting(const bool b) const
void ApplyInputs(const float strength) const