monster #20
5
.gitignore
vendored
5
.gitignore
vendored
@@ -35,4 +35,7 @@ build/
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
### Mac OS ###
|
### Mac OS ###
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
## Personal notes
|
||||||
|
notes.md
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
7
src/main/java/Entity/Movable.java
Normal file
7
src/main/java/Entity/Movable.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package Entity;
|
||||||
|
|
||||||
|
|
||||||
|
public interface Movable {
|
||||||
|
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();
|
||||||
|
}
|
||||||
6
src/main/java/Job/HasJob.java
Normal file
6
src/main/java/Job/HasJob.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package Job;
|
||||||
|
|
||||||
|
public interface HasJob {
|
||||||
|
Job getJob();
|
||||||
|
void learnJob(Job job);
|
||||||
|
}
|
||||||
41
src/main/java/Job/Job.java
Normal file
41
src/main/java/Job/Job.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package Job;
|
||||||
|
|
||||||
|
import Shared.HasExperience;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public abstract class Job implements HasExperience {
|
||||||
|
protected int level;
|
||||||
|
protected int experience;
|
||||||
|
protected String name;
|
||||||
|
|
||||||
|
Job(String name) {
|
||||||
|
this.name = name;
|
||||||
|
this.level = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
27
src/main/java/Job/Miner.java
Normal file
27
src/main/java/Job/Miner.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package Job;
|
||||||
|
|
||||||
|
import Action.Actor;
|
||||||
|
import Inventory.HasInventory;
|
||||||
|
|
||||||
|
|
||||||
|
public class Miner extends Job {
|
||||||
|
public Miner() {
|
||||||
|
super("Miner");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dig(Actor actor) {
|
||||||
|
if (actor instanceof HasInventory a) {
|
||||||
|
a.getInventory().add("Stone");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getExperience() {
|
||||||
|
return experience;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void gainExperience(int exp) {
|
||||||
|
experience += exp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
public class Player extends Character {
|
|
||||||
|
|
||||||
|
|
||||||
public Player(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void spawn(int x, int y) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
11
src/main/java/Shared/HasExperience.java
Normal file
11
src/main/java/Shared/HasExperience.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package Shared;
|
||||||
|
|
||||||
|
public interface HasExperience {
|
||||||
|
int getLevel();
|
||||||
|
|
||||||
|
int getExperience();
|
||||||
|
|
||||||
|
void gainExperience(int exp);
|
||||||
|
|
||||||
|
void levelUp();
|
||||||
|
}
|
||||||
25
src/test/java/MinerTest.java
Normal file
25
src/test/java/MinerTest.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import Job.Miner;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class MinerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void miner_can_level_up() {
|
||||||
|
var job = new Miner();
|
||||||
|
assertEquals(1, job.getLevel());
|
||||||
|
job.levelUp();
|
||||||
|
assertEquals(2, job.getLevel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void miner_can_gain_xp() {
|
||||||
|
var job = new Miner();
|
||||||
|
job.gainExperience(25);
|
||||||
|
assertEquals(25, job.getExperience());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
|
import Action.DigAction;
|
||||||
|
import Entity.Position;
|
||||||
|
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.*;
|
||||||
|
|
||||||
class PlayerTest {
|
class PlayerTest {
|
||||||
|
|
||||||
private Player defaultPlayer() {
|
private Player defaultPlayer() {
|
||||||
return new Player("abc");
|
return new Player("abc");
|
||||||
}
|
}
|
||||||
@@ -18,9 +21,26 @@ 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void miner_can_dig() {
|
||||||
|
var p = new Player("John");
|
||||||
|
var job = new Miner();
|
||||||
|
p.learnJob(job);
|
||||||
|
p.performAction(new DigAction());
|
||||||
|
assertTrue(p.getInventory().contains("Stone"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user