From 1a9310d3683d9b6d82647162e1deab23cc50f274 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 24 Oct 2024 11:23:46 -0400 Subject: [PATCH] v3.0.0: refactored cleric spells to better support variable functions --- .../actors/hero/spells/ClericSpell.java | 49 ++++-------- .../actors/hero/spells/GuidingLight.java | 11 +-- .../actors/hero/spells/HolyWard.java | 4 +- .../actors/hero/spells/HolyWeapon.java | 4 +- .../hero/spells/InventoryClericSpell.java | 74 +++++++++++++++++++ .../hero/spells/TargetedClericSpell.java | 53 +++++++++++++ .../windows/WndClericSpells.java | 14 +--- 7 files changed, 151 insertions(+), 58 deletions(-) create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/InventoryClericSpell.java create mode 100644 core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/TargetedClericSpell.java diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/ClericSpell.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/ClericSpell.java index 096ab6835..7d959f6f5 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/ClericSpell.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/ClericSpell.java @@ -21,54 +21,21 @@ package com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells; -import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.HolyTome; -import com.shatteredpixel.shatteredpixeldungeon.mechanics.Ballistica; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; -import com.shatteredpixel.shatteredpixeldungeon.scenes.CellSelector; -import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; import com.shatteredpixel.shatteredpixeldungeon.ui.HeroIcon; +import java.util.ArrayList; + public abstract class ClericSpell { - public void use(HolyTome tome, Hero hero ){ - if (targetingPrompt() == null){ - activate(tome, hero, hero.pos); - } else { - GameScene.selectCell(new CellSelector.Listener() { - @Override - public void onSelect(Integer cell) { - activate(tome, hero, cell); - } - - @Override - public String prompt() { - return targetingPrompt(); - } - }); - } - } - - //leave null for no targeting - public String targetingPrompt(){ - return null; - } - - public boolean useTargeting(){ - return targetingPrompt() != null; - } - - public int targetedPos(Char user, int dst ){ - return new Ballistica( user.pos, dst, Ballistica.PROJECTILE ).collisionPos; - } + public abstract void onCast(HolyTome tome, Hero hero ); public float chargeUse( Hero hero ){ return 1; } - protected abstract void activate( HolyTome tome, Hero hero, Integer target ); - public String name(){ return Messages.get(this, "name"); } @@ -85,4 +52,14 @@ public abstract class ClericSpell { return HeroIcon.NONE; } + public static ArrayList getSpellList(Hero cleric){ + ArrayList spells = new ArrayList<>(); + + spells.add(GuidingLight.INSTANCE); + spells.add(HolyWeapon.INSTANCE); + spells.add(HolyWard.INSTANCE); + + return spells; + }; + } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/GuidingLight.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/GuidingLight.java index 7ad22b94c..cca7cca46 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/GuidingLight.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/GuidingLight.java @@ -38,9 +38,9 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Callback; import com.watabou.utils.Random; -public class GuidingLight extends ClericSpell { +public class GuidingLight extends TargetedClericSpell { - public static GuidingLight INSTANCE = new GuidingLight(); + public static final GuidingLight INSTANCE = new GuidingLight(); @Override public int icon() { @@ -48,12 +48,7 @@ public class GuidingLight extends ClericSpell { } @Override - public String targetingPrompt() { - return Messages.get(this, "prompt"); - } - - @Override - protected void activate(HolyTome tome, Hero hero, Integer target) { + protected void onTargetSelected(HolyTome tome, Hero hero, Integer target) { if (target == null){ return; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWard.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWard.java index 65ae94bd6..74b865984 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWard.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWard.java @@ -33,7 +33,7 @@ import com.watabou.noosa.audio.Sample; public class HolyWard extends ClericSpell { - public static HolyWard INSTANCE = new HolyWard(); + public static final HolyWard INSTANCE = new HolyWard(); @Override public int icon() { @@ -41,7 +41,7 @@ public class HolyWard extends ClericSpell { } @Override - protected void activate(HolyTome tome, Hero hero, Integer target) { + public void onCast(HolyTome tome, Hero hero) { Buff.affect(hero, HolyArmBuff.class, 50f); Item.updateQuickslot(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWeapon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWeapon.java index 13a89e7be..e8fc34c6f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWeapon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/HolyWeapon.java @@ -33,7 +33,7 @@ import com.watabou.noosa.audio.Sample; public class HolyWeapon extends ClericSpell { - public static HolyWeapon INSTANCE = new HolyWeapon(); + public static final HolyWeapon INSTANCE = new HolyWeapon(); @Override public int icon() { @@ -46,7 +46,7 @@ public class HolyWeapon extends ClericSpell { } @Override - protected void activate(HolyTome tome, Hero hero, Integer target) { + public void onCast(HolyTome tome, Hero hero) { Buff.affect(hero, HolyWepBuff.class, 50f); Item.updateQuickslot(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/InventoryClericSpell.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/InventoryClericSpell.java new file mode 100644 index 000000000..d5cd54347 --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/InventoryClericSpell.java @@ -0,0 +1,74 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2024 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells; + +import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; +import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.HolyTome; +import com.shatteredpixel.shatteredpixeldungeon.items.bags.Bag; +import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; +import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag; + +public abstract class InventoryClericSpell extends ClericSpell { + + @Override + public void onCast(HolyTome tome, Hero hero) { + GameScene.selectItem(new WndBag.ItemSelector() { + + @Override + public String textPrompt() { + return inventoryPrompt(); + } + + @Override + public Class preferredBag() { + return InventoryClericSpell.this.preferredBag(); + } + + @Override + public boolean itemSelectable(Item item) { + return usableOnItem(item); + } + + @Override + public void onSelect(Item item) { + onItemSelected(tome, hero, item); + } + }); + } + + protected String inventoryPrompt(){ + return Messages.get(this, "prompt"); + } + + protected Class preferredBag() { + return null; //defaults to no preference + } + + protected boolean usableOnItem( Item item ){ + return true; + } + + protected abstract void onItemSelected( HolyTome tome, Hero hero, Item item ); + +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/TargetedClericSpell.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/TargetedClericSpell.java new file mode 100644 index 000000000..2186462a2 --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/spells/TargetedClericSpell.java @@ -0,0 +1,53 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2024 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells; + +import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; +import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.HolyTome; +import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; +import com.shatteredpixel.shatteredpixeldungeon.scenes.CellSelector; +import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; + +public abstract class TargetedClericSpell extends ClericSpell { + + @Override + public void onCast(HolyTome tome, Hero hero ){ + GameScene.selectCell(new CellSelector.Listener() { + @Override + public void onSelect(Integer cell) { + onTargetSelected(tome, hero, cell); + } + + @Override + public String prompt() { + return targetingPrompt(); + } + }); + } + + protected String targetingPrompt(){ + return Messages.get(this, "prompt"); + } + + protected abstract void onTargetSelected(HolyTome tome, Hero hero, Integer target); + +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndClericSpells.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndClericSpells.java index bb173aafc..4db0254d2 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndClericSpells.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndClericSpells.java @@ -25,9 +25,7 @@ import com.shatteredpixel.shatteredpixeldungeon.Chrome; import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells.ClericSpell; -import com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells.GuidingLight; -import com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells.HolyWard; -import com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells.HolyWeapon; +import com.shatteredpixel.shatteredpixeldungeon.actors.hero.spells.TargetedClericSpell; import com.shatteredpixel.shatteredpixeldungeon.items.artifacts.HolyTome; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; @@ -72,11 +70,7 @@ public class WndClericSpells extends Window { msg.setPos(0, title.bottom()+4); add(msg); - //TODO build spell list - ArrayList spells = new ArrayList<>(); - spells.add(GuidingLight.INSTANCE); - spells.add(HolyWeapon.INSTANCE); - spells.add(HolyWard.INSTANCE); + ArrayList spells = ClericSpell.getSpellList(cleric); ArrayList spellBtns = new ArrayList<>(); @@ -139,10 +133,10 @@ public class WndClericSpells extends Window { GameScene.show(new WndTitledMessage(new HeroIcon(spell), Messages.titleCase(spell.name()), spell.desc())); } else { hide(); - spell.use(tome, Dungeon.hero); + spell.onCast(tome, Dungeon.hero); //TODO, probably need targeting logic here - if (spell.useTargeting() && Dungeon.quickslot.contains(tome)){ + if (spell instanceof TargetedClericSpell && Dungeon.quickslot.contains(tome)){ QuickSlotButton.useTargeting(Dungeon.quickslot.getSlot(tome)); } }