TX2D TriggerMessage Component

TriggerMessage.cs download
This component allows you to display any graphical scene object into the current camera view for a specified amount of time. Its a great way to display a message onto the screen during any triggered event.

TriggerMessage Screen Shot

Activate Leave unchecked unless the message needs to display right when the scene loads.

CameraName The scene camera name. The component relocates the message to the center of the screen

Position Offset the message; X:0,Y:0 is center screen.

DisplayTime Duration in seconds to display message.

FadeIn/OutSpeed Smaller is faster.

UnregisterOnFadeOut Unregister component after message has been displayed and faded out.

Enable Required for the component to function.


TriggerMessage.cs Code:

// TriggerMessage.cs v1.1
// -------------------
// Aaron Scovel - TopNotched.com
/////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T2D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;
using GarageGames.Torque.Materials;

namespace StarterGame2D
{

    [TorqueXmlSchemaType]
    public class TriggerMessage : TorqueComponent, ITickObject
    {

        #region Public properties, operators, constants, and enums

        public T2DSceneObject SceneObject
        {
            get { return Owner as T2DSceneObject; }
        }

        [TorqueXmlSchemaType(DefaultValue = "false")]
        public bool Activate
        {
            get { return _activate; }
            set { _activate = value; }
        }

        public string CameraName
        {
            get { return _cameraName; }
            set { _cameraName = value; }
        }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "10")]
        public int DisplayTime
        {
            get { return _displayTime; }
            set { _displayTime = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "1")]
        public float FadeInSpeed
        {
            get { return _fadeInSpeed; }
            set { _fadeInSpeed = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "1")]
        public float FadeOutSpeed
        {
            get { return _fadeOutSpeed; }
            set { _fadeOutSpeed = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "false")]
        public bool UnregisterOnFadeOut
        {
            get { return _unregister; }
            set { _unregister = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "true")]
        public bool Enable
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        #endregion

        #region Public methods

        public virtual void ProcessTick(Move move, float dt)
        {
            if (_enabled)
            {
                if (_activate)
                {
                    SceneObject.Position = _camera.Position + _position;
                    if (!_beginDisplyTimer)
                    {
                        if (SceneObject.VisibilityLevel >= 1.00f)
                        {
                            SceneObject.VisibilityLevel = 1.00f;
                            _beginDisplyTimer = true;
                        }
                        else
                        {
                            SceneObject.VisibilityLevel += _fadeInSpeed * dt;
                        }
                    }
                    else
                    {
                        if (_currentTime >= _displayTime)
                        {
                            if (SceneObject.VisibilityLevel <= 0.00f)
                            {
                                SceneObject.VisibilityLevel = 0.00f;
                                _activate = false;
                                if (_unregister)
                                {
                                    TorqueObjectDatabase.Instance.Unregister(SceneObject);
                                }
                            }
                            else
                            {
                                SceneObject.VisibilityLevel -= _fadeOutSpeed * dt;
                            }
                        }
                        else
                        {
                            _currentTime += dt;
                        }
                    }
                }
            }
        }

        public virtual void InterpolateTick(float k)
        {
        }

        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);
        }

        #endregion

        #region Private, protected, internal methods

        protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
                return false;

            _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>(_cameraName);

            if (SceneObject is T2DStaticSprite)
                _material = (SimpleMaterial)((T2DStaticSprite)SceneObject).Material;
            if (SceneObject is T2DAnimatedSprite)
                _material = (SimpleMaterial)((T2DAnimatedSprite)SceneObject).AnimationData.Material;
            if (SceneObject is T2DScroller)
                _material = (SimpleMaterial)((T2DScroller)SceneObject).Material;

            _material.IsTranslucent = true;
            _material.IsColorBlended = true;
            SceneObject.VisibilityLevel = 0.00f;

            ProcessList.Instance.AddTickCallback(Owner, this);
            return true;
        }

        protected override void _OnUnregister()
        {
            base._OnUnregister();
        }

        protected override void _RegisterInterfaces(TorqueObject owner)
        {
            base._RegisterInterfaces(owner);
        }

        #endregion

        #region Private, protected, internal fields

        T2DSceneCamera _camera;
        SimpleMaterial _material;
        string _cameraName;
        bool _activate = false;
        bool _enabled = true;
        bool _unregister = false;
        bool _beginDisplyTimer = false;
        int _displayTime;
        float _fadeInSpeed;
        float _fadeOutSpeed;
        float _currentTime;
        Vector2 _position;

        #endregion
    }
}

To activate the component, you need to first find the object which the component is attached to and then set Activate to true. Example 1:

// Find the Object
T2DSceneObject _obj1 = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("ObjectSourceName");
// Find the Objects Component
TriggerMessage Obj1 = _obj1.Components.FindComponent<TriggerMessage>();
// Activate the Component
Obj1.Activate = true;

If you would like to use the T2DTriggerComponent to trigger the component, then you need to setup a delegate.
Example 2:

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T2D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;

namespace StarterGame2D
{
    [TorqueXmlSchemaType]
    public class delegates
    {
        public static T2DTriggerComponentOnEnterDelegate DisplayObj1
        {
            get { return _displayObj1; }
        }

        public static void _displayObj1(T2DSceneObject ourObject, T2DSceneObject theirObject)
        {
            // Find the Object
            T2DSceneObject _obj1 = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("ObjectSourceName");
            // Find the Objects Component
            TriggerMessage Obj1 = _obj1.Components.FindComponent<TriggerMessage>();
            // Activate the Component
            Obj1.Activate = true;
        }

    }
}
Powered by WordPress | Designed by: Download Free WordPress Themes | Thanks to wordpress themes 2012, Themes Gallery and Best Free WordPress Themes