Basic job implementation #5

Merged
erns6604 merged 10 commits from player into main 2025-10-17 10:06:38 +02:00
9 changed files with 77 additions and 42 deletions
Showing only changes of commit f730cee3a3 - Show all commits

View File

@@ -1,5 +1,5 @@
package Action;
public abstract class Action {
public static Dig Dig = new Dig();
public interface Action {
void exectue(Actor player);
}

View File

@@ -0,0 +1,8 @@
package Action;
import Job.Job;
public interface Actor {
Job getJob();
void performAction(Action action);
}

View File

@@ -1,5 +1,8 @@
package Action;
public class Dig extends Action {
public class Dig implements Action {
@Override
public void exectue(Actor actor) {
actor.performAction(this);
}
}

View File

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

View File

@@ -0,0 +1,5 @@
package Job;
public interface HasJob {
Job getJob();
}

View File

@@ -1,21 +1,40 @@
package Job;
public abstract class Job {
private int level;
abstract void performJobAction();
import Action.Actor;
Job() {
import java.util.Objects;
public abstract class Job {
protected int level;
protected String name;
Job(String name) {
this.name = name;
this.level = 1;
}
Job(int level) {
this.level = level;
}
public double getLevel() {
return level;
};
}
public void levelUp() {
level++;
}
@Override
public String toString() {
return String.format("Job: %s. Level: %d", name, level);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}

View File

@@ -1,30 +1,18 @@
package Job;
import java.util.Objects;
import Action.Actor;
import Character.HasInventory;
public class Miner extends Job {
private final String jobName = "Miner";
public Miner() {}
@Override
public void performJobAction() {
System.out.println("Dig...");
public Miner() {
super("Miner");
}
@Override
public String toString() {
return "Miner";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
void performJobAction(Actor actor) {
if(actor instanceof HasInventory a) {
a.getInventory().add("Rock");
}
@Override
public int hashCode() {
return Objects.hash(jobName);
}
}

View File

@@ -1,11 +1,14 @@
import Action.Action;
import Action.Actor;
import Action.Dig;
import Job.Job;
import Job.HasJob;
import Character.HasInventory;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
public class Player extends Character implements Movable {
public class Player extends Character implements Movable, Actor, HasInventory, HasJob {
protected Job job;
protected List<String> items = new LinkedList<>();
public Player(String name, Job job) {
@@ -30,11 +33,13 @@ public class Player extends Character implements Movable {
return job;
}
public void performAction(Dig dig) {
items.add("Stone");
}
public List<String> getInventory() {
return items;
}
@Override
public void performAction(Action action) {
action.exectue(this);
}
}

View File

@@ -1,23 +1,23 @@
import Action.Action;
import Job.Miner;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import Action.Dig;
public class MinerTest {
@Test
void miner_can_dig() {
var p = new Player("Steve");
var job = new Miner();
p.learnJob(job);
p.performAction(Action.Dig);
p.performAction(new Dig());
assertTrue(p.getInventory().contains("Stone"));
}
@Test
void miner_can_level_up() {
var p = new Player("Steve");
var job = new Miner();
assertEquals(1, job.getLevel());
job.levelUp();