I was doing some reading on ECS/Composition over inheritance and decided to modify a text adventure game that I'm currently working on to better understand the concept.
I decided to try it using a specific weapon in my game.
Now, in my game the logic is that every Weapon is considered an IGameObject. With all this in mind I came up with the following:
Weapon Interface:
public interface IWeapon {
int getWeaponDamage();
}
Weapon class:
public class Weapon implements IWeapon {
private int damage;
public Weapon(int damage)
{
this.damage = damage;
}
@Override
public int getWeaponDamage() {
return this.damage;
}
IGameObject interface:
public interface IGameObject {
String getGameObjectID();
String getGameObjectName();
String getGameObjectDescription();
}
GameObject class:
public class GameObject implements IGameObject {
private String ID;
private String name;
private String Desc;
public GameObject(String ID, String name, String Desc)
{
this.ID = ID;
this.name = name;
this.Desc = Desc;
}
The abstract class that uses all this:
public abstract class GameGun implements IGameObject, IWeapon {
IGameObject gameObj;
IWeapon weaponObj;
//Left out getters to keep it simple and easy to read.
public GameGun(IGameObject game, IWeapon weapon)
{
this.gameObj = game;
this.weaponObj = weapon;
}
public void fire()
{
System.out.println("Shot Fired");
}
public void reload()
{
//implementation for reload method
}
Putting it all together:
Main Method:
GameObject sleep = new GameObject("SLP-1","Sleep Weapon","A Custom Made Gun");
Weapon sleepDamage = new Weapon(10);
GameGun sleepWeapon = new SleepWeapon(sleep,sleepDamage);
Questions:
I've been correcting many stuff and get it working. You can play with it now :D PLease take the gist in order to have all the working code.
CONSOLE
Revolver Gun:Gun reloaded
Revolver Gun:Gun shots => 10 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Revolver Gun:Gun shots => 10 damage(s)
CODE
package com.rizze.test.labs.sof.gamegun;
import org.junit.Test;
public class GameGunTest {
@Test
public void betterCode2() {
Game sleep = new Game("SLP-1","Sleep Weapon","A Custom Made Gun");
Gun gun = new Gun();
Sword sword = new Sword();
gun.hit();
sword.hit();
sword.hit();
sword.hit();
gun.hit();
}
public class Gun implements IWeapon {
private int damage;
private String name;
public Gun(int damage)
{
this.damage = damage;
this.reload();
}
public Gun()
{
this.damage = 10;
this.name= "Revolver Gun";
this.reload();
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( this.name + ":Gun shots => "+damage +" damage(s)");
return this.damage;
}
@Override
public void reload() {
System.out.println(this.name+":Gun reloaded");
}
}
public class Sword implements IWeapon {
private String name;
private int damage;
public Sword(String name){
this.damage = 5;
this.name=name;
}
public Sword(){
this.damage = 5;
this.name="Basic Sword";
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( name+ ":Sword cuts => "+damage +" damage(s)");
return damage;
}
@Override
public void reload() {
System.out.println( name+ ":Sword - skipped reload" );
}
}
public class Game implements IGame {
private String id;
private String name;
private String desc;
public Game(String ID, String name, String Desc)
{
this.id = ID;
this.name = name;
this.desc = Desc;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getName() {
return this.getName();
}
@Override
public String getDesc() {
return this.desc;
}
}
}
//IGAME
public interface IGame {
String getId();
String getName();
String getDesc();
}
//IWEAPON
package com.rizze.test.labs.sof.gamegun;
public interface IWeapon {
int getDamages();
int hit();
void reload();
}
GIST here is the complete GIST :https://gist.github.com/jeorfevre/92d093853bfd3fc5c666b915b309abf1