Skip to content

Commit

Permalink
implemented tool durability for all tools (+inc version to 2.0.5-dev1)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj42 committed Oct 20, 2018
1 parent 91d28fa commit 3b5778d
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Game {
public static boolean HAS_GUI = true;

public static final String NAME = "Minicraft Plus"; // This is the name on the application window
public static final Version VERSION = new Version("2.0.4");
public static final Version VERSION = new Version("2.0.5-dev1");

public static InputHandler input; // input used in Game, Player, and just about all the *Menu classes.
public static Player player;
Expand Down
8 changes: 4 additions & 4 deletions src/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import minicraft.item.Items;
import minicraft.item.PotionType;
import minicraft.item.ToolItem;
import minicraft.item.ToolType;
import minicraft.level.Level;
import minicraft.screen.LoadingDisplay;
import minicraft.screen.RelPos;
Expand Down Expand Up @@ -250,9 +249,10 @@ else if(Game.isValidClient()) {
}
}

// FISHING ROD STATUS
if (player.activeItem instanceof ToolItem && ((ToolItem)player.activeItem).type == ToolType.FishingRod) {
int dura = ((ToolItem)player.activeItem).dur * 100 / ((ToolItem)player.activeItem).type.durability;
// TOOL DURABILITY STATUS
if (player.activeItem instanceof ToolItem) {
ToolItem tool = (ToolItem) player.activeItem;
int dura = tool.dur * 100 / (tool.type.durability * (tool.level+1));
//if (dura > 100) dura = 100;
Font.draw(dura + "%", screen, 164, Screen.h - 16, Color.get(0, 30));
}
Expand Down
22 changes: 16 additions & 6 deletions src/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,11 @@ protected void attack() {
// the player is holding a tool, and has stamina available.
ToolItem tool = (ToolItem) activeItem;

if (tool.type == ToolType.Bow && inventory.count(Items.arrowItem) > 0) { // if the player is holding a bow, and has arrows...
if (tool.type == ToolType.Bow && tool.dur > 0 && inventory.count(Items.arrowItem) > 0) { // if the player is holding a bow, and has arrows...
if (!Game.isMode("creative")) inventory.removeItem(Items.arrowItem);
level.add(new Arrow(this, attackDir, tool.level));
attackTime = 10;
if (!Game.isMode("creative")) tool.dur--;
return; // we have attacked!
}
}
Expand Down Expand Up @@ -579,14 +580,17 @@ protected void attack() {
if (activeItem == null || activeItem.canAttack()) { // if there is no active item, OR if the item can be used to attack...
attackTime = 5;
// attacks the enemy in the appropriate direction.
hurt(getInteractionBox(ATTACK_DIST));
boolean used = hurt(getInteractionBox(ATTACK_DIST));

// attempts to hurt the tile in the appropriate direction.
Point t = getInteractionTile();
if (t.x >= 0 && t.y >= 0 && t.x < level.w && t.y < level.h) {
Tile tile = level.getTile(t.x, t.y);
tile.hurt(level, t.x, t.y, this, random.nextInt(3) + 1, attackDir);
used = tile.hurt(level, t.x, t.y, this, random.nextInt(3) + 1, attackDir) || used;
}

if(used && activeItem instanceof ToolItem)
((ToolItem)activeItem).payDurability();
}
}

Expand Down Expand Up @@ -644,13 +648,19 @@ private boolean interact(Rectangle area) {
}

/** same, but for attacking. */
private void hurt(Rectangle area) {
private boolean hurt(Rectangle area) {
List<Entity> entities = level.getEntitiesInRect(area);
int maxDmg = 0;
for (int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
if (e != this && e instanceof Mob) ((Mob)e).hurt(this, getAttackDamage(e), attackDir); // note: this really only does something for mobs.
if (e != this && e instanceof Mob) {
int dmg = getAttackDamage(e);
maxDmg = Math.max(dmg, maxDmg);
((Mob)e).hurt(this, dmg, attackDir); // note: this really only does something for mobs.
}
if (e != this && e instanceof Furniture) e.interact(this, null, attackDir); // note: this really only does something for mobs.
}
return maxDmg > 0;
}

/**
Expand Down Expand Up @@ -961,7 +971,7 @@ protected String getUpdateString() {
";hunger,"+hunger+
";attackTime,"+attackTime+
";attackDir,"+attackDir.ordinal()+
";activeItem,"+(activeItem==null?"null": activeItem.getName());
";activeItem,"+(activeItem==null?"null": activeItem.getData());

return updates;
}
Expand Down
4 changes: 4 additions & 0 deletions src/minicraft/item/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static Item get(String name, boolean allowNull) {
name = name.toUpperCase();
//System.out.println("fetching name: \"" + name + "\"");
int amount = 1;
boolean hadUnderscore = false;
if(name.contains("_")) {
hadUnderscore = true;
try {
amount = Integer.parseInt(name.substring(name.indexOf("_")+1));
} catch(Exception ex) {
Expand Down Expand Up @@ -90,6 +92,8 @@ public static Item get(String name, boolean allowNull) {
if(i != null) {
if(i instanceof StackableItem)
((StackableItem)i).count = amount;
if(i instanceof ToolItem && hadUnderscore)
((ToolItem)i).dur = amount;
return i.clone();
} else {
System.out.println(Network.onlinePrefix()+"ITEMS GET: invalid name requested: \"" + name + "\"");
Expand Down
24 changes: 17 additions & 7 deletions src/minicraft/item/ToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ protected static ArrayList<Item> getAllInstances() {

private Random random = new Random();

public static final int MAX_LEVEL = 5; // How many different levels of tools there are
public static final String[] LEVEL_NAMES = {"Wood", "Rock", "Iron", "Gold", "Gem"}; // The names of the different levels. A later level means a stronger tool.

public ToolType type; // Type of tool (Sword, hoe, axe, pickaxe, shovel)
public int level; // Level of said tool
public int dur; // the durability of the tool; currently only used for fishing rod.
// TODO implement durabilities for all tools?
public int dur; // the durability of the tool

public static final int[] LEVEL_COLORS = { // Colors of the tools, same position as LEVEL_NAMES
Color.get(-1, 100, 321, 431), // wood
Expand All @@ -63,7 +61,7 @@ public ToolItem(ToolType type, int level) {
this.type = type;
this.level = level;

dur = type.durability; // initial durability fetched from the ToolType
dur = type.durability * (level+1); // initial durability fetched from the ToolType
}

private static int getColor(ToolType type, int level) {
Expand All @@ -87,10 +85,9 @@ public String getDisplayName() {

@Override
public boolean interactOn(Tile tile, Level level, int xt, int yt, Player player, Direction attackDir) {
if (type == ToolType.FishingRod && dur > 0) {
if (tile == Tiles.get("water")) {
if (type == ToolType.FishingRod && tile == Tiles.get("water")) {
if (payDurability()) {
player.goFishing(player.x - 5, player.y - 5);
if(!Game.isMode("creative")) dur--;
return true;
}
}
Expand All @@ -107,8 +104,16 @@ public boolean canAttack() {
return true;
}

public boolean payDurability() {
if(dur <= 0) return false;
if(!Game.isMode("creative")) dur--;
return true;
}

/** Gets the attack damage bonus from an item/tool (sword/axe) */
public int getAttackDamageBonus(Entity e) {
if(!payDurability())
return 0;

if(e instanceof Mob) {
if (type == ToolType.Axe) {
Expand All @@ -128,6 +133,11 @@ public int getAttackDamageBonus(Entity e) {
return 0;
}

@Override
public String getData() {
return super.getData()+"_"+dur;
}

/** Sees if this item equals another. */
@Override
public boolean equals(Item item) {
Expand Down
16 changes: 8 additions & 8 deletions src/minicraft/item/ToolType.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package minicraft.item;

public enum ToolType {
Shovel (0),
Hoe (1),
Sword (2),
Pickaxe (3),
Axe (4),
Bow (5),
FishingRod (6, 15), // if there's a second number, it specifies durability.
Claymore (7);
Shovel (0, 24), // if there's a second number, it specifies durability.
Hoe (1, 20),
Sword (2, 42),
Pickaxe (3, 28),
Axe (4, 24),
Bow (5, 20),
FishingRod (6, 16),
Claymore (7, 34);
//Hatchet (10),
//Spade (11),
//Pick (12);
Expand Down
3 changes: 2 additions & 1 deletion src/minicraft/level/tile/CactusTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public boolean mayPass(Level level, int x, int y, Entity e) {
return false;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
int damage = level.getData(x, y) + dmg;
int cHealth = 10;
if (Game.isMode("creative")) dmg = damage = cHealth;
Expand All @@ -44,6 +44,7 @@ public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attac
} else {
level.setData(x, y, damage);
}
return true;
}

public void bumpedInto(Level level, int x, int y, Entity entity) {
Expand Down
5 changes: 3 additions & 2 deletions src/minicraft/level/tile/CloudCactusTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ public boolean mayPass(Level level, int x, int y, Entity e) {
return e instanceof AirWizard;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
hurt(level, x, y, 0);
return true;
}

public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Pickaxe) {
if (player.payStamina(6 - tool.level)) {
if (player.payStamina(6 - tool.level) && tool.payDurability()) {
hurt(level, xt, yt, 1);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/minicraft/level/tile/DirtTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Shovel) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("hole"));
level.dropItem(xt*16+8, yt*16+8, Items.get("dirt"));
Sound.monsterHurt.play();
return true;
}
}
if (tool.type == ToolType.Hoe) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("farmland"));
Sound.monsterHurt.play();
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/minicraft/level/tile/DoorTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Pickaxe) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get(id+3)); // will get the corresponding floor tile.
level.dropItem(xt*16+8, yt*16+8, Items.get(type.name() + " Door"));
Sound.monsterHurt.play();
Expand All @@ -61,11 +61,12 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
return false;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
if(source instanceof Player) {
boolean closed = level.getData(x, y) == 0;
level.setData(x, y, closed?1:0);
}
return false;
}

public boolean mayPass(Level level, int x, int y, Entity e) {
Expand Down
2 changes: 1 addition & 1 deletion src/minicraft/level/tile/FarmTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Shovel) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("dirt"));
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/minicraft/level/tile/FloorTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Pickaxe) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("hole"));
Item drop;
switch(type) {
Expand Down
5 changes: 3 additions & 2 deletions src/minicraft/level/tile/FlowerTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public boolean interact(Level level, int x, int y, Player player, Item item, Dir
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Shovel) {
if (player.payStamina(2 - tool.level)) {
if (player.payStamina(2 - tool.level) && tool.payDurability()) {
level.dropItem(x*16+8, y*16+8, Items.get("Flower"));
level.dropItem(x*16+8, y*16+8, Items.get("Rose"));
level.setTile(x, y, Tiles.get("grass"));
Expand All @@ -50,9 +50,10 @@ public boolean interact(Level level, int x, int y, Player player, Item item, Dir
return false;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
level.dropItem(x*16+8, y*16+8, 1, 2, Items.get("Flower"));
level.dropItem(x*16+8, y*16+8, 0, 1, Items.get("Rose"));
level.setTile(x, y, Tiles.get("grass"));
return true;
}
}
4 changes: 2 additions & 2 deletions src/minicraft/level/tile/GrassTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Shovel) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("dirt"));
Sound.monsterHurt.play();
if (random.nextInt(5) == 0) {
Expand All @@ -57,7 +57,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
}
}
if (tool.type == ToolType.Hoe) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
Sound.monsterHurt.play();
if (random.nextInt(5) == 0) {
level.dropItem(xt*16+8, yt*16+8, Items.get("seeds"));
Expand Down
6 changes: 3 additions & 3 deletions src/minicraft/level/tile/HardRockTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ public boolean mayPass(Level level, int x, int y, Entity e) {
return false;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
hurt(level, x, y, 0);
return true;
}

public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (Game.isMode("creative")) return true;
if (tool.type == ToolType.Pickaxe && tool.level == 4) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
hurt(level, xt, yt, random.nextInt(10) + (tool.level) * 5 + 10);
return true;
}
}
else Game.notifications.add("Gem Pickaxe Required.");
}
return Game.isMode("creative");

}

public void hurt(Level level, int x, int y, int dmg) {
Expand Down
2 changes: 1 addition & 1 deletion src/minicraft/level/tile/LavaBrickTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Pickaxe) {
if (player.payStamina(4 - tool.level)) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
level.setTile(xt, yt, Tiles.get("lava"));
Sound.monsterHurt.play();
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/minicraft/level/tile/OreTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ public boolean mayPass(Level level, int x, int y, Entity e) {
return false;
}

public void hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
public boolean hurt(Level level, int x, int y, Mob source, int dmg, Direction attackDir) {
int playHurt;
if (Game.isMode("creative")) playHurt = random.nextInt(4);
else {
playHurt = 0;
}
hurt(level, x, y, playHurt);
return true;
}

public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Pickaxe) {
if (player.payStamina(6 - tool.level)) {
if (player.payStamina(6 - tool.level) && tool.payDurability()) {
hurt(level, xt, yt, 1);
return true;
}
Expand Down
Loading

0 comments on commit 3b5778d

Please sign in to comment.