v0.3.0: BIG refactor to buff display, more clear and extendable code, instead of a nest of instanceof statements

This commit is contained in:
Evan Debenham 2015-04-29 01:55:41 -04:00
parent 9fad20a071
commit 6a7c42634a
29 changed files with 205 additions and 120 deletions

View File

@ -348,93 +348,26 @@ public abstract class Char extends Actor {
buffs.add( buff );
Actor.add( buff );
if (sprite != null) {
if (buff instanceof Poison) {
CellEmitter.center( pos ).burst( PoisonParticle.SPLASH, 5 );
sprite.showStatus( CharSprite.NEGATIVE, "poisoned" );
} else if (buff instanceof Amok) {
sprite.showStatus( CharSprite.NEGATIVE, "amok" );
} else if (buff instanceof Slow) {
sprite.showStatus( CharSprite.NEGATIVE, "slowed" );
} else if (buff instanceof Chill) {
sprite.showStatus( CharSprite.NEGATIVE, "chilled" );
sprite.add( CharSprite.State.CHILLED );
} else if (buff instanceof MindVision) {
sprite.showStatus( CharSprite.POSITIVE, "mind" );
sprite.showStatus( CharSprite.POSITIVE, "vision" );
} else if (buff instanceof Paralysis) {
sprite.add( CharSprite.State.PARALYSED );
sprite.showStatus( CharSprite.NEGATIVE, "paralysed" );
} else if (buff instanceof Terror) {
sprite.showStatus( CharSprite.NEGATIVE, "frightened" );
} else if (buff instanceof Roots) {
sprite.showStatus( CharSprite.NEGATIVE, "rooted" );
} else if (buff instanceof Cripple) {
sprite.showStatus( CharSprite.NEGATIVE, "crippled" );
} else if (buff instanceof Bleeding) {
sprite.showStatus( CharSprite.NEGATIVE, "bleeding" );
} else if (buff instanceof Vertigo) {
sprite.showStatus( CharSprite.NEGATIVE, "dizzy" );
} else if (buff instanceof Sleep) {
sprite.idle();
if (sprite != null)
switch(buff.type){
case POSITIVE:
sprite.showStatus(CharSprite.POSITIVE, buff.toString()); break;
case NEGATIVE:
sprite.showStatus(CharSprite.NEGATIVE, buff.toString());break;
case NEUTRAL:
sprite.showStatus(CharSprite.NEUTRAL, buff.toString()); break;
case SILENT: default:
break; //show nothing
}
else if (buff instanceof Burning) {
sprite.add( CharSprite.State.BURNING );
} else if (buff instanceof Levitation) {
sprite.add( CharSprite.State.LEVITATING );
} else if (buff instanceof Frost) {
sprite.add( CharSprite.State.FROZEN );
} else if (buff instanceof Invisibility || buff instanceof CloakOfShadows.cloakStealth) {
if (!(buff instanceof Shadows)) {
sprite.showStatus( CharSprite.POSITIVE, "invisible" );
}
sprite.add( CharSprite.State.INVISIBLE );
}
}
}
public void remove( Buff buff ) {
buffs.remove( buff );
Actor.remove( buff );
if (buff instanceof Burning) {
sprite.remove( CharSprite.State.BURNING );
} else if (buff instanceof Levitation) {
sprite.remove( CharSprite.State.LEVITATING );
} else if ((buff instanceof Invisibility || buff instanceof CloakOfShadows.cloakStealth) && invisible <= 0) {
sprite.remove( CharSprite.State.INVISIBLE );
} else if (buff instanceof Paralysis) {
sprite.remove( CharSprite.State.PARALYSED );
} else if (buff instanceof Frost) {
sprite.remove( CharSprite.State.FROZEN );
} else if (buff instanceof Chill) {
sprite.remove( CharSprite.State.CHILLED );
}
}
public void remove( Class<? extends Buff> buffClass ) {
@ -452,21 +385,7 @@ public abstract class Char extends Actor {
public void updateSpriteState() {
for (Buff buff:buffs) {
if (buff instanceof Burning) {
sprite.add( CharSprite.State.BURNING );
} else if (buff instanceof Levitation) {
sprite.add( CharSprite.State.LEVITATING );
} else if (buff instanceof Invisibility || buff instanceof CloakOfShadows.cloakStealth) {
sprite.add( CharSprite.State.INVISIBLE );
} else if (buff instanceof Paralysis) {
sprite.add( CharSprite.State.PARALYSED );
} else if (buff instanceof Frost) {
sprite.add( CharSprite.State.FROZEN );
} else if (buff instanceof Light) {
sprite.add( CharSprite.State.ILLUMINATED );
} else if (buff instanceof Chill) {
sprite.add( CharSprite.State.CHILLED );
}
buff.fx( true );
}
}

View File

@ -21,6 +21,10 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Amok extends FlavourBuff {
{
type = buffType.NEGATIVE;
}
@Override
public int icon() {

View File

@ -28,6 +28,10 @@ import com.watabou.utils.PointF;
import com.watabou.utils.Random;
public class Bleeding extends Buff {
{
type = buffType.NEGATIVE;
}
protected int level;

View File

@ -21,6 +21,10 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Blindness extends FlavourBuff {
{
type = buffType.NEGATIVE;
}
@Override
public void detach() {

View File

@ -27,6 +27,11 @@ public class Buff extends Actor {
public Char target;
//determines how the buff is announced when it is shown.
//buffs that work behind the scenes, or have other visual indicators can usually be silent.
public enum buffType {POSITIVE, NEGATIVE, NEUTRAL, SILENT};
public buffType type = buffType.SILENT;
public HashSet<Class<?>> resistances = new HashSet<Class<?>>();
public HashSet<Class<?>> immunities = new HashSet<Class<?>>();
@ -40,10 +45,15 @@ public class Buff extends Actor {
this.target = target;
target.add( this );
return target.buffs().contains(this);
if (target.buffs().contains(this)){
if (target.sprite != null) fx( true );
return true;
} else
return false;
}
public void detach() {
fx( false );
target.remove( this );
}
@ -57,6 +67,14 @@ public class Buff extends Actor {
return BuffIndicator.NONE;
}
public void fx(boolean on) {
//do nothing by default
};
public String desc(){
return "";
}
public static<T extends Buff> T append( Char target, Class<T> buffClass ) {
try {
T buff = buffClass.newInstance();
@ -103,8 +121,4 @@ public class Buff extends Actor {
public static void detach( Char target, Class<? extends Buff> cl ) {
detach( target.buff( cl ) );
}
public String desc(){
return "";
}
}

View File

@ -34,6 +34,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.rings.RingOfElements.Resis
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll;
import com.shatteredpixel.shatteredpixeldungeon.levels.Level;
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.watabou.utils.Bundle;
@ -49,6 +50,10 @@ public class Burning extends Buff implements Hero.Doom {
private float left;
private static final String LEFT = "left";
{
type = buffType.NEGATIVE;
}
@Override
public void storeInBundle( Bundle bundle ) {
@ -133,7 +138,13 @@ public class Burning extends Buff implements Hero.Doom {
public int icon() {
return BuffIndicator.FIRE;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.BURNING);
else target.sprite.remove(CharSprite.State.BURNING);
}
@Override
public String toString() {
return "Burning";

View File

@ -28,6 +28,10 @@ public class Charm extends FlavourBuff {
private static final String OBJECT = "object";
{
type = buffType.NEGATIVE;
}
@Override
public void storeInBundle( Bundle bundle ) {
super.storeInBundle( bundle );

View File

@ -8,6 +8,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.food.FrozenCarpaccio;
import com.shatteredpixel.shatteredpixeldungeon.items.food.MysteryMeat;
import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.watabou.utils.Random;
@ -19,6 +20,10 @@ public class Chill extends FlavourBuff {
private static final String TXT_FREEZES = "%s freezes!";
{
type = buffType.NEGATIVE;
}
@Override
public boolean attachTo(Char target) {
//can't chill what's frozen!
@ -70,6 +75,12 @@ public class Chill extends FlavourBuff {
return BuffIndicator.FROST;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.CHILLED);
else target.sprite.remove(CharSprite.State.CHILLED);
}
@Override
public String toString() {
return "Chilled";

View File

@ -22,6 +22,10 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Cripple extends FlavourBuff {
public static final float DURATION = 10f;
{
type = buffType.NEGATIVE;
}
@Override
public int icon() {

View File

@ -23,6 +23,10 @@ import com.watabou.utils.Random;
public class Drowsy extends Buff {
{
type = buffType.NEUTRAL;
}
@Override
public int icon() {
return BuffIndicator.DROWSY;

View File

@ -27,6 +27,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.food.MysteryMeat;
import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion;
import com.shatteredpixel.shatteredpixeldungeon.items.rings.RingOfElements.Resistance;
import com.shatteredpixel.shatteredpixeldungeon.levels.Level;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
@ -35,6 +36,10 @@ public class Frost extends FlavourBuff {
private static final String TXT_FREEZES = "%s freezes!";
private static final float DURATION = 5f;
{
type = buffType.NEGATIVE;
}
@Override
public boolean attachTo( Char target ) {
@ -91,7 +96,13 @@ public class Frost extends FlavourBuff {
public int icon() {
return BuffIndicator.FROST;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.FROZEN);
else target.sprite.remove(CharSprite.State.FROZEN);
}
@Override
public String toString() {
return "Frozen";

View File

@ -22,6 +22,10 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Fury extends Buff {
public static float LEVEL = 0.4f;
{
type = buffType.POSITIVE;
}
@Override
public boolean act() {
@ -41,6 +45,6 @@ public class Fury extends Buff {
@Override
public String toString() {
return "Fury";
return "Furious";
}
}

View File

@ -21,11 +21,16 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.CloakOfShadows;
import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.TimekeepersHourglass;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Invisibility extends FlavourBuff {
public static final float DURATION = 15f;
{
type = buffType.POSITIVE;
}
@Override
public boolean attachTo( Char target ) {
@ -48,7 +53,13 @@ public class Invisibility extends FlavourBuff {
public int icon() {
return BuffIndicator.INVISIBLE;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add( CharSprite.State.INVISIBLE );
else if (target.invisible == 0) target.sprite.remove( CharSprite.State.INVISIBLE );
}
@Override
public String toString() {
return "Invisible";

View File

@ -19,6 +19,7 @@ package com.shatteredpixel.shatteredpixeldungeon.actors.buffs;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Levitation extends FlavourBuff {
@ -47,7 +48,13 @@ public class Levitation extends FlavourBuff {
public int icon() {
return BuffIndicator.LEVITATION;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.LEVITATING);
else target.sprite.remove(CharSprite.State.LEVITATING);
}
@Override
public String toString() {
return "Levitating";

View File

@ -19,6 +19,7 @@ package com.shatteredpixel.shatteredpixeldungeon.actors.buffs;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Light extends FlavourBuff {
@ -50,7 +51,13 @@ public class Light extends FlavourBuff {
public int icon() {
return BuffIndicator.LIGHT;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.ILLUMINATED);
else target.sprite.remove(CharSprite.State.ILLUMINATED);
}
@Override
public String toString() {
return "Illuminated";

View File

@ -28,6 +28,10 @@ public class MagicalSleep extends Buff {
private static final float STEP = 1f;
public static final float SWS = 1.5f;
{
type = buffType.NEUTRAL;
}
@Override
public boolean attachTo( Char target ) {
if (super.attachTo( target ) && !target.immunities().contains(Sleep.class)) {

View File

@ -25,6 +25,10 @@ public class MindVision extends FlavourBuff {
public static final float DURATION = 20f;
public int distance = 2;
{
type = buffType.POSITIVE;
}
@Override
public int icon() {

View File

@ -28,6 +28,10 @@ import com.watabou.utils.Random;
public class Ooze extends Buff {
private static final String TXT_HERO_KILLED = "%s killed you...";
{
type = buffType.NEGATIVE;
}
@Override
public int icon() {

View File

@ -19,11 +19,16 @@ package com.shatteredpixel.shatteredpixeldungeon.actors.buffs;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.items.rings.RingOfElements.Resistance;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Paralysis extends FlavourBuff {
private static final float DURATION = 10f;
{
type = buffType.NEGATIVE;
}
@Override
public boolean attachTo( Char target ) {
@ -45,7 +50,13 @@ public class Paralysis extends FlavourBuff {
public int icon() {
return BuffIndicator.PARALYSIS;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add(CharSprite.State.PARALYSED);
else target.sprite.remove(CharSprite.State.PARALYSED);
}
@Override
public String toString() {
return "Paralysed";

View File

@ -22,10 +22,11 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.ResultDescriptions;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
import com.shatteredpixel.shatteredpixeldungeon.effects.CellEmitter;
import com.shatteredpixel.shatteredpixeldungeon.effects.particles.PoisonParticle;
import com.shatteredpixel.shatteredpixeldungeon.items.rings.RingOfElements.Resistance;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.shatteredpixel.shatteredpixeldungeon.utils.Utils;
import com.watabou.utils.Bundle;
public class Poison extends Buff implements Hero.Doom {
@ -33,6 +34,10 @@ public class Poison extends Buff implements Hero.Doom {
protected float left;
private static final String LEFT = "left";
{
type = buffType.NEGATIVE;
}
@Override
public void storeInBundle( Bundle bundle ) {
@ -60,7 +65,16 @@ public class Poison extends Buff implements Hero.Doom {
public String toString() {
return "Poisoned";
}
@Override
public boolean attachTo(Char target) {
if (super.attachTo(target)){
CellEmitter.center(target.pos).burst( PoisonParticle.SPLASH, 5 );
return true;
} else
return false;
}
@Override
public boolean act() {
if (target.isAlive()) {

View File

@ -21,6 +21,10 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Roots extends FlavourBuff {
{
type = buffType.NEGATIVE;
}
@Override
public boolean attachTo( Char target ) {

View File

@ -18,7 +18,12 @@
package com.shatteredpixel.shatteredpixeldungeon.actors.buffs;
public class Sleep extends FlavourBuff {
@Override
public void fx(boolean on) {
if (on) target.sprite.idle();
}
public static final float SWS = 1.5f;
}

View File

@ -23,6 +23,10 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
public class Slow extends FlavourBuff {
{
type = buffType.NEGATIVE;
}
private static final float DURATION = 10f;
@Override

View File

@ -29,6 +29,10 @@ public class Terror extends FlavourBuff {
private static final String OBJECT = "object";
{
type = buffType.NEGATIVE;
}
@Override
public void storeInBundle( Bundle bundle ) {
super.storeInBundle(bundle);
@ -48,7 +52,7 @@ public class Terror extends FlavourBuff {
@Override
public String toString() {
return "Terror";
return "Terrified";
}
public static void recover( Char target ) {

View File

@ -14,6 +14,10 @@ public class Venom extends Poison implements Hero.Doom {
private static final String DAMAGE = "damage";
{
type = buffType.NEGATIVE;
}
@Override
public void storeInBundle( Bundle bundle ) {
super.storeInBundle( bundle );

View File

@ -24,6 +24,10 @@ public class Vertigo extends FlavourBuff {
public static final float DURATION = 10f;
{
type = buffType.NEGATIVE;
}
@Override
public int icon() {
return BuffIndicator.VERTIGO;

View File

@ -26,6 +26,10 @@ public class Weakness extends FlavourBuff {
private static final float DURATION = 40f;
{
type = buffType.NEGATIVE;
}
@Override
public int icon() {
return BuffIndicator.WEAKNESS;

View File

@ -1158,7 +1158,7 @@ public class Hero extends Char {
return;
super.add( buff );
if (sprite != null) {
if (buff instanceof Burning) {
GLog.w( "You catch fire!" );
@ -1179,7 +1179,6 @@ public class Hero extends Char {
GLog.w( "You are blinded!" );
} else if (buff instanceof Fury) {
GLog.w( "You become furious!" );
sprite.showStatus( CharSprite.POSITIVE, "furious" );
} else if (buff instanceof Charm) {
GLog.w( "You are charmed!" );
} else if (buff instanceof Cripple) {
@ -1194,10 +1193,7 @@ public class Hero extends Char {
GLog.w("Everything is spinning around you!");
interrupt();
}
else if (buff instanceof Light) {
sprite.add( CharSprite.State.ILLUMINATED );
}
}
BuffIndicator.refreshHero();
@ -1207,9 +1203,7 @@ public class Hero extends Char {
public void remove( Buff buff ) {
super.remove( buff );
if (buff instanceof Light) {
sprite.remove( CharSprite.State.ILLUMINATED );
} else if (buff instanceof RingOfMight.Might){
if (buff instanceof RingOfMight.Might){
if (((RingOfMight.Might)buff).level > 0){
HT -= ((RingOfMight.Might) buff).level * 5;
HP = Math.min(HT, HP);

View File

@ -6,6 +6,7 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
@ -229,6 +230,12 @@ public class CloakOfShadows extends Artifact {
return true;
}
@Override
public void fx(boolean on) {
if (on) target.sprite.add( CharSprite.State.INVISIBLE );
else if (target.invisible == 0) target.sprite.remove( CharSprite.State.INVISIBLE );
}
@Override
public String toString() {
return "Cloaked";
@ -241,7 +248,6 @@ public class CloakOfShadows extends Artifact {
stealthed = false;
cooldown = 10 - (level / 3);
updateQuickslot();
super.detach();
}