Skip to content

Commit

Permalink
generalized items states, with compatiblity code for RingOfLife
Browse files Browse the repository at this point in the history
  • Loading branch information
nhnb committed Sep 25, 2023
1 parent 49a0647 commit 119c364
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/games/stendhal/server/entity/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public static void generateRPClass() {

// marked scroll
entity.addAttribute("dest", Type.STRING, Definition.VOLATILE);

// state (e. g. broken)
entity.addAttribute("state", Type.INT);
}


Expand Down
20 changes: 20 additions & 0 deletions src/games/stendhal/server/entity/item/RingOfLife.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import games.stendhal.server.entity.RPEntity;
import games.stendhal.server.entity.player.Player;
import marauroa.common.game.RPObject;


/**
Expand All @@ -23,6 +24,7 @@
public class RingOfLife extends Item {
public RingOfLife(final String name, final String clazz, final String subclass, final Map<String, String> attributes) {
super(name, clazz, subclass, attributes);
updateState();
}

/**
Expand All @@ -32,6 +34,7 @@ public RingOfLife(final String name, final String clazz, final String subclass,
*/
public RingOfLife(final RingOfLife item) {
super(item);
updateState();
}

/**
Expand All @@ -40,6 +43,21 @@ public RingOfLife(final RingOfLife item) {
public RingOfLife() {
super("emerald ring", "ring", "emerald-ring", null);
put("amount", 1);
updateState();
}

private void updateState() {
if (isBroken()) {
put("state", 1);
} else {
put("state", 0);
}
}

@Override
public void fill(RPObject rpobject) {
super.fill(rpobject);
updateState();
}

/**
Expand All @@ -57,11 +75,13 @@ public boolean isBroken() {
*/
public void damage() {
put("amount", 0);
put("state", 1);
}

@Override
public void repair() {
put("amount", 1);
put("state", 0);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions srcjs/stendhal/entity/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,8 @@ export class Item extends Entity {
}

override draw(ctx: CanvasRenderingContext2D) {
if (this["name"] === "emerald ring") {
// FIXME: can we put breakable item in its own class?
if (this["amount"] == 0) {
this.sprite.offsetY = 32;
} else {
this.sprite.offsetY = 0;
}
} else if (this.isAnimated()) {
this.stepAnimation();
}
this.sprite.offsetY = (this["state"] || 0) * 32
this.stepAnimation();

this.drawAt(ctx, this["x"] * 32, this["y"] * 32);
}
Expand Down
6 changes: 2 additions & 4 deletions srcjs/stendhal/ui/component/ItemContainerImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ export class ItemContainerImplementation {
this.dirty = this.dirty || o !== (e as any).dataItem;
const item = <Item> o;
let xOffset = 0;
let yOffset = 0;
if (item["name"] === "emerald ring" && item["amount"] == 0) {
yOffset = -32;
} else if (item.isAnimated()) {
let yOffset = (item["state"] || 0) * -32;
if (item.isAnimated()) {
item.stepAnimation();
xOffset = -(item.getXFrameIndex() * 32);
}
Expand Down

0 comments on commit 119c364

Please sign in to comment.