Example #1
0
        // This is a lot of complex math on choosing some "random" movement, keeping it near home and handling error cases.
        private void Movement(float speed, float chaos)
        {
            // Initialize a random number generator.
            Random rng = new Random();

            // Pick a direction to start with - mostly in the X/Y plane, but just a little push up.
            Direction = new Sansar.Vector((float)(0.5 - rng.NextDouble()), (float)(0.5 - rng.NextDouble()), 0.02f, 0.0f);

            // This will just continually try to move the object (the Wait at the bottom is important!)
            while (true)
            {
                // Calculate how far we are from our home point.
                float distance = (RigidBody.GetPosition() - Home).Length();

                // Pick a new direction based on Chaos level, or if we have wandered off too far
                if (rng.NextDouble() <= Chaos || distance > Range)
                {
                    // If we are far from home point us at home before adjusting the position.
                    if (distance > Range)
                    {
                        // Move toward the center.
                        Direction = (Home - RigidBody.GetPosition()).Normalized();

                        // Note: still letting the randomize adjust the heading.
                    }

                    // This is the most bogus direction adjusting logic you will see today.
                    Direction.X = Direction.X + (float)(0.5 - rng.NextDouble());
                    Direction.Y = Direction.Y + (float)(0.5 - rng.NextDouble());
                    Direction.Z = 0.02f;
                    Direction   = Direction.Normalized();
                }

                // AddLinearImpulse can be picky on accepted values, especially if any math above breaks or the speed is set too high.
                // It will throw an exception if it doesn't like it. This will just skip
                try
                {
                    Log.Write("PUSH! " + Direction.ToString() + " * " + speed + " => " + (Direction * speed).ToString());
                    RigidBody.AddLinearImpulse(Direction * speed);
                }
                catch (Exception e)
                {
                    Log.Write("Exception " + e.GetType().ToString() + " in AddLinearImpulse for value: " + (Direction * speed).ToString());

                    // That direction was bad, so lets choose a new one.
                    Direction.X = (float)(0.5 - rng.NextDouble());
                    Direction.Y = (float)(0.5 - rng.NextDouble());
                    Direction.Z = 0.02f;
                    Direction   = Direction.Normalized();
                }

                // Wait after each push for between 0.5 and 1.5 seconds.
                Wait(TimeSpan.FromSeconds(0.5 + rng.NextDouble()));
            }
        }
Example #2
0
        // Putting the collision event into a coroutine helps to reduce duplicate events
        // After every collision we give a small bump then sleep this coroutine before waiting on another collision.
        // An Action event handler would get a large queue of events for colliding with the same object which is harder to deal with.
        private void CheckForCollisions()
        {
            while (true)
            {
                // This will block the coroutine until a collision happens
                CollisionData data = (CollisionData)WaitFor(RigidBody.Subscribe, CollisionEventType.AllCollisions, Sansar.Script.ComponentId.Invalid);

                if (data.HitObject == null)
                {
                    // This is slightly more common, collided with something were no object was given from the physics system.
                    // Again, just sleep a little and continue.
                    Log.Write("Hit nothing? " + data.HitComponentId);
                    Wait(TimeSpan.FromSeconds(0.2));
                    continue;
                }

                // This position - collision object position gives a vector away from the object we collided with.
                // This is not "away from the collision point". Complex shapes or large objects will confuse this simple math.
                Sansar.Vector direction = ObjectPrivate.Position - data.HitObject.Position;
                direction = direction.Normalized();

                if (Math.Abs(direction.X) < 0.1 && Math.Abs(direction.Y) < 0.1)
                {
                    // This object is mostly above or below us: it is probably the floor.
                    // This is overly simplistic and will fail for large objects or sloped floors.
                    // Sleep a little and continue.
                    Wait(TimeSpan.FromSeconds(0.2));
                    continue;
                }

                // direction is now pointing _away_ from what we collided with, set it as our Direction for Movement.
                Direction = direction;

                try
                {
                    // Apply an immediate bump away from what we collided with.
                    Log.Write("Bump! " + Direction.ToString() + " * " + Speed + " => " + (Direction * Speed).ToString());
                    RigidBody.AddLinearImpulse(Direction * Speed);
                }
                catch (Exception e)
                {
                    Log.Write("Collision Exception " + e.GetType().ToString() + " in AddLinearImpulse for value: " + (Direction * Speed).ToString());
                }

                // Wait before checking for more collisions to avoid duplicate collisions and give a chance to separate from the other object.
                Wait(TimeSpan.FromSeconds(0.2));
            }
        }
Example #3
0
        private void SetRandomColorAndIntensityOfAllLights()
        {
            foreach (var light in lights)
            {
                // Pick a random color but don't let it be too dark or else the relative intensity doesn't work well
                Sansar.Vector randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble());
                if (randomVector.LengthSquared() < 0.5f)
                {
                    randomVector = randomVector.Normalized();
                }
                Sansar.Color randomColor = new Sansar.Color(randomVector.X, randomVector.Y, randomVector.Z);

                // Pick a random intensity from min to max
                float randomIntensity = randomMinIntensity + (randomMaxIntensity - randomMinIntensity) * (float)rnd.NextDouble();

                light.SetColorAndIntensity(randomColor, randomIntensity);
            }
        }
    public void OnFire(AnimationComponent animationComponent)
    {
        if (RezThrottled == true)
        {   // TODO: Play a "click" / misfire sound.
            return;
        }

        ObjectPrivate characterObject = ScenePrivate.FindObject(animationComponent.ComponentId.ObjectId);

        if (Fire_Sound != null)
        {
            ScenePrivate.PlaySoundAtPosition(Fire_Sound, characterObject.Position, SoundSettings);
        }

        Sansar.Vector cameraForward = animationComponent.GetVectorAnimationVariable("LLCameraForward");
        cameraForward.W = 0.0f;
        cameraForward   = cameraForward.Normalized();

        Sansar.Vector offset = new Sansar.Vector(cameraForward[0], cameraForward[1], 0, 0).Normalized();

        Sansar.Vector new_pos = new Sansar.Vector(0f, 0f, 1.5f, 0f);
        new_pos += characterObject.Position; // This script is on the Avatar, so the Avatar is the owning object.
        new_pos += (offset * 0.6f);          // Add to the world just in front of the avatar.

        float speed = InitialSpeed;

        if (InitialSpeed < 1.0f || InitialSpeed > 200.0f)
        {
            const float defaultSpeed = 10.0f;
            Log.Write(string.Format("Bad CannonBall Speed: {0}, using default {1}", InitialSpeed, defaultSpeed));
            speed = defaultSpeed;
        }

        Sansar.Vector vel = cameraForward * speed;

        StartCoroutine(RezCannonball, new_pos, Quaternion.FromLook(cameraForward, Vector.Up), vel);

        Timer.Create(TimeSpan.FromSeconds(Delay), () => { animationComponent.Subscribe(ListenEvent, (data) => { OnFire(animationComponent); }, false); });
    }
Example #5
0
        private void Subscribe(ScriptEventData sed)
        {
            if (subscriptions == null)
            {
                subscriptions = SubscribeToAll(TurnOnEvent, (data) =>
                {
                    if (TurnOnFadeTime > 0.0f)
                    {
                        previousColor         = lights[0].GetNormalizedColor();
                        previousIntensity     = lights[0].GetRelativeIntensity();
                        targetColor           = initialColor;
                        targetIntensity       = initialIntensity;
                        interpolationDuration = TurnOnFadeTime;
                        interpolationTime     = TurnOnFadeTime;
                        interpolationActive   = true;
                    }
                    else
                    {
                        interpolationActive = false;
                        SetColorAndIntensityOfAllLights(initialColor, initialIntensity);
                    }
                });

                subscriptions += SubscribeToAll(TurnOffEvent, (data) =>
                {
                    if (TurnOffFadeTime > 0.0f)
                    {
                        previousColor         = lights[0].GetNormalizedColor();
                        previousIntensity     = lights[0].GetRelativeIntensity();
                        targetColor           = Sansar.Color.Black;
                        targetIntensity       = 0.0f;
                        interpolationDuration = TurnOffFadeTime;
                        interpolationTime     = TurnOffFadeTime;
                        interpolationActive   = true;
                    }
                    else
                    {
                        interpolationActive = false;
                        SetColorAndIntensityOfAllLights(Sansar.Color.Black, 0.0f);
                    }
                });

                subscriptions += SubscribeToAll(TurnRandomEvent, (data) =>
                {
                    if (TurnRandomFadeTime > 0.0f)
                    {
                        // Pick a random color but don't let it be too dark or else the relative intensity doesn't work well
                        Sansar.Vector randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble());
                        while (randomVector.LengthSquared() < 0.1f)
                        {
                            randomVector = new Sansar.Vector((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble());
                        }
                        if (randomVector.LengthSquared() < 0.5f)
                        {
                            randomVector = randomVector.Normalized();
                        }
                        Sansar.Color randomColor = new Sansar.Color(randomVector.X, randomVector.Y, randomVector.Z);

                        // Pick a random intensity from min to max
                        float randomIntensity = randomMinIntensity + (randomMaxIntensity - randomMinIntensity) * (float)rnd.NextDouble();

                        previousColor         = lights[0].GetNormalizedColor();
                        previousIntensity     = lights[0].GetRelativeIntensity();
                        targetColor           = randomColor;
                        targetIntensity       = randomIntensity;
                        interpolationDuration = TurnRandomFadeTime;
                        interpolationTime     = TurnRandomFadeTime;
                        interpolationActive   = true;
                    }
                    else
                    {
                        interpolationActive = false;
                        SetRandomColorAndIntensityOfAllLights();
                    }
                });
            }

            if (HasFadeTime())
            {
                StartInterpolation();
            }
        }