예제 #1
0
    public Vehicle CreateChassis(Vector2 colliderCenter, float colliderRadius, int massDensity, Vector3 exhaustPosition,
                                 ParticleEffect2D exhaustParticles, Sound engineSound, Sound tireSound, Sound[] suspensionSounds,
                                 int horsePower, int maxSpeedFwd, int maxSpeedBwd, int rollForce)
    {
        // We set out private fields
        _horsePower = horsePower;
        _maxSpdFwd  = maxSpeedFwd;
        _maxSpdBwd  = maxSpeedBwd;
        _rollForce  = rollForce;
        _rigidBody  = GetComponent <RigidBody2D>();

        // We add the collider (circle collider at the moment)
        var col = Racer2D.AddCollider <CollisionCircle2D>(Node, dens: massDensity, fric: 0);

        col.SetRadius(colliderRadius);
        col.SetCenter(colliderCenter);

        // We create the exhaust particle system
        var exhaustParticlesNode = Node.CreateChild();

        exhaustParticlesNode.SetPosition(exhaustPosition);
        _exhaustParticles = exhaustParticlesNode.CreateComponent <ParticleEmitter2D>();
        _exhaustParticles.SetEffect(exhaustParticles);

        // We setup the engine sound and other sound effect
        engineSound.SetLooped(true);
        _soundSource = Node.CreateComponent <SoundSource3D>();
        _soundSource.SetNearDistance(10);
        _soundSource.SetFarDistance(50);
        _accelSound            = engineSound;
        _brakeSound            = tireSound;
        _suspensionSoundSource = Node.CreateComponent <SoundSource3D>();
        _suspensionSounds      = suspensionSounds;

        // We return the Vehicle for convenience, since this function is intended to be the vehicle's init function
        return(this);
    }
예제 #2
0
    public Node CreateWheel(Sprite2D sprite, Vector2 relativePosition, float radius, int suspensionFrequency, float suspensionDamping,
                            ParticleEffect2D particles, float distanceToEmitParticles)
    {
        Node wheelNode = Racer2D.CreateSpriteNode(sprite);

        wheelNode.SetPosition2D(relativePosition);

        // CreateSpriteNode adds a RigidBody for us, so we get it here
        RigidBody2D wheelRigidBody = wheelNode.GetComponent <RigidBody2D>();

        // We activate CCD
        wheelRigidBody.SetBullet(true);

        Racer2D.AddCollider <CollisionCircle2D>(wheelNode).SetRadius(radius);

        // The Box2D wheel joint provides spring for simulating suspension
        ConstraintWheel2D wheelJoint = Node.CreateComponent <ConstraintWheel2D>();

        wheelJoint.SetOtherBody(wheelRigidBody);
        wheelJoint.SetAnchor(relativePosition);
        wheelJoint.SetAxis(Vector2.UnitY);
        wheelJoint.SetFrequencyHz(suspensionFrequency);
        wheelJoint.SetDampingRatio(suspensionDamping);

        // Each wheel has a particle emitter to emit particles when it's in contact with the surface
        Node particlesNode = Node.Scene.CreateChild();

        particlesNode.SetPosition(new Vector3(relativePosition.X, relativePosition.Y, 14));
        ParticleEmitter2D particleEmitter = particlesNode.CreateComponent <ParticleEmitter2D>();

        particleEmitter.SetEffect(particles);

        // We create a new Wheel struct and add to the _wheels list
        _wheels.Add(new Wheel(wheelRigidBody, wheelJoint, particleEmitter, distanceToEmitParticles));

        return(wheelNode);
    }
예제 #3
0
    public Node CreateHead(Sprite2D sprite, Vector3 relativePosition, float colliderRadius, Vector2 neckAnchor)
    {
        Node head = Racer2D.CreateSpriteNode(sprite);

        head.SetPosition(relativePosition);
        Racer2D.AddCollider <CollisionCircle2D>(head).SetRadius(colliderRadius);

        // This is the actual neck joint
        ConstraintRevolute2D joint = head.CreateComponent <ConstraintRevolute2D>();

        joint.SetOtherBody(_rigidBody);
        joint.SetAnchor(neckAnchor);

        // This is the spring, it's attached to the body with an offset
        ConstraintDistance2D spring = head.CreateComponent <ConstraintDistance2D>();

        spring.SetOtherBody(_rigidBody);
        spring.SetOwnerBodyAnchor(-Vector2.UnitY * 2);
        spring.SetOtherBodyAnchor(Node.WorldToLocal2D(head.WorldPosition2D - Vector2.UnitY * 2));
        spring.SetFrequencyHz(3);
        spring.SetDampingRatio(0.4f);

        return(head);
    }