diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 9711281..0f484ac 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -7,6 +7,7 @@ import javax.swing.JFrame; +import model.Map; import model.Merchant; import model.Player; import view.MapUI; @@ -266,6 +267,46 @@ public static void main(String[] args) throws InterruptedException playerInventory.addAll(_rpg.getMerchantInventoryList(3)); _rpg.buildPlayer("test", 500, playerInventory); _rpg.getPlayer().getItem("armor").increaseQuantity(15); - _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + + + // 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) + } } } diff --git a/MerchantRPGCSE2102/src/model/Map.java b/MerchantRPGCSE2102/src/model/Map.java index bff4432..f410d11 100644 --- a/MerchantRPGCSE2102/src/model/Map.java +++ b/MerchantRPGCSE2102/src/model/Map.java @@ -6,6 +6,7 @@ public class Map { private Graph _mapGraph; // Graph representation of the map private int _rows, _cols; // The n*n dimension of the map + private int[] initialPlayerLocation = new int[2]; private Player _player; public Map(int rows, int cols) { @@ -22,7 +23,9 @@ public Map(int rows, int cols) { * @param column The column of the player's location */ public void initializePlayer(Player player, int row, int col) { - _player = player; + _player = player; + initialPlayerLocation[0] = row; + initialPlayerLocation[1] = col; int vertexNum = row*_cols + col; player.setCol(col); player.setRow(row); @@ -134,6 +137,11 @@ public boolean collisionTo(String direction) { return false; } + public void resetPlayerLocation() { + _player.setCol(initialPlayerLocation[1]); + _player.setRow(initialPlayerLocation[0]); + } + public Player getPlayer() { return _player; } diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 220e65a..0eaf106 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -15,6 +15,8 @@ public class PlayerSprite { private int dx, dy = 0; // Velocity of the sprite private int changeInX = 15; // Counts the number of pixels moved between vertices (in reference to the center of the sprite) private int changeInY = 15; + private int initialX; + private int initialY; // Keep track of which keys are currently being pressed private boolean leftBeingPressed = false; @@ -24,7 +26,9 @@ public class PlayerSprite { public PlayerSprite(MapUI mapui, int row, int col) { this.x = col*WIDTH; + initialX = x; this.y = row*WIDTH; + initialY = y; this.mapui = mapui; } @@ -57,6 +61,12 @@ 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 @@ -163,6 +173,11 @@ public void move() { } } + + public void resetLocation() { + x = initialX; + y = initialY; + } /** * Paints the sprite at its current location diff --git a/MerchantRPGCSE2102/src/tests/MockGame.java b/MerchantRPGCSE2102/src/tests/MockGame.java index f4b2692..237ec45 100644 --- a/MerchantRPGCSE2102/src/tests/MockGame.java +++ b/MerchantRPGCSE2102/src/tests/MockGame.java @@ -12,6 +12,7 @@ public class MockGame { public static void main(String[] args) throws InterruptedException { + RPGame rpg = new RPGame(); // SETTING UP PLAYER AND MERCHANT Player player = new Player("Player", 0, null); Merchant merch1 = new Merchant("Bill", 0, null); @@ -29,7 +30,7 @@ public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Merchant RPG"); // CREATING MAPUI AND SPRITES - MapUI mapui = new MapUI(map); + MapUI mapui = new MapUI(map, rpg); mapui.createPlayerSprite(15, 20); mapui.addMerchantSprite(15, 0); mapui.addMerchantSprite(29, 20); diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index 044fec6..e2f597f 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -14,17 +14,21 @@ import model.Map; import sprites.MerchantSprite; import sprites.PlayerSprite; +import controller.RPGame; @SuppressWarnings("serial") public class MapUI extends JPanel { + private RPGame game; private Map map; private PlayerSprite player; private ArrayList merchants = new ArrayList(); + private boolean transactionAvailable = false; - public MapUI(Map map) { + public MapUI(Map map, RPGame game) { this.map = map; + this.game = game; addKeyListener(new KeyListener() { @Override @@ -76,6 +80,11 @@ public void paint(Graphics g) { 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 (transactionAvailable) { + g2d.setFont(new Font("Verdana", Font.BOLD, 18)); + g2d.drawString("Would you like to trade with merchant?", 800, 60); + } } /** @@ -124,6 +133,22 @@ public void move() { map.movePlayer("south"); player.setChangeInY(1); } + + // Checking if a Merchant is nearby + if (map.collisionTo("north")||map.collisionTo("south")||map.collisionTo("east")||map.collisionTo("west")||map.collisionTo("southeast")||map.collisionTo("southwest")||map.collisionTo("northeast")||map.collisionTo("northwest")) { + transactionAvailable = true; + } else { + transactionAvailable = false; + } + } + + /** + * Calls the method for advancing the day in the instance of RPGame + * @return + */ + public void nextDay() { + game.advanceDailyCycle(); + map.resetPlayerLocation(); } public Map getMap() {