package com.lucasdnd.ags.ui;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.FontUtils;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.gameplay.Misc;
import com.lucasdnd.ags.gameplay.Shelf;
import com.lucasdnd.ags.gameplay.Table;
import com.lucasdnd.ags.gameplay.Tv;
import com.lucasdnd.ags.gameplay.market.MarketAsset;
import com.lucasdnd.ags.gameplay.market.MarketMisc;
import com.lucasdnd.ags.gameplay.market.MarketShelf;
import com.lucasdnd.ags.gameplay.market.MarketTable;
import com.lucasdnd.ags.gameplay.market.MarketTv;
import com.lucasdnd.ags.map.OffsetUtil;
import com.lucasdnd.ags.system.GameSystem;
import com.lucasdnd.ags.system.MainState;
import com.lucasdnd.ags.system.ResourceLoader;
public class BuyFurnituresListRow extends ListRow {
// The game this List Item refers to
protected MarketAsset referringMarketAsset;
protected Button buyButton;
private float topMargin = 9f;
private float lineSize = 30f;
private BuyFurnituresListRow() throws SlickException {
buyButton = new Button(ResourceLoader.getInstance().greenBuyButtonImages, ResourceLoader.getInstance().disabledBuyButtonImage, 1f, 0f, 0f);
}
public BuyFurnituresListRow(MarketShelf referringMarketShelf, Business business) throws SlickException {
this();
this.referringMarketAsset = referringMarketShelf;
}
public BuyFurnituresListRow(MarketTable referringMarketTable, Business business) throws SlickException {
this();
this.referringMarketAsset = referringMarketTable;
}
public BuyFurnituresListRow(MarketTv referringMarketTv, Business business) throws SlickException {
this();
this.referringMarketAsset = referringMarketTv;
}
public BuyFurnituresListRow(MarketMisc referringMarketMisc, Business business) throws SlickException {
this();
this.referringMarketAsset = referringMarketMisc;
}
/**
* The update method, where we call the super class update (for mouse overs) and check for clicks.
*/
public void update(GameContainer container, StateBasedGame game, int delta, Business business, MainState mainState,
boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
// Super class update. This is where we check for mouse overs.
super.update(container, game, delta, business, leftMouseClicked, mouseX, mouseY,
referringMarketAsset.getReleaseDate() <= mainState.getTimeController().getTime());
// Buy Button Position
buyButton.update(container, game, delta, leftMouseClicked, false, mouseX, mouseY);
buyButton.setxPos(OffsetUtil.getPreciseOffset(this.xPos + 488f));
buyButton.setyPos(OffsetUtil.getPreciseOffset(this.yPos + 46f));
// Buy Button State
if(business.getMoney() >= referringMarketAsset.getPrice() && isAvailable) {
buyButton.setEnabled(true);
} else {
buyButton.setEnabled(false);
}
// Check if this List Row has been clicked
if(isAvailable && gotLeftClicked(leftMouseClicked, mouseX, mouseY)) {
// Check if the player has the money to buy the Furniture but DON'T subtract it yet!
if(business.getMoney() >= referringMarketAsset.getPrice()) {
// Enter the appropriate control state inside each if
// Instantiate the Furniture and give it to the Main State for placement
if(referringMarketAsset instanceof MarketShelf) {
mainState.setPlacingAsset(new Shelf(business.getStore().getShelves().size(), (MarketShelf)referringMarketAsset));
} else if(referringMarketAsset instanceof MarketTable) {
mainState.setPlacingAsset(new Table(business.getStore().getTables().size(), (MarketTable)referringMarketAsset));
} else if(referringMarketAsset instanceof MarketTv) {
mainState.setPlacingAsset(new Tv(business.getStore().getTvs().size(), (MarketTv)referringMarketAsset));
} else if(referringMarketAsset instanceof MarketMisc) {
mainState.setPlacingAsset(new Misc(business.getStore().getMiscs().size(), (MarketMisc)referringMarketAsset));
}
// Se the Buying Asset State
mainState.setCurrentMode(MainState.BUYING_ASSET);
// Placing Asset Settings
mainState.getPlacingAsset().setBeingMoved(true);
mainState.getPlacingAsset().setXTile(business.getStore().getMap().getXTileAtMouse());
mainState.getPlacingAsset().setYTile(business.getStore().getMap().getYTileAtMouse());
business.getStore().getMap().snapToGrid(mainState.getPlacingAsset());
// Close the Panel so the player can place the Console
mainState.hideAllPanels();
} else {
// Play a sound
// Show the floating text with the price in RED
}
}
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g, Font panelFont, Font disabledFont, Font shadowFont) throws SlickException {
// Calculate the Console sprite position
float spriteXPos = this.xPos + 38f;
float spriteYPos = this.yPos + 4f;
if (referringMarketAsset instanceof MarketTable) {
spriteXPos -= 12f;
spriteYPos += 36f;
} else if (referringMarketAsset instanceof MarketMisc) {
spriteYPos += 16f;
} else if (referringMarketAsset instanceof MarketTv) {
spriteYPos += 38f;
}
spriteXPos /= 2f;
spriteYPos /= 2f;
// Switch to Global Graphics Scale for a while here
g.scale(GameSystem.globalScale / 2f, GameSystem.globalScale / 2f);
g.drawImage(referringMarketAsset.getSpriteSheet().getSprite(0, 0), (int)spriteXPos, (int)spriteYPos);
g.scale(2f / GameSystem.globalScale, 2f / GameSystem.globalScale);
// If it's a shelf, show its capacity
if (referringMarketAsset instanceof MarketShelf) {
// Asset Name, capacity and release date
shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize/2f) + 1, referringMarketAsset.getName());
panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize/2f), referringMarketAsset.getName());
shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize + lineSize/2f) + 1, "Capacity: " + ((MarketShelf)referringMarketAsset).getCapacity());
panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize + lineSize/2f), "Capacity: " + ((MarketShelf)referringMarketAsset).getCapacity());
shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize * 2 + lineSize/2f) + 1, "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketAsset.getReleaseDate())));
panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize * 2 + lineSize/2f), "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketAsset.getReleaseDate())));
} else {
// Asset Name and Release date
shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize) + 1, referringMarketAsset.getName());
panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize), referringMarketAsset.getName());
shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize * 2) + 1, "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketAsset.getReleaseDate())));
panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize * 2), "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketAsset.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketAsset.getReleaseDate())));
}
// Price
shadowFont.drawString((int)(this.xPos + 492) + 1, (int)(this.yPos + topMargin + lineSize / 2 - 4) + 1, GameSystem.printMoney(referringMarketAsset.getPrice(), true));
panelFont.drawString((int)(this.xPos + 492), (int)(this.yPos + topMargin + lineSize / 2 - 4), GameSystem.printMoney(referringMarketAsset.getPrice(), true));
// Buy Button
buyButton.render(container, game, g);
Font tinyWhiteFont = ResourceLoader.getInstance().tinyWhiteFont;
FontUtils.drawCenter(tinyWhiteFont, "Buy", (int)buyButton.getxPos(), (int)(buyButton.getyPos() + buyButton.getHeight() / 2 - 9f), (int)(buyButton.getWidth()));
// Render the rectangle around
g.setLineWidth(4f);
Color previousColor = g.getColor();
Color strokeColor = new Color(0, 0, 0);
g.setColor(strokeColor);
g.draw(rectangle);
g.setColor(previousColor);
}
public MarketAsset getReferringMarketFurniture() {
return referringMarketAsset;
}
public void setReferringMarketFurniture(MarketAsset referringMarketFurniture) {
this.referringMarketAsset = referringMarketFurniture;
}
}