diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index f9d9229..165db89 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -13,23 +13,80 @@ import view.MapUI; public class RPGame { + + // Window properties public static final int WIDTH = 1206; public static final int HEIGHT = 929; + + // Inventory lists from file private ArrayList merchantInventoryList1 = new ArrayList(); // merchant 1's inventory list private ArrayList merchantInventoryList2 = new ArrayList(); // merchant 2's inventory list private ArrayList merchantInventoryList3 = new ArrayList(); // merchant 3's inventory list private ArrayList playerInventoryList = new ArrayList(); // the player's inventory list - private Player _player; - private Merchant _merchant1, _merchant2, _merchant3; + + // Character instances + private Player _player; + private Merchant _merchant1; + private Merchant _merchant2; + private Merchant _merchant3; + + // Map instance + private Map map; + + // MapUI instance and properties + private MapUI mapui; + + // Current game properties public boolean _movement = true; private int _currentDay; private int _transactionLimit; - public RPGame() { + public RPGame(int transactionLimit, int tileSize) { + + // Initializing member variables _currentDay = 1; - _transactionLimit = 3; + _transactionLimit = transactionLimit; + + // Calculating number of rows and columns for map + int rows = (HEIGHT - 29)/tileSize; + int cols = (WIDTH - 6)/tileSize; + + // Read inventory lists from file inventoryFromFile(); + + // Build merchant and player instances + buildMerchants(); + buildPlayer("test", 500); + + // Initialiing Map instance and adding the player and merchants to the map + map = new Map(rows, cols); + map.initializePlayer(_player, rows/2, cols/2); + map.initializeMerchant(_merchant1, rows/2, 1); + map.initializeMerchant(_merchant2, rows-2, cols/2); + map.initializeMerchant(_merchant3, rows/2, cols-2); + + + // Creating the MapUI instance and Sprites cooresponding to characters + mapui = new MapUI(map, this, tileSize); + mapui.createPlayerSprite(rows/2, cols/2); + mapui.addMerchantSprite(rows/2, 1); + mapui.addMerchantSprite(rows-2, cols/2); + mapui.addMerchantSprite(rows/2, cols-2); + + // Creating JFrame window + JFrame frame = new JFrame("Merchant RPG"); + + // Adding MapUI instance to window + frame.add(mapui); + + // Setting properties of JFrame + frame.setSize(RPGame.WIDTH, RPGame.HEIGHT); + frame.setResizable(false); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocationRelativeTo(null); + } /** @@ -75,20 +132,19 @@ else if (currentMerchant == 3) */ public void buildMerchants() { - _merchant1 = new Merchant("Cheap Merchant", 200, merchantInventoryList1); - _merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2); - _merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3); + _merchant1 = new Merchant("Cheap Merchant", 200, merchantInventoryList1, 0); + _merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2, merchantInventoryList1.size()); + _merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3, merchantInventoryList1.size()+ merchantInventoryList2.size()); } /** * Generates the player * @param name Player name * @param startingCash Amount of cash the player starts with - * @param startingInventory The Player's starting inventory */ - public void buildPlayer(String name, int startingCash, ArrayList startingInventory) + public void buildPlayer(String name, int startingCash) { - _player = new Player(name, startingCash, startingInventory); + _player = new Player(name, startingCash, playerInventoryList); } /** @@ -132,6 +188,7 @@ public void createTransaction(Player player, Merchant targetMerchant) { toggleMovement("OFF"); Transaction newTransaction = new Transaction(player, targetMerchant, this); + newTransaction.runTransaction(); _transactionLimit -= 1; } else @@ -167,7 +224,7 @@ public void advanceDailyCycle(){ if( (_currentDay % 6) == 0) //will increase the merchant's base cash every 6 days { int multiplier = (int) Math.floor(_currentDay / 10) + 1; - + addAndRefreshCash(_merchant1, multiplier * 100); addAndRefreshCash(_merchant2, multiplier * 100); addAndRefreshCash(_merchant3, multiplier * 100); @@ -195,7 +252,7 @@ private void scaleAllMerchantPrices(Merchant[] merchants) m.scaleAllAdjustedPrices(); } } - + /** * Will call addCash on the merchant provided then refreshes their cash * @@ -207,6 +264,15 @@ public void addAndRefreshCash (Merchant targetMerchant, int increaseAmount) targetMerchant.addCash(increaseAmount); targetMerchant.refreshCash(); } + + /** + * Updates the MapUI to move characters and detect events + * + */ + public void updateMapUI() { + mapui.move(); + mapui.repaint(); + } /** * Toggles the movement on or off based on the input @@ -245,7 +311,7 @@ else if(merchantNum == 2) else return _merchant3; } - + /** * Getter for the current day number * @return The day number @@ -254,7 +320,7 @@ public int getDay() { return _currentDay; } - + /** * * @return Movement variable @@ -271,51 +337,12 @@ public boolean getMovement() */ public static void main(String[] args) throws InterruptedException { - RPGame _rpg = new RPGame(); - _rpg.buildMerchants(); - ArrayList playerInventory = _rpg.getMerchantInventoryList(1); - playerInventory.addAll(_rpg.getMerchantInventoryList(2)); - playerInventory.addAll(_rpg.getMerchantInventoryList(3)); - _rpg.buildPlayer("test", 500, playerInventory); - - // SETTING UP PLAYER AND MERCHANT - Player player = new Player("Player", 0, null); - Merchant merch1 = new Merchant("Bill", 0, null); - Merchant merch2 = new Merchant("Joe", 0, null); - Merchant merch3 = new Merchant("Sam", 0, null); - - // CREATING MAP AND INITIALIZE PLAYER AND MERCHANTS - Map map = new Map(30, 40); - map.initializePlayer(player, 15, 20); - map.initializeMerchant(merch1, 15, 0); - map.initializeMerchant(merch2, 29, 20); - map.initializeMerchant(merch3, 15, 39); - - // CREATING JFRAME WINDOW - JFrame frame = new JFrame("Merchant RPG"); - - // CREATING MAPUI AND SPRITES - MapUI mapui = new MapUI(map,_rpg); - mapui.createPlayerSprite(15, 20); - mapui.addMerchantSprite(15, 0); - mapui.addMerchantSprite(29, 20); - mapui.addMerchantSprite(15, 39); - - // ADDING MAPUI TO JFRAME - frame.add(mapui); - - // SETTING PROPERTIES OF JFRAME - frame.setSize(RPGame.WIDTH, RPGame.HEIGHT); - frame.setResizable(false); - frame.setVisible(true); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setLocationRelativeTo(null); - - // MAIN GAME LOOP - while (true) { - mapui.move(); - mapui.repaint(); - Thread.sleep(100/12); // Controls the speed of the game (currently 120 frames/second) - } + RPGame rpgGame = new RPGame(3, 30); + + // MAIN GAME LOOP + while (true) { + rpgGame.updateMapUI(); + Thread.sleep(1000/144); // Controls the speed of the game (currently 144 frames/second) + } } } diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index e7b4fb9..6c005c6 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -19,16 +19,16 @@ public Transaction(Player player, Merchant targetMerchant, RPGame game) _player = player; _targetMerchant = targetMerchant; _game = game; - _window = new TransactionUI(this); - _window.setVisible(true); } /** * Will be Transaction class's main method - * incomplete method + * */ public void runTransaction() { + _window = new TransactionUI(this); + _window.setVisible(true); } /** diff --git a/MerchantRPGCSE2102/src/graph/Vertex.java b/MerchantRPGCSE2102/src/graph/Vertex.java index c904344..a4dce98 100644 --- a/MerchantRPGCSE2102/src/graph/Vertex.java +++ b/MerchantRPGCSE2102/src/graph/Vertex.java @@ -2,12 +2,14 @@ import java.util.LinkedList; +import model.Character; + public class Vertex { private int _cellNum; private int _row; private int _col; - private Object _occupant = null; + private Character _occupant = null; private LinkedList _incidentEdges = new LinkedList(); @@ -37,11 +39,11 @@ public LinkedList getIncidentList() { return _incidentEdges; } - public Object getOccupant() { + public Character getOccupant() { return _occupant; } - public void setOccupant(Object occupant) { + public void setOccupant(Character occupant) { _occupant = occupant; } diff --git a/MerchantRPGCSE2102/src/images/Merchant-real.png b/MerchantRPGCSE2102/src/images/Merchant-real.png new file mode 100644 index 0000000..739eb79 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/Merchant-real.png differ diff --git a/MerchantRPGCSE2102/src/images/RPG_samplemap.png b/MerchantRPGCSE2102/src/images/RPG_samplemap.png new file mode 100644 index 0000000..03fbf72 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/RPG_samplemap.png differ diff --git a/MerchantRPGCSE2102/src/images/dialogbubble-reverse.png b/MerchantRPGCSE2102/src/images/dialogbubble-reverse.png new file mode 100644 index 0000000..bd8e592 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/dialogbubble-reverse.png differ diff --git a/MerchantRPGCSE2102/src/images/dialogbubble.png b/MerchantRPGCSE2102/src/images/dialogbubble.png new file mode 100644 index 0000000..d9500ad Binary files /dev/null and b/MerchantRPGCSE2102/src/images/dialogbubble.png differ diff --git a/MerchantRPGCSE2102/src/images/merchant.png b/MerchantRPGCSE2102/src/images/merchant.png new file mode 100644 index 0000000..9436d87 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/merchant.png differ diff --git a/MerchantRPGCSE2102/src/images/scrollbutton.jpg b/MerchantRPGCSE2102/src/images/scrollbutton.jpg new file mode 100644 index 0000000..428b9ac Binary files /dev/null and b/MerchantRPGCSE2102/src/images/scrollbutton.jpg differ diff --git a/MerchantRPGCSE2102/src/images/testsprite.png b/MerchantRPGCSE2102/src/images/testsprite.png index 99d0222..0d38f61 100644 Binary files a/MerchantRPGCSE2102/src/images/testsprite.png and b/MerchantRPGCSE2102/src/images/testsprite.png differ diff --git a/MerchantRPGCSE2102/src/model/Character.java b/MerchantRPGCSE2102/src/model/Character.java index 37a63ab..53db13f 100644 --- a/MerchantRPGCSE2102/src/model/Character.java +++ b/MerchantRPGCSE2102/src/model/Character.java @@ -7,7 +7,7 @@ public class Character { /** - * Searches through the player's inventory for the item corresponding to the specified item name + * Searches through the character's inventory for the item corresponding to the specified item name * * @param itemName string containing the name of the item * @return the item matching the specified item name @@ -25,7 +25,7 @@ public Item getItem(String itemName) } /** - * Returns the merchant's item inventory + * Returns the item inventory * @return item inventory array */ public Item[] getInventory() @@ -34,9 +34,9 @@ public Item[] getInventory() } /** - * Returns a string containing the name of the merchant + * Returns a string containing the name of the character * - * @return merchant name string + * @return character name string */ public String getName() { diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java index f410d11..b42c479 100644 --- a/MerchantRPGCSE2102/src/model/Map.java +++ b/MerchantRPGCSE2102/src/model/Map.java @@ -8,6 +8,7 @@ public class Map { private int _rows, _cols; // The n*n dimension of the map private int[] initialPlayerLocation = new int[2]; private Player _player; + private Merchant currentNearbyMerchant; public Map(int rows, int cols) { _rows = rows; @@ -112,8 +113,17 @@ public Graph getGraph() { public boolean isOccupied(int col, int row) { if (row < 0 || col < 0 || row >= _rows || col >= _cols) return false; - else - return _mapGraph.getVertex(row*_cols + col).getOccupant() != null; + else { + if (_mapGraph.getVertex(row*_cols + col).getOccupant() != null) { + System.out.println(_mapGraph.getVertex(row*_cols + col).getOccupant().getClass().getName()); + if (_mapGraph.getVertex(row*_cols + col).getOccupant().getClass().getName().equals("model.Merchant")) + setCurrentNearbyMerchant((Merchant) _mapGraph.getVertex(row*_cols + col).getOccupant()); + return true; + } + } + + setCurrentNearbyMerchant(null); + return false; } public boolean collisionTo(String direction) { @@ -137,7 +147,10 @@ public boolean collisionTo(String direction) { return false; } + public void resetPlayerLocation() { + _mapGraph.getVertex(_player.getRow()*_cols + _player.getCol()).setOccupant(null); + _mapGraph.getVertex(initialPlayerLocation[0]*_cols + initialPlayerLocation[1]).setOccupant(_player); _player.setCol(initialPlayerLocation[1]); _player.setRow(initialPlayerLocation[0]); } @@ -146,4 +159,20 @@ public Player getPlayer() { return _player; } + public Merchant getCurrentNearbyMerchant() { + return currentNearbyMerchant; + } + + public void setCurrentNearbyMerchant(Merchant currentNearbyMerchant) { + this.currentNearbyMerchant = currentNearbyMerchant; + } + + public int getRows() { + return _rows; + } + + public int getCols() { + return _cols; + } + } diff --git a/MerchantRPGCSE2102/src/model/Merchant.java b/MerchantRPGCSE2102/src/model/Merchant.java index e9651c7..2e938fb 100644 --- a/MerchantRPGCSE2102/src/model/Merchant.java +++ b/MerchantRPGCSE2102/src/model/Merchant.java @@ -8,13 +8,15 @@ public class Merchant extends Character private int _currentCash; private int _baseCash; private int _dailyRandomPercent; + private int inventoryStartIndex; - public Merchant(String name, int cashLimit, ArrayList inventory) + public Merchant(String name, int cashLimit, ArrayList inventory, int inventoryStartIndex) { _name = name; _currentCash = cashLimit; _baseCash = cashLimit; + this.setInventoryStartIndex(inventoryStartIndex); if (inventory != null) generateMerchantInventory(inventory); _dailyRandomPercent = 0; //placeholder percentage @@ -162,4 +164,12 @@ public int getRandomPercent() { return _dailyRandomPercent; } + + public int getInventoryStartIndex() { + return inventoryStartIndex; + } + + public void setInventoryStartIndex(int inventoryStartIndex) { + this.inventoryStartIndex = inventoryStartIndex; + } } diff --git a/MerchantRPGCSE2102/src/model/Player.java b/MerchantRPGCSE2102/src/model/Player.java index dc6a9a6..4f77878 100644 --- a/MerchantRPGCSE2102/src/model/Player.java +++ b/MerchantRPGCSE2102/src/model/Player.java @@ -60,9 +60,16 @@ public boolean buy(String itemName, Merchant targetMerchant, int amount) deductCash(totalPrice); Item targetItem = getItem(itemName); targetItem.increaseQuantity(amount); + printInventory(); return true; } } + + public void printInventory() { + for (Item item : _inventory) { + System.out.println(item.getItemName() + " " + item.getQuantity()); + } + } /** * Allows player to sell an item to a target merchant diff --git a/MerchantRPGCSE2102/src/sprites/MerchantSprite.java b/MerchantRPGCSE2102/src/sprites/MerchantSprite.java index ba6414b..668b44d 100644 --- a/MerchantRPGCSE2102/src/sprites/MerchantSprite.java +++ b/MerchantRPGCSE2102/src/sprites/MerchantSprite.java @@ -2,6 +2,11 @@ import java.awt.Graphics2D; import java.awt.Rectangle; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; public class MerchantSprite { private int x, y; @@ -17,8 +22,21 @@ public MerchantSprite(int row, int col) { * @param g Graphics2D for painting */ public void paint(Graphics2D g) { - g.fillRect(x, y, WIDTH, WIDTH); + g.drawImage(loadSprite("merchant.png"),x, y, null); } + + public static BufferedImage loadSprite(String file) { + + BufferedImage sprite = null; + + try { + sprite = ImageIO.read(new File("src/images/" + file)); + } catch (IOException e) { + e.printStackTrace(); + } + + return sprite; + } /** * Gets the bounds of the sprite in the form of a Rectangle diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index e922ba4..184cfc0 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -1,6 +1,5 @@ package sprites; -import java.awt.Color; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.KeyEvent; @@ -10,8 +9,7 @@ public class PlayerSprite { MapUI mapui; // Instance of MapUI which created this PlayerSprite - private static final int WIDTH = 30; // Width of the PlayerSprite - private Color color = new Color(0, 0, 0); // Color of the PlayerSprite (this will no longer be needed once we have a sprite sheet) + private int width = 30; // Width of the PlayerSprite private int x, y; // x and y coordinates (in pixels) private int dx, dy = 0; // Velocity of the sprite @@ -25,30 +23,31 @@ public class PlayerSprite { private boolean rightBeingPressed = false; private boolean downBeingPressed = false; private boolean upBeingPressed = false; - + // Images for each animation - private BufferedImage[] walkingLeft = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet - private BufferedImage[] walkingRight = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)}; - private BufferedImage[] walkingUp = {Sprite.getSprite(0, 1), Sprite.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet - private BufferedImage[] walkingDown = {Sprite.getSprite(0, 2), Sprite.getSprite(2, 1)}; - private BufferedImage[] standing = {Sprite.getSprite(1, 0)}; + private BufferedImage[] walkingLeft = {SpriteLoader.getSprite(0, 1), SpriteLoader.getSprite(1, 1), SpriteLoader.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet + private BufferedImage[] walkingRight = {SpriteLoader.getSprite(0, 2), SpriteLoader.getSprite(1, 2), SpriteLoader.getSprite(2, 2)}; + private BufferedImage[] walkingUp = {SpriteLoader.getSprite(0, 3), SpriteLoader.getSprite(1, 3), SpriteLoader.getSprite(2, 3)}; // Gets the upper left images of my sprite sheet + private BufferedImage[] walkingDown = {SpriteLoader.getSprite(0, 0), SpriteLoader.getSprite(1, 0), SpriteLoader.getSprite(2, 0)}; + private BufferedImage[] standing = {SpriteLoader.getSprite(1, 0)}; // Animation states private Animation walkLeft = new Animation(walkingLeft, 10); private Animation walkRight = new Animation(walkingRight, 10); private Animation walkUp = new Animation(walkingUp, 10); private Animation walkDown = new Animation(walkingDown, 10); - private Animation standing1 = new Animation(standing, 10); + private Animation stand = new Animation(standing, 10); - // Actual animation - private Animation animation = standing1; + // Current animation + private Animation animation; - public PlayerSprite(MapUI mapui, int row, int col) { - this.x = col*WIDTH; + public PlayerSprite(MapUI mapui, int row, int col, int width) { + this.x = col*width; initialX = x; - this.y = row*WIDTH; + this.y = row*width; initialY = y; this.mapui = mapui; + animation = stand; } /** @@ -59,26 +58,26 @@ public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; animation = walkLeft; - animation.start(); + animation.start(); leftBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dx = 1; animation = walkRight; - animation.start(); + animation.start(); rightBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; animation = walkUp; - animation.start(); + animation.start(); upBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { dy = 1; animation = walkDown; - animation.start(); + animation.start(); downBeingPressed = true; } } @@ -88,53 +87,61 @@ public void keyPressed(KeyEvent e) { * @param e The KeyEvent detected by the system */ public void keyReleased(KeyEvent e) { - + if(e.getKeyChar()=='n') { mapui.nextDay(); resetLocation(); } - + if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftBeingPressed = false; if (rightBeingPressed == false) // Sprite will stop only when the RIGHT key is not being pressed - dx = 0; + animation = stand; + dx = 0; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightBeingPressed = false; if (leftBeingPressed == false) // Sprite will stop only when the LEFT key is not being pressed - dx = 0; + animation = stand; + dx = 0; } if (e.getKeyCode() == KeyEvent.VK_UP) { upBeingPressed = false; if (downBeingPressed == false) // Sprite will stop only when the DOWN key is not being pressed - dy = 0; + animation = stand; + dy = 0; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { downBeingPressed = false; if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed - dy = 0; + animation = stand; + dy = 0; } - if (e.getKeyCode() == KeyEvent.VK_F){ - if((mapui.getMap().collisionTo("east"))||(mapui.getMap().collisionTo("west"))||(mapui.getMap().collisionTo("south"))||(mapui.getMap().collisionTo("north"))||(mapui.getMap().collisionTo("northeast"))||(mapui.getMap().collisionTo("northwest"))||(mapui.getMap().collisionTo("southeast"))||(mapui.getMap().collisionTo("southwest"))){ - System.out.println("Transaction starting"); // For testing purposes - mapui.game.createTransaction(null, null);//RPGame initialize Trade - } - else{ - return; - } + if (e.getKeyCode() == KeyEvent.VK_F) { + if (isMerchantNearby()) + mapui.game.createTransaction(mapui.map.getPlayer(), mapui.map.getCurrentNearbyMerchant()); // RPGame initializes Trade } + // This fixes some bugs with holding three keys down at once and letting go of two (Basically ensures that dx and dy match which keys are currently being held) - if(leftBeingPressed) + if(leftBeingPressed) { dx = -1; - if(rightBeingPressed) + animation = walkLeft; + } + if(rightBeingPressed) { dx = 1; - if(downBeingPressed) + animation = walkRight; + } + if(downBeingPressed) { dy = 1; - if(upBeingPressed) + animation = walkDown; + } + if(upBeingPressed) { dy = -1; + animation = walkUp; + } } @@ -201,7 +208,7 @@ public void move() { } // Checks that the sprite is moving within the bounds of the panel - if (getX() + dx >= 0 && getX() + dx <= mapui.getWidth() - WIDTH && getY() + dy >= 0 && getY() + dy <= mapui.getHeight() - WIDTH) { + if (getX() + dx >= 0 && getX() + dx <= mapui.getWidth() - width && getY() + dy >= 0 && getY() + dy <= mapui.getHeight() - width) { setX(getX() + dx); setChangeInX(getChangeInX() + dx); setY(getY() + dy); @@ -209,13 +216,13 @@ public void move() { } } - + public void resetLocation() { x = initialX; y = initialY; changeInX = 15; changeInY = 15; - + } /** @@ -223,8 +230,9 @@ public void resetLocation() { * @param g Graphics2D for painting */ public void paint(Graphics2D g) { - + g.drawImage(animation.getSprite(), x, y, null); + animation.update(); } /** @@ -232,7 +240,7 @@ public void paint(Graphics2D g) { * @return the Rectangle formed by the sprite */ public Rectangle getBounds() { - return new Rectangle(getX(), getY(), WIDTH, WIDTH); + return new Rectangle(getX(), getY(), width, width); } /** @@ -278,4 +286,8 @@ public void setY(int y) { this.y = y; } + public boolean isMerchantNearby() { + return (mapui.getMap().collisionTo("east"))||(mapui.getMap().collisionTo("west"))||(mapui.getMap().collisionTo("south"))||(mapui.getMap().collisionTo("north"))||(mapui.getMap().collisionTo("northeast"))||(mapui.getMap().collisionTo("northwest"))||(mapui.getMap().collisionTo("southeast"))||(mapui.getMap().collisionTo("southwest")) && (mapui.getMap().getCurrentNearbyMerchant() != null); + } + } diff --git a/MerchantRPGCSE2102/src/sprites/Sprite.java b/MerchantRPGCSE2102/src/sprites/SpriteLoader.java similarity index 92% rename from MerchantRPGCSE2102/src/sprites/Sprite.java rename to MerchantRPGCSE2102/src/sprites/SpriteLoader.java index 81711fa..5aa2c18 100644 --- a/MerchantRPGCSE2102/src/sprites/Sprite.java +++ b/MerchantRPGCSE2102/src/sprites/SpriteLoader.java @@ -6,7 +6,7 @@ import javax.imageio.ImageIO; -public class Sprite { +public class SpriteLoader { private static BufferedImage spriteSheet; private static final int TILE_SIZE = 32; diff --git a/MerchantRPGCSE2102/src/tests/TestPlayer.java b/MerchantRPGCSE2102/src/tests/TestPlayer.java index 1052553..a03b91c 100644 --- a/MerchantRPGCSE2102/src/tests/TestPlayer.java +++ b/MerchantRPGCSE2102/src/tests/TestPlayer.java @@ -15,7 +15,7 @@ public class TestPlayer extends TestCase public void setup() { - RPGame game = new RPGame(); //starting a new game + RPGame game = new RPGame(3, 30); //starting a new game game.inventoryFromFile(); //read from file p = new Player("TestPlayer", 1000, game.getPlayerInventoryList()); m = new Merchant("TestMerchant1", 100000, game.getMerchantInventoryList(1)); diff --git a/MerchantRPGCSE2102/src/tests/TestRPGame.java b/MerchantRPGCSE2102/src/tests/TestRPGame.java index 272bbbb..bab1507 100644 --- a/MerchantRPGCSE2102/src/tests/TestRPGame.java +++ b/MerchantRPGCSE2102/src/tests/TestRPGame.java @@ -11,12 +11,12 @@ public class TestRPGame extends TestCase public void setup() { - game = new RPGame(); + game = new RPGame(0, 0); game.buildMerchants(); ArrayList playerInventory = game.getMerchantInventoryList(1); playerInventory.addAll(game.getMerchantInventoryList(2)); playerInventory.addAll(game.getMerchantInventoryList(3)); - game.buildPlayer("Test", 1000, playerInventory); + game.buildPlayer("Test", 1000); } public void testFile() diff --git a/MerchantRPGCSE2102/src/tests/TestTransaction.java b/MerchantRPGCSE2102/src/tests/TestTransaction.java index 876e1be..f881795 100644 --- a/MerchantRPGCSE2102/src/tests/TestTransaction.java +++ b/MerchantRPGCSE2102/src/tests/TestTransaction.java @@ -11,13 +11,13 @@ public class TestTransaction extends TestCase public void setup() { - _rpg = new RPGame(); + _rpg = new RPGame(0, 0); _rpg.inventoryFromFile(); _rpg.buildMerchants(); ArrayList playerInventory = _rpg.getMerchantInventoryList(1); playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); - _rpg.buildPlayer("test", 500, playerInventory); + _rpg.buildPlayer("test", 500); } public void testActionSell() diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index 1535611..4e26292 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -7,31 +7,35 @@ import java.awt.RenderingHints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import javax.imageio.ImageIO; import javax.swing.JPanel; import controller.RPGame; - import model.Map; import sprites.MerchantSprite; import sprites.PlayerSprite; -import controller.RPGame; + @SuppressWarnings("serial") public class MapUI extends JPanel { public RPGame game; public Map map; + public int tileSize; private PlayerSprite player; private ArrayList merchants = new ArrayList(); - public MapUI(Map map,RPGame Game) { + public MapUI(Map map, RPGame Game, int tileSize) { this.map = map; this.game= Game; - + this.tileSize = tileSize; addKeyListener(new KeyListener() { @Override @@ -59,33 +63,33 @@ public void keyTyped(KeyEvent e) { @Override public void paint(Graphics g) { super.paint(g); // repaints the canvas - + Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Prevents aliasing - - // For testing purposes. Creates a grid of lines on the canvas - for (int i = 0; i < map.getGraph().getCols(); i++) { - for (int j = 0; j < map.getGraph().getRows(); j++) { - g2d.fillRect(i*30, j*30, 1, 30); - g2d.fillRect(i*30, j*30, 30, 1); - } - } - + + g2d.drawImage(loadImage("RPG_samplemap.png"), 0, 0, null); + // Paints the player onto the canvas player.paint(g2d); // Paints each merchant onto the canvas for (MerchantSprite merchant : merchants) merchant.paint(g2d); - - // For testing purposes. Shows the Player's current x and y position as well as the current Vertex - g2d.setColor(Color.GRAY); - g2d.setFont(new Font("Verdana", Font.BOLD, 20)); - g2d.drawString("x: " + player.getX() + " y : " + player.getY(), 10, 20); - g2d.drawString("delta X: " + player.getChangeInX() +" delta Y: " + player.getChangeInY(), 10, 50); - g2d.drawString("Vertex: (" + map.getPlayer().getRow() + ", " + map.getPlayer().getCol() + ")", 1000, 20); + + if (player.isMerchantNearby()) { + if (map.getCurrentNearbyMerchant().getCol() > map.getCols()/2) + g2d.drawImage(loadImage("dialogbubble-reverse.png"), map.getCurrentNearbyMerchant().getCol()*tileSize - 85, map.getCurrentNearbyMerchant().getRow()*tileSize - 60, null); + else + g2d.drawImage(loadImage("dialogbubble.png"), (map.getCurrentNearbyMerchant().getCol() + 1)*tileSize, map.getCurrentNearbyMerchant().getRow()*tileSize - 60, null); + } + + g2d.setColor(Color.BLUE); + g2d.setFont(new Font("Verdana", Font.BOLD, 18)); + // For testing purposes. Shows the Player's current Vertex position + //g2d.drawString("Vertex: (" + map.getPlayer().getRow() + ", " + map.getPlayer().getCol() + ")", 1000, 20); + //g2d.dispose(); } - + /** * Adds a MerchantSprite to the list of MerchantSprite's to be painted on the canvas * @param row The row of the merchant @@ -102,11 +106,11 @@ public void addMerchantSprite(int row, int col) { * @param y The column of the player */ public void createPlayerSprite(int row, int col) { - player = new PlayerSprite(this, row, col); + player = new PlayerSprite(this, row, col, tileSize); } - + public void changeState(){ - + } /** @@ -115,37 +119,50 @@ public void changeState(){ public void move() { //check transaction if (game._movement==false){ - + } else{ - - - player.move(); - - // Every 30 pixels, the player will move to a new Vertex - // I used the values 0, 1, 30, 31 to establish a border between crossing Vertices - // For example, if the player reaches a changeInX of 31, this means they are moving to a Vertex in the east direction and their changeInX is reset to 1 - // if they then decide to move west, they will move to the next Vertex when their changeInX reaches 0. - // Thus, the border shared by two vertices is inbetween 0 and 1 as well as 30 and 31. - if (player.getChangeInX() == 31) { - map.movePlayer("east"); - player.setChangeInX(1); - } - if (player.getChangeInX() == 0) { - map.movePlayer("west"); - player.setChangeInX(30); - } - if (player.getChangeInY() == 0) { - map.movePlayer("north"); - player.setChangeInY(30); - } - if (player.getChangeInY() == 31) { - map.movePlayer("south"); - player.setChangeInY(1); + + + player.move(); + + // Every 30 pixels, the player will move to a new Vertex + // I used the values 0, 1, 30, 31 to establish a border between crossing Vertices + // For example, if the player reaches a changeInX of 31, this means they are moving to a Vertex in the east direction and their changeInX is reset to 1 + // if they then decide to move west, they will move to the next Vertex when their changeInX reaches 0. + // Thus, the border shared by two vertices is inbetween 0 and 1 as well as 30 and 31. + if (player.getChangeInX() == tileSize + 1) { + map.movePlayer("east"); + player.setChangeInX(1); + } + if (player.getChangeInX() == 0) { + map.movePlayer("west"); + player.setChangeInX(tileSize); + } + if (player.getChangeInY() == 0) { + map.movePlayer("north"); + player.setChangeInY(tileSize); + } + if (player.getChangeInY() == tileSize + 1) { + map.movePlayer("south"); + player.setChangeInY(1); + } } + } + + public static BufferedImage loadImage(String fileName) { + + BufferedImage image = null; + + try { + image = ImageIO.read(new File("src/images/" + fileName)); + } catch (IOException e) { + e.printStackTrace(); } + + return image; } - + /** * Calls the method for advancing the day in the instance of RPGame * @return @@ -154,11 +171,11 @@ public void nextDay() { game.advanceDailyCycle(); map.resetPlayerLocation(); } - + public Map getMap() { return map; } - + public ArrayList getMerchants() { return merchants; } diff --git a/MerchantRPGCSE2102/src/view/ScrollButton.java b/MerchantRPGCSE2102/src/view/ScrollButton.java new file mode 100644 index 0000000..d8bd633 --- /dev/null +++ b/MerchantRPGCSE2102/src/view/ScrollButton.java @@ -0,0 +1,46 @@ +package view; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.swing.JButton; + +public class ScrollButton extends JButton{ + + private static final long serialVersionUID = 1L; + BufferedImage image; + String text; + + public ScrollButton(String text) { + this.text = text; + try { + image = ImageIO.read(new File("src/images/scrollbutton.jpg")); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d= (Graphics2D) g; + g.drawImage(image, 0, 0, null); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setColor(Color.BLACK); + g2d.setFont(new Font("Elephant", Font.BOLD, 15)); + g2d.drawString(text, 12, 30); + } + + @Override + public Dimension getPreferredSize() { + return new Dimension(image.getWidth(), image.getHeight()); + } +} diff --git a/MerchantRPGCSE2102/src/view/StartScreen.java b/MerchantRPGCSE2102/src/view/StartScreen.java index 47e6bc9..1e0bf3c 100644 --- a/MerchantRPGCSE2102/src/view/StartScreen.java +++ b/MerchantRPGCSE2102/src/view/StartScreen.java @@ -17,82 +17,62 @@ import javax.swing.*; public class StartScreen extends JFrame { - + private static final long serialVersionUID = 1L; - + JPanel startMenu = new JPanel(); - - + + public static void main(String[] args) throws IOException { new StartScreen(); } - + public StartScreen() throws IOException { - - - - - + JLabel MainMenuBG = new JLabel(new ImageIcon("src/images/background.jpg")); MainMenuBG.setVisible(true); - - - - - - - Image icon, background; icon = loadIcon(); background = loadBackground(); BackgroundPanel panel = new BackgroundPanel(background, BackgroundPanel.ACTUAL, 1.0f, 0.5f); this.setIconImage(icon); - this.setTitle("The Merchant RPG"); this.setSize(1500, 844); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); - - + JButton start = new JButton("Start"); start.setVisible(true); - start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println("Game started"); } - }); - + }); + JButton instructions = new JButton("Instructions"); instructions.setVisible(true); - - instructions.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println("Instructions opened"); } + }); - }); - this.getContentPane().setLayout(null); start.setBounds(650, 382, 200, 80); instructions.setBounds(650, 482, 200, 40); panel.setBounds(0, 0, 1500, 844); start.setBackground(null); instructions.setBackground(null); - - this.getContentPane().add(instructions); this.getContentPane().add(start); - this.add(panel); } - + public BufferedImage loadIcon() throws IOException { BufferedImage img = null; try { @@ -103,7 +83,7 @@ public BufferedImage loadIcon() throws IOException { return null; } } - + public BufferedImage loadBackground() throws IOException { BufferedImage img = null; try { diff --git a/MerchantRPGCSE2102/src/view/TransactionPanel.java b/MerchantRPGCSE2102/src/view/TransactionPanel.java new file mode 100644 index 0000000..a8c6525 --- /dev/null +++ b/MerchantRPGCSE2102/src/view/TransactionPanel.java @@ -0,0 +1,45 @@ +package view; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.LayoutManager; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.swing.JPanel; + +public class TransactionPanel extends JPanel { + + private static final long serialVersionUID = 1L; + BufferedImage background = null; + + + TransactionPanel(LayoutManager lm) { + super(lm); + background = loadImage("Merchant-real.png"); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g2d.drawImage(background, 0, 0, null); + + } + + public BufferedImage loadImage(String fileName) { + + BufferedImage image = null; + + try { + image = ImageIO.read(new File("src/images/" + fileName)); + } catch (IOException e) { + e.printStackTrace(); + } + + return image; + } + +} diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index bfc722c..94057d8 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -1,143 +1,201 @@ package view; -import javax.swing.*; - +import java.awt.BorderLayout; import java.awt.Font; - +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import controller.Transaction; import exceptions.MerchantNotEnoughCashException; import exceptions.NotInInventoryException; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; - @SuppressWarnings("serial") public class TransactionUI extends JFrame { - private static JPanel transactionPanel; - private static Transaction MASTER; //TransactionUI class will hold the instance of the Transaction that created it - private static boolean _isWarning; + private JPanel transactionPanel; + private JPanel buttonPanel; + private GridBagConstraints buttonLayoutConstraints; + private Transaction transaction; + private boolean _isWarning; + + + public TransactionUI(Transaction transaction) { + + this.transaction = transaction; + setupJFrame(); + setupButtons(); + setupLabels(); + addComponentsToFrame(); + + } /** - * Create the frame. + * Configures the JFrame of the transaction window + * */ - public TransactionUI(Transaction master) { - MASTER = master; - _isWarning = false; - setTitle("Transaction Window"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(100, 100, 600, 430); - transactionPanel = new JPanel(); - transactionPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); - transactionPanel.setLayout(null); - - JButton btnBuy = new JButton("BUY"); //creates a "Buy" button - btnBuy.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - } - }); - btnBuy.addMouseListener(new MouseAdapter() { + public void setupJFrame() { + setTitle("Merchant Transaction"); + setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + setSize(600, 430); + transactionPanel = new TransactionPanel(new BorderLayout()); + buttonPanel = new JPanel(new GridBagLayout()); + buttonLayoutConstraints = new GridBagConstraints(); + buttonLayoutConstraints.insets = new Insets(10,10,10,10); + } + + /** + * Creates the buy, sell, and end transaction buttons for the transaction window + * + */ + public void setupButtons() { + // Creating Buy button + ScrollButton buyButton = new ScrollButton("Buy Item"); + + buttonLayoutConstraints.gridx = 0; + buttonLayoutConstraints.gridy = 1; + + buyButton.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - System.out.println("BUY"); //temporary test code createBuyWindow(); } }); - btnBuy.setBounds(58, 155, 169, 105); - transactionPanel.add(btnBuy); - JButton btnSell = new JButton("SELL"); //creates a "Sell" button - btnSell.addMouseListener(new MouseAdapter() { + buttonPanel.add(buyButton, buttonLayoutConstraints); + + // Creating Sell button + ScrollButton sellButton = new ScrollButton("Sell Item"); + + buttonLayoutConstraints.gridx = 2; + buttonLayoutConstraints.gridy = 1; + + sellButton.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { - System.out.println("SELL"); //temporary test code createSellWindow(); } }); - btnSell.setBounds(351, 155, 169, 105); - transactionPanel.add(btnSell); - JButton btnCancel = new JButton("End Transaction"); //creates a button to end the transaction - btnCancel.addMouseListener(new MouseAdapter() { + buttonPanel.add(sellButton, buttonLayoutConstraints); + + + // Creating End Transaction button + JButton cancelButton = new JButton("End Transaction"); + buttonLayoutConstraints.gridx = 1; + buttonLayoutConstraints.gridy = 2; + + cancelButton.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { - System.out.println("Cancel"); //temporary test code - - MASTER.endTransaction(); - exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open + exitWindow(); } }); - btnCancel.setBounds(210, 310, 160, 50); - transactionPanel.add(btnCancel); - - JLabel lblWouldYouLike = new JLabel("Would you like to:"); - lblWouldYouLike.setFont(new Font("Tahoma", Font.PLAIN, 15)); - lblWouldYouLike.setBounds(233, 76, 193, 32); - transactionPanel.add(lblWouldYouLike); - - JLabel lblOr = new JLabel("OR"); - lblOr.setFont(new Font("Tahoma", Font.PLAIN, 15)); - lblOr.setBounds(277, 189, 35, 32); - transactionPanel.add(lblOr); + + buttonPanel.add(cancelButton, buttonLayoutConstraints); + + // Creating labels + JLabel label1 = new JLabel("Would you like to:"); + label1.setFont(new Font("Elephant", Font.BOLD, 20)); + buttonLayoutConstraints.gridx = 0; + buttonLayoutConstraints.gridy = 0; + buttonLayoutConstraints.gridwidth = 3; + buttonPanel.add(label1, buttonLayoutConstraints); + buttonLayoutConstraints.gridwidth = 1; + } + + /** + * Creates the labels for the main transaction window + */ + public void setupLabels() { + + JLabel label2 = new JLabel("OR"); + label2.setFont(new Font("Elephant", Font.BOLD, 15)); + buttonLayoutConstraints.gridx = 1; + buttonLayoutConstraints.gridy = 1; + buttonPanel.add(label2, buttonLayoutConstraints); + } + + public void addComponentsToFrame() { + + buttonPanel.setOpaque(false); + transactionPanel.add(buttonPanel, BorderLayout.WEST); add(transactionPanel); setLocationRelativeTo(null); } /** - * Will exit the transaction window - * incomplete method + * Ends transaction and disposes of JFrame + * */ public void exitWindow() { + transaction.endTransaction(); this.dispose(); } /** - * Will create a window for the BUY option - * INCOMPLETE METHOD + * Creates a buy window to replace the current transaction window + * */ public void createBuyWindow() { - JPanel contentPane = new JPanel(); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - contentPane.setLayout(null); + // Creates panel for the components of the buy window + JPanel buyPanel = new JPanel(); + buyPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + buyPanel.setLayout(null); // We should get out of the habit of using null layouts. We may change this later. + + // Removes the current components of the JFrame and adds new panel getContentPane().removeAll(); - add(contentPane); - setVisible(false); - setVisible(true); - setVisible(true); + add(buyPanel); + + // Refreshes the new panel + buyPanel.revalidate(); + buyPanel.repaint(); - String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory - for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list + int itemListLength = transaction.getTargetMerchant().getInventory().length; + String[] itemList = new String[itemListLength]; //create a new array of strings with the length of the player's inventory + for(int i = 0; i < itemListLength; i++) //adds all the item names to the item list { - itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); - itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; + itemList[i] = transaction.getTargetMerchant().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " ($" + transaction.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; } - JLabel lblYouHave = new JLabel("You have: $" + MASTER.getPlayer().getPlayerCash()); + JLabel lblYouHave = new JLabel("You have: $" + transaction.getPlayer().getPlayerCash()); lblYouHave.setBounds(61, 27, 86, 14); - contentPane.add(lblYouHave); + buyPanel.add(lblYouHave); @SuppressWarnings({ "rawtypes", "unchecked" }) final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from comboBox.setBounds(61, 141, 250, 20); - contentPane.add(comboBox); + buyPanel.add(comboBox); final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into textField.setBounds(61, 238, 86, 20); - contentPane.add(textField); + buyPanel.add(textField); textField.setColumns(10); JLabel lblSelectTheItem = new JLabel("Select the Item you want:"); //label containing instructions lblSelectTheItem.setBounds(61, 105, 168, 14); - contentPane.add(lblSelectTheItem); + buyPanel.add(lblSelectTheItem); JLabel lblSelectTheAmount = new JLabel("Enter the amount you wish to buy:"); //label containing instructions lblSelectTheAmount.setBounds(61, 197, 275, 30); - contentPane.add(lblSelectTheAmount); + buyPanel.add(lblSelectTheAmount); JButton btnCancel = new JButton("Cancel"); //cancel button, re-enables the player's ability to call new transaction windows btnCancel.addMouseListener(new MouseAdapter() { @@ -153,7 +211,7 @@ public void mouseReleased(MouseEvent arg0) { } }); btnCancel.setBounds(61, 306, 89, 23); - contentPane.add(btnCancel); + buyPanel.add(btnCancel); JButton btnAccept = new JButton("Accept"); //create an accept button to consume the information the player entered into the menu and text field btnAccept.addMouseListener(new MouseAdapter() { @@ -166,7 +224,7 @@ public void mouseReleased(MouseEvent e) { try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot int chosenAmount = Integer.parseInt(textField.getText()); - if(MASTER.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item + if(transaction.actionBuy(chosenItem, chosenAmount)) //will attempt to buy the specified amount of the chosen item { System.out.println(chosenAmount); getContentPane().removeAll(); @@ -185,7 +243,7 @@ public void mouseReleased(MouseEvent e) { } }); btnAccept.setBounds(247, 306, 89, 23); - contentPane.add(btnAccept); + buyPanel.add(btnAccept); } /** @@ -203,15 +261,17 @@ public void createSellWindow() setVisible(false); setVisible(true); - String[] itemList = new String[MASTER.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory + String[] itemList = new String[transaction.getTargetMerchant().getInventory().length]; //create a new array of strings with the length of the player's inventory + int playerInventoryIndex = 0; for(int i = 0; i < itemList.length; i++) //adds all the item names to the item list - { - itemList[i] = MASTER.getTargetMerchant().getInventory()[i].getItemName(); - itemList[i] = itemList[i] + " (x" + MASTER.getPlayer().getInventory()[i].getQuantity() + ")"; //appends the quantity of items that the player has - itemList[i] = itemList[i] + " ($" + MASTER.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; //appends the price of the item + { + playerInventoryIndex = transaction.getTargetMerchant().getInventoryStartIndex() + i; + itemList[i] = transaction.getTargetMerchant().getInventory()[i].getItemName(); + itemList[i] = itemList[i] + " (x" + transaction.getPlayer().getInventory()[playerInventoryIndex].getQuantity() + ")"; //appends the quantity of items that the player has + itemList[i] = itemList[i] + " ($" + transaction.getTargetMerchant().getInventory()[i].getAdjustedPrice() + ")"; //appends the price of the item } - JLabel lblYouHave = new JLabel(MASTER.getTargetMerchant().getName() + " has: $" + MASTER.getTargetMerchant().getCurrentCash()); + JLabel lblYouHave = new JLabel(transaction.getTargetMerchant().getName() + " has: $" + transaction.getTargetMerchant().getCurrentCash()); lblYouHave.setBounds(61, 27, 299, 14); contentPane.add(lblYouHave); @@ -242,6 +302,8 @@ public void mouseReleased(MouseEvent arg0) { add(transactionPanel); setVisible(false); setVisible(true); + revalidate(); + repaint(); } } }); @@ -262,7 +324,7 @@ public void mouseReleased(MouseEvent e) { //information is consumed try{ try{ //will attempt to parse the number player entered, will stay on the same screen if it cannot int chosenAmount = Integer.parseInt(textField.getText()); - isGood = MASTER.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item + isGood = transaction.actionSell(chosenItem, chosenAmount); //checks if the merchant will buy that item if(isGood) //will attempt to sell the specified amount of the chosen item { System.out.println(chosenAmount); @@ -270,6 +332,8 @@ public void mouseReleased(MouseEvent e) { //information is consumed add(transactionPanel); setVisible(false); setVisible(true); + revalidate(); + repaint(); } else throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false @@ -297,8 +361,21 @@ public void mouseReleased(MouseEvent e) { //information is consumed // frame.setLocationRelativeTo(null); } + public BufferedImage loadImage(String fileName) { + + BufferedImage image = null; + + try { + image = ImageIO.read(new File("src/images/" + fileName)); + } catch (IOException e) { + e.printStackTrace(); + } + + return image; + } + + public static void createWarning(String message) { - _isWarning = true; final JFrame contentFrame = new JFrame(); JPanel warningPane = new JPanel(); contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -312,7 +389,6 @@ public static void createWarning(String message) { btnOk.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - _isWarning = false; contentFrame.dispose(); } });