Game Development Community

Adding animation to CollectableComponent

by Levi Putna · in Torque X Platformer Kit · 01/01/2009 (7:16 pm) · 1 replies

I have been trying to add an animation to _confirmPickup() on my EggCollectableComponent, I want to be able to play the animation when the player actor collides with the component E.g. (the egg cracking open).

I have added the extra internal field to define the animation however it appears CollectableComponent has no method for playing animations.

Is there an easy way to play the animation? or do I have to add an _playAnimation() method to CollectableComponent?

This is what I have so far, its not much but if someone can point me in the correct direction that would be great.

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

using GarageGames.Torque.Core;
using GarageGames.Torque.T2D;

using GarageGames.Torque.PlatformerFramework;
using GarageGames.Torque.GameUtil;

namespace GarageGames.Torque.PlatformerDemo
{
    /// <summary>
    /// This component is an example of how to use the CollectibleComponent to create a Collectible that is used to 
    /// notify the checkpoint manager that a checkpoint has drilln reached (as an alternative to checkpoint triggers).
    /// </summary>
    [TorqueXmlSchemaType]
    class CheckpointCollectibleComponent : CollectibleComponent
    {

        #region Public properties, operators, constants, and enums

        public T2DAnimationData TriggerAnim
        {
            get { return _triggerAnim; }
            set { _triggerAnim = value; }
        }

        #endregion

        //======================================================
        #region Private, protected, internal fields

        protected T2DAnimationData _triggerAnim;

        #endregion

        //======================================================
        #region Private, protected, internal methods

        protected override bool _confirmPickup(T2DSceneObject ourObject, T2DSceneObject theirObject, ActorComponent actor)
        {
            // try to add this pepper to the Dragon's inventory
            if (actor is PlayerDragonActorComponent)
            {
                // check if this object was spawned
                if (ourObject.TestObjectType(PlatformerData.SpawnedObjectType))
                {
                    // check for a spawned object component and notify it that we don't want it to respawn
                    CheckpointSystemSpawnedObjectComponent spawned = ourObject.Components.FindComponent<CheckpointSystemSpawnedObjectComponent>();

                    if (spawned != null)
                        spawned.Recover = false;
                }

                // play the checkpoint reached sound
                SoundManager.Instance.PlaySound("general", "checkpoint_reached");

                /// TODO: play animation ////////////////////////////////////////////////////////////////////////////////////

                // tell the checkpoint manager that a checkpoint was reached
                CheckpointManager.Instance.CheckpointReached();

                // set the new respawn position of the actor
                if (SceneObject != null)
                    actor.RespawnPosition = SceneObject.Position;
                else
                    actor.RespawnPosition = actor.Actor.Position;

                // true = yes, i was picked up. delete me!
                return true;
            }

            // false = no, this guy didn't pick me up.
            return false;
        }

        #endregion
    }
}


Thanks.

#1
01/02/2009 (1:03 am)
Ok I was a little confused and going about this all backwards. got it working and it was fairly simple.

If anyone is interested here is the class


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

using GarageGames.Torque.Core;
using GarageGames.Torque.T2D;

using GarageGames.Torque.PlatformerFramework;
using GarageGames.Torque.GameUtil;

namespace GarageGames.Torque.PlatformerDemo
{
    /// <summary>
    /// This component is an example of how to use the CollectibleComponent to create a Collectible that is used to 
    /// notify the checkpoint manager that a checkpoint has drilln reached (as an alternative to checkpoint triggers).
    /// </summary>
    [TorqueXmlSchemaType]
    class CheckpointCollectibleComponent : CollectibleComponent
    {

        #region Public properties, operators, constants, and enums

        public T2DAnimationData TriggerAnim
        {
            get { return _triggerAnim; }
            set { _triggerAnim = value; }
        }

        #endregion

        //======================================================
        #region Private, protected, internal fields

        protected T2DAnimationData _triggerAnim;

        #endregion

        //======================================================
        #region Private, protected, internal methods

        protected override bool _confirmPickup(T2DSceneObject ourObject, T2DSceneObject theirObject, ActorComponent actor)
        {
            // try to add this pepper to the Dragon's inventory
            if (actor is PlayerDragonActorComponent)
            {
                // check if this object was spawned
                if (ourObject.TestObjectType(PlatformerData.SpawnedObjectType))
                {
                    // check for a spawned object component and notify it that we don't want it to respawn
                    CheckpointSystemSpawnedObjectComponent spawned = ourObject.Components.FindComponent<CheckpointSystemSpawnedObjectComponent>();

                    if (spawned != null)
                        spawned.Recover = false;
                }

                // play the checkpoint reached sound
                SoundManager.Instance.PlaySound("general", "checkpoint_reached");

                /// play animation
                T2DAnimatedSprite sprite = ourObject as T2DAnimatedSprite;

                if (sprite != null)
                    sprite.PlayAnimation(_triggerAnim); 

                // tell the checkpoint manager that a checkpoint was reached
                CheckpointManager.Instance.CheckpointReached();

                // set the new respawn position of the actor
                if (SceneObject != null)
                    actor.RespawnPosition = SceneObject.Position;
                else
                    actor.RespawnPosition = actor.Actor.Position;

                // true = yes, i was picked up. delete me!
                return true;
            }

            // false = no, this guy didn't pick me up.
            return false;
        }

        protected override void _onEnter(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info)
        {

            ActorComponent actor = theirObject.Components.FindComponent<ActorComponent>();

            if (actor == null || ourObject == null)
                return;

            if (_confirmPickup(ourObject, theirObject, actor))
            {
                ourObject.Visible = true;
                ourObject.CollisionsEnabled = false;

                if (actor.Controller != null)
                    (actor.Controller as ActorController).ActorCollectedItem(actor, this);
            }
        }

        #endregion
    }
}