-
-
Notifications
You must be signed in to change notification settings - Fork 10
behaviour
mtanksl edited this page Aug 22, 2023
·
10 revisions
Every game object (item, monster, npc and player) can have some Behaviour attached to it. Behaviour is like Unity's Component.
Let the monster talk something every 30 seconds (approximately).
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;
DateTime lastTalk = DateTime.MinValue;
globalTick = Context.Server.EventHandlers.Subscribe<GlobalTickEventArgs>( (context, e) =>
{
if (DateTime.UtcNow > lastTalk)
{
lastTalk = DateTime.UtcNow.Add(TimeSpan.FromSeconds(30) );
return Context.AddCommand(new ShowTextCommand(creature, talkType, Context.Server.Randomization.Take(sentences) ) );
}
return Promise.Completed;
} );
}
public override void Stop()
{
Context.Server.EventHandlers.Unsubscribe<GlobalTickEventArgs>(globalTick);
}
}