From 317bda86118f5777355372926a73f75de00f691d Mon Sep 17 00:00:00 2001 From: John Bojorquez Date: Sat, 18 Apr 2015 13:08:49 -0400 Subject: [PATCH] Refactored RPGame and TransactionUI classes and added graphics for the map as well as transactions. The player and merchant instances are now accessable during a tansaction and the transaction is able to be fully completed. Tested with next day cycle and it works as expected. Planning to further polish the game this upcoming weak and finish the Start menu. --- MerchantRPGCSE2102/src/controller/RPGame.java | 147 +++++----- .../src/controller/Transaction.java | 6 +- MerchantRPGCSE2102/src/graph/Vertex.java | 8 +- MerchantRPGCSE2102/src/images/testsprite.png | Bin 8443 -> 4184 bytes MerchantRPGCSE2102/src/model/Character.java | 8 +- MerchantRPGCSE2102/src/model/Map.java | 16 +- MerchantRPGCSE2102/src/model/Merchant.java | 12 +- MerchantRPGCSE2102/src/model/Player.java | 7 + .../src/sprites/MerchantSprite.java | 20 +- .../src/sprites/PlayerSprite.java | 54 ++-- .../{Sprite.java => SpriteLoader.java} | 2 +- MerchantRPGCSE2102/src/tests/TestPlayer.java | 2 +- MerchantRPGCSE2102/src/tests/TestRPGame.java | 4 +- .../src/tests/TestTransaction.java | 4 +- MerchantRPGCSE2102/src/view/MapUI.java | 48 ++-- .../src/view/TransactionUI.java | 250 ++++++++++++------ 16 files changed, 382 insertions(+), 206 deletions(-) rename MerchantRPGCSE2102/src/sprites/{Sprite.java => SpriteLoader.java} (92%) 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/testsprite.png b/MerchantRPGCSE2102/src/images/testsprite.png index 99d022279cbc8c4d3240f8a877a9bcbea3b4b05c..0d38f6169ffaa71fda72202dc31ef291a5dd7f21 100644 GIT binary patch literal 4184 zcmeH~`8(8o7stQfukSEsQezpRDTD0F(u6y^LdiN*L>Vc|ZQplhtO>WNJ6nWqxrwr* zEXlqMN`x$>Y}rBvg<%@c^k+QR^PKBCpL2cQpX+>nc%5^8I7t>}Mm#7H6aWA`7tWs} zvLlJ@WF!asY=?4hvjgZ)G|~sk-{F?n7uZG5R1W~E($Gvt1ORXV7ADpPY+XP=Ktxng zUS3Qck5^ZfI;CMAsC~lG`c#Ocs4>yq&B;12EG;J9|51u{d%pT`3URq#eWWAo_3PKm z)$#k|&-NGNXJ%&D|NP(nPvF0fz|TUOK6_4p0HT@I{{OZCR@+>X>;iK8oVWD{fP?M7 z9i(}e++>SF0R}b!R^HA5!H#~n03Sy;uYi+Yw*v5llS(H~--=hc1pw%(3+ME#UC*XI z0G<9hgNX~Z#^{+Tgt7J=zU?_zQ(*2d_7yM3SKdlJK9hWTE!t?v9(vGa!~&nRDC_yx zfBwF`Hy70W*(>w2a5$q3fP?+eFwaUfB8mJ^cn<-CH{c7>L)?QARs5HncO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KauBR$a8bXlq*@Pt&KSi34;}sR0wE@j5I-SB zM$n)lv!%?xI`_PjFpqRk4{==Zbjil{t>9^PW=+26Wd#6OnyI&ESvU3t?&3IR?;NC( z{jk3Ni46+ly;V-{w6LDo|tx9JbtAphOuB-RqpA6 zWjikfR&wcV1Q9g~hK>x11iyoBl%%S1Cx*;yY--?GPWagCoCc#7|Ms3^i%r(yEz&6p zaWSkj1_WzWMF#EBx^6PU(vuz<_!mluc}j6q^kjm(9#!S)^!xiut6_)^K zw~A>2RkO@bz|6XyxWX<=#g@c;a>9#Vu9^?O-wJrCfqxWK4Z&QYp~R#Nz0^qFOmXZ2 zw-e_xMzz$;sCdSzOn%aVR# zZlFTccXt@;7y&Qh`^oGjY934@oOqOGOR5XhSvvB;m1WRm&8!{ z7Jv-AO1Q!8dqDa^Yo!7r5Ld->bQrlij-oiw8~S$R_hPWE6463yW?RH$5#vrV0>>J5^9LmbRNh39XtlM!**Y68?V|pk%sS`tI;g zZ}bqoqH0#))l@7~Ex{?A xGGOh(qblC}*LVa@#hR%Q0Fv-0pBw(%V= _rows || col >= _cols) return false; - else + else { + setCurrentNearbyMerchant((Merchant) _mapGraph.getVertex(row*_cols + col).getOccupant()); return _mapGraph.getVertex(row*_cols + col).getOccupant() != null; + } } public boolean collisionTo(String direction) { @@ -137,7 +140,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 +152,12 @@ public Player getPlayer() { return _player; } + public Merchant getCurrentNearbyMerchant() { + return currentNearbyMerchant; + } + + public void setCurrentNearbyMerchant(Merchant currentNearbyMerchant) { + this.currentNearbyMerchant = currentNearbyMerchant; + } + } 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..0a0007e 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 @@ -27,28 +25,29 @@ public class PlayerSprite { 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; } /** @@ -97,29 +96,33 @@ public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftBeingPressed = false; if (rightBeingPressed == false) // Sprite will stop only when the RIGHT key is not being pressed + 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 + 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 + 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 + 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 + mapui.game.createTransaction(mapui.map.getPlayer(), mapui.map.getCurrentNearbyMerchant());//RPGame initialize Trade } else{ return; @@ -127,14 +130,22 @@ public void keyReleased(KeyEvent e) { } // 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 +212,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); @@ -225,6 +236,7 @@ public void resetLocation() { public void paint(Graphics2D g) { g.drawImage(animation.getSprite(), x, y, null); + animation.update(); } /** @@ -232,7 +244,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); } /** 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..ea577e5 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -7,12 +7,15 @@ 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; @@ -23,15 +26,16 @@ 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 @@ -63,13 +67,7 @@ public void paint(Graphics g) { 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(loadWorld("RPG_samplemap.png"), 0, 0, null); // Paints the player onto the canvas player.paint(g2d); @@ -77,12 +75,11 @@ public void paint(Graphics g) { 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); + // For testing purposes. Shows the Player's current Vertex position + g2d.setColor(Color.BLACK); 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); + //g2d.dispose(); } @@ -102,7 +99,7 @@ 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(){ @@ -127,25 +124,38 @@ public void move() { // 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) { + if (player.getChangeInX() == tileSize + 1) { map.movePlayer("east"); player.setChangeInX(1); } if (player.getChangeInX() == 0) { map.movePlayer("west"); - player.setChangeInX(30); + player.setChangeInX(tileSize); } if (player.getChangeInY() == 0) { map.movePlayer("north"); - player.setChangeInY(30); + player.setChangeInY(tileSize); } - if (player.getChangeInY() == 31) { + if (player.getChangeInY() == tileSize + 1) { map.movePlayer("south"); player.setChangeInY(1); } } } + public static BufferedImage loadWorld(String fileName) { + + BufferedImage worldImage = null; + + try { + worldImage = ImageIO.read(new File("src/images/" + fileName)); + } catch (IOException e) { + e.printStackTrace(); + } + + return worldImage; + } + /** * Calls the method for advancing the day in the instance of RPGame * @return 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(); } });