// Looks for a decoration of given role starting from given decorator and then through its decorated objects. public static EffectDecorator getRole(EffectDecorator startingDecorator, string role) { if (startingDecorator.roleName == role) { return(startingDecorator); } EffectDecorator parent = recursiveSearch(startingDecorator, role); return(parent == null ? null : (EffectDecorator)parent.fighter); }
private float applyImmunity(float incomingDamage) { EffectDecorator godMode = getRole(this, "godmode"); if (godMode != null && godMode is InvulnerabilityDecorator) { ((InvulnerabilityDecorator)godMode).nullifyHit(incomingDamage); Console.WriteLine("Target receives 0 damage"); return(0); } return(incomingDamage); }
virtual public float receiveDamage(float baseDamage) { EffectDecorator godMode = getRole(this, "godmode"); if (godMode != null && godMode is InvulnerabilityDecorator) { ((InvulnerabilityDecorator)godMode).nullifyHit(baseDamage); Console.WriteLine("Target receives 0 damage"); return(0); } return(fighter.receiveDamage(baseDamage)); }
// Removes the first decorator (that is not given decorator) of a given role. public static EffectDecorator removeRole(EffectDecorator eff, string role) { EffectDecorator parent = recursiveSearch(eff, role); if (parent == null) { return(null); } EffectDecorator toRemove = (EffectDecorator)parent.fighter; IFightableObject afterDeleted = toRemove.fighter; parent.fighter = afterDeleted; return(toRemove); }
//Return the parent of sought role. If decorator of such role was not found, returns null. private static EffectDecorator recursiveSearch(EffectDecorator parent, string role) { if (parent.fighter == null || parent.fighter.GetType() != typeof(EffectDecorator)) { return(null); } EffectDecorator nextDecorator = ((EffectDecorator)(parent.fighter)); if (nextDecorator.roleName == role) { return(parent); } return(recursiveSearch(nextDecorator, role)); }
// Removed EffectDecorators lose their last reference and should be collected by the garbage collector. static void removeExpiredEffects(EffectDecorator start) { EffectDecorator currentDecorator = start, decorated; while (!currentDecorator.fighter.GetType().IsSubclassOf(typeof(Fightable))) { decorated = (EffectDecorator)currentDecorator.fighter; if (decorated.duration > 0) { currentDecorator = decorated; } else { currentDecorator.fighter = decorated.fighter; } } }
private void applyOverTimeEffects(EffectDecorator startingEffect) { EffectDecorator currentEffect = startingEffect; OverTimeEffectDecorator overTimeEffect; while (currentEffect != null) { overTimeEffect = (OverTimeEffectDecorator)getRole(currentEffect, overTimeEffectRoleName); if (overTimeEffect == null) { break; } overTimeEffect.activate(); if (overTimeEffect.fighter.GetType() != typeof(EffectDecorator)) { break; } currentEffect = (EffectDecorator)overTimeEffect.fighter; } }
static void Main(string[] args) { IFightableObject player = new Player(100f, 3f, 5f), mob = new NPC(40f, 2f, 3f); // Over time effect role name: string oteRoleName = EffectDecorator.overTimeEffectRoleName; EffectDecorator playerBuffs = new EffectDecorator( new DamageBuffDecorator( new DefenceBuffDecorator(player, 1, 0.2f), 3, 1.4f), 9999), mobBuffs = new EffectDecorator( new DamageBuffDecorator(mob, 3, 5f), 9999); IFightableObject buffedPlayer = playerBuffs, buffedMob = mobBuffs; // turn 1: buffedPlayer.dealDamage(buffedMob); buffedMob.dealDamage(buffedPlayer); endTurn(buffedMob, buffedPlayer); // turn 2: Console.WriteLine("Player uses a healing skill on himself"); EffectDecorator.addEffect(new HotDecorator(null, 3, 10f, oteRoleName), playerBuffs); Console.WriteLine("Mob causes the player to bleed"); EffectDecorator.addEffect(new DotDecorator(null, 3, 15f, oteRoleName), playerBuffs); endTurn(buffedMob, buffedPlayer); // turn 3: Console.WriteLine("Player becomes invulnerable for 1 turn"); EffectDecorator.addEffect(new InvulnerabilityDecorator(null, 1, 5, 10, "godmode"), playerBuffs); buffedMob.dealDamage(buffedPlayer); endTurn(buffedMob, buffedPlayer); Console.Read(); }
// Adds a new effect to the entity. The new effect decorator will be decorated by the second argument. public static void addEffect(EffectDecorator toAdd, EffectDecorator decoratesAdded) { toAdd.fighter = decoratesAdded.fighter; decoratesAdded.fighter = toAdd; }