I'm stuck with event implementation in C# code. I have class Characters
, which contains Name, HP, DMG and DEF. Then I have few random enemies and one hero based on Characters
class.
Next I have method, what is picking random attacker and random defender. That means - I have Hero, Dragon, and Wolf. The method will pick random character, let's say the Hero and mark him as "defender". Then it will randomly pick between Dragon and Wolf and mark it as "attacker". And I want to create an event which will be triggered inside this method and it will show which characters are against. I.e. "Hero has chosen the Dragon as his opponent".
I've tried it like
public delegate void ChooseOpponentEventHandler(object source, EventArgs args);
public event ChooseOpponentEventHanlder ChooseEvent;
//Method for choosing random enemy
abstract public bool ChooseAttacker(string Attacker, string Defender);
virtual public bool ChooseOpponent(string Attacker, string Defender, int OppNum, int AttNum)
{
if(ChooseAttacker(Attacker, Defender) == true && AttNum != OppNum
{
return true;
}
else return false;
}
protected virtual void OnChooseOpponent()
{
if (ChooseEvent != null)
ChooseEvent(this, Event.Args.Empty);
}
I have no idea how to put the method OnChooseOpponent()
inside the ChooseOpponent()
method and also how to make the Method say
Console.WriteLine(Attacker.Name + "has chosen" + Defender.Name + " as his opponent")
or something like that, because the Method OnChooseOpponent
doesn't know, who Attacker or Defender is.