Job + Spell merge #9
7
pom.xml
7
pom.xml
@@ -20,6 +20,13 @@
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>2.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
|
||||
19
src/main/java/Action/CastAction.java
Normal file
19
src/main/java/Action/CastAction.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package Action;
|
||||
|
||||
import Job.HasJob;
|
||||
import Job.Wizard;
|
||||
|
||||
public class CastAction implements Action {
|
||||
@Override
|
||||
public void exectue(Actor actor) {
|
||||
Wizard wizard = requireWizard(actor);
|
||||
wizard.castSpell(actor);
|
||||
}
|
||||
|
||||
private Wizard requireWizard(Actor actor) {
|
||||
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
||||
return wizard;
|
||||
}
|
||||
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
||||
}
|
||||
}
|
||||
19
src/main/java/Action/LearnSpellAction.java
Normal file
19
src/main/java/Action/LearnSpellAction.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package Action;
|
||||
|
||||
import Job.HasJob;
|
||||
import Job.Wizard;
|
||||
|
||||
public class LearnSpellAction implements Action {
|
||||
@Override
|
||||
public void exectue(Actor actor) {
|
||||
Wizard wizard = requireWizard(actor);
|
||||
wizard.learnSpell(actor);
|
||||
}
|
||||
|
||||
private Wizard requireWizard(Actor actor) {
|
||||
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
|
||||
return wizard;
|
||||
}
|
||||
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
|
||||
}
|
||||
}
|
||||
7
src/main/java/Character/HasSpellBook.java
Normal file
7
src/main/java/Character/HasSpellBook.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package Character;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface HasSpellBook {
|
||||
List<String> getSpellBook();
|
||||
}
|
||||
@@ -2,5 +2,6 @@ package Combat;
|
||||
|
||||
public interface HasHealth {
|
||||
void setHealth(int health);
|
||||
int getHealth();
|
||||
boolean isAlive();
|
||||
}
|
||||
|
||||
6
src/main/java/Combat/HasMana.java
Normal file
6
src/main/java/Combat/HasMana.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package Combat;
|
||||
|
||||
public interface HasMana {
|
||||
void setMana(int mana);
|
||||
int getMana();
|
||||
}
|
||||
18
src/main/java/Combat/OffensiveDamageSpell.java
Normal file
18
src/main/java/Combat/OffensiveDamageSpell.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package Combat;
|
||||
|
||||
import Entity.Entity;
|
||||
|
||||
public class OffensiveDamageSpell extends Spell {
|
||||
|
||||
public OffensiveDamageSpell(String spellName, int cost, int potency) {
|
||||
super(spellName, cost, potency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cast(Entity source, Entity target) {
|
||||
if(target instanceof HasHealth health && source instanceof HasMana mana) {
|
||||
mana.setMana(mana.getMana() - this.getCost());
|
||||
health.setHealth(health.getHealth() - this.getPotency());
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/main/java/Combat/Spell.java
Normal file
54
src/main/java/Combat/Spell.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package Combat;
|
||||
|
||||
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) {
|
||||
this.spellName = spellName;
|
||||
this.cost = cost;
|
||||
this.potency = potency;
|
||||
}
|
||||
|
||||
public String getSpellName() {
|
||||
return spellName;
|
||||
}
|
||||
|
||||
public int getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public int getPotency() {
|
||||
return potency;
|
||||
}
|
||||
|
||||
public abstract void cast(Entity source, Entity target);
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Spell spell = (Spell) o;
|
||||
|
||||
if (cost != spell.cost) return false;
|
||||
if (potency != spell.potency) return false;
|
||||
return spellName.equals(spell.spellName);
|
||||
}
|
||||
|
||||
@Override
|
||||
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 + ")";
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,26 @@ package Entity;
|
||||
import Action.Action;
|
||||
import Action.Actor;
|
||||
import Combat.HasHealth;
|
||||
import Combat.HasMana;
|
||||
import Combat.Spell;
|
||||
import Inventory.Inventory;
|
||||
import Job.Job;
|
||||
import Job.HasJob;
|
||||
import Inventory.HasInventory;
|
||||
import Inventory.HasSpellBook;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasJob, HasHealth {
|
||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana {
|
||||
protected int health;
|
||||
protected int mana;
|
||||
protected Job job;
|
||||
protected Position position = new Position(0,0);
|
||||
protected Inventory inventory= new Inventory();
|
||||
protected Position position;
|
||||
protected List<String> items = new LinkedList<>();
|
||||
protected List<Spell> spells = new LinkedList<>();
|
||||
public Player(String name, Job job) {
|
||||
super(name);
|
||||
this.job = job;
|
||||
@@ -35,7 +42,7 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
|
||||
|
||||
@Override
|
||||
public boolean canMoveTo(Position position) {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
public Job getJob() {
|
||||
return job;
|
||||
@@ -50,6 +57,8 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public List<Spell> getSpellBook() {return spells;}
|
||||
|
||||
@Override
|
||||
public void performAction(Action action) {
|
||||
action.execute(this);
|
||||
@@ -70,6 +79,15 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
|
||||
this.health = health;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHealth() {return health;}
|
||||
|
||||
@Override
|
||||
public void setMana(int mana) {this.mana = mana;}
|
||||
|
||||
@Override
|
||||
public int getMana() {return mana;}
|
||||
|
||||
@Override
|
||||
public boolean isAlive() {
|
||||
return health > 0;
|
||||
|
||||
8
src/main/java/Inventory/HasSpellBook.java
Normal file
8
src/main/java/Inventory/HasSpellBook.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Inventory;
|
||||
|
||||
import java.util.List;
|
||||
import Combat.Spell;
|
||||
|
||||
public interface HasSpellBook {
|
||||
List<Spell> getSpellBook();
|
||||
}
|
||||
70
src/main/java/Job/Wizard.java
Normal file
70
src/main/java/Job/Wizard.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package Job;
|
||||
|
||||
import Action.Actor;
|
||||
import Combat.HasMana;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Entity.Entity;
|
||||
import Inventory.HasSpellBook;
|
||||
import Combat.Spell;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Wizard extends Job {
|
||||
private final Scanner scanner;
|
||||
public Wizard(Scanner scanner) {
|
||||
super("Wizard");
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
public Wizard() {
|
||||
this(new Scanner(System.in));
|
||||
}
|
||||
|
||||
public void learnSpell(Actor actor) {
|
||||
if(actor instanceof HasSpellBook a ) {
|
||||
OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
a.getSpellBook().add(defaultSpell);
|
||||
}
|
||||
}
|
||||
|
||||
public void castSpell(Actor actor) {
|
||||
if(actor instanceof HasSpellBook spellbook && actor instanceof HasMana mana && actor instanceof Entity entity) {
|
||||
if(spellbook.getSpellBook().isEmpty()){
|
||||
System.out.println("You haven't learned any spells");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Which spell do you want to cast?");
|
||||
for(int i = 0; i < spellbook.getSpellBook().size(); i++){
|
||||
System.out.println((i + 1) + ". " + spellbook.getSpellBook().get(i).toString());
|
||||
}
|
||||
|
||||
int spellIndex = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
if(spellIndex < 1 || spellIndex > spellbook.getSpellBook().size()) {
|
||||
System.out.println("Invalid choice!");
|
||||
return;
|
||||
}
|
||||
|
||||
Spell spell = spellbook.getSpellBook().get(spellIndex - 1);
|
||||
|
||||
if (mana.getMana() >= spell.getCost()){
|
||||
spell.cast(entity, entity);
|
||||
}
|
||||
else{
|
||||
System.out.println("Not enough mana!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getExperience() {
|
||||
return experience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gainExperience(int exp) {
|
||||
experience += exp;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
public class Spell {
|
||||
private final String spellName;
|
||||
private int cost;
|
||||
private int potency;
|
||||
|
||||
public Spell(String spellName, int cost, int potency) {
|
||||
this.spellName = spellName;
|
||||
this.cost = cost;
|
||||
this.potency = potency;
|
||||
}
|
||||
|
||||
public String getSpellName() {return spellName;}
|
||||
|
||||
public int getCost() {return cost;}
|
||||
|
||||
public int getPotency() {return potency;}
|
||||
}
|
||||
|
||||
73
src/test/java/InterestingTests.java
Normal file
73
src/test/java/InterestingTests.java
Normal file
@@ -0,0 +1,73 @@
|
||||
import Action.Actor;
|
||||
import Combat.HasMana;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.Spell;
|
||||
import Entity.Entity;
|
||||
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.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class InterestingTests {
|
||||
|
||||
private Wizard wizard;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
wizard = new Wizard();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCastSpell_successfulCast() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Gandalf", wizard);
|
||||
|
||||
player.setMana(50);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 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
|
||||
public void testCastSpell_notEnoughMana() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Merlin", wizard);
|
||||
player.setMana(10);
|
||||
player.setHealth(100);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 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));
|
||||
}
|
||||
}
|
||||
61
src/test/java/MagicSystemTest.java
Normal file
61
src/test/java/MagicSystemTest.java
Normal file
@@ -0,0 +1,61 @@
|
||||
import Action.CastAction;
|
||||
import Action.LearnSpellAction;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Entity.Player;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class MagicSystemTest {
|
||||
|
||||
private Player defaultPlayer() {
|
||||
return new Player("Alex");
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_without_being_wizard(){
|
||||
var p = defaultPlayer();
|
||||
CastAction action = new CastAction();
|
||||
IllegalStateException exception = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> action.exectue(p)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
|
||||
}
|
||||
|
||||
@Test void cant_learn_spell_without_being_wizard(){
|
||||
var p = defaultPlayer();
|
||||
LearnSpellAction action = new LearnSpellAction();
|
||||
IllegalStateException exception = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> action.exectue(p)
|
||||
);
|
||||
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
|
||||
}
|
||||
|
||||
@Test void damage_spell_damages_target(){
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var p = defaultPlayer();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
defaultSpell.cast(p, p);
|
||||
assertEquals(10, p.getHealth());
|
||||
}
|
||||
|
||||
@Test void damage_spell_drains_mana_from_caster(){
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
var p = defaultPlayer();
|
||||
p.setMana(30);
|
||||
p.setHealth(30);
|
||||
defaultSpell.cast(p, p);
|
||||
assertEquals(10, p.getMana());
|
||||
}
|
||||
|
||||
@Test void cant_cast_spell_with_empty_spellbook(){
|
||||
var p = defaultPlayer();
|
||||
var job = new Wizard();
|
||||
p.learnJob(job);
|
||||
p.performAction(new CastAction());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
import Action.DigAction;
|
||||
import Action.LearnSpellAction;
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Entity.Position;
|
||||
import Job.Miner;
|
||||
import Entity.Player;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import Job.Wizard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -18,6 +21,18 @@ class PlayerTest {
|
||||
assertFalse(p.isAlive());
|
||||
}
|
||||
|
||||
@Test void get_health_returns_health() {
|
||||
var p = defaultPlayer();
|
||||
p.setHealth(10);
|
||||
assertEquals(10, p.getHealth());
|
||||
}
|
||||
|
||||
@Test void get_mana_returns_mana() {
|
||||
var p = defaultPlayer();
|
||||
p.setMana(10);
|
||||
assertEquals(10, p.getMana());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void can_change_position() {
|
||||
var p = defaultPlayer();
|
||||
@@ -42,4 +57,15 @@ class PlayerTest {
|
||||
p.performAction(new DigAction());
|
||||
assertTrue(p.getInventory().containsItem("Stone"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wizard_can_learn_spell() {
|
||||
var p = new Player("Bob");
|
||||
var job = new Wizard();
|
||||
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
|
||||
p.learnJob(job);
|
||||
p.performAction(new LearnSpellAction());
|
||||
System.out.println(p.getSpellBook());
|
||||
assertTrue(p.getSpellBook().contains(defaultSpell));
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import Combat.OffensiveDamageSpell;
|
||||
import Combat.Spell;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -5,19 +7,19 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
public class SpellTest {
|
||||
|
||||
private Spell defaultSpell() {
|
||||
return new Spell("fireball", 20, 40);
|
||||
return new OffensiveDamageSpell("fireball", 20, 40);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSpellNameOnCreation(){
|
||||
var spell = defaultSpell();
|
||||
assertEquals("fireball", spell.getSpellName(), "Spell name should be set");
|
||||
assertEquals("fireball", spell.getSpellName(), "Combat.Spell name should be set");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCostOnCreation() {
|
||||
var spell = defaultSpell();
|
||||
assertEquals(20, spell.getCost(), "Spell cost should have been set");
|
||||
assertEquals(20, spell.getCost(), "Combat.Spell cost should have been set");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
26
src/test/java/WizardTest.java
Normal file
26
src/test/java/WizardTest.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class WizardTest {
|
||||
|
||||
@Test
|
||||
void wizard_has_level() {
|
||||
var job = new Wizard();
|
||||
assertEquals(1, job.getLevel());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wizard_can_level_up() {
|
||||
var job = new Wizard();
|
||||
job.levelUp();
|
||||
assertEquals(2, job.getLevel());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wizard_can_gain_xp(){
|
||||
var job = new Wizard();
|
||||
job.gainExperience(40);
|
||||
assertEquals(40, job.getExperience());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user