Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ICmpMotion.h" 21 : 22 : #include "graphics/Terrain.h" 23 : #include "maths/Fixed.h" 24 : #include "maths/FixedVector3D.h" 25 : #include "maths/Vector3D.h" 26 : #include "simulation2/MessageTypes.h" 27 : #include "simulation2/components/ICmpPosition.h" 28 : #include "simulation2/helpers/Position.h" 29 : #include "simulation2/system/Component.h" 30 : #include "simulation2/system/Message.h" 31 : 32 : #include <cmath> 33 : #include <string> 34 : 35 0 : class CCmpMotionBall final : public ICmpMotion 36 : { 37 : public: 38 137 : static void ClassInit(CComponentManager& componentManager) 39 : { 40 137 : componentManager.SubscribeToMessageType(MT_Update); 41 : } 42 : 43 0 : DEFAULT_COMPONENT_ALLOCATOR(MotionBall) 44 : 45 : // Current speed in metres per second 46 : float m_SpeedX, m_SpeedZ; 47 : 48 137 : static std::string GetSchema() 49 : { 50 274 : return "<a:component type='test'/><ref name='anything'/>"; 51 : } 52 : 53 0 : void Init(const CParamNode&) override 54 : { 55 0 : m_SpeedX = 0; 56 0 : m_SpeedZ = 0; 57 0 : } 58 : 59 0 : void Deinit() override 60 : { 61 0 : } 62 : 63 0 : void Serialize(ISerializer& serialize) override 64 : { 65 0 : serialize.NumberFloat_Unbounded("speed x", m_SpeedX); 66 0 : serialize.NumberFloat_Unbounded("speed z", m_SpeedZ); 67 0 : } 68 : 69 0 : void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override 70 : { 71 0 : Init(paramNode); 72 0 : deserialize.NumberFloat_Unbounded("speed x", m_SpeedX); 73 0 : deserialize.NumberFloat_Unbounded("speed z", m_SpeedZ); 74 0 : } 75 : 76 0 : void HandleMessage(const CMessage& msg, bool /*global*/) override 77 : { 78 0 : switch (msg.GetType()) 79 : { 80 0 : case MT_Update: 81 0 : { 82 0 : fixed dt = static_cast<const CMessageUpdate&> (msg).turnLength; 83 0 : Move(dt); 84 0 : break; 85 : } 86 : } 87 0 : } 88 : 89 : void Move(fixed dt); 90 : }; 91 : 92 0 : void CCmpMotionBall::Move(fixed dt) 93 : { 94 0 : CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle()); 95 0 : if (!cmpPosition) 96 0 : return; 97 : 98 : // TODO: this is all FP-unsafe 99 : 100 0 : CFixedVector3D pos = cmpPosition->GetPosition(); 101 0 : float x = pos.X.ToFloat(); 102 0 : float z = pos.Z.ToFloat(); 103 : 104 0 : CVector3D normal = GetSimContext().GetTerrain().CalcExactNormal(x, z); 105 : // Flatten the vector, to get the downhill force 106 0 : float g = 10.f; 107 0 : CVector3D force(g * normal.X, 0.f, g * normal.Z); 108 : 109 0 : m_SpeedX += force.X; 110 0 : m_SpeedZ += force.Z; 111 : 112 0 : float drag = 0.5f; // fractional decay per second 113 : 114 0 : float dt_ = dt.ToFloat(); 115 : 116 0 : m_SpeedX *= powf(drag, dt_); 117 0 : m_SpeedZ *= powf(drag, dt_); 118 : 119 0 : cmpPosition->MoveTo(entity_pos_t::FromFloat(x + m_SpeedX * dt_), entity_pos_t::FromFloat(z + m_SpeedZ * dt_)); 120 : } 121 : 122 274 : REGISTER_COMPONENT_TYPE(MotionBall)