magic #11

Merged
masi0885 merged 4 commits from magic into main 2025-10-29 10:30:18 +01:00
9 changed files with 31 additions and 18 deletions
Showing only changes of commit bcfe476c67 - Show all commits

View File

@ -5,8 +5,8 @@ import Entity.Entity;
public class OffensiveDamageSpell extends Spell { public class OffensiveDamageSpell extends Spell {
private final int potency; private final int potency;
public OffensiveDamageSpell(String spellName, int cost, int potency) { public OffensiveDamageSpell(String spellName, int cost, int range, int potency) {
super(spellName, cost); super(spellName, cost, range);
this.potency = potency; this.potency = potency;
} }

View File

@ -6,8 +6,8 @@ public class OffensiveStatusSpell extends Spell {
private final String status; private final String status;
public OffensiveStatusSpell(String spellName, int cost, String status) { public OffensiveStatusSpell(String spellName, int cost, int range, String status) {
super(spellName, cost); super(spellName, cost, range);
this.status = status; this.status = status;
} }

View File

@ -4,11 +4,13 @@ import Entity.Entity;
public abstract class Spell { public abstract class Spell {
private final String spellName; private final String spellName;
private int cost; private final int cost;
private final int range;
public Spell(String spellName, int cost) { public Spell(String spellName, int cost, int range) {
this.spellName = spellName; this.spellName = spellName;
this.cost = cost; this.cost = cost;
this.range = range;
} }
public String getSpellName() { public String getSpellName() {
@ -19,6 +21,8 @@ public abstract class Spell {
return cost; return cost;
} }
public int getRange() {return range;}
public abstract void cast(Entity source, Entity target); public abstract void cast(Entity source, Entity target);
@Override @Override
@ -29,6 +33,7 @@ public abstract class Spell {
Spell spell = (Spell) o; Spell spell = (Spell) o;
if (cost != spell.cost) return false; if (cost != spell.cost) return false;
if (range != spell.range) return false;
return spellName.equals(spell.spellName); return spellName.equals(spell.spellName);
} }
@ -36,11 +41,12 @@ public abstract class Spell {
public int hashCode() { public int hashCode() {
int result = spellName.hashCode(); int result = spellName.hashCode();
result = 31 * result + cost; result = 31 * result + cost;
result = 31 * result + range;
return result; return result;
} }
@Override @Override
public String toString() { public String toString() {
return spellName + " (Cost: " + cost + ")"; return spellName + " (Cost: " + cost + ", Range: " + range + ")";
} }
} }

View File

@ -1,13 +1,20 @@
package Entity; package Entity;
import java.util.ArrayList;
import java.util.List;
public abstract class Entity implements HasPosition { public abstract class Entity implements HasPosition {
protected String name; protected String name;
protected Position position; protected Position position;
private static final List<Entity> entities = new ArrayList<Entity>();
public Entity(String name) { public Entity(String name) {
this.name = name; this.name = name;
entities.add(this);
} }
public static List<Entity> getEntities() {
return entities;
}
} }

View File

@ -22,7 +22,7 @@ public class Wizard extends Job {
public void learnSpell(Actor actor) { public void learnSpell(Actor actor) {
if(actor instanceof HasSpellBook a ) { if(actor instanceof HasSpellBook a ) {
OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 20); OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 1,20);
a.getSpellBook().add(defaultSpell); a.getSpellBook().add(defaultSpell);
} }
} }

View File

@ -32,7 +32,7 @@ public class InterestingTests {
player.setMana(50); player.setMana(50);
player.setHealth(100); player.setHealth(100);
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15); OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1,15);
player.getSpellBook().add(fireball); player.getSpellBook().add(fireball);
int initialMana = player.getMana(); int initialMana = player.getMana();
@ -51,7 +51,7 @@ public class InterestingTests {
player.setMana(10); player.setMana(10);
player.setHealth(100); player.setHealth(100);
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15); OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1,15);
player.getSpellBook().add(fireball); player.getSpellBook().add(fireball);
int initialMana = player.getMana(); int initialMana = player.getMana();
@ -70,7 +70,7 @@ public class InterestingTests {
player.setMana(50); player.setMana(50);
player.setHealth(100); player.setHealth(100);
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15); OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1, 15);
player.getSpellBook().add(fireball); player.getSpellBook().add(fireball);
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); ByteArrayOutputStream outContent = new ByteArrayOutputStream();
@ -95,7 +95,7 @@ public class InterestingTests {
Player player = new Player("Alfon", wizard); Player player = new Player("Alfon", wizard);
player.setMana(50); player.setMana(50);
OffensiveStatusSpell poisonSpray = new OffensiveStatusSpell("poisonSpray", 20, "Poison"); OffensiveStatusSpell poisonSpray = new OffensiveStatusSpell("poisonSpray", 20, 1, "Poison");
player.getSpellBook().add(poisonSpray); player.getSpellBook().add(poisonSpray);
wizard.castSpell(player); wizard.castSpell(player);

View File

@ -38,7 +38,7 @@ public class MagicSystemTest {
} }
@Test void damage_spell_damages_target(){ @Test void damage_spell_damages_target(){
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20); var defaultSpell = new OffensiveDamageSpell("fireball", 20, 1, 20);
var p = defaultPlayer(); var p = defaultPlayer();
p.setMana(30); p.setMana(30);
p.setHealth(30); p.setHealth(30);
@ -47,7 +47,7 @@ public class MagicSystemTest {
} }
@Test void damage_spell_drains_mana_from_caster(){ @Test void damage_spell_drains_mana_from_caster(){
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20); var defaultSpell = new OffensiveDamageSpell("fireball", 20, 1, 20);
var p = defaultPlayer(); var p = defaultPlayer();
p.setMana(30); p.setMana(30);
p.setHealth(30); p.setHealth(30);

View File

@ -80,7 +80,7 @@ class PlayerTest {
void wizard_can_learn_spell() { void wizard_can_learn_spell() {
var p = new Player("Bob"); var p = new Player("Bob");
var job = new Wizard(); var job = new Wizard();
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20); var defaultSpell = new OffensiveDamageSpell("fireball", 20, 1, 20);
p.learnJob(job); p.learnJob(job);
p.performAction(new LearnSpellAction()); p.performAction(new LearnSpellAction());
System.out.println(p.getSpellBook()); System.out.println(p.getSpellBook());

View File

@ -8,9 +8,9 @@ import static org.junit.jupiter.api.Assertions.*;
public class SpellTest { public class SpellTest {
private Spell defaultSpell() { private Spell defaultSpell() {
return new OffensiveDamageSpell("fireball", 20, 40); return new OffensiveDamageSpell("fireball", 20, 1, 40);
} }
private Spell statusSpell() {return new OffensiveStatusSpell("poisonSpray", 20, "Poison");} private Spell statusSpell() {return new OffensiveStatusSpell("poisonSpray", 20, 1, "Poison");}
@Test @Test
void setSpellNameOnCreation(){ void setSpellNameOnCreation(){