Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Allowing drop action in my inventory?

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 :)

like image 551
Kris Rice Avatar asked Oct 31 '22 13:10

Kris Rice


1 Answers

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

like image 144
Kris Rice Avatar answered Nov 09 '22 10:11

Kris Rice