From cc4866cb5beaad40ec9eb6bb2a06c7585667b985 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Wed, 13 Nov 2019 14:12:37 -0500 Subject: [PATCH] v0.8.0: tweaked buff find and detach logic to better handle inheritance --- .../shatteredpixeldungeon/actors/Char.java | 4 +++- .../shatteredpixeldungeon/actors/buffs/Berserk.java | 9 ++++++--- .../shatteredpixeldungeon/actors/buffs/Buff.java | 10 +++------- .../actors/buffs/Invisibility.java | 5 ++--- .../shatteredpixeldungeon/actors/hero/Hero.java | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java index d4d5890de..040f8e590 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Char.java @@ -489,6 +489,7 @@ public abstract class Char extends Actor { } @SuppressWarnings("unchecked") + //returns all buffs assignable from the given buff class public synchronized HashSet buffs( Class c ) { HashSet filtered = new HashSet<>(); for (Buff b : buffs) { @@ -500,9 +501,10 @@ public abstract class Char extends Actor { } @SuppressWarnings("unchecked") + //returns an instance of the specific buff class, if it exists. Not just assignable public synchronized T buff( Class c ) { for (Buff b : buffs) { - if (c.isInstance( b )) { + if (b.getClass() == c) { return (T)b; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java index 4aec36995..84435a130 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java @@ -79,12 +79,15 @@ public class Berserk extends Buff { if (berserking()){ ShieldBuff buff = target.buff(WarriorShield.class); if (target.HP <= 0) { + int dmg = 1 + (int)Math.ceil(target.shielding() * 0.1f); if (buff != null && buff.shielding() > 0) { - buff.absorbDamage(1 + (int)Math.ceil(target.shielding() * 0.1f)); + buff.absorbDamage(dmg); } else { //if there is no shield buff, or it is empty, then try to remove from other shielding buffs - buff = target.buff(ShieldBuff.class); - if (buff != null) buff.absorbDamage(1 + (int)Math.ceil(target.shielding() * 0.1f)); + for (ShieldBuff s : target.buffs(ShieldBuff.class)){ + dmg = s.absorbDamage(dmg); + if (dmg == 0) break; + } } if (target.shielding() <= 0) { target.die(this); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java index d4fedfdb1..7f5b3cc2b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java @@ -147,13 +147,9 @@ public class Buff extends Actor { return buff; } - public static void detach( Buff buff ) { - if (buff != null) { - buff.detach(); + public static void detach( Char target, Class cl ) { + for ( Buff b : target.buffs( cl )){ + b.detach(); } } - - public static void detach( Char target, Class cl ) { - detach( target.buff( cl ) ); - } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Invisibility.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Invisibility.java index 826f9cacc..df2dc6e78 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Invisibility.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Invisibility.java @@ -89,9 +89,8 @@ public class Invisibility extends FlavourBuff { } public static void dispel() { - Invisibility buff = Dungeon.hero.buff( Invisibility.class ); - if (buff != null) { - buff.detach(); + for ( Buff invis : Dungeon.hero.buffs( Invisibility.class )){ + invis.detach(); } CloakOfShadows.cloakStealth cloakBuff = Dungeon.hero.buff( CloakOfShadows.cloakStealth.class ); if (cloakBuff != null) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java index 129cde958..c27ed33e3 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java @@ -511,7 +511,7 @@ public class Hero extends Char { } checkVisibleMobs(); - if (buff(FlavourBuff.class) != null) { + if (!buffs(FlavourBuff.class).isEmpty()) { BuffIndicator.refreshHero(); }