This site requires JavaScript, please enable it in your browser!
Greenfoot back
XardasTheSilent
XardasTheSilent wrote ...

2019/11/22

Need help with making an inventory with Singleton pattern and ArrayList

XardasTheSilent XardasTheSilent

2019/11/22

#
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Inventory {

    private static final Inventory intstance = new Inventory();

    private Inventory() {

    }

    public void showInventory() {
        System.out.println("Inventory is empty");
    }

    public static Inventory getInstance(){
        return intstance;
    }

    public List<Inventory> itemInv = new List<Inventory>() {
        @Override
        public int size() {
            return 0;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public boolean contains(Object o) {
            return false;
        }

        @Override
        public Iterator<Inventory> iterator() {
            return null;
        }

        @Override
        public Object[] toArray() {
            return new Object[0];
        }

        @Override
        public <T> T[] toArray(T[] a) {
            return null;
        }

        @Override
        public boolean add(Inventory inventory) {
            return false;
        }

        @Override
        public boolean remove(Object o) {
            return false;
        }

        @Override
        public boolean containsAll(Collection<?> c) {
            return false;
        }

        @Override
        public boolean addAll(Collection<? extends Inventory> c) {
            return false;
        }

        @Override
        public boolean addAll(int index, Collection<? extends Inventory> c) {
            return false;
        }

        @Override
        public boolean removeAll(Collection<?> c) {
            return false;
        }

        @Override
        public boolean retainAll(Collection<?> c) {
            return false;
        }

        @Override
        public void clear() {

        }

        @Override
        public Inventory get(int index) {
            return null;
        }

        @Override
        public Inventory set(int index, Inventory element) {
            return null;
        }

        @Override
        public void add(int index, Inventory element) {

        }

        @Override
        public Inventory remove(int index) {
            return null;
        }

        @Override
        public int indexOf(Object o) {
            return 0;
        }

        @Override
        public int lastIndexOf(Object o) {
            return 0;
        }

        @Override
        public ListIterator<Inventory> listIterator() {
            return null;
        }

        @Override
        public ListIterator<Inventory> listIterator(int index) {
            return null;
        }

        @Override
        public List<Inventory> subList(int fromIndex, int toIndex) {
            return null;
        }
    };

}
XardasTheSilent XardasTheSilent

2019/11/22

#
I'm a beginner with coding and doing a project for school. The project is about making a simple platforming game but I wanted to add an inventory to make it more interesting, my teacher told me to use Singleton Design Pattern with an Arraylist to make an inventory system. This is the code I have now.
danpost danpost

2019/11/22

#
I am puzzled on how you can have a list of Inventory objects while having a singleton Inventory object. You should probably be listing Object or Actor objects; although, you could add a Collectible interface and list Collectible objects. It would help to know exactly what the inventory is to be used for so that any help given will not be in vane. I am not totally convinced that a Singleton Design Pattern is in order here.
XardasTheSilent XardasTheSilent

2019/11/22

#
Thank you for your reply. I was thinking of using an inventory to save the objects picked up by the hero class so for example: If the hero has picked up a specific color key or crystal it would go to the inventory so if you go to the next level the hero remember what it has. My idea for it is to make an easter egg at the end of the game.
XardasTheSilent XardasTheSilent

2019/11/22

#
This is what I have now.
public class Inventory {

    private static final Inventory instance = new Inventory();

    private Inventory() {

    }

    public void showInventory() {
        System.out.println(inventory);
    }

    public static Inventory getInstance(){
        return instance;
    }

    ArrayList<String> inventory = new ArrayList<String>();


    public void addItemToInventory() {
        inventory.add("Key");
        inventory.add("Crystal");
    }
}
XardasTheSilent XardasTheSilent

2019/11/22

#
Update: I have it semi-working now.
public class Inventory {

    private static final Inventory instance = new Inventory();

    private Inventory() {

    }

    public void showInventory() {
        System.out.println(inventory);
    }

    public static Inventory getInstance() {
        return instance;
    }

    ArrayList<String> inventory = new ArrayList<>();


    public void addItemToInventory(Collectable.type type, Collectable.color color) {
        inventory.add(type + " " + color);
    }
}
The Collectable class knows which type and color the collectables are
public class Collectable extends Mover {

/*
Dit zorgt er voor dat een collectable op z'n plek blijft staan als je het oppakt.
Je geeft de methode de waarde (die in Scoreboard.java staat) true aan in ScoreBoard.class zodat applyVelocity(); word toegepast.
*/

    public enum type {

        CRYSTAL,
        KEY,
        COIN,
        LIFE,
    }

    public enum color {

        RED,
        GREEN,
        BLUE,
        YELLOW,
        SILVER,
        GOLD,
        CRIMSON,
    }

    public type getCollectibleType() {
        return collectibleType;
    }

    public color getCollectibleColor() {
        return collectibleColor;
    }

    private Collectable.type collectibleType;
    private Collectable.color collectibleColor;

    public boolean isAlwaysOnScreen() {
        return alwaysOnScreen;
    }

    public void setAlwaysOnScreen(boolean alwaysOnScreen) {
        this.alwaysOnScreen = alwaysOnScreen;
    }

    private boolean alwaysOnScreen =  false;

    public Collectable(Collectable.type type, Collectable.color color) {
        super();
        this.collectibleType = type;
        this.collectibleColor = color;

        if(Collectable.type.CRYSTAL == type) {
            if(Collectable.color.RED == color) {
                setImage("gemRed.png");
            }
            if(Collectable.color.GREEN == color) {
                setImage("gemGreen.png");
            }
            if(Collectable.color.BLUE == color) {
                setImage("gemBlue.png");
            }
            if(Collectable.color.YELLOW == color) {
                setImage("gemYellow.png");
            }
        }

        if(Collectable.type.KEY == type) {
            if(Collectable.color.RED == color) {
                setImage("keyRed.png");
            }
            if(Collectable.color.GREEN == color) {
                setImage("keyGreen.png");
            }
            if(Collectable.color.BLUE == color) {
                setImage("keyBlue.png");
            }
            if(Collectable.color.YELLOW == color) {
                setImage("keyYellow.png");
            }
        }

        if(Collectable.type.COIN == type) {
            if(Collectable.color.SILVER == color) {
                setImage("coinSilver.png");
            }
            if(Collectable.color.GOLD == color) {
                setImage("coinGold.png");
            }
        }

        if (Collectable.type.LIFE == type) {
            if (Collectable.color.CRIMSON == color) {
                setImage("hud_heartFull.png");
            }
        }
    }
And here is how the hero uses it
 for (Actor collectible : getIntersectingObjects(Collectable.class)) {
            Collectable col = (Collectable) collectible;
            if (col != null) {
//                Hier (buiten de ifs die bepalen welke type/kleur) declareren en initialiseren van de x en y positie van waar de crystallen en sleutels komen te staan op het scherm
                int xPos = 0;
                int yPos = 0;
                    if(col.getCollectibleType() == Collectable.type.KEY){
                        Greenfoot.playSound("PickUpKey.wav");
                        xPos = 90;
                        yPos = 80;
                        heroHasKey = true;
                    } else if
                    (col.getCollectibleType() == Collectable.type.CRYSTAL) {
                        Greenfoot.playSound("DOOM.wav");
                        xPos = 90;
                        yPos = 130;
                    }

                    else if (col.getCollectibleType() == Collectable.type.COIN) {
                        Greenfoot.playSound("PickUpCoin.wav");
                        if(col.getCollectibleColor() == Collectable.color.SILVER) {
                            xPos = xPosSilverCoin;
                            xPosSilverCoin -= 15;
                            yPos = 60;
                            heroScore += 5;
                        }
                        else {
                            xPos = xPosGoldCoin;
                            xPosGoldCoin -= 15;
                            yPos = 100;
                            heroScore += 10;
                        }
                    }
                    if (heroScore == 20) {
                        getWorld().addObject(new Collectable(Collectable.type.LIFE, Collectable.color.CRIMSON, true), 90,30 );
                        Greenfoot.playSound("1UP.wav");
                        heroScore = 0;
                }
                Inventory.getInstance().addItemToInventory(col.getCollectibleType(), col.getCollectibleColor());
//                 Dit staat hier zodat een 'groot stuk' code gelijk in 1 keer word uitgevoerd ipv elke keer opnieuw voor een nieuwe kleur sleutel/crystal.
                getWorld().addObject(new Collectable(col.getCollectibleType(), col.getCollectibleColor(),true), xPos, yPos);

//                 Nadat we alles hebben gecontroleerd van de collectible willen wij hem pas verwijderen
                getWorld().removeObject(col);
            }
        }
danpost danpost

2019/11/22

#
If it is just inventory of the hero, you only need a List<Collectable> inventory field for the hero. No need to have an Inventory class. When changing levels, either use same hero instance or assign inventory (of that hero instance) to new hero instance.
XardasTheSilent XardasTheSilent

2019/11/27

#
Sorry for the late reply. Could you give me an example? I think I know what you mean but i'm not sure.
XardasTheSilent XardasTheSilent

2019/11/27

#
My idea for the inventory is that if the hero goes to another level that it keeps it's inventory. So for example it will keep the green key if the hero switches level. My teacher helped and this is what I have now.
public List<Collectable> getInventory() {
        return inventory;
    }

    public boolean hasKey(Collectable.color color) {
        for (Collectable col: inventory) {
            if(col.getCollectibleType() == Collectable.type.KEY && col.getCollectibleColor() == color) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns a list of keys with the color
     * @param color
     * @return returns null if no key has been found
     */
    public List<Collectable> getKeys(Collectable.color color) {
        if(hasKey(color)) {
            List<Collectable> list = new ArrayList<>();
            // For loop to fill the list
            
            return list;
        }
        return null;
    }
XardasTheSilent XardasTheSilent

2019/11/27

#
Nvm. Teacher misunderstood. The Getkeys method is something that I don't need.
danpost danpost

2019/11/27

#
XardasTheSilent wrote...
Sorry for the late reply. Could you give me an example? I think I know what you mean but i'm not sure.
My idea was simply like this:
public Player extends Actor
{
    public ArrayList<Collectable> inventory = new ArrayList<Collectable>();
    
    // etc.
}
without an Inventory class.
XardasTheSilent XardasTheSilent

2019/11/29

#
Thank you for the example. I modified my inventory a little and know I have this:
public boolean hasKey(Collectable.color color) {
        for (Collectable col : inventory) {
            if(col.getCollectibleType() == Collectable.type.KEY && col.getCollectibleColor() == color) {
                return true;
            }
        }
        return false;
    }

public boolean hasCrystal(Collectable.color color) {
        for (Collectable col : inventory) {
            if(col.getCollectibleType() == Collectable.type.CRYSTAL && col.getCollectibleColor() == color) {
                return true;
            }
        }
A for loop in the hero makes it that it knows which key it is and which level to start up
for (Actor door : getIntersectingObjects(Door.class)) {
            if (door != null) {
                if(Inventory.getInstance().hasKey(Collectable.color.GREEN)) {
                    Greenfoot.setWorld(new TestLevel2());
                }

                if(Inventory.getInstance().hasKey(Collectable.color.RED)) {
                    Greenfoot.setWorld(new TestLevel3());
                }

                if(Inventory.getInstance().hasKey(Collectable.color.BLUE)) {
                    Greenfoot.setWorld(new TestLevel4());
                }

                if(Inventory.getInstance().hasKey(Collectable.color.YELLOW)) {
//                  Volgend level
                }

                if(Inventory.getInstance().hasAllKeys() && Inventory.getInstance().hasAllCrystals()) {
                    Greenfoot.setWorld(new TestEasterEgg());
                }
            }
        }
You need to login to post a reply.