magic #11
@ -17,4 +17,23 @@ public abstract class Entity implements HasPosition {
|
||||
public static List<Entity> getEntities() {
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
|
||||
Entity entity = (Entity) obj;
|
||||
|
||||
return name.equals(entity.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,10 @@ import Combat.OffensiveDamageSpell;
|
||||
import Entity.Entity;
|
||||
import Inventory.HasSpellBook;
|
||||
import Combat.Spell;
|
||||
import Entity.Position;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Wizard extends Job {
|
||||
@ -48,9 +51,10 @@ public class Wizard extends Job {
|
||||
}
|
||||
|
||||
Spell spell = spellbook.getSpellBook().get(spellIndex - 1);
|
||||
Entity target = selectSpellTarget(spell, entity);
|
||||
|
||||
if (mana.getMana() >= spell.getCost()){
|
||||
spell.cast(entity, entity);
|
||||
spell.cast(entity, target);
|
||||
}
|
||||
else{
|
||||
System.out.println("Not enough mana!");
|
||||
@ -58,6 +62,36 @@ public class Wizard extends Job {
|
||||
}
|
||||
}
|
||||
|
||||
public Entity selectSpellTarget(Spell spell, Entity caster) {
|
||||
int range = spell.getRange();
|
||||
Position position = caster.getPosition();
|
||||
int x = position.x();
|
||||
int y = position.y();
|
||||
List<Entity> acceptableTargets = new LinkedList<>();
|
||||
for(Entity entity : Entity.getEntities()) {
|
||||
if(x + range >= entity.getPosition().x() && x - range <= entity.getPosition().x()) {
|
||||
if(y + range >= entity.getPosition().y() && y - range <= entity.getPosition().y()) {
|
||||
acceptableTargets.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Who do you want to target with the spell?");
|
||||
for(int i = 0; i < acceptableTargets.size(); i++){
|
||||
System.out.println((i + 1) + ". " + acceptableTargets.get(i).toString());
|
||||
}
|
||||
|
||||
int EntityIndex = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
if(EntityIndex < 1 || EntityIndex > acceptableTargets.size()) {
|
||||
System.out.println("Invalid choice!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return acceptableTargets.get(EntityIndex - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getExperience() {
|
||||
return experience;
|
||||
|
||||
@ -2,6 +2,8 @@ import Combat.OffensiveDamageSpell;
|
||||
|
||||
import Combat.OffensiveStatusSpell;
|
||||
|
||||
import Entity.Position;
|
||||
import Entity.Entity;
|
||||
import Entity.Player;
|
||||
import Job.Wizard;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@ -26,9 +28,14 @@ public class InterestingTests {
|
||||
wizard = new Wizard();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void cleanup() {
|
||||
Entity.getEntities().clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCastSpell_successfulCast() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Gandalf", wizard);
|
||||
@ -48,7 +55,7 @@ public class InterestingTests {
|
||||
|
||||
@Test
|
||||
public void testCastSpell_notEnoughMana() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Merlin", wizard);
|
||||
@ -93,7 +100,7 @@ public class InterestingTests {
|
||||
|
||||
@Test
|
||||
public void condition_spell_adds_condition() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n1\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
|
||||
Player player = new Player("Alfon", wizard);
|
||||
@ -105,4 +112,23 @@ public class InterestingTests {
|
||||
wizard.castSpell(player);
|
||||
assertThat(player.getConditions().contains("Poison"), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void select_spell_target_returns_correct_target() {
|
||||
Scanner testScanner = new Scanner(new ByteArrayInputStream("2\n".getBytes()));
|
||||
wizard = new Wizard(testScanner);
|
||||
Position position = new Position(1,1);
|
||||
|
||||
Player player = new Player("Alfon", wizard);
|
||||
Player player2 = new Player("Bob", wizard);
|
||||
player.setMana(50);
|
||||
player2.moveTo(position);
|
||||
|
||||
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 1, 20);
|
||||
player.getSpellBook().add(fireball);
|
||||
|
||||
Entity target = wizard.selectSpellTarget(fireball, player);
|
||||
|
||||
assertThat(target, is(player2));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user