behaviour - mtanksl/OpenTibia GitHub Wiki
Introduction
Every game object (item, monster, npc and player) can have some Behaviour
attached to it. Behaviour is like Unity's Component.
Example
Let the monster talk something every 30 seconds.
public class CreatureTalkBehaviour : Behaviour
{
private TalkType talkType;
private string[] sentences;
public CreatureTalkBehaviour(TalkType talkType, string[] sentences)
{
this.talkType = talkType;
this.sentences = sentences;
}
private Guid globalTick;
public override void Start()
{
Creature creature = (Creature)GameObject;
int ticks = 30000;
globalTick = Context.Server.EventHandlers.Subscribe(GlobalTickEventArgs.Instance(creature.Id), (context, e) =>
{
ticks -= e.Ticks;
if (ticks <= 0)
{
ticks += 30000;
return Context.AddCommand(new ShowTextCommand(creature, talkType, Context.Server.Randomization.Take(sentences) ) );
}
return Promise.Completed;
} );
}
public override void Stop()
{
Context.Server.EventHandlers.Unsubscribe(globalTick);
}
}