<commit_before>///////////////////////////////////////////////////////////////////////////////
// This source file is part of Zeroth.
//
// Copyright (c) 2014 <NAME>
//
///////////////////////////////////////////////////////////////////////////////
#include "ShipControlSystem.h"

#include <Hect/Logic/World.h>
#include <Hect/Logic/Components/RigidBody.h>
#include <Hect/Logic/Systems/PhysicsSystem.h>
#include <Hect/Logic/Components/Transform.h>

#include "Components/Thruster.h"

ShipControlSystem::ShipControlSystem(World& world) :
    System(world)
{
}

void ShipControlSystem::controlShip(Entity& ship, const Vector3& angularAmount, Real thrustAmount, Real timeStep)
{
    PhysicsSystem& physicsSystem = world().system<PhysicsSystem>();

    auto transform = ship.component<Transform>();
    if (transform)
    {
        auto rigidBody = ship.component<RigidBody>();
        if (rigidBody)
        {
            {
                Vector3 angularVelocity = rigidBody->angularVelocity;
                angularVelocity = angularVelocity - angularVelocity * timeStep * 1.0;

                Vector3 angularDelta = transform->globalRotation * angularAmount;
                angularDelta *= timeStep * 2.0;

                physicsSystem.setAngularVelocity(*rigidBody, angularVelocity + angularDelta);
            }
            {
                Vector3 linearVelocity = rigidBody->linearVelocity;
                linearVelocity = linearVelocity - linearVelocity * timeStep * 0.5;
                physicsSystem.setLinearVelocity(*rigidBody, linearVelocity);

                auto thrusterEntities = ship.findDescendants([](const Entity& entity)
                {
                    return entity.component<Thruster>();
                });

                for (auto thrusterEntity : thrusterEntities)
                {
                    auto thruster = thrusterEntity->component<Thruster>();
                    if (thruster)
                    {
                        auto thrusterTransform = thrusterEntity->component<Transform>();
                        if (thrusterTransform)
                        {
                            Vector3 thrustVector = transform->globalRotation * thruster->direction;
                            thrustVector = thrustVector.normalized() * thruster->power * thrustAmount;

                            physicsSystem.applyForce(*rigidBody, thrustVector, Vector3::zero());
                        }
                    }
                }
            }
        }
    }
}<commit_msg>Supporting changes<commit_after>///////////////////////////////////////////////////////////////////////////////
// This source file is part of Zeroth.
//
// Copyright (c) 2014 <NAME>
//
///////////////////////////////////////////////////////////////////////////////
#include "ShipControlSystem.h"

#include <Hect/Logic/World.h>
#include <Hect/Logic/Components/RigidBody.h>
#include <Hect/Logic/Systems/PhysicsSystem.h>
#include <Hect/Logic/Components/Transform.h>

#include "Components/Thruster.h"

ShipControlSystem::ShipControlSystem(World& world) :
    System(world)
{
}

void ShipControlSystem::controlShip(Entity& ship, const Vector3& angularAmount, Real thrustAmount, Real timeStep)
{
    PhysicsSystem& physicsSystem = world().system<PhysicsSystem>();

    auto transform = ship.component<Transform>();
    if (transform)
    {
        auto rigidBody = ship.component<RigidBody>();
        if (rigidBody)
        {
            {
                Vector3 angularVelocity = rigidBody->angularVelocity;
                angularVelocity = angularVelocity - angularVelocity * timeStep * 1.0;

                Vector3 angularDelta = transform->globalRotation * angularAmount;
                angularDelta *= timeStep * 2.0;

                rigidBody->angularVelocity = angularVelocity + angularDelta;
            }
            {
                Vector3 linearVelocity = rigidBody->linearVelocity;
                linearVelocity = linearVelocity - linearVelocity * timeStep * 0.5;
                rigidBody->linearVelocity = linearVelocity;

                auto thrusterEntities = ship.findDescendants([](const Entity& entity)
                {
                    return entity.component<Thruster>();
                });

                for (auto thrusterEntity : thrusterEntities)
                {
                    auto thruster = thrusterEntity->component<Thruster>();
                    if (thruster)
                    {
                        auto thrusterTransform = thrusterEntity->component<Transform>();
                        if (thrusterTransform)
                        {
                            Vector3 thrustVector = transform->globalRotation * thruster->direction;
                            thrustVector = thrustVector.normalized() * thruster->power * thrustAmount;

                            physicsSystem.applyForce(*rigidBody, thrustVector, Vector3::zero());
                        }
                    }
                }
            }

            physicsSystem.updateRigidBody(*rigidBody);
        }
    }
}
{'fraction_non_alphanumeric': 0.15164661802405838, 'fraction_numerical': 0.006310392427529087, 'mean_word_length': 1.544907175112895, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7571453', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1187, 'n_words': 222}