Skip to content

Commit

Permalink
fixed bugs with item usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj42 committed May 9, 2018
1 parent 5fba477 commit 3b00526
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ public void handleInput(Vector2 mouseInput) {

if(!isKnockedBack()) {
if(ClientCore.input.pressingKey(Input.Keys.C))
ClientCore.getClient().send(new InteractRequest(true, new PositionUpdate(this), getDirection()));
ClientCore.getClient().send(new InteractRequest(true, new PositionUpdate(this), getDirection(), hands.getSelection()));
else if(ClientCore.input.pressingKey(Input.Keys.V))
ClientCore.getClient().send(new InteractRequest(false, new PositionUpdate(this), getDirection()));
ClientCore.getClient().send(new InteractRequest(false, new PositionUpdate(this), getDirection(), hands.getSelection()));
}
//if(Gdx.input.isKeyPressed(Input.Keys.C) || Gdx.input.isKeyPressed(Input.Keys.V))
// animator.requestState(AnimationState.ATTACK);
Expand Down
6 changes: 4 additions & 2 deletions core/game/src/miniventure/game/GameProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,14 @@ class InteractRequest {
public final boolean attack;
public final PositionUpdate playerPosition;
public final Direction dir;
public final int hotbarIndex;

private InteractRequest() { this(false, null, null); }
public InteractRequest(boolean attack, PositionUpdate playerPosition, Direction dir) {
private InteractRequest() { this(false, null, null, 0); }
public InteractRequest(boolean attack, PositionUpdate playerPosition, Direction dir, int hotbarIndex) {
this.attack = attack;
this.playerPosition = playerPosition;
this.dir = dir;
this.hotbarIndex = hotbarIndex;
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/game/src/miniventure/game/item/Hands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean addItem(Item item) {
}

public void setSelection(int idx) { selection = idx; }
protected int getSelection() { return selection; }
public int getSelection() { return selection; }

void swapItem(Inventory inv, int invIdx, int hotbarIdx) {
Item item = inv.replaceItemAt(invIdx, getItemAt(hotbarIdx));
Expand Down
2 changes: 2 additions & 0 deletions core/game/src/miniventure/game/item/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Item getItemAt(int idx) {
Item replaceItemAt(int idx, Item item) {
checkIndex(idx);
// okay to replace with hand item.
if(item == null)
item = hand;

Item cur = items[idx];
itemCounter.removeInstance(cur);
Expand Down
2 changes: 2 additions & 0 deletions core/server/src/miniventure/game/item/ServerHands.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void resetItemUsage() {
dropItem(newItem); // if there was a new item, and it couldn't be picked up, then the count is not decreased.
}
}*/
if(newItem == null && player.getInventory().removeItem(item))
newItem = item.copy();
replaceItemAt(getSelection(), newItem);

ServerCore.getServer().sendToPlayer(player, new InventoryUpdate(player.getInventory(), player.getHands()));
Expand Down
1 change: 1 addition & 0 deletions core/server/src/miniventure/game/server/GameServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public void received (Connection connection, Object object) {
connection.sendTCP(new PositionUpdate(client)); // fix the player's position

client.setDirection(r.dir);
client.getHands().setSelection(r.hotbarIndex);
if(r.attack) client.attack();
else client.interact();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private Array<WorldObject> getInteractionQueue() {
}

public void attack() {
//System.out.println("server player attacking; dir="+getDirection()+", pos="+getPosition(true));
//System.out.println("server player attacking; item = "+hands.getSelectedItem());
if(!hands.hasUsableItem()) return; // only ever not true for attacks in the same frame or if not enough stamina

Level level = getLevel();
Expand Down Expand Up @@ -202,6 +202,7 @@ public void attack() {
}

public void interact() {
//System.out.println("server player interacting; item = "+hands.getSelectedItem());
if(!hands.hasUsableItem()) return;

Item heldItem = hands.getSelectedItem();
Expand Down

0 comments on commit 3b00526

Please sign in to comment.