magic #11
9
src/main/java/Combat/HasConditions.java
Normal file
9
src/main/java/Combat/HasConditions.java
Normal file
@ -0,0 +1,9 @@
|
||||
package Combat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface HasConditions {
|
||||
void addCondition(String condition);
|
||||
void removeCondition(String condition);
|
||||
List<String> getConditions();
|
||||
}
|
||||
@ -3,9 +3,11 @@ package Combat;
|
||||
import Entity.Entity;
|
||||
|
||||
public class OffensiveDamageSpell extends Spell {
|
||||
private final int potency;
|
||||
|
||||
public OffensiveDamageSpell(String spellName, int cost, int potency) {
|
||||
super(spellName, cost, potency);
|
||||
public OffensiveDamageSpell(String spellName, int cost, int range, int potency) {
|
||||
super(spellName, cost, range);
|
||||
this.potency = potency;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -15,4 +17,6 @@ public class OffensiveDamageSpell extends Spell {
|
||||
health.setHealth(health.getHealth() - this.getPotency());
|
||||
}
|
||||
}
|
||||
|
||||
public int getPotency() {return potency;}
|
||||
}
|
||||
|
||||
22
src/main/java/Combat/OffensiveStatusSpell.java
Normal file
22
src/main/java/Combat/OffensiveStatusSpell.java
Normal file
@ -0,0 +1,22 @@
|
||||
package Combat;
|
||||
|
||||
import Entity.Entity;
|
||||
|
||||
public class OffensiveStatusSpell extends Spell {
|
||||
|
||||
private final String status;
|
||||
|
||||
public OffensiveStatusSpell(String spellName, int cost, int range, String status) {
|
||||
super(spellName, cost, range);
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void cast(Entity source, Entity target) {
|
||||
if(source instanceof HasMana mana && target instanceof HasConditions conditions) {
|
||||
mana.setMana(mana.getMana() - this.getCost());
|
||||
conditions.addCondition(this.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
public String getStatus() {return status;}
|
||||
}
|
||||
@ -4,13 +4,13 @@ import Entity.Entity;
|
||||
|
||||
public abstract class Spell {
|
||||
private final String spellName;
|
||||
private int cost;
|
||||
private int potency;
|
||||
private final int cost;
|
||||
private final int range;
|
||||
|
||||
public Spell(String spellName, int cost, int potency) {
|
||||
public Spell(String spellName, int cost, int range) {
|
||||
this.spellName = spellName;
|
||||
this.cost = cost;
|
||||
this.potency = potency;
|
||||
this.range = range;
|
||||
}
|
||||
|
||||
public String getSpellName() {
|
||||
@ -21,9 +21,7 @@ public abstract class Spell {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public int getPotency() {
|
||||
return potency;
|
||||
}
|
||||
public int getRange() {return range;}
|
||||
|
||||
public abstract void cast(Entity source, Entity target);
|
||||
|
||||
@ -35,7 +33,7 @@ public abstract class Spell {
|
||||
Spell spell = (Spell) o;
|
||||
|
||||
if (cost != spell.cost) return false;
|
||||
if (potency != spell.potency) return false;
|
||||
if (range != spell.range) return false;
|
||||
return spellName.equals(spell.spellName);
|
||||
}
|
||||
|
||||
@ -43,12 +41,12 @@ public abstract class Spell {
|
||||
public int hashCode() {
|
||||
int result = spellName.hashCode();
|
||||
result = 31 * result + cost;
|
||||
result = 31 * result + potency;
|
||||
result = 31 * result + range;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return spellName + " (Cost: " + cost + ", Potency: " + potency + ")";
|
||||
return spellName + " (Cost: " + cost + ", Range: " + range + ")";
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,20 @@
|
||||
package Entity;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Entity implements HasPosition {
|
||||
protected String name;
|
||||
protected Position position;
|
||||
|
||||
private static final List<Entity> entities = new ArrayList<Entity>();
|
||||
|
||||
public Entity(String name) {
|
||||
this.name = name;
|
||||
entities.add(this);
|
||||
}
|
||||
|
||||
public static List<Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package Entity;
|
||||
|
||||
import Action.Action;
|
||||
import Action.Actor;
|
||||
import Combat.HasConditions;
|
||||
import Combat.HasHealth;
|
||||
import Combat.HasMana;
|
||||
import Combat.Spell;
|
||||
@ -14,13 +15,14 @@ import Inventory.HasSpellBook;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana {
|
||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana, HasConditions {
|
||||
protected int health;
|
||||
protected int mana;
|
||||
protected Job job;
|
||||
protected Position position = new Position(0,0);
|
||||
protected Inventory inventory= new Inventory();
|
||||
protected List<Spell> spells = new LinkedList<>();
|
||||
protected List<String> conditions = new LinkedList<>();
|
||||
public Player(String name, Job job) {
|
||||
super(name);
|
||||
this.job = job;
|
||||
@ -57,6 +59,15 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS
|
||||
|
||||
public List<Spell> getSpellBook() {return spells;}
|
||||
|
||||
@Override
|
||||
public List<String> getConditions() {return conditions;}
|
||||
|
||||
@Override
|
||||
public void addCondition(String condition) {this.conditions.add(condition);}
|
||||
|
||||
@Override
|
||||
public void removeCondition(String condition) {this.conditions.remove(condition);}
|
||||
|
||||
@Override
|
||||
public void performAction(Action action) {
|
||||
action.execute(this);
|
||||
|
||||
@ -22,7 +22,7 @@ public class Wizard extends Job {
|
||||
|
||||
public void learnSpell(Actor actor) {
|
||||
if(actor instanceof HasSpellBook a ) {
|
||||
OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 1,20);
|
||||
a.getSpellBook().add(defaultSpell);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import Combat.OffensiveDamageSpell;
|
||||
|
||||
import Combat.OffensiveStatusSpell;
|
||||
|
||||
import Entity.Player;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -10,6 +13,9 @@ import static org.hamcrest.Matchers.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class InterestingTests {
|
||||
|
||||
@ -30,16 +36,14 @@ public class InterestingTests {
|
||||
player.setMana(50);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1,15);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
int initialMana = player.getMana();
|
||||
int initialHealth = player.getHealth();
|
||||
|
||||
wizard.castSpell(player);
|
||||
|
||||
assertThat(player.getMana(), is(initialMana - fireball.getCost()));
|
||||
assertThat(player.getHealth(), is(initialHealth - fireball.getPotency()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,15 +55,54 @@ public class InterestingTests {
|
||||
player.setMana(10);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1,15);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
int initialMana = player.getMana();
|
||||
int initialHealth = player.getHealth();
|
||||
|
||||
wizard.castSpell(player);
|
||||
|
||||
assertThat(player.getMana(), is(initialMana));
|
||||
assertThat(player.getHealth(),is(initialHealth));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCastSpell_indexOutOfBounds() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("2\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Brick", wizard);
|
||||
player.setMana(50);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1, 15);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
PrintStream originalOut = System.out;
|
||||
System.setOut(new PrintStream(outContent));
|
||||
|
||||
try {
|
||||
wizard.castSpell(player);
|
||||
String output = outContent.toString();
|
||||
assertTrue(output.contains("Invalid choice!"));
|
||||
} finally {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void condition_spell_adds_condition() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Alfon", wizard);
|
||||
player.setMana(50);
|
||||
|
||||
OffensiveStatusSpell poisonSpray = new OffensiveStatusSpell("poisonSpray", 20, 1, "Poison");
|
||||
player.getSpellBook().add(poisonSpray);
|
||||
|
||||
wizard.castSpell(player);
|
||||
assertThat(player.getConditions().contains("Poison"), is(true));
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,10 @@ import Combat.OffensiveDamageSpell;
|
||||
import Entity.Player;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class MagicSystemTest {
|
||||
@ -34,7 +38,7 @@ public class MagicSystemTest {
|
||||
}
|
||||
|
||||
@Test void damage_spell_damages_target(){
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 1, 20);
|
||||
var p = defaultPlayer();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
@ -43,7 +47,7 @@ public class MagicSystemTest {
|
||||
}
|
||||
|
||||
@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();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
@ -52,10 +56,22 @@ public class MagicSystemTest {
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_with_empty_spellbook(){
|
||||
var p = defaultPlayer();
|
||||
var job = new Wizard();
|
||||
p.learnJob(job);
|
||||
p.performAction(new CastAction());
|
||||
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
PrintStream OriginalOut = System.out;
|
||||
System.setOut(new PrintStream(outContent));
|
||||
|
||||
try {
|
||||
var p = defaultPlayer();
|
||||
var job = new Wizard();
|
||||
p.learnJob(job);
|
||||
|
||||
p.performAction(new CastAction());
|
||||
|
||||
String output = outContent.toString();
|
||||
assertTrue(output.contains("You haven't learned any spells"));
|
||||
} finally {
|
||||
System.setOut(OriginalOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -21,18 +21,35 @@ class PlayerTest {
|
||||
assertFalse(p.isAlive());
|
||||
}
|
||||
|
||||
@Test void get_health_returns_health() {
|
||||
@Test
|
||||
void get_health_returns_health() {
|
||||
var p = defaultPlayer();
|
||||
p.setHealth(10);
|
||||
assertEquals(10, p.getHealth());
|
||||
}
|
||||
|
||||
@Test void get_mana_returns_mana() {
|
||||
@Test
|
||||
void get_mana_returns_mana() {
|
||||
var p = defaultPlayer();
|
||||
p.setMana(10);
|
||||
assertEquals(10, p.getMana());
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_conditions_returns_conditions() {
|
||||
var p = defaultPlayer();
|
||||
p.addCondition("Poison");
|
||||
assertTrue(p.getConditions().contains("Poison"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove_condition_removes_condition() {
|
||||
var p = defaultPlayer();
|
||||
p.addCondition("Poison");
|
||||
p.removeCondition("Poison");
|
||||
assertFalse(p.getConditions().contains("Poison"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void can_change_position() {
|
||||
var p = defaultPlayer();
|
||||
@ -62,7 +79,7 @@ class PlayerTest {
|
||||
void wizard_can_learn_spell() {
|
||||
var p = new Player("Bob");
|
||||
var job = new Wizard();
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 1, 20);
|
||||
p.learnJob(job);
|
||||
p.performAction(new LearnSpellAction());
|
||||
System.out.println(p.getSpellBook());
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.OffensiveStatusSpell;
|
||||
import Combat.Spell;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -7,8 +8,9 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
public class SpellTest {
|
||||
|
||||
private Spell defaultSpell() {
|
||||
return new OffensiveDamageSpell("fireball", 20, 40);
|
||||
return new OffensiveDamageSpell("fireball", 20, 1, 40);
|
||||
}
|
||||
private Spell statusSpell() {return new OffensiveStatusSpell("poisonSpray", 20, 1, "Poison");}
|
||||
|
||||
@Test
|
||||
void setSpellNameOnCreation(){
|
||||
@ -25,6 +27,12 @@ public class SpellTest {
|
||||
@Test
|
||||
void setPotencyOnCreation() {
|
||||
var spell = defaultSpell();
|
||||
assertEquals(40, spell.getPotency(), "spell potency should have been set");
|
||||
assertEquals(40, ((OffensiveDamageSpell) spell).getPotency(), "spell potency should have been set");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setConditionOnCreation() {
|
||||
var spell = statusSpell();
|
||||
assertEquals("Poison", ((OffensiveStatusSpell) spell).getStatus(), "spell status should have been set");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user