Beispiel #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);
    }
Beispiel #2
0
        public override void OnAttachedToNode(Node node)
        {
            base.OnAttachedToNode(node);

            this.planetFactory = this.Scene.GetComponent <PlanetFactory>()
                                 ?? throw new InvalidOperationException("'PlanetFactory' not found");

            this.joystickServer = this.Scene.GetComponent <JoystickServer>()
                                  ?? throw new InvalidOperationException("'JoystickServer' not found");

            this.focusManager = this.Scene.GetComponent <FocusManager>()
                                ?? throw new InvalidOperationException("'FocusManager' not found");

            this.cameraNode = this.Scene.GetChild("MainCamera", false)
                              ?? throw new InvalidOperationException("'MainCamera' not found");

            // Geometry.
            this.geometryNode = this.Node.CreateChild("Geometry");
            var planeModel = this.geometryNode.CreateComponent <StaticModel>();

            planeModel.Model    = this.Application.ResourceCache.GetModel("Models\\Rocket.mdl");
            planeModel.Material = this.Application.ResourceCache.GetMaterial("Materials\\RocketMaterial.xml");

            // Gravity.
            this.rigidBody      = this.Node.CreateComponent <RigidBody>();
            this.rigidBody.Mass = Constants.RocketDefaultMass;
            this.rigidBody.LinearRestThreshold = 0.0003f;
            this.rigidBody.AngularDamping      = 0;
            this.rigidBody.SetAngularFactor(Vector3.Zero);
            rigidBody.SetLinearVelocity(this.cameraNode.Rotation * Constants.RocketLaunchVelocity);

            // Engine particle emitter.
            var engineParticleNode = this.Node.CreateChild("RocketEngine");

            this.engineParticleEmitter         = engineParticleNode.CreateComponent <ParticleEmitter>();
            this.engineParticleEmitter.Enabled = false;
            this.engineParticleEmitter.Effect  = this.Application.ResourceCache.GetParticleEffect("Particles\\RocketEngine.xml");
            engineParticleNode.Translate(new Vector3(0, 0, -0.03f));

            // Collision particles.
            var collisionParticleNode = this.Node.CreateChild("CollisionParticle");

            this.collisionParticleEmitter         = collisionParticleNode.CreateComponent <ParticleEmitter>();
            this.collisionParticleEmitter.Enabled = false;
            this.collisionParticleEmitter.Effect  = this.Application.ResourceCache.GetParticleEffect("Particles\\Explosion.xml");

            // Collision detection.
            this.collisionShape = this.Node.CreateComponent <CollisionShape>();
            this.collisionShape.SetCylinder(0.07f, 0.015f, Vector3.Zero, Quaternion.Identity);

            // Background sound.
            this.rocketSoundSource = this.Node.CreateComponent <SoundSource3D>();
            this.rocketSoundSource.SetDistanceAttenuation(0.0f, 2.0f, 1.0f);
            var sound = this.Application.ResourceCache.GetSound("Sounds\\Rocket.wav");

            sound.Looped = true;
            this.rocketSoundSource.Play(sound);
            this.rocketSoundSource.Gain = 0.1f;
            this.soundBaseFrequency     = this.rocketSoundSource.Frequency;

            // Collision sound.
            this.collisionSoundSource = this.Node.CreateComponent <SoundSource3D>();
            this.collisionSound       = this.Application.ResourceCache.GetSound("Sounds\\Collision.wav");
            this.collisionSoundSource.SetDistanceAttenuation(0.0f, 5.0f, 3.0f);

            // Engine sound.
            this.engineSoundSource = this.Node.CreateComponent <SoundSource3D>();
            this.engineSound       = this.Application.ResourceCache.GetSound("Sounds\\RocketEngine.wav");
            this.engineSoundSource.SetDistanceAttenuation(0.0f, 5.0f, 1.0f);
            this.engineSound.Looped = true;
        }