Original Source
Hey guys. My name is Chris. I'm a lead programmer on the Asylum project. We're currently using Realmware's engine to make a full scale game. Since diving into the available code, we decided to write up some programming tutorials to help get people started and to find some workarounds for some common problems. Our goal with these tutorials is for any programmer to be able to follow along with minimal effort. If you have any questions or comment, feel free to let us know!
This tutorial is for Beta 3.2.
-Chris
------------------------------------------------------------------------------------------------------------------------------
This tutorial was tested in the Black Skull Camp Tech Demo with the casual human male.
To get an avatar to melee attack with an animation the .cs file will need the additional usings:
using Visual3D.Commands;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
First we need to set up the attack animation within the OnConstructPrototype() method:
AnimationSet.ConfigureAnimations(
new Animation(AvatarCommands.AnimationFireWeapon, "AnimationName", animSpeed, null, true)
);
Replace "AnimationName" with the name of the attack animation. If you are going through this tutorial using the casual human male avatar as well, you'll notice that the animation will have to be defined in a method DefineCustomAnimations(), which is called in the OnConstructPrototype() method of it's parent class, CasualHumanAvatar. You should also comment out the Action1 animation.
Next we add in some new commands also in OnConstructPrototype():
CommandSpec newChildren = new CommandSpec(AvatarCommands.LaunchMissile, new MethodCommand(Attack, null));
AddCommand(newChildren);
ReplaceCommand(new AutoFirePlusAnimationActivity(AvatarCommands.Action1, "AnimationFireWeapon", Attack));
The last thing to do is to make the Attack() method, this one is pretty much ripped straight from what they have in the Lizardman class:
public void Attack(object args)
{
float maxDist2 = range * range;
foreach (Avatar a in Avatars)
{
if (a == this)
continue;
Vector3 targetOffset = a.WorldPosition - WorldPosition;
float targetDist2 = targetOffset.LengthSquared();
if (targetDist2 <= maxDist2)
{
Quaternion thisOrientation = WorldOrientation;
Quaternion targetOrientation = a.WorldOrientation;
float angle = Quats.Angle(ref thisOrientation, ref targetOrientation);
if (angle < 315f || angle > 45f)
_currentAttackTargets.Add(a);
}
}
foreach (Avatar target in _currentAttackTargets)
{
target.OnDamage(new Combat.DamagedEvent(this, target, 10));
}
_currentAttackTargets.Clear();
}
The range is a float that can be set in the OnPrepareForUse() method, again I ripped this straight from the lizardman:
Spatial.SetLocalBoundingBox(Spatial.LocalBoundingBox.Min / 2, Spatial.LocalBoundingBox.Max);
range = Spatial.BoundingRadius * 2f;
The damage done is in that last foreach statement in the Attack() method, it's the number 10. It'd probably be best to set up an int in the OnPrepareForUse() method too, but I was lazy and didn't do that.
-Tashi Duane, Asylum Project