Create modules #6
@@ -18,10 +18,10 @@ RWRD implements common elements from roguelikes, and games in general.
|
|||||||
### The player
|
### The player
|
||||||
The player object is a representation of the user and it's agency in the game world
|
The player object is a representation of the user and it's agency in the game world
|
||||||
|
|
||||||
### Player
|
### Entity.Player
|
||||||
|
|
||||||
|
|
||||||
- Player
|
- Entity.Player
|
||||||
- Enemies
|
- Enemies
|
||||||
-- Bosses
|
-- Bosses
|
||||||
- Class system
|
- Class system
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package Action;
|
|
||||||
|
|
||||||
public class Dig implements Action {
|
|
||||||
@Override
|
|
||||||
public void exectue(Actor actor) {
|
|
||||||
actor.performAction(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
20
src/main/java/Action/DigAction.java
Normal file
20
src/main/java/Action/DigAction.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package Action;
|
||||||
|
|
||||||
|
import Job.HasJob;
|
||||||
|
import Job.Miner;
|
||||||
|
|
||||||
|
public class DigAction implements Action {
|
||||||
|
@Override
|
||||||
|
public void exectue(Actor actor) {
|
||||||
|
Miner miner = requireMiner(actor);
|
||||||
|
miner.dig(actor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Miner requireMiner(Actor actor) {
|
||||||
|
if (actor instanceof HasJob hasJob
|
||||||
|
&& hasJob.getJob() instanceof Miner miner) {
|
||||||
|
return miner;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException(actor + " cannot perform this action without being a Miner!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
public abstract class Character extends Entity {
|
|
||||||
protected String name;
|
|
||||||
protected int health;
|
|
||||||
protected int level;
|
|
||||||
|
|
||||||
|
|
||||||
public Character(String name) {
|
|
||||||
super(new Position(0, 0));
|
|
||||||
this.name = name;
|
|
||||||
this.health = 10;
|
|
||||||
this.level = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHealth(int i) {
|
|
||||||
this.health = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isAlive() {
|
|
||||||
return health > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
6
src/main/java/Combat/HasHealth.java
Normal file
6
src/main/java/Combat/HasHealth.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package Combat;
|
||||||
|
|
||||||
|
public interface HasHealth {
|
||||||
|
void setHealth(int health);
|
||||||
|
boolean isAlive();
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
13
src/main/java/Entity/Entity.java
Normal file
13
src/main/java/Entity/Entity.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class Entity implements HasPosition {
|
||||||
|
protected String name;
|
||||||
|
protected Position position;
|
||||||
|
|
||||||
|
|
||||||
|
public Entity(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
6
src/main/java/Entity/HasPosition.java
Normal file
6
src/main/java/Entity/HasPosition.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
|
public interface HasPosition {
|
||||||
|
Position getPosition();
|
||||||
|
void setPosition(Position position);
|
||||||
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
|
|
||||||
public interface Movable {
|
public interface Movable {
|
||||||
void moveTo(Position position);
|
void moveTo(Position position);
|
||||||
|
boolean canMoveTo(Position position);
|
||||||
}
|
}
|
||||||
76
src/main/java/Entity/Player.java
Normal file
76
src/main/java/Entity/Player.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
|
import Action.Action;
|
||||||
|
import Action.Actor;
|
||||||
|
import Combat.HasHealth;
|
||||||
|
import Job.Job;
|
||||||
|
import Job.HasJob;
|
||||||
|
import Inventory.HasInventory;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Player extends Entity implements Movable, Actor, HasInventory, HasJob, HasHealth {
|
||||||
|
protected int health;
|
||||||
|
protected Job job;
|
||||||
|
protected Position position;
|
||||||
|
protected List<String> items = new LinkedList<>();
|
||||||
|
public Player(String name, Job job) {
|
||||||
|
super(name);
|
||||||
|
this.job = job;
|
||||||
|
this.position = new Position(0,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void moveTo(Position position) {
|
||||||
|
if(canMoveTo(position)){
|
||||||
|
setPosition(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canMoveTo(Position position) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public Job getJob() {
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void learnJob(Job job) {
|
||||||
|
this.job = job;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getInventory() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void performAction(Action action) {
|
||||||
|
action.exectue(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPosition(Position position) {
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHealth(int health) {
|
||||||
|
this.health = health;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAlive() {
|
||||||
|
return health > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,3 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
public record Position(int x, int y) {}
|
public record Position(int x, int y) {}
|
||||||
7
src/main/java/Inventory/HasInventory.java
Normal file
7
src/main/java/Inventory/HasInventory.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package Inventory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface HasInventory {
|
||||||
|
List<String> getInventory();
|
||||||
|
}
|
||||||
@@ -2,4 +2,5 @@ package Job;
|
|||||||
|
|
||||||
public interface HasJob {
|
public interface HasJob {
|
||||||
Job getJob();
|
Job getJob();
|
||||||
|
void learnJob(Job job);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package Job;
|
package Job;
|
||||||
|
|
||||||
import Action.Actor;
|
import Action.Actor;
|
||||||
import Character.HasInventory;
|
import Inventory.HasInventory;
|
||||||
|
|
||||||
public class Miner extends Job {
|
public class Miner extends Job {
|
||||||
public Miner() {
|
public Miner() {
|
||||||
@@ -9,10 +9,10 @@ public class Miner extends Job {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
public void dig(Actor actor) {
|
||||||
void performJobAction(Actor actor) {
|
|
||||||
if(actor instanceof HasInventory a) {
|
if(actor instanceof HasInventory a) {
|
||||||
a.getInventory().add("Rock");
|
a.getInventory().add("Stone");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
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 performAction(Action action) {
|
|
||||||
action.exectue(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
|
import Entity.Player;
|
||||||
import Job.Miner;
|
import Job.Miner;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import Action.Dig;
|
import Action.DigAction;
|
||||||
|
|
||||||
public class MinerTest {
|
public class MinerTest {
|
||||||
@Test
|
@Test
|
||||||
void miner_can_dig() {
|
void miner_can_dig() {
|
||||||
var p = new Player("Steve");
|
var p = new Player("John");
|
||||||
var job = new Miner();
|
var job = new Miner();
|
||||||
p.learnJob(job);
|
p.learnJob(job);
|
||||||
p.performAction(new Dig());
|
p.performAction(new DigAction());
|
||||||
assertTrue(p.getInventory().contains("Stone"));
|
assertTrue(p.getInventory().contains("Stone"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import Entity.Position;
|
||||||
import Job.Miner;
|
import Job.Miner;
|
||||||
|
import Entity.Player;
|
||||||
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.*;
|
||||||
|
|||||||
Reference in New Issue
Block a user