void PlayerInputSystem::update(float delta) { // Adding PROFILE to a function adds the function to the PropellerProfiler. This can be used to easily diagnose performance issues on any platform. PROFILE; // the PlayerInputComponent had some parts where the force was applied as impulse directly. // this is a example of handling the constant movement per tick. // as you cannot loop the components, you cannot do anything that lasts over frames there. // System update() is called constantly, so you can handle stuff that needs to be update every // tick with it. // Remember to handle the "delta" time! or your system will f**k up everything with different // devices. // this code is not tested and should be just used as a reference on how the components-systems works for (unsigned i = 0; i < components.size(); ++i) { PlayerInputComponent *inputComponent = rtti::dynamicCast<PlayerInputComponent>(components[i]); physics::Box2DCharacterComponent *rigidBody = inputComponent->getParent()->findComponent<physics::Box2DCharacterComponent>(); const math::Vec2 &movement = inputComponent->getMovement(); // Apply force rigidBody->applyForce(movement * delta); // deltatime applied to the force! } }
void PlayerInputSystem::update(float delta) { PROFILE; for (unsigned i = 0; i < components.size(); ++i) { PlayerInputComponent *inputComponent = rtti::dynamicCast<PlayerInputComponent>(components[i]); physics::Box2DCharacterComponent *rigidBody = inputComponent->getParent()->findComponent<physics::Box2DCharacterComponent>(); const math::Vec2 &movement = inputComponent->getMovement(); // Apply force /* math::Vec2 vel = rigidBody->getVelocity(); float maxVel = 25.f; if (vel.x > maxVel) return; if (vel.x < -maxVel) return; rigidBody->applyForce(movement * 150); */ // Limit max speed //var speed : Number = body.m_linearVelocity.Normalize(); // sets vector length to 1, returns original length of vector //body.m_linearVelocity.Multiply(Math.min(speed, maxSpeed)); } }