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.geom.Rectangle; import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.util.FontUtils; import com.lucasdnd.ags.gameplay.Business; import com.lucasdnd.ags.gameplay.Console; import com.lucasdnd.ags.gameplay.market.MarketConsole; 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 BuyConsolesListRow extends ListRow { // The game this List Item refers to protected MarketConsole referringMarketConsole; protected Button buyButton; private float topMargin = 9f; private float lineSize = 30f; /** * Market Game Constructor * @param marketGame * @throws SlickException */ public BuyConsolesListRow(MarketConsole referringMarketConsole, Business business) throws SlickException { // The referring Product this.referringMarketConsole = referringMarketConsole; // Buy button buyButton = new Button(ResourceLoader.getInstance().greenBuyButtonImages, ResourceLoader.getInstance().disabledBuyButtonImage, 1f, 0f, 0f); } /** * 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, referringMarketConsole.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() >= referringMarketConsole.getPrice() && isAvailable) { buyButton.setEnabled(true); } else { buyButton.setEnabled(false); } // Check if this List Row has been clicked if(buyButton.gotLeftClicked(leftMouseClicked, mouseX, mouseY)) { // Check if the player has the money to buy the Console but DON'T subtract it yet! if(isAvailable && business.getMoney() >= referringMarketConsole.getPrice()) { // Enter the PLACING_ASSET State mainState.setCurrentMode(MainState.BUYING_ASSET); // Instantiate the Console and give it to the Main State for placement mainState.setPlacingAsset(new Console(business.getStore().getTables().size(), referringMarketConsole)); 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(); } } } @Override public void render(GameContainer container, StateBasedGame game, Graphics g, Font panelFont, Font disabledFont, Font shadowFont) throws SlickException { // Calculate the Console sprite position float consoleSpriteXPos = this.xPos + 25f; consoleSpriteXPos /= GameSystem.globalScale; float consoleSpriteYPos = this.yPos + 35f; consoleSpriteYPos /= GameSystem.globalScale; // Switch to Global Graphics Scale for a while here g.scale(GameSystem.globalScale, GameSystem.globalScale); g.drawImage(referringMarketConsole.getSpriteSheet().getSprite(0, 0), (int)consoleSpriteXPos, (int)consoleSpriteYPos); g.scale(1f/GameSystem.globalScale, 1f/GameSystem.globalScale); // Console Name shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin) + 1, referringMarketConsole.getName()); panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin), referringMarketConsole.getName()); // Release Date shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize) + 1, "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketConsole.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketConsole.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketConsole.getReleaseDate()))); panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize), "Released: " + new SimpleDateFormat("dd").format(new Date(referringMarketConsole.getReleaseDate())) + "/" + new SimpleDateFormat("MM").format(new Date(referringMarketConsole.getReleaseDate())) + "/" + new SimpleDateFormat("yyyy").format(new Date(referringMarketConsole.getReleaseDate()))); // Available Games shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize * 2) + 1, "Available games: " + referringMarketConsole.getNumberOfGames()); panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize * 2), "Available games: " + referringMarketConsole.getNumberOfGames()); // Console Rating shadowFont.drawString((int)(this.xPos + 140) + 1, (int)(this.yPos + topMargin + lineSize * 3) + 1, "Reviews rating: "); panelFont.drawString((int)(this.xPos + 140), (int)(this.yPos + topMargin + lineSize * 3), "Reviews rating: "); g.drawImage(ResourceLoader.getInstance().gameRatingImages[referringMarketConsole.getQuality()], (int)(this.xPos + 326f), (int)(this.yPos + 100f)); // Price shadowFont.drawString((int)(this.xPos + 492) + 1, (int)(this.yPos + topMargin + lineSize / 2 - 4) + 1, GameSystem.printMoney(referringMarketConsole.getPrice(), true)); panelFont.drawString((int)(this.xPos + 492), (int)(this.yPos + topMargin + lineSize / 2 - 4), GameSystem.printMoney(referringMarketConsole.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); } @Override public void createRectangle() { rectangle = new Rectangle(this.xPos, this.yPos, this.width, this.height); } public MarketConsole getReferringMarketConsole() { return referringMarketConsole; } public void setReferringMarketConsole(MarketConsole referringMarketConsole) { this.referringMarketConsole = referringMarketConsole; } }