Job + Spell merge #9

Merged
erns6604 merged 23 commits from Job into main 2025-10-27 12:18:22 +01:00
7 changed files with 70 additions and 0 deletions
Showing only changes of commit c0e93c9a42 - Show all commits

View 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!");
}
}

View File

@@ -0,0 +1,7 @@
package Character;
import java.util.List;
public interface HasSpellBook {
List<String> getSpellBook();
}

View File

@@ -6,6 +6,7 @@ import Combat.HasHealth;
import Job.Job; import Job.Job;
import Job.HasJob; import Job.HasJob;
import Inventory.HasInventory; import Inventory.HasInventory;
import Inventory.HasSpellBook;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@@ -15,6 +16,7 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
protected Job job; protected Job job;
protected Position position; protected Position position;
protected List<String> items = new LinkedList<>(); protected List<String> items = new LinkedList<>();
protected List<String> spells = new LinkedList<>();
public Player(String name, Job job) { public Player(String name, Job job) {
super(name); super(name);
this.job = job; this.job = job;
@@ -49,6 +51,8 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
return items; return items;
} }
public List<String> getSpellBook() {return spells;}
@Override @Override
public void performAction(Action action) { public void performAction(Action action) {
action.exectue(this); action.exectue(this);

View File

@@ -0,0 +1,7 @@
package Inventory;
import java.util.List;
public interface HasSpellBook {
List<String> getSpellBook();
}

View File

@@ -0,0 +1,24 @@
package Job;
import Action.Actor;
import Character.HasSpellBook;
public class Wizard extends Job {
public Wizard() {super("Wizard");}
public void learnSpell(Actor actor) {
if(actor instanceof HasSpellBook a ) {
a.getSpellBook().add("Fireball");
}
}
@Override
public int getExperience() {
return experience;
}
@Override
public void gainExperience(int exp) {
experience += exp;
}
}

View File

@@ -1,8 +1,10 @@
import Action.DigAction; import Action.DigAction;
import Action.learnSpellAction;
import Entity.Position; import Entity.Position;
import Job.Miner; import Job.Miner;
import Entity.Player; import Entity.Player;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import Job.Wizard;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;

View File

@@ -0,0 +1,7 @@
import Job.Wizard;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class WizardTest {
}