Basic job implementation #5
3
.gitignore
vendored
3
.gitignore
vendored
@@ -36,3 +36,6 @@ build/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
|
||||
## Personal notes
|
||||
notes.md
|
||||
|
||||
5
src/main/java/Action/Action.java
Normal file
5
src/main/java/Action/Action.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package Action;
|
||||
|
||||
public interface Action {
|
||||
void exectue(Actor player);
|
||||
}
|
||||
8
src/main/java/Action/Actor.java
Normal file
8
src/main/java/Action/Actor.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Action;
|
||||
|
||||
import Job.Job;
|
||||
|
||||
public interface Actor {
|
||||
Job getJob();
|
||||
void performAction(Action action);
|
||||
}
|
||||
8
src/main/java/Action/Dig.java
Normal file
8
src/main/java/Action/Dig.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Action;
|
||||
|
||||
public class Dig implements Action {
|
||||
@Override
|
||||
public void exectue(Actor actor) {
|
||||
actor.performAction(this);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,16 @@
|
||||
public abstract class Character {
|
||||
public abstract class Character extends Entity {
|
||||
protected String name;
|
||||
protected int health;
|
||||
protected int level;
|
||||
protected Position position;
|
||||
|
||||
|
||||
public Character(String name) {
|
||||
super(new Position(0, 0));
|
||||
this.name = name;
|
||||
this.health = 10;
|
||||
this.level = 1;
|
||||
}
|
||||
|
||||
public Character(String name, int level) {
|
||||
this.name = name;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public void setHealth(int i) {
|
||||
this.health = i;
|
||||
}
|
||||
@@ -24,5 +19,4 @@ public abstract class Character {
|
||||
return health > 0;
|
||||
}
|
||||
|
||||
public abstract void spawn(int x, int y);
|
||||
}
|
||||
|
||||
7
src/main/java/Character/HasInventory.java
Normal file
7
src/main/java/Character/HasInventory.java
Normal 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
14
src/main/java/Entity.java
Normal 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;
|
||||
}
|
||||
}
|
||||
5
src/main/java/Job/HasJob.java
Normal file
5
src/main/java/Job/HasJob.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package Job;
|
||||
|
||||
public interface HasJob {
|
||||
Job getJob();
|
||||
}
|
||||
40
src/main/java/Job/Job.java
Normal file
40
src/main/java/Job/Job.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
18
src/main/java/Job/Miner.java
Normal file
18
src/main/java/Job/Miner.java
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/main/java/Movable.java
Normal file
3
src/main/java/Movable.java
Normal file
@@ -0,0 +1,3 @@
|
||||
public interface Movable {
|
||||
void moveTo(Position position);
|
||||
}
|
||||
@@ -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) {
|
||||
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
|
||||
public void spawn(int x, int y) {
|
||||
|
||||
public void performAction(Action action) {
|
||||
action.exectue(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
29
src/test/java/MinerTest.java
Normal file
29
src/test/java/MinerTest.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Job.Miner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PlayerTest {
|
||||
|
||||
private Player defaultPlayer() {
|
||||
return new Player("abc");
|
||||
}
|
||||
@@ -18,9 +18,17 @@ class PlayerTest {
|
||||
@Test
|
||||
public void can_change_position() {
|
||||
var p = defaultPlayer();
|
||||
assertEquals(new Position(0,0), p.getPosition());
|
||||
p.moveTo(new Position(1,1));
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user