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);
|
||||
super(spellName, cost);
|
||||
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, String status) {
|
||||
super(spellName, cost);
|
||||
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;}
|
||||
}
|
||||
@ -5,12 +5,10 @@ import Entity.Entity;
|
||||
public abstract class Spell {
|
||||
private final String spellName;
|
||||
private int cost;
|
||||
private int potency;
|
||||
|
||||
public Spell(String spellName, int cost, int potency) {
|
||||
public Spell(String spellName, int cost) {
|
||||
this.spellName = spellName;
|
||||
this.cost = cost;
|
||||
this.potency = potency;
|
||||
}
|
||||
|
||||
public String getSpellName() {
|
||||
@ -21,10 +19,6 @@ public abstract class Spell {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public int getPotency() {
|
||||
return potency;
|
||||
}
|
||||
|
||||
public abstract void cast(Entity source, Entity target);
|
||||
|
||||
@Override
|
||||
@ -35,7 +29,6 @@ public abstract class Spell {
|
||||
Spell spell = (Spell) o;
|
||||
|
||||
if (cost != spell.cost) return false;
|
||||
if (potency != spell.potency) return false;
|
||||
return spellName.equals(spell.spellName);
|
||||
}
|
||||
|
||||
@ -43,12 +36,11 @@ public abstract class Spell {
|
||||
public int hashCode() {
|
||||
int result = spellName.hashCode();
|
||||
result = 31 * result + cost;
|
||||
result = 31 * result + potency;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return spellName + " (Cost: " + cost + ", Potency: " + potency + ")";
|
||||
return spellName + " (Cost: " + cost + ")";
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package Entity;
|
||||
|
||||
import Action.Action;
|
||||
import Action.Actor;
|
||||
import Combat.HasConditions;
|
||||
import Combat.HasHealth;
|
||||
import Combat.HasMana;
|
||||
import Combat.Spell;
|
||||
@ -13,13 +14,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;
|
||||
protected List<String> items = new LinkedList<>();
|
||||
protected List<Spell> spells = new LinkedList<>();
|
||||
protected List<String> conditions = new LinkedList<>();
|
||||
public Player(String name, Job job) {
|
||||
super(name);
|
||||
this.job = job;
|
||||
@ -56,6 +58,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.exectue(this);
|
||||
|
||||
@ -1,23 +1,17 @@
|
||||
import Action.Actor;
|
||||
import Combat.HasMana;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.Spell;
|
||||
import Entity.Entity;
|
||||
import Combat.OffensiveStatusSpell;
|
||||
import Entity.Player;
|
||||
import Inventory.HasSpellBook;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class InterestingTests {
|
||||
|
||||
@ -42,12 +36,10 @@ public class InterestingTests {
|
||||
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
|
||||
@ -63,11 +55,50 @@ public class InterestingTests {
|
||||
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, 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, "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 {
|
||||
@ -52,10 +56,22 @@ public class MagicSystemTest {
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_with_empty_spellbook(){
|
||||
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();
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.OffensiveStatusSpell;
|
||||
import Combat.Spell;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -9,6 +10,7 @@ public class SpellTest {
|
||||
private Spell defaultSpell() {
|
||||
return new OffensiveDamageSpell("fireball", 20, 40);
|
||||
}
|
||||
private Spell statusSpell() {return new OffensiveStatusSpell("poisonSpray", 20, "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