Basic job implementation #5

Merged
erns6604 merged 10 commits from player into main 2025-10-17 10:06:38 +02:00
14 changed files with 188 additions and 14 deletions

3
.gitignore vendored
View File

@@ -36,3 +36,6 @@ build/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
## Personal notes
notes.md

View File

@@ -0,0 +1,5 @@
package Action;
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

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

View File

@@ -1,21 +1,16 @@
public abstract class Character { public abstract class Character extends Entity {
protected String name; protected String name;
protected int health; protected int health;
protected int level; protected int level;
protected Position position;
public Character(String name) { public Character(String name) {
super(new Position(0, 0));
this.name = name; this.name = name;
this.health = 10; this.health = 10;
this.level = 1; this.level = 1;
} }
public Character(String name, int level) {
this.name = name;
this.level = level;
}
public void setHealth(int i) { public void setHealth(int i) {
this.health = i; this.health = i;
} }
@@ -24,5 +19,4 @@ public abstract class Character {
return health > 0; return health > 0;
} }
public abstract void spawn(int x, int y);
} }

View File

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

14
src/main/java/Entity.java Normal file
View File

@@ -0,0 +1,14 @@
public class Entity {
private Position position;
public Entity(Position position) {
this.position = position;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}

View File

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

View File

@@ -0,0 +1,40 @@
package Job;
import Action.Actor;
import java.util.Objects;
public abstract class Job {
protected int level;
protected String name;
Job(String name) {
this.name = name;
this.level = 1;
}
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

@@ -0,0 +1,18 @@
package Job;
import Action.Actor;
import Character.HasInventory;
public class Miner extends Job {
public Miner() {
super("Miner");
}
@Override
void performJobAction(Actor actor) {
if(actor instanceof HasInventory a) {
a.getInventory().add("Rock");
}
}
}

View File

@@ -0,0 +1,3 @@
public interface Movable {
void moveTo(Position position);
}

View File

@@ -1,13 +1,45 @@
public class Player extends Character { import Action.Action;
import Action.Actor;
import Action.Dig;
import Job.Job;
import Job.HasJob;
import Character.HasInventory;
import java.util.LinkedList;
import java.util.List;
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) {
super(name);
this.job = job;
}
public Player(String name) { public Player(String name) {
super(name); super(name);
} }
@Override
public void moveTo(Position position) {
setPosition(position);
}
public void learnJob(Job job) {
this.job = job;
}
public Job getJob() {
return job;
}
public List<String> getInventory() {
return items;
}
@Override @Override
public void spawn(int x, int y) { public void performAction(Action action) {
action.exectue(this);
} }
} }

View File

@@ -0,0 +1,29 @@
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(new Dig());
assertTrue(p.getInventory().contains("Stone"));
}
@Test
void miner_can_level_up() {
var job = new Miner();
assertEquals(1, job.getLevel());
job.levelUp();
assertEquals(2, job.getLevel());
}
}

View File

@@ -1,9 +1,9 @@
import Job.Miner;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class PlayerTest { class PlayerTest {
private Player defaultPlayer() { private Player defaultPlayer() {
return new Player("abc"); return new Player("abc");
} }
@@ -18,9 +18,17 @@ class PlayerTest {
@Test @Test
public void can_change_position() { public void can_change_position() {
var p = defaultPlayer(); var p = defaultPlayer();
assertEquals(new Position(0,0), p.getPosition());
p.moveTo(new Position(1,1)); p.moveTo(new Position(1,1));
assertEquals(new Position(1,1), p.getPosition()); assertEquals(new Position(1,1), p.getPosition());
} }
@Test
public void can_change_job() {
var p = defaultPlayer();
assertNull(p.getJob());
var job = new Miner();
p.learnJob(job);
assertEquals(new Miner(), p.getJob());
}
} }