5

For my game I've implemented an inventory system. When the screen is clicked, a MousePressedEventis passed through all layers in the game, to all objects that inherit EventListener (My EventListener). The EventListener class works fine and, using it as shown below, I've managed to get my inventory so that you can remove items from a slot and put them back. What I'd like to do however is be able to take them out of any slot that contains items, and place them in any other of the slots (as long as the target slot is empty). I thought what I had would allow for this, as in the if statement I don't check that if the slot is selected, I add it to the slot regardless. But this doesn't actually work. Any ideas?

Code in Slot.java class:

public boolean onMousePressed(MousePressedEvent e) {
    Point p = new Point(Mouse.getX(), Mouse.getY());
    if (!this.getBounds().contains(p)) return false;
    boolean left = (e.getButton() == MouseEvent.BUTTON1);
    boolean right = (e.getButton() == MouseEvent.BUTTON3);
    boolean hasItems = (items.size() > 0);
    if (this.getBounds().contains(p)){
        if (right && !selected && hasItems){
            select(true);
            s = new Slot(new Vector2i(Mouse.getX(), Mouse.getY()));
            addComponent(s);
            s.add(items.get(0));
            remove(items.get(items.size() - 1));
        } else if (right && selected){
            s.add(items.get(0));
            remove(items.get(items.size() - 1));
            if (items.size() == 0) {
                setBackgroundImage(ImageUtil.getImage("/ui/panels/inventory/slot.png"));
                selected = false;
                return true;
            }
            return true;
        } else if ((left || right) && s==null) {
            return true;
        } else if (left && s != null){ //If left clicked, add to the slot from s regardless of if we are selected.
            add(s.getItems().get(0));
            s.remove(s.getItems().get(s.getItems().size() - 1));
            if (s.getItems().size() == 0){
                s.setBackgroundImage(ImageUtil.getImage("/ui/panels/inventory/slot.png"));
                removeComponent(s);
                s = null;
                selected = false;
                return true;
            }
        }
    }
    return false;
}

In pseudo code:

If (Mouse is clicked) :
  if (the mouse isn't the bounds of the slot) return false (alert we haven't handled the event)
  if (we contain the mouse cursor) :
    if (right is pressed and we aren't selected) :
      select
      create a temporary slot at the mouse location
      remove item from this slot
      add it to the temporary slot
      return true
    else if (right is pressed and we are selected) :
      add item to temporary slot
      remove item from selected slot
      return true
    else if (we press left or right while temporary slot is null) :
      return true (tell the dispatcher we have handled the event)
    //This following else if statement is supposed to add an item to a clicked slot whether that slot is selected or not, but doesn't work
    else if (left is pressed and temporary slot isn't null) :
      add the item to the clicked slot
      remove it from the temporary one
      return true
  return false if none of the above applies

Thanks :)

Kris Rice
  • 559
  • 1
  • 5
  • 23
  • 1
    I don't see how this code relates to the description. Can you strip down the code to a minimal example and give an explanation of what it is doing and what it isn't doing in programming terms like lists and loops and ifs and such? The concepts of your game aren't really relevant for the code. – zapl Dec 09 '15 at 17:12
  • 1
    @zapl better? I added a pseudo code version to clarify things – Kris Rice Dec 09 '15 at 17:23
  • 1
    Can you specify what part of your pseudo code isn't working as you expect? There's a lot of edge cases.... – Roddy of the Frozen Peas Dec 09 '15 at 17:24
  • 1
    I actually meant the other way round, like "I remove an object from a list and in the next iteration of the loop there is a null" :) Most importantly, I still don't understand what misbehaves in your program code. – zapl Dec 09 '15 at 17:26
  • 1
    @zapl no nulls or anything. I believe it's just a logic error but i can't seem to find it. I want to know if the code logically makes sense because for some reason the if statement that should allow you to add items to a slot if it isn't selected doesn't work – Kris Rice Dec 09 '15 at 17:28
  • 1
    @RoddyoftheFrozenPeas added :) – Kris Rice Dec 09 '15 at 17:29
  • 1
    When you put a println in all the if/else cases, do you see it printed, i.e. does the code ever reach the if statement you want? Maybe you constructed your if/else conditions in such a way that it goes into a different case. – zapl Dec 09 '15 at 17:45
  • 1
    @zapl thanks for the suggestion, I added the print lines and found the error :') – Kris Rice Dec 09 '15 at 17:51

1 Answers1

2

By adding in print lines in each else if statement, I found that when I try to add an item from the temporary slot into another slot, the temporary slot is null. This is due to the fact that when the temporary slot is created, it is created by the instance of slot you selected the first time, so the one you are trying to add doesn't have access to the temp slot. To get around this, I moved the temp slot as a per-instance variable, by making it static. The code now works fine

Kris Rice
  • 559
  • 1
  • 5
  • 23