package com.lucasdnd.ags.gameplay; import java.util.ArrayList; import java.util.Random; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame; import com.lucasdnd.ags.gameplay.market.Market; import com.lucasdnd.ags.gameplay.market.MarketConsole; import com.lucasdnd.ags.gameplay.market.MarketGame; import com.lucasdnd.ags.map.Map; import com.lucasdnd.ags.map.terrain.Terrain; import com.lucasdnd.ags.system.Camera; import com.lucasdnd.ags.system.MainState; import com.lucasdnd.ags.ui.ViewGamesList; /** * This is one instance of the Store * @author tulio * */ public class Store { private String name; // The playable Map private Map map; // The Entities private ArrayList<Character> characters; private ArrayList<Table> tables; private ArrayList<Shelf> shelves; private ArrayList<Game> games; private ArrayList<Tv> tvs; private ArrayList<Console> consoles; private ArrayList<Misc> miscs; private int gameStorageLimit; // This is the max number of games the player can have at a time. Influenced by the amount of Shelves in the Store private int rating; // The store rating, kinda thumbs up - thumbs down total /** * Basic Constructor * @param map */ public Store(String name, Map map, Market market) { // Set the Map this.name = name; this.map = map; // Basic Attributes gameStorageLimit = 0; rating = 0; // Entities characters = new ArrayList<Character>(); tables = new ArrayList<Table>(); shelves = new ArrayList<Shelf>(); games = new ArrayList<Game>(); tvs = new ArrayList<Tv>(); consoles = new ArrayList<Console>(); miscs = new ArrayList<Misc>(); } /** * Updates the Map and the Stores' Entities * @param container * @param game * @param delta * @param input * @param mouseX * @param mouseY * @throws SlickException */ public void update(GameContainer container, StateBasedGame game, int delta, Input input, boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY, int currentMode, Business business, Asset placingAsset, Camera camera, MainState mainState, boolean isShiftDown, ViewGamesList viewGamesList, int simulationSpeed, Market market, int month) throws SlickException { // Updates the Map map.update(delta, currentMode, placingAsset, mouseX, mouseY, tables, leftMouseClicked, leftMouseDown, business); // Updates the Characters for(Character c : characters) { // Common to all Characters c.setxOffset(camera.getxOffset()); c.setyOffset(camera.getyOffset()); if(c instanceof Customer) { // Customer update! ((Customer)c).update(container, game, delta, input, leftMouseClicked, mouseX, mouseY, business, tables, shelves, viewGamesList, simulationSpeed, market, month); } } // Updates the Tables for(Table t : tables) { t.setxOffset(camera.getxOffset()); t.setyOffset(camera.getyOffset()); t.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, isShiftDown, business, mainState); } // Updates the games capacity gameStorageLimit = 0; for(Shelf f : shelves) { gameStorageLimit += f.getCapacity(); } // Updates the Shelves for (Shelf f : shelves) { f.setxOffset(camera.getxOffset()); f.setyOffset(camera.getyOffset()); f.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, business, mainState); } // Updates the Tvs for(Tv t : tvs) { t.setxOffset(camera.getxOffset()); t.setyOffset(camera.getyOffset()); t.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, business, mainState); } // Updates the Consoles for(Console c : consoles) { c.setxOffset(camera.getxOffset()); c.setyOffset(camera.getyOffset()); c.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, business, mainState, isShiftDown); } // Updates the Miscs for(Misc m : miscs) { m.setxOffset(camera.getxOffset()); m.setyOffset(camera.getyOffset()); m.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, business, mainState); } // Updates the Games (for rent control) for(Game g : games) { g.update(delta); } // Updates the Terrain (walls, floors) for (Terrain t : map.getTerrain()) { t.setxOffset(camera.getxOffset()); t.setyOffset(camera.getyOffset()); } } /** * Returns all Assets * @return */ public ArrayList<Asset> getAssets() { ArrayList<Asset> assets = new ArrayList<Asset>(); assets.addAll(tvs); assets.addAll(consoles); assets.addAll(tables); assets.addAll(shelves); assets.addAll(miscs); return assets; } /** * Returns all Entities * @return */ public ArrayList<Entity> getEntities() { ArrayList<Entity> entities = new ArrayList<Entity>(); entities.addAll(characters); entities.addAll(tables); entities.addAll(shelves); entities.addAll(tvs); entities.addAll(consoles); entities.addAll(miscs); return entities; } /** * Adds an Asset to the Store, no matter the kind * @param asset */ public void addAsset(Asset asset) { if(asset instanceof Shelf) { shelves.add((Shelf)asset); } else if(asset instanceof Table) { tables.add((Table)asset); } else if(asset instanceof Tv) { tvs.add((Tv)asset); } else if(asset instanceof Console) { consoles.add((Console)asset); } else if(asset instanceof Misc) { miscs.add((Misc)asset); } } /** * Removes an Asset from the store, no matter the kind * @param asset */ public void removeAsset(Asset asset) { if(asset instanceof Shelf) { shelves.remove((Shelf)asset); } else if(asset instanceof Table) { tables.remove((Table)asset); } else if(asset instanceof Tv) { tvs.remove((Tv)asset); } else if(asset instanceof Console) { consoles.remove((Console)asset); } else if(asset instanceof Misc) { miscs.remove((Misc)asset); } } /** * Returns the Table by its id. Null if not found * @param id */ public Table getTable(int id) { for(Table c : tables) { if(c.getReferringMarketAsset().getId() == id) { return c; } } return null; } /** * Get all tables of a kind * @param id * @return */ public ArrayList<Table> getTables(int id) { // If there aren't any consoles, return null if(tables.size() == 0) { return null; } // Make an Array List ArrayList<Table> tables = new ArrayList<Table>(); // Get the List of Consoles of that kind for(Table c : tables) { if(c.getReferringMarketAsset().getId() == id) { tables.add(c); } } return tables; } /** * Remove Character use references from this Asset. Use it before moving or deleting an Asset * @param asset */ public void removeCharacterReferences(Asset asset) { for(Character c : characters) { if(c.getTargetAsset() != null) { if(c.getTargetAsset().equals(asset)) { c.setTargetAsset(null); } } } } /** * Get only games that are not rented or being played at the moment * @return */ public int getTotalNumberOfAvailableGames() { int count = 0; for(Game g : games) { if(!g.isBeingPlayed() && !g.isRented()) { count++; } } return count; } /** * Get the number of Games of the specified Id that are not rented or being played at the moment * @param gameId * @return */ public int getNumberOfAvailableGames(int gameId) { int count = 0; for(Game g : games) { if(g.getReferredMarketGame().getId() == gameId) { if(!g.isBeingPlayed() && !g.isRented()) { count++; } } } return count; } /** * Returns a random game the player OWNS, taking into consideration their quality (higher quality games will be more frequently picked) * @return */ public MarketGame getRandomGameBasedOnQuality() { MarketGame result = null; // Has no game if (games.size() == 0) { return result; } int totalQuality = 0; for(Game game : games) { totalQuality += game.getReferredMarketGame().getQuality() + 1; } int selectedQuality = new Random().nextInt(totalQuality) + 1; int count = 0; int previousCount = 0; for(Game game : games) { previousCount = count; count += game.getReferredMarketGame().getQuality() + 1; if(selectedQuality <= count && selectedQuality >= previousCount) { result = game.getReferredMarketGame(); } } return result; } /** * Returns a random game the player OWNS, taking into consideration their quality (higher quality games will be more frequently picked) * @return */ public MarketGame getRandomGameBasedOnQuality(MarketConsole marketConsole) { MarketGame result = null; // Create a list of games of this Console ArrayList<Game> gamesOfThisConsole = new ArrayList<Game>(); for (Game game : games) { if (game.getReferredMarketGame().getMarketConsole() == marketConsole) { gamesOfThisConsole.add(game); } } // Has no game if (gamesOfThisConsole.size() == 0) { return result; } int totalQuality = 0; for(Game game : gamesOfThisConsole) { totalQuality += game.getReferredMarketGame().getQuality() + 1; } int selectedQuality = new Random().nextInt(totalQuality) + 1; int count = 0; int previousCount = 0; for(Game game : gamesOfThisConsole) { previousCount = count; count += game.getReferredMarketGame().getQuality() + 1; if(selectedQuality <= count && selectedQuality >= previousCount) { result = game.getReferredMarketGame(); } } return result; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public ArrayList<Game> getGames() { return games; } public void setGames(ArrayList<Game> games) { this.games = games; } public int getGameStorageLimit() { return gameStorageLimit; } public void setGameStorageLimit(int gameStorageLimit) { this.gameStorageLimit = gameStorageLimit; } public int getNumberOfGames(MarketGame referredMarketGame) { int count = 0; for (Game g : games) { if (g.getReferredMarketGame() == referredMarketGame) { count++; } } return count; } public int getRating() { return rating; } public void setRating(int rating) { this.rating = rating; } public ArrayList<Character> getCharacters() { return characters; } public void setCharacters(ArrayList<Character> characters) { this.characters = characters; } public ArrayList<Table> getTables() { return tables; } public void setTables(ArrayList<Table> tables) { this.tables = tables; } public ArrayList<Shelf> getShelves() { return shelves; } public void setFurnitures(ArrayList<Shelf> shelfs) { this.shelves = shelfs; } public ArrayList<Tv> getTvs() { return tvs; } public void setTvs(ArrayList<Tv> tvs) { this.tvs = tvs; } public ArrayList<Console> getConsoles() { return consoles; } public void setConsoles(ArrayList<Console> consoles) { this.consoles = consoles; } public void setShelves(ArrayList<Shelf> shelves) { this.shelves = shelves; } public ArrayList<Misc> getMiscs() { return miscs; } public void setMiscs(ArrayList<Misc> miscs) { this.miscs = miscs; } public String getName() { return name; } public void setName(String name) { this.name = name; } }