package com.lucasdnd.ags.system;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.gameplay.Asset;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.gameplay.Character;
import com.lucasdnd.ags.gameplay.Console;
import com.lucasdnd.ags.gameplay.Customer;
import com.lucasdnd.ags.gameplay.Entity;
import com.lucasdnd.ags.gameplay.Game;
import com.lucasdnd.ags.gameplay.Shelf;
import com.lucasdnd.ags.gameplay.Store;
import com.lucasdnd.ags.gameplay.Table;
import com.lucasdnd.ags.gameplay.Tv;
import com.lucasdnd.ags.gameplay.market.Market;
import com.lucasdnd.ags.map.Map;
import com.lucasdnd.ags.map.terrain.Wall;
import com.lucasdnd.ags.ui.Button;
import com.lucasdnd.ags.ui.BuyConsolesPanel;
import com.lucasdnd.ags.ui.BuyFurnituresPanel;
import com.lucasdnd.ags.ui.BuyGamesPanel;
import com.lucasdnd.ags.ui.FinancesPanel;
import com.lucasdnd.ags.ui.IconButton;
import com.lucasdnd.ags.ui.Menu;
import com.lucasdnd.ags.ui.Panel;
import com.lucasdnd.ags.ui.UIOverlay;
import com.lucasdnd.ags.ui.ViewGamesPanel;
import com.lucasdnd.ags.ui.popup.FloatingText;
public class MainState extends BasicGameState {
private int id;
private boolean showDebugInfo = false;
// Game Control Modes
private int currentMode = 0;
public static final int SELECTION_MODE = 0;
public static final int BUYING_ASSET = 1;
public static final int MOVING_ASSET = 2;
public static final int BROWSING_LISTS = 3;
public static final int MINI_PANEL = 4;
public static final int HIRING_STAFF = 5;
public static final int SELECTING_CHARACTER = 6;
public static final int BUYING_MAP = 7;
// The Entity currently selected by the player
private Entity selectedEntity;
// The Asset and Staff the player is currently PLACING or MOVING
private Asset placingAsset;
// UI
private Panel currentlyActivePanel;
private float panelsXPos, panelsYPos, panelsWidth, panelsHeight; // Panel positions and sizes
private UIOverlay uiOverlay; // The UI Overlay
private Menu gameMenu, buySubmenu; // The menus
private IconButton buyButton, inventoryButton, buyTilesButton, financesButton, buyGamesButton, buyConsolesButton, buyTvsButton, buyFurnituresButton, buyDecorationsButton;
private BuyGamesPanel buyGamesPanel;
private ViewGamesPanel viewGamesPanel;
private BuyConsolesPanel buyConsolesPanel;
private BuyFurnituresPanel buyFurnituresPanel, buyTvsPanel, buyDecorationsPanel;
private FinancesPanel financesPanel;
private FloatingText tooltip, notification;
private boolean alreadyShowedRToRotateTooltip;
private Button pauseButton, speed1, speed2, speed3;
// Input
private Input input;
// Market and Business
private Market market;
private Business business;
// Camera - Controls the offset, views, cameras, etc
private Camera camera;
// Time
private TimeController timeController;
private int simulationSpeed;
private SimpleDateFormat hourFormat, dayFormat, monthFormat, yearFormat; // Use this to draw the dates
private Date currentTime; // Used to render the date using the SimpleDateFormats
private boolean showWalls = true;
/**
* Constructor
*/
public MainState() {id = GameSystem.MAIN_STATE;}
/**
* Initialization
*/
@Override
public void init(GameContainer container, StateBasedGame game) throws SlickException {
// Creates and initializes the Market, Business, Store and Map
GameSystem gameSystem = ((GameSystem)game);
market = new Market(gameSystem.getDifficulty());
business = new Business(gameSystem);
// Map Size
String mapSize = "";
if (gameSystem.getMapSize() == GameSystem.MapSize.SMALL) {
mapSize = "small";
} else if (gameSystem.getMapSize() == GameSystem.MapSize.MEDIUM) {
mapSize = "medium";
} else { // if (gameSystem.getMapSize() == GameSystem.MapSize.LARGE) {
mapSize = "large";
}
business.setStore(new Store(gameSystem.getStoreName(), new Map("./res/map/map_" + mapSize + ".tmx", "./res/map", (int)(container.getWidth() / 2 / GameSystem.globalScale) - (int)(64 / GameSystem.globalScale), 0), market));
business.getStore().getMap().setyOffset(-50f); // Adjust the starting camera position
selectedEntity = null;
// Creates Time
timeController = new TimeController();
simulationSpeed = 1;
// Size of Panels
panelsWidth = 1024f;
panelsHeight = 595f;
panelsXPos = (container.getWidth() - panelsWidth) / 2f;
panelsYPos = 33f;
// Size of the Buy Consoles Panel
float bigPanelWidth = 605f;
float bigPanelHeight = panelsHeight;
float bigPanelXPos = (container.getWidth() - bigPanelWidth) / 2f;
float bigPanelYPos = panelsYPos;
// Create the Panels
// My eyes
buyGamesPanel = new BuyGamesPanel("Buy Games", panelsXPos - 86f, panelsYPos, panelsWidth + 166f, panelsHeight, business, market.getMarketGames());
buyConsolesPanel = new BuyConsolesPanel("Buy Consoles", bigPanelXPos, bigPanelYPos, bigPanelWidth, bigPanelHeight, business, market.getMarketConsoles());
buyFurnituresPanel = new BuyFurnituresPanel("Buy Furniture", bigPanelXPos, bigPanelYPos, bigPanelWidth, bigPanelHeight, business, market.getMarketShelves(), market.getMarketTables(), null, null);
viewGamesPanel = new ViewGamesPanel("View Games", panelsXPos - 160f, panelsYPos, panelsWidth + 320f, panelsHeight, business, buyGamesPanel.getBuyGamesList(), market);
financesPanel = new FinancesPanel("Management and Finances", panelsXPos - 160f, panelsYPos, panelsWidth + 320f, panelsHeight - 16f, business, market);
buyTvsPanel = new BuyFurnituresPanel("Buy TVs", bigPanelXPos, bigPanelYPos, bigPanelWidth, bigPanelHeight, business, null, null, market.getMarketTvs(), null);
buyDecorationsPanel = new BuyFurnituresPanel("Buy Decoration", bigPanelXPos, bigPanelYPos, bigPanelWidth, bigPanelHeight, business, null, null, null, market.getMarketMiscs());
// Create the UI Overlay
uiOverlay = new UIOverlay(container.getWidth(), container.getHeight());
// Create the UI Buttons and Menu
buyButton = new IconButton(ResourceLoader.getInstance().buyButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/buy.png"));
inventoryButton = new IconButton(ResourceLoader.getInstance().inventoryButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/package.png"));
buyTilesButton = new IconButton(ResourceLoader.getInstance().buyTilesButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/minitile.png"));
financesButton = new IconButton(ResourceLoader.getInstance().financesButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/money.png"));
ArrayList<Button> listOfButtons = new ArrayList<Button>();
listOfButtons.add(buyButton);
listOfButtons.add(inventoryButton);
listOfButtons.add(buyTilesButton);
listOfButtons.add(financesButton);
gameMenu = new Menu(listOfButtons, 6f, 50f, 6f, Menu.VERTICAL);
gameMenu.setVisible(true);
// Buy Submenu
buyGamesButton = new IconButton(ResourceLoader.getInstance().buyGamesButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/game.png"));
buyConsolesButton = new IconButton(ResourceLoader.getInstance().buyConsolesButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/console.png"));
buyTvsButton = new IconButton(ResourceLoader.getInstance().buyTvsButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/tv.png"));
buyFurnituresButton = new IconButton(ResourceLoader.getInstance().buyFurnituresButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/table.png"));
buyDecorationsButton = new IconButton(ResourceLoader.getInstance().buyDecorationsButtonImages, 4f, 0, 0, null, new Image("./res/img/gui/buttons/plant.png"));
ArrayList<Button> listOfBuySubButtons = new ArrayList<Button>();
listOfBuySubButtons.add(buyGamesButton);
listOfBuySubButtons.add(buyConsolesButton);
listOfBuySubButtons.add(buyTvsButton);
listOfBuySubButtons.add(buyFurnituresButton);
listOfBuySubButtons.add(buyDecorationsButton);
buySubmenu = new Menu(listOfBuySubButtons, 38f, gameMenu.getyPos(), 6f, Menu.VERTICAL);
buySubmenu.setVisible(false);
// Speed Buttons
float speedXPos = 570f;
float speedYPos = 654f;
float speedSpace= 40f;
pauseButton = new Button(ResourceLoader.getInstance().pauseButtonImages, speedXPos, speedYPos);
speed1 = new Button(ResourceLoader.getInstance().speed1, speedXPos + speedSpace, speedYPos);
speed2 = new Button(ResourceLoader.getInstance().speed2, speedXPos + speedSpace * 2, speedYPos);
speed3 = new Button(ResourceLoader.getInstance().speed3, speedXPos + speedSpace * 3, speedYPos);
speed1.setSelected(true);
// Tooltip, notification
tooltip = new FloatingText(ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, 0f, 0f, 0f, 0f, 200f, 110f, 3000);
notification = new FloatingText(ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, 0f, 0f, 0f, 0f, 200f, 110f, 10000);
// Creates the Camera and the Clipping
camera = new Camera(business.getStore().getMap(), container.getWidth(), container.getHeight());
container.getGraphics().setClip(0, 0, container.getWidth(), container.getHeight());
// Date Rendering
hourFormat = new SimpleDateFormat("HH");
dayFormat = new SimpleDateFormat("dd");
monthFormat = new SimpleDateFormat("MM");
yearFormat = new SimpleDateFormat("yyyy");
currentTime = new Date();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
// Simulation Speed
delta *= simulationSpeed;
/** Time Control */
timeController.passTime(delta);
// Check for hour, day and month changes
if(timeController.isHourChanged()) {
// Simulation speed here is used to change Character's timing modifiers
market.hourUpdate(
business, timeController, notification,
buyConsolesPanel.getBuyConsolesList(),
buyGamesPanel.getBuyGamesList(), this, ((GameSystem)game).getDifficulty(),
simulationSpeed, currentTime.getTime());
}
if(timeController.isDayChanged()) {
market.dayUpdate(business, timeController, notification, buyConsolesPanel.getBuyConsolesList(),
buyGamesPanel.getBuyGamesList(), this, ((GameSystem)game).getDifficulty());
}
if(timeController.isMonthChanged()) {
business.monthUpdate((GameSystem)game);
}
/** Main Market Update - spawns Characters and stuff */
market.update(business, delta);
/** Input Control */
// Get input
input = container.getInput();
boolean leftMouseClicked = input.isMousePressed(Input.MOUSE_LEFT_BUTTON);
boolean rightMouseClicked = input.isMousePressed(Input.MOUSE_RIGHT_BUTTON);
boolean leftMouseDown = input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
boolean isShiftDown = input.isKeyDown(Input.KEY_LSHIFT);
// Show Debugging Info
if(input.isKeyPressed(Input.KEY_F3)) {
showDebugInfo = !showDebugInfo;
}
// Change simulation speed
if(input.isKeyPressed(Input.KEY_Q)) {
changeSimulationSpeed(0);
} else if(input.isKeyPressed(Input.KEY_1)) {
changeSimulationSpeed(1);
} else if(input.isKeyPressed(Input.KEY_2)) {
changeSimulationSpeed(3);
} else if(input.isKeyPressed(Input.KEY_3)) {
changeSimulationSpeed(10);
}
// Main Menu
if(input.isKeyPressed(Input.KEY_ESCAPE)) {
showWalls = !showWalls;
}
// Button Updates
if (currentMode != BROWSING_LISTS) {
gameMenu.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
buySubmenu.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
}
pauseButton.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
speed1.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
speed2.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
speed3.update(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY);
if(pauseButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
changeSimulationSpeed(0);
} else if(speed1.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
changeSimulationSpeed(1);
} else if(speed2.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
changeSimulationSpeed(3);
} else if(speed3.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
changeSimulationSpeed(10);
}
/** Manage Control Modes */
// Select Units (default mode)
if(currentMode == SELECTION_MODE) {
updateSelectionMode(leftMouseClicked, mouseX, mouseY);
}
// Buying Asset Mode
else if(currentMode == BUYING_ASSET && placingAsset != null) {
updateBuyingAssetMode(leftMouseClicked, mouseX, mouseY, business.getStore().getMap().getXTileAtMouse(), business.getStore().getMap().getYTileAtMouse());
}
// Moving Asset Mode
else if(currentMode == MOVING_ASSET && placingAsset != null) {
updateMovingAssetMode(leftMouseClicked, business.getStore().getMap().getXTileAtMouse(), business.getStore().getMap().getYTileAtMouse());
}
// Browsing Lists Mode
else if(currentMode == BROWSING_LISTS) {
updateBrowsingListsMode(container, game, delta, leftMouseClicked, leftMouseDown, mouseX, mouseY, isShiftDown);
}
// Browsing Mini Panels Mode
else if(currentMode == MINI_PANEL) {
updateMiniPanelMode(leftMouseClicked, mouseX, mouseY);
}
// Buying Map Mode
else if (currentMode == BUYING_MAP) {
updateBuyingMapMode(container, game, delta, leftMouseClicked, leftMouseDown, rightMouseClicked, mouseX, mouseY);
}
/** Update the Tooltip and the Notification */
tooltip.setxPos(mouseX * 2f);
tooltip.setyPos(mouseY * 2f);
tooltip.setxOffset(-80f);
tooltip.setyOffset(180f);
tooltip.update(container, game, delta / (simulationSpeed == 0 ? 1 : simulationSpeed), 0f, 0f);
notification.setxPos(container.getWidth() * 0.8f); // move these to the constructor once they're good
notification.setyPos(280f);
notification.update(container, game, delta / (simulationSpeed == 0 ? 1 : simulationSpeed), container.getWidth() * 0.6f + 12f, 45f);
/** Right Clicks cancel all actions */
if(rightMouseClicked) {
// If we were Moving an Asset, we get it back to its normal position first
if(currentMode == MOVING_ASSET) {
// Release the Kr... Asset
placingAsset.setBeingMoved(false);
business.getStore().getMap().releaseMovingObject(placingAsset, business.getStore().getCharacters());
}
// Free up the Placing Asset and the Selected Character
selectedEntity = null;
placingAsset = null;
// Return to mode NONE
currentMode = SELECTION_MODE;
// Unhighlights all Tiles
business.getStore().getMap().unhighlightAll();
// Hide all panels
this.hideAllPanels();
}
/** Key Control ([R] on Buying or Moving an Asset)
*
* Should we bring this into the updateBuyingAssetMode() method?
*
* */
if(input.isKeyPressed(Input.KEY_R) && placingAsset != null
&& (currentMode == BUYING_ASSET || currentMode == MOVING_ASSET)) {
// Rotate the Asset and update its position
placingAsset.rotate();
business.getStore().getMap().snapToGrid(placingAsset);
// Refresh the Highlighted Tiles
business.getStore().getMap().unhighlightAll();
}
/** Camera Control */
// Move the Camera when the Mouse is at the corner of the screen
// OR when the player presses one of the WASD keys
if (input.getMouseX() <= 3 || input.isKeyDown(Input.KEY_A)) {
camera.setCameraMovingDirection(Camera.LEFT);
camera.updateOffset(container, business.getStore().getMap(), delta / (simulationSpeed == 0 ? 1 : simulationSpeed), 1, 0);
}
else if (input.getMouseX() >= container.getWidth() - 3 || input.isKeyDown(Input.KEY_D)) {
camera.setCameraMovingDirection(Camera.RIGHT);
camera.updateOffset(container, business.getStore().getMap(), delta / (simulationSpeed == 0 ? 1 : simulationSpeed), -1, 0);
}
if (input.getMouseY() <= 3 || input.isKeyDown(Input.KEY_W)) {
camera.setCameraMovingDirection(Camera.UP);
camera.updateOffset(container, business.getStore().getMap(), delta / (simulationSpeed == 0 ? 1 : simulationSpeed), 0, 1);
}
else if (input.getMouseY() >= container.getHeight() - 3 || input.isKeyDown(Input.KEY_S)) {
camera.setCameraMovingDirection(Camera.DOWN);
camera.updateOffset(container, business.getStore().getMap(), delta / (simulationSpeed == 0 ? 1 : simulationSpeed), 0, -1);
}
// Applies the Offset to the Map. It will then propagate to the Entities in the Store update
business.getStore().getMap().setxOffset(camera.getxOffset());
business.getStore().getMap().setyOffset(camera.getyOffset());
/** Updates the Business, Stores, Maps and their Entities */
// If we are moving an asset, we send that Asset's product reference
Calendar c = Calendar.getInstance();
c.setTimeInMillis(timeController.getTime());
int month = c.get(Calendar.MONTH);
business.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, currentMode,
placingAsset, camera, this, isShiftDown, viewGamesPanel.getViewGamesList(), simulationSpeed, market, month);
// Updates the PlacingAsset, if it exists
if(placingAsset != null) {
if(placingAsset instanceof Table) {
((Table)placingAsset).update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, isShiftDown, business, this);
} else {
placingAsset.update(container, game, delta, input, leftMouseClicked, leftMouseDown, mouseX, mouseY, business, this);
}
}
/** Character Disposal */
Character disposedCharacter = null;
for(Character ch : business.getStore().getCharacters()) {
if(ch.canBeDisposed()) {
disposedCharacter = ch;
}
}
if(disposedCharacter != null) {
if(disposedCharacter instanceof Customer) {
Customer disposedCustomer = (Customer)disposedCharacter;
if(disposedCustomer.getVisitRating() > 0) business.getStore().setRating(business.getStore().getRating() + 1);
else if(disposedCustomer.getVisitRating() < 0) business.getStore().setRating(business.getStore().getRating() - 1);
}
// Remove the Character
business.getStore().getCharacters().remove(disposedCharacter);
}
/** Asset Disposal */
Asset disposedAsset = null;
for(Asset a : business.getStore().getAssets()) {
if(a.canBeDisposed()) {
disposedAsset = a;
}
}
if(disposedAsset != null) {
// Remove it
business.getStore().removeAsset(disposedAsset);
}
}
/**
* Changes the simulation speed and store the previous one
* @param newSimulationSpeed
*/
private void changeSimulationSpeed(int newSimulationSpeed) {
// Change the speed, store the previous one
simulationSpeed = newSimulationSpeed;
// Disable all buttons
pauseButton.setSelected(false);
pauseButton.setBlinking(false);
speed1.setSelected(false);
speed2.setSelected(false);
speed3.setSelected(false);
// Enable the clicked one
if (simulationSpeed == 0) {
pauseButton.setSelected(true);
pauseButton.setBlinking(true);
} else if (simulationSpeed == 1) {
speed1.setSelected(true);
} else if (simulationSpeed == 3) {
speed2.setSelected(true);
} else if (simulationSpeed == 10) {
speed3.setSelected(true);
}
}
/**
* Closes all the Panels
*/
public void hideAllPanels() {
// Close the main Panels
viewGamesPanel.setVisible(false);
buyConsolesPanel.setVisible(false);
buyGamesPanel.setVisible(false);
buyFurnituresPanel.setVisible(false);
financesPanel.setVisible(false);
buyTvsPanel.setVisible(false);
buyDecorationsPanel.setVisible(false);
// Close the Mini Panels
for(Asset a : business.getStore().getAssets()) {
if(a.getMiniPanel() != null) {
a.getMiniPanel().setVisible(false);
}
}
// Close the Sub Menus
buySubmenu.setVisible(false);
}
/**
* Rendering
*/
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
// Rendering Settings
g.scale(GameSystem.globalScale, GameSystem.globalScale);
g.setColor(Color.black);
g.setBackground(Color.white);
// Render the base layers of the map Map (floor, lines)
business.getStore().getMap().render(container, game, g, camera.getCameraSpeed(), placingAsset);
// Refresh Entities Rendering Status
for(Entity e : business.getStore().getEntities()) {
e.setAlreadyRendered(false);
}
// Render the Entities using the Y then X algorithm
for(int x = 0; x < business.getStore().getMap().getWidth(); x++) {
for(int y = 0; y < business.getStore().getMap().getHeight(); y++) {
// Map Walls
for (Wall w : business.getStore().getMap().getWalls()) {
if (w.getXTile() == x && w.getYTile() == y) {
w.render(container, game, g);
}
}
// Each Entity
for(Entity e : business.getStore().getEntities()) {
// Is Visible?
if(e.isVisible() == false) {
continue;
}
// Is it an Asset on a Table? If so, skip this one -- the Table itself will render it
if(e instanceof Asset && ((Asset) e).isOnTable()) {
continue;
}
// Is it in the same X and Y Tile?
if(e.getXTile() == x && e.getYTile() == y && ((e instanceof Wall) == false)) {
if(e instanceof Character) {
((Character)e).render(container, game, g, simulationSpeed);
} else {
e.render(container, game, g);
}
}
// Is it a Table? If so, we need to check if we can render its Chairs.
if(e instanceof Table) {
if(((Table)e).getChair1().getxTile() == x && ((Table)e).getChair1().getyTile() == y) {
((Table)e).getChair1().render(container, game, g, e.getFacingDirection(), ((Table) e).isBeingMoved());
}
if(((Table)e).getChair2().getxTile() == x && ((Table)e).getChair2().getyTile() == y) {
((Table)e).getChair2().render(container, game, g, e.getFacingDirection(), ((Table) e).isBeingMoved());
}
}
}
}
}
// Render the Placing Asset, if one exists
if(placingAsset != null) {
placingAsset.render(container, game, g);
if(placingAsset instanceof Table) {
((Table)placingAsset).getChair2().render(container, game, g, placingAsset.getFacingDirection(), ((Table) placingAsset).isBeingMoved());
((Table)placingAsset).getChair1().render(container, game, g, placingAsset.getFacingDirection(), ((Table) placingAsset).isBeingMoved());
}
}
// Render the UI Text, Panels and Lists.
// From here on, we use the Global UI Scale and the Unicode Font to render
g.scale(1/GameSystem.globalScale, 1/GameSystem.globalScale);
// Render Floating Texts
for(Character c : business.getStore().getCharacters()) {
c.getFloatingText().render(container, game, g);
c.getFloatingMoney().render(container, game, g);
}
// Render the Tooltip and the Notification
tooltip.render(container, game, g);
notification.render(container, game, g);
// Settings for UI rendering
g.setLineWidth(8f);
// UI Overlay, Menu and Buttons
uiOverlay.render(container, game, g);
gameMenu.render(container, game, g);
buySubmenu.render(container, game, g);
pauseButton.render(container, game, g);
speed1.render(container, game, g);
speed2.render(container, game, g);
speed3.render(container, game, g);
// Basic UI indicators
ResourceLoader.getInstance().grayFont.drawString(250 + 1, 656 + 1, business.getStore().getName());
ResourceLoader.getInstance().blackFont.drawString(250, 656, business.getStore().getName());
if(business.getMoney() > 0) {
ResourceLoader.getInstance().grayFont.drawString(250 + 1, 698 + 1, GameSystem.printMoney(business.getMoney(), true));
ResourceLoader.getInstance().blackFont.drawString(250, 698, GameSystem.printMoney(business.getMoney(), true));
} else {
ResourceLoader.getInstance().grayFont.drawString(250 + 1, 698 + 1, GameSystem.printMoney(business.getMoney(), true));
ResourceLoader.getInstance().redFont.drawString(250, 698, GameSystem.printMoney(business.getMoney(), true));
}
ResourceLoader.getInstance().grayFont.drawString(720 + 1, 698 + 1, "Store rating: " + business.getBusinessRating());
ResourceLoader.getInstance().blackFont.drawString(720, 698, "Store rating: " + business.getBusinessRating());
ResourceLoader.getInstance().grayFont.drawString(983 + 1, 698 + 1, "Generation: " + market.getCurrentGeneration());
ResourceLoader.getInstance().blackFont.drawString(983, 698, "Generation: " + market.getCurrentGeneration());
// Date
currentTime.setTime(timeController.getTime());
ResourceLoader.getInstance().grayFont.drawString(1000 + 1, 658 + 1, dayFormat.format(currentTime));
ResourceLoader.getInstance().blackFont.drawString(1000, 658, dayFormat.format(currentTime));
ResourceLoader.getInstance().grayFont.drawString(1055 + 1, 658 + 1, monthFormat.format(currentTime));
ResourceLoader.getInstance().blackFont.drawString(1055, 658, monthFormat.format(currentTime));
ResourceLoader.getInstance().grayFont.drawString(1110 + 1, 658 + 1, yearFormat.format(currentTime));
ResourceLoader.getInstance().blackFont.drawString(1110, 658, yearFormat.format(currentTime));
// Hour
ResourceLoader.getInstance().grayFont.drawString(878 + 1, 658 + 1, hourFormat.format(currentTime) + "h");
ResourceLoader.getInstance().blackFont.drawString(878, 658, hourFormat.format(currentTime) + "h");
// Render Mini Panels
for(Asset a : business.getStore().getAssets()) {
if(a.getMiniPanel() != null) {
a.getMiniPanel().render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyLightGrayFont);
}
}
// Render the Panels
if(buyGamesPanel.isVisible()) {
buyGamesPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (viewGamesPanel.isVisible()) {
viewGamesPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (buyConsolesPanel.isVisible()) {
buyConsolesPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (buyFurnituresPanel.isVisible()) {
buyFurnituresPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (financesPanel.isVisible()) {
financesPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (buyTvsPanel.isVisible()) {
buyTvsPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
} else if (buyDecorationsPanel.isVisible()) {
buyDecorationsPanel.render(container, game, g, ResourceLoader.getInstance().tinyBlackFont, ResourceLoader.getInstance().tinyGrayFont, ResourceLoader.getInstance().tinyLightGrayFont);
}
// Draw the debug strings
if(showDebugInfo) {
g.scale(GameSystem.globalScale, GameSystem.globalScale);
g.scale(1/GameSystem.globalScale, 1/GameSystem.globalScale);
g.setColor(Color.black);
g.setFont(ResourceLoader.getInstance().blackFont);
// Map debug
g.drawString("tile x: " + business.getStore().getMap().getXTileAtMouse(), 0f, 0f);
g.drawString("tile y: " + business.getStore().getMap().getYTileAtMouse(), 0f, 20f);
// Game debug
for (Game gam: business.getStore().getGames()) {
g.drawString("game: " + gam.getReferredMarketGame().getName(), 0, 0);
g.drawString("game's console: " + gam.getReferredMarketGame().getMarketConsole().getName(), 0, 20);
}
// Console debug
for(Console c : business.getStore().getConsoles()) {
g.drawString("console: " + c.getReferringMarketConsole().getName(), 0, 40);
}
// Tv debug
for(Tv c : business.getStore().getTvs()) {
g.drawString("tv xTile = " + c.getXTile(), 0, 310);
g.drawString("tv yTile = " + c.getYTile(), 0, 330);
g.drawString("tv xPos = " + c.getXPos(), 0, 350);
g.drawString("tv yPos = " + c.getYPos(), 0, 370);
g.drawString("tv can move = " + c.canMove(), 0, 390);
}
}
}
/**
* Check if the user clicked any part of the UI (Buttons)
* This is done so clicking the main UI Buttons has priority over clicking game objects, as well as Panels
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @return
* @throws SlickException
*/
private boolean clickedUI(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
// Otherwise, check if the player clicked any of the Menu Buttons
if(gameMenu.isVisible()) {
// Buy Button
if(buyButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
if(currentMode == BROWSING_LISTS) {hideAllPanels(); currentMode = SELECTION_MODE;} else {
buySubmenu.setVisible(!buySubmenu.isVisible());
}
return true;
}
// Inventory Button
else if(inventoryButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {
currentMode = SELECTION_MODE;
} else {
viewGamesPanel.getViewGamesList().recreateList(business, market);
viewGamesPanel.setVisible(true);
currentMode = BROWSING_LISTS;
}
return true;
}
// Buy Tiles Button
else if (buyTilesButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if (currentMode == BUYING_MAP) {
currentMode = SELECTION_MODE;
} else {
currentMode = BUYING_MAP;
}
return true;
}
// Finances Button
else if(financesButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {financesPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
}
// Buy Submenu
if(buySubmenu.isVisible()) {
// Buy Games Button
if(buyGamesButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {buyGamesPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
// Buy Consoles Button
else if(buyConsolesButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {buyConsolesPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
// Buy Tvs Button
else if(buyTvsButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {buyTvsPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
// Buy Furnitures Buttonaw
else if(buyFurnituresButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {buyFurnituresPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
// Buy Decorations Button
else if(buyDecorationsButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
hideAllPanels();
if(currentMode == BROWSING_LISTS) {currentMode = SELECTION_MODE;} else {buyDecorationsPanel.setVisible(true); currentMode = BROWSING_LISTS;}
return true;
}
}
return false;
}
/**
* Selection Mode
* @param rightMouseClicked
* @param mouseX
* @param mouseY
*/
private void updateSelectionMode(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
// Check if the user clicked the Open Menu Button.
if(leftMouseClicked == false || clickedUI(leftMouseClicked, mouseX, mouseY)) {
return;
}
int xTileAtMouse = business.getStore().getMap().getXTileAtMouse();
int yTileAtMouse = business.getStore().getMap().getYTileAtMouse();
for(Asset a : business.getStore().getAssets()) {
// Go through the Tiles it occupies
if(a.tilesGotLeftClicked(leftMouseClicked, xTileAtMouse, yTileAtMouse) && !a.isOnTable()) {
// Enter Mini Panel mode
a.getMiniPanel().setxPos(mouseX);
a.getMiniPanel().setyPos(mouseY);
a.getMiniPanel().setVisible(true);
currentMode = MINI_PANEL;
selectedEntity = a;
break;
}
}
}
/**
* Browsing Lists Mode
* @param leftMouseClicked
* @param mouseX
* @param mouseY
*/
private void updateBrowsingListsMode(GameContainer container, StateBasedGame game, int delta,
boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY, boolean isShiftDown) throws SlickException {
currentlyActivePanel = null;
/** Updates the Panels only if they're visible */
if(buyGamesPanel.isVisible()) {
buyGamesPanel.update(container, game, delta, this, business, viewGamesPanel.getViewGamesList(), market, leftMouseClicked, leftMouseDown, mouseX, mouseY);
currentlyActivePanel = buyGamesPanel;
} else if(viewGamesPanel.isVisible()) {
viewGamesPanel.update(container, game, delta, this, business, leftMouseClicked, leftMouseDown, mouseX, mouseY, isShiftDown);
currentlyActivePanel = viewGamesPanel;
} else if(buyConsolesPanel.isVisible()) {
buyConsolesPanel.update(container, game, delta, business, this, leftMouseClicked, leftMouseDown, mouseX, mouseY);
currentlyActivePanel = buyConsolesPanel;
} else if(buyFurnituresPanel.isVisible()) {
buyFurnituresPanel.update(container, game, delta, business, this, leftMouseClicked, leftMouseDown, mouseX, mouseY);
currentlyActivePanel = buyFurnituresPanel;
} else if(financesPanel.isVisible()) {
financesPanel.update(container, game, business, delta, this, leftMouseClicked, mouseX, mouseY, leftMouseDown, isShiftDown);
currentlyActivePanel = financesPanel;
} else if(buyTvsPanel.isVisible()) {
buyTvsPanel.update(container, game, delta, business, this, leftMouseClicked, leftMouseDown, mouseX, mouseY);
currentlyActivePanel = buyTvsPanel;
} else if(buyDecorationsPanel.isVisible()) {
buyDecorationsPanel.update(container, game, delta, business, this, leftMouseClicked, leftMouseDown, mouseX, mouseY);
currentlyActivePanel = buyDecorationsPanel;
} else {
currentMode = SELECTION_MODE;
}
// Check if the user clicked outside the Panels
if(currentlyActivePanel != null && leftMouseClicked) {
if(!currentlyActivePanel.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
currentlyActivePanel.setVisible(false);
currentMode = SELECTION_MODE;
}
}
}
/**
* When viewing Mini Panels
* @param container
* @param game
* @param delta
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @throws SlickException
*/
private void updateMiniPanelMode(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
// We only do something if the Player Clicked
if(leftMouseClicked == false) {
return;
}
// Check if there's an asset selected
if(selectedEntity == null || (selectedEntity instanceof Asset) == false) {
return;
}
// Check if the User clicked outside the Mini Panel
if(!((Asset)selectedEntity).getMiniPanel().gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
// Hide the Mini Panel
this.hideAllPanels();
// Return to Mode 0
selectedEntity = null;
currentMode = SELECTION_MODE;
}
}
/**
* Buying Asset Mode
* @throws SlickException
*/
private void updateBuyingAssetMode(boolean leftMouseClicked, int mouseX, int mouseY, int xTileAtMouse, int yTileAtMouse) throws SlickException {
// Show the "R to Rotate tooltip"
if(!alreadyShowedRToRotateTooltip) {
tooltip.show("Press R to rotate");
alreadyShowedRToRotateTooltip = true;
}
if(leftMouseClicked == false || clickedUI(leftMouseClicked, mouseX, mouseY)) {
return;
}
// Has money?
if(business.getMoney() < placingAsset.getReferringMarketAsset().getPrice()) {
ResourceLoader.getInstance().popSound.play();
return;
}
// Valid placement?
if(placingAsset.isValidPlacement() == false) {
ResourceLoader.getInstance().popSound.play();
return;
}
// Place the asset
if(placeAsset(xTileAtMouse, yTileAtMouse)) {
// Do Shelf Stuff
if(placingAsset instanceof Shelf) {
// Updates the Game Storage Limit
business.getStore().setGameStorageLimit(business.getStore().getGameStorageLimit() + ((Shelf)placingAsset).getCapacity());
}
// If everything is ok, we make the purchase and place the Asset on the Map
business.buyAsset(placingAsset.getReferringMarketAsset());
// Revert back to normal settings
placingAsset.setBeingMoved(false);
// Adds it to the Store
business.getStore().addAsset(placingAsset);
// Then we play a cute purchase sound and show the amount of money spent
ResourceLoader.getInstance().cashRegisterSound.play();
// Go back to the Selection Mode
placingAsset = null;
currentMode = SELECTION_MODE;
tooltip.setVisible(false);
}
}
/**
* Moving Asset Mode
* @throws SlickException
*/
private void updateMovingAssetMode(boolean leftMouseClicked, int xTileAtMouse, int yTileAtMouse) throws SlickException {
// Show the "R to Rotate tooltip"
if(!alreadyShowedRToRotateTooltip) {
tooltip.show("Press R to rotate");
alreadyShowedRToRotateTooltip = true;
}
if(leftMouseClicked) {
// Here, the player selected an Asset and clicked to MOVE it
// Check if it is a new valid placement
if(placingAsset.isValidPlacement()) {
// Place the Asset!
placeAsset(xTileAtMouse, yTileAtMouse);
// No longer being moved
placingAsset.setBeingMoved(false);
// Play a cute placement sound
ResourceLoader.getInstance().placementSound.play();
// Go back to the Selection Mode
placingAsset = null;
currentMode = SELECTION_MODE;
tooltip.setVisible(false);
} else {
// In this case, the placement is not valid. Play a deny sound
ResourceLoader.getInstance().popSound.play();
}
}
}
/**
* Shows a tooltip with the price when buying a Map piece
* @param container
* @param game
* @param delta
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @throws SlickException
*/
private void updateBuyingMapMode(GameContainer container, StateBasedGame game, int delta,
boolean leftMouseClicked, boolean leftMouseDown, boolean rightMouseClicked, int mouseX, int mouseY) throws SlickException {
tooltip.show("Click to buy a tile for " + GameSystem.printMoney(business.getTilePrice(), true));
if (leftMouseClicked) {
if (!clickedUI(leftMouseClicked, mouseX, mouseY)) {
business.getStore().getMap().buyTile(business);
} else {
currentMode = SELECTION_MODE;
tooltip.setVisible(false);
}
} else if (rightMouseClicked) {
currentMode = SELECTION_MODE;
tooltip.setVisible(false);
}
}
/**
* Place Asset
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @throws SlickException
*/
public boolean placeAsset(int xTileAtMouse, int yTileAtMouse) throws SlickException {
// Tvs and Consoles have different placement procedures
if(placingAsset instanceof Tv) {
// The Table
Table table = null;
// Check if the Player clicked one of the Tiles the Table occupies
for(Table t : business.getStore().getTables())
if(t.tilesGotLeftClicked(true, xTileAtMouse, yTileAtMouse)) {
table = t;
break;
}
// Placing on the Table
if(table != null) {
// Check if the Table doesn't have a Tv yet
if(table.getTv() == null) {
// Tell the Tv it is on the table
placingAsset.setOnTable(true);
// Add the Tv to the Table
table.setTv((Tv)placingAsset);
// All good!
return true;
} else {
// The Table already has a Tv.
ResourceLoader.getInstance().popSound.play();
}
} else {
// Placing on the Floor
business.getStore().getMap().placeObject(placingAsset,
business.getStore().getCharacters(),
business.getStore().getMap().getXTileAtMouse(),
business.getStore().getMap().getYTileAtMouse());
// All good!
return true;
}
} else if(placingAsset instanceof Console) {
// The Table
Table table = null;
// Check if the Player clicked one of the Tiles the Table occupies
for(Table t : business.getStore().getTables())
if(t.tilesGotLeftClicked(true, xTileAtMouse, yTileAtMouse)) {
table = t;
break;
}
// Placing on the Table
if(table != null) {
// Check if the Table doesn't have a Tv yet
if(table.getConsole() == null) {
// Tell the console it is on the table
placingAsset.setOnTable(true);
// Add the Tv to the Table
table.setConsole((Console)placingAsset);
// All good!
return true;
} else {
// The Table already has a Tv.
ResourceLoader.getInstance().popSound.play();
}
} else {
// Placing on the Floor
business.getStore().getMap().placeObject(placingAsset,
business.getStore().getCharacters(),
business.getStore().getMap().getXTileAtMouse(),
business.getStore().getMap().getYTileAtMouse());
// All good!
return true;
}
} else {
// All other Assets
business.getStore().getMap().placeObject(placingAsset,
business.getStore().getCharacters(),
business.getStore().getMap().getXTileAtMouse(),
business.getStore().getMap().getYTileAtMouse());
// All good!
return true;
}
return false;
}
public int getCurrentMode() {
return currentMode;
}
public void setCurrentMode(int currentMode) {
this.currentMode = currentMode;
}
public Asset getPlacingAsset() {
return placingAsset;
}
public void setPlacingAsset(Asset placingAsset) {
this.placingAsset = placingAsset;
}
public TimeController getTimeController() {
return timeController;
}
public void setTimeController(TimeController timeController) {
this.timeController = timeController;
}
public Entity getSelectedEntity() {
return selectedEntity;
}
public void setSelectedEntity(Entity selectedEntity) {
this.selectedEntity = selectedEntity;
}
public int getSimulationSpeed() {
return simulationSpeed;
}
public void setSimulationSpeed(int simulationSpeed) {
this.simulationSpeed = simulationSpeed;
}
public FloatingText getTooltip() {
return tooltip;
}
@Override
public int getID() {return id;}
public Market getMarket() {
return market;
}
public FinancesPanel getFinancesPanel() {
return financesPanel;
}
public Input getInput() {
return input;
}
public Business getBusiness() {
return business;
}
public Camera getCamera() {
return camera;
}
}