Game Development Community

How to kill an enemy with a custom state

by Ricardo Bencz · in Torque X Platformer Kit · 05/09/2010 (3:48 pm) · 4 replies

Hello guys.

I have created an attack state for my player (in a new project demo, the basic), and it works nice. With the "X" button, it plays the "climb" animation.

What I want is just to kill an enemy when it colides with the player when the player is in attack state.

What's the best way guys?

Thanks for all.

Regards.

#1
05/16/2010 (4:05 pm)
Easiest way would probably be to write a custom collision delegate and check what state the player is in when the collision happens.

and if its in the attack state you could then apply dammage to the enemy.
#2
05/30/2010 (12:21 pm)
You can also mount a "sword" to your player or rather, just update a sword position to your player position since mount physics aren't implemented. The benefit would be a larger hit radius, which would probably exceed the hit radius for the player, which would allow your player to attack and avoid taking damage easier. Also, badguy's which are actively looking to collide with a player, would have to get through the sword first... meaning your player could kill the badguy before he took damage.
#3
05/30/2010 (8:38 pm)
I decided to show off this sword component, since it was easy to write, and this forums is meant to help people. Note, make sure your actor does not collide with all objects, or he'll also collide with his own sword.

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

using Microsoft.Xna.Framework;

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

using GarageGames.Torque.PlatformerFramework;

namespace GarageGames.Torque.PlatformerDemo
{
    [TorqueXmlSchemaType]
    public class swordComponent : TorqueComponent, ITickObject
    {
        //======================================================
        #region Static methods, fields, constructors
        #endregion

        //======================================================
        #region Constructors
        #endregion

        //======================================================
        #region Public properties, operators, constants, and enums

        public T2DSceneObject SceneObject
        {
            get { return Owner as T2DSceneObject; }
        }
        [TorqueXmlSchemaType]
        public static T2DOnCollisionDelegate OnCollisionDelegate
        {
            get { return _onCollisionDelegate; }
            set { _onCollisionDelegate = value; }
        }

        [TorqueXmlSchemaType(DefaultValue = "100")]
        public float AttackValue
        {
            get { return _attackValue; }
            set { _attackValue = value; }
        }

            
        [TorqueXmlSchemaType]
        public  T2DSceneObject OwnerPlayer
        {
            get { return _ownerPlayer; }
            set { _ownerPlayer = value; }
        }

        public ActorComponent OwnerActorCompenent
        {
            get { return _ownerActorComponent; }
            set { _ownerActorComponent = value; }
        }



        public static void OnCollision(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolve, ref T2DCollisionMaterial physicsMaterial)
        {
            ActorComponent thierActor = theirObject.Components.FindComponent<ActorComponent>();
            swordComponent ourSword = ourObject.Components.FindComponent<swordComponent>();

            if (thierActor != null && ourSword != null)
            {
                if (ourSword.OwnerActorCompenent.Attacking != true)
                    return;
                thierActor.TakeDamage(ourSword.AttackValue, ourObject);
            }
        } 

        #endregion

        //======================================================
        #region Public methods

        public virtual void ProcessTick(Move move, float dt)
        {
            _sceneObject.Position = _ownerPlayer.Position;
        }

        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;
            _sceneObject = owner as T2DSceneObject;
            _sceneObject.Collision.CollidesWith += PlatformerData.EnemyObjectType;
            ProcessList.Instance.AddTickCallback(owner, this);
            _ownerActorComponent = _ownerPlayer.Components.FindComponent<ActorComponent>();
            return true;
        }
        protected override void _PostRegister()
        {
            base._PostRegister();
            SceneObject.Visible = false;
        }
        protected override void _OnUnregister()
        {
            base._OnUnregister();
        }

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


        #endregion

        //======================================================
        #region Private, protected, internal fields
        static T2DOnCollisionDelegate _onCollisionDelegate = OnCollision;
        T2DSceneObject _sceneObject;
        T2DSceneObject _ownerPlayer;
        ActorComponent _ownerActorComponent;
        private float _attackValue = 100;
        #endregion
    }
}
#4
06/01/2010 (4:35 pm)
Thanks a lot Will and Scott.

I will try your suggestion Will and I'll post here if it works or not, anywhere, thank you for the code.

Best regards.