From 394b8475bbbb85716835ef4075f96e3f627d4945 Mon Sep 17 00:00:00 2001 From: lul11003 Date: Sat, 4 Apr 2015 12:55:00 -0400 Subject: [PATCH 01/13] wip animation --- MerchantRPGCSE2102/.classpath | 3 +- .../.settings/org.eclipse.jdt.core.prefs | 6 +- MerchantRPGCSE2102/src/images/testsprite.png | Bin 0 -> 8443 bytes MerchantRPGCSE2102/src/sprites/Animation.java | 102 ++++++++++++++++++ MerchantRPGCSE2102/src/sprites/Frame.java | 29 +++++ .../src/sprites/PlayerSprite.java | 28 +++++ MerchantRPGCSE2102/src/sprites/Sprite.java | 36 +++++++ 7 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 MerchantRPGCSE2102/src/images/testsprite.png create mode 100644 MerchantRPGCSE2102/src/sprites/Animation.java create mode 100644 MerchantRPGCSE2102/src/sprites/Frame.java create mode 100644 MerchantRPGCSE2102/src/sprites/Sprite.java diff --git a/MerchantRPGCSE2102/.classpath b/MerchantRPGCSE2102/.classpath index e1badea..964c4ef 100644 --- a/MerchantRPGCSE2102/.classpath +++ b/MerchantRPGCSE2102/.classpath @@ -1,8 +1,9 @@ - + + diff --git a/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs b/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs index 838bd9d..54e493c 100644 --- a/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs +++ b/MerchantRPGCSE2102/.settings/org.eclipse.jdt.core.prefs @@ -1,11 +1,11 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/MerchantRPGCSE2102/src/images/testsprite.png b/MerchantRPGCSE2102/src/images/testsprite.png new file mode 100644 index 0000000000000000000000000000000000000000..99d022279cbc8c4d3240f8a877a9bcbea3b4b05c GIT binary patch literal 8443 zcmWlf1ymGW7=~wAxF$tD1w=|g>24`W>26p+7FfxD z{+*qfbMD@`^PTU`x$pZuJ5jIH6^Zca@BsiIQdW}F1ka}6j=;qLkA7{}N8kzDTvbsH zxc`4EXe~_vTku?z-ns(-anJudB<%??JpeER%5pN=-Z>)yL3&!cKQ1(TaLE;AH%VjT zI79hrg9|zeCZ>vXXe}9RcU%ueZfO>X%ig!tqxxY#$dh%w4lyzdL$; zGL{|+69RVYZbwTA74QIUoy@!6A315aE~5K);4gCYOOG^^H30bcARQ6b(WpuGP6}Z#Xl&Nwx4ksSXn+*?5%KEi z5=UwX$e8H$VE~Q?5e+>L2$r$j{-$({*49hF_|Vm(RBQJr`fnS{@T+gYs!Sq zwR{mJ=N%e^*wbuOen`l#b&Ib)SBfPGAZ%qwkO}WX(;nZ6&8#fXrX`o?<*`o>tDk2m z6r^! zt@u>@$qsg;Cm>Ty(n4IoZ!TK`Eqicoxg(m#e7JKiRAZVGDnZxt1mL4#+jJ$3kN0}e zF&PpG1zs7KaJb|$GV_z~fz$6hiH#LSaHp{nte~tCWlH*6PCZ=>hO_*#BN1?A0Dw`+ zQE>26;#C5O>%z%-S==>kv$LfcZA>*8FAqsXr*1U47V7%}ZTD<~6}jM=%D)Yz4jKp! z{x7}m*}@nd2Dv38)pG?~Bvm_izGVWbddHsz$&`VlEMyEQE_ zi-3)^g(2%%1FHCD0)U`TInQW}FV<~M%dGE8p`u&vewCSfDj??pb8Ne%B>G3)US8lX z{(a(GHBq*jO>?Fnv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p*7Hh}j1qDvHeWMATfv(g#uoTvx&Rag7E~!+kRhOPqb5 zm586NB@gikvi@h=Ly4m4`b3QRN>PBDlbiXwyH zEaH=!z)zAq?tJ&!d?ziG5UPQs>Kb7o*QOJ>k;s%$U-`g%K^nH4!ViIgQI7;Z6IO>N zUXri6ttC>EF1dC{$NE@yah`lSc_IMBI3Rzp9Fm`<|FhnHOGJ7#ZXCchB*9yo#SKLm*J%HM^cZKauBR$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 frames = new ArrayList(); // Arraylist of frames + + public Animation(BufferedImage[] frames, int frameDelay) { + this.frameDelay = frameDelay; + this.stopped = true; + + for (int i = 0; i < frames.length; i++) { + addFrame(frames[i], frameDelay); + } + + this.frameCount = 0; + this.frameDelay = frameDelay; + this.currentFrame = 0; + this.animationDirection = 1; + this.totalFrames = this.frames.size(); + + } + + public void start() { + if (!stopped) { + return; + } + + if (frames.size() == 0) { + return; + } + + stopped = false; + } + + public void stop() { + if (frames.size() == 0) { + return; + } + + stopped = true; + } + + public void restart() { + if (frames.size() == 0) { + return; + } + + stopped = false; + currentFrame = 0; + } + + public void reset() { + this.stopped = true; + this.frameCount = 0; + this.currentFrame = 0; + } + + private void addFrame(BufferedImage frame, int duration) { + if (duration <= 0) { + System.err.println("Invalid duration: " + duration); + throw new RuntimeException("Invalid duration: " + duration); + } + + frames.add(new Frame(frame, duration)); + currentFrame = 0; + } + + public BufferedImage getSprite() { + return frames.get(currentFrame).getFrame(); + } + + public void update() { + if (!stopped) { + frameCount++; + + if (frameCount > frameDelay) { + frameCount = 0; + currentFrame += animationDirection; + + if (currentFrame > totalFrames - 1) { + currentFrame = 0; + } + else if (currentFrame < 0) { + currentFrame = totalFrames - 1; + } + } + } + + } + +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/sprites/Frame.java b/MerchantRPGCSE2102/src/sprites/Frame.java new file mode 100644 index 0000000..eebca6a --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Frame.java @@ -0,0 +1,29 @@ +import java.awt.image.BufferedImage; + +public class Frame { + + private BufferedImage frame; + private int duration; + + public Frame(BufferedImage frame, int duration) { + this.frame = frame; + this.duration = duration; + } + + public BufferedImage getFrame() { + return frame; + } + + public void setFrame(BufferedImage frame) { + this.frame = frame; + } + + public int getDuration() { + return duration; + } + + public void setDuration(int duration) { + this.duration = duration; + } + +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 220e65a..3c20c36 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -4,6 +4,8 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; + import view.MapUI; public class PlayerSprite { @@ -21,6 +23,23 @@ 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)}; + + // 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); + + // Actual animation + private Animation animation = standing1; public PlayerSprite(MapUI mapui, int row, int col) { this.x = col*WIDTH; @@ -35,19 +54,27 @@ public PlayerSprite(MapUI mapui, int row, int col) { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; + animation = walkLeft; + animation.start(); leftBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dx = 1; + animation = walkRight; + animation.start(); rightBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; + animation = walkUp; + animation.start(); upBeingPressed = true; } if (e.getKeyCode() == KeyEvent.VK_DOWN) { dy = 1; + animation = walkDown; + animation.start(); downBeingPressed = true; } } @@ -171,6 +198,7 @@ public void move() { public void paint(Graphics2D g) { g.setColor(color); g.fillOval(getX(), getY(), WIDTH, WIDTH); + g.drawImage(animation.getSprite(), x, y, null); } /** diff --git a/MerchantRPGCSE2102/src/sprites/Sprite.java b/MerchantRPGCSE2102/src/sprites/Sprite.java new file mode 100644 index 0000000..81711fa --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Sprite.java @@ -0,0 +1,36 @@ +package sprites; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class Sprite { + + private static BufferedImage spriteSheet; + private static final int TILE_SIZE = 32; + + public static BufferedImage loadSprite(String file) { + + BufferedImage sprite = null; + + try { + sprite = ImageIO.read(new File("src/images/testsprite.png")); + } catch (IOException e) { + e.printStackTrace(); + } + + return sprite; + } + + public static BufferedImage getSprite(int xGrid, int yGrid) { + + if (spriteSheet == null) { + spriteSheet = loadSprite("AnimationSpriteSheet"); + } + + return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE); + } + +} \ No newline at end of file From 521ca549e34fb9f06710aed800d79c5ab9a8bc8a Mon Sep 17 00:00:00 2001 From: lul11003 Date: Sat, 4 Apr 2015 13:08:04 -0400 Subject: [PATCH 02/13] 1 --- MerchantRPGCSE2102/src/sprites/PlayerSprite.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 3c20c36..134d742 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -196,8 +196,7 @@ public void move() { * @param g Graphics2D for painting */ public void paint(Graphics2D g) { - g.setColor(color); - g.fillOval(getX(), getY(), WIDTH, WIDTH); + g.drawImage(animation.getSprite(), x, y, null); } From d64b67e6da2dc7242db31669fbd6e6371cae0778 Mon Sep 17 00:00:00 2001 From: lul11003 Date: Sat, 4 Apr 2015 13:17:32 -0400 Subject: [PATCH 03/13] wip animation --- MerchantRPGCSE2102/src/sprites/Animation.java | 2 ++ MerchantRPGCSE2102/src/sprites/Frame.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/MerchantRPGCSE2102/src/sprites/Animation.java b/MerchantRPGCSE2102/src/sprites/Animation.java index c01e1f7..bc64c9d 100644 --- a/MerchantRPGCSE2102/src/sprites/Animation.java +++ b/MerchantRPGCSE2102/src/sprites/Animation.java @@ -1,3 +1,5 @@ +package sprites; + import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; diff --git a/MerchantRPGCSE2102/src/sprites/Frame.java b/MerchantRPGCSE2102/src/sprites/Frame.java index eebca6a..f74ac07 100644 --- a/MerchantRPGCSE2102/src/sprites/Frame.java +++ b/MerchantRPGCSE2102/src/sprites/Frame.java @@ -1,3 +1,5 @@ +package sprites; + import java.awt.image.BufferedImage; public class Frame { From cebba3b97255fcdc06b918d42dd6b2c3ed435f32 Mon Sep 17 00:00:00 2001 From: lul11003 Date: Sat, 11 Apr 2015 14:25:23 -0400 Subject: [PATCH 04/13] 1 --- MerchantRPGCSE2102/src/controller/RPGame.java | 42 ++++++++++++++++++- .../src/sprites/PlayerSprite.java | 8 ++++ MerchantRPGCSE2102/src/view/MapUI.java | 10 +++-- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 9711281..7894242 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; @@ -140,6 +141,7 @@ public void createTransaction(Player player, Merchant targetMerchant) } } + /** * Will refresh number of transactions the Player has available * @@ -266,6 +268,44 @@ 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/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 134d742..ec33a2a 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -106,6 +106,14 @@ public void keyReleased(KeyEvent e) { if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed dy = 0; } + if (e.getKeyCode() == KeyEvent.VK_F){ + if(collision()){ + mapui.game.createTransaction(null, null);//RPGame initialize Trade + } + else{ + return; + } + } // 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) diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index 044fec6..bb04f55 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -11,6 +11,8 @@ import javax.swing.JPanel; +import controller.RPGame; + import model.Map; import sprites.MerchantSprite; import sprites.PlayerSprite; @@ -19,12 +21,14 @@ public class MapUI extends JPanel { private Map map; + public RPGame game; private PlayerSprite player; private ArrayList merchants = new ArrayList(); - public MapUI(Map map) { + public MapUI(Map map,RPGame Game) { this.map = map; + this.game= Game; addKeyListener(new KeyListener() { @Override @@ -70,7 +74,7 @@ public void paint(Graphics g) { for (MerchantSprite merchant : merchants) merchant.paint(g2d); - // For testing purposes. Showes the Player's current x and y position as well as the current Vertex + // 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); @@ -87,7 +91,7 @@ public void addMerchantSprite(int row, int col) { MerchantSprite merchant = new MerchantSprite(row, col); merchants.add(merchant); } - + /** * Creates a player sprite to be painted on the canvas * @param row The row of the player From 7963decb11e10b4491b820c942498ca00942e7ef Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 11 Apr 2015 14:25:41 -0400 Subject: [PATCH 05/13] Editing Game Values, made new Items, changed price increase algorithm, Extended the combobox in TransactionUI --- MerchantRPGCSE2102/src/config/inventory.txt | 35 ++++++++++++++----- MerchantRPGCSE2102/src/controller/RPGame.java | 29 ++++++++++----- .../src/view/TransactionUI.java | 4 +-- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/MerchantRPGCSE2102/src/config/inventory.txt b/MerchantRPGCSE2102/src/config/inventory.txt index fcf6cb0..55691b5 100644 --- a/MerchantRPGCSE2102/src/config/inventory.txt +++ b/MerchantRPGCSE2102/src/config/inventory.txt @@ -1,11 +1,30 @@ merchant 1 -water 3 -armor 5 -food 10 +Water 2 +Candy 5 +Apple 7 +Bread 3 +Cake 14 +Jerky 9 merchant 2 -wood 3 -tarp 6 +PigIron 19 +HighQualityIron 25 +Steel 29 +WoodPlanks 10 +HighQualityWood 18 +FairyTreeWood 40 +WornSword 23 +IronSword 29 +SteelSword 38 +FairySteelSword 95 +BlessedIronDaggerofPig 50000 merchant 3 -glass 3 -tape 13 -rope 5 \ No newline at end of file +LooseGems 60 +FineCutDiamond 70 +SilverBracelets 85 +SilverNecklace 93 +SilverEmeraldRing 100 +GoldBracelets 84 +GoldNecklace 103 +GoldSapphireRing 133 +GoldDiamondCrown 210 +ExpensiveJewlery? 500 \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 9711281..c79d02c 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -74,9 +74,9 @@ else if (currentMerchant == 3) */ public void buildMerchants() { - _merchant1 = new Merchant("Merchant 1", 1000, merchantInventoryList1); - _merchant2 = new Merchant("Merchant 2", 1000, merchantInventoryList2); - _merchant3 = new Merchant("Merchant 3", 1000, merchantInventoryList3); + _merchant1 = new Merchant("Cheap Merchant", 200, merchantInventoryList1); + _merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2); + _merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3); } /** @@ -163,11 +163,13 @@ public void advanceDailyCycle(){ scaleAllMerchantPrices(allMerchants); //will scale all the prices of the items that the merchants hold based on their dailyRandomPercent - if( (_currentDay % 3) == 0) //will increase the merchant's base cash every 3 days + if( (_currentDay % 6) == 0) //will increase the merchant's base cash every 6 days { - addAndRefreshCash(_merchant1, 100); - addAndRefreshCash(_merchant2, 100); - addAndRefreshCash(_merchant3, 100); + int multiplier = (int) Math.floor(_currentDay / 10) + 1; + + addAndRefreshCash(_merchant1, multiplier * 100); + addAndRefreshCash(_merchant2, multiplier * 100); + addAndRefreshCash(_merchant3, multiplier * 100); } else //if not on a third day, will just refresh the merchant's cash amount { @@ -251,6 +253,15 @@ public int getDay() { return _currentDay; } + + /** + * + * @return Movement variable + */ + public boolean getMovement() + { + return _movement; + } /** * Main method used to test the GUI components since test classes do not maintain the GUI @@ -265,7 +276,7 @@ public static void main(String[] args) throws InterruptedException playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); _rpg.buildPlayer("test", 500, playerInventory); - _rpg.getPlayer().getItem("armor").increaseQuantity(15); - _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(1)); + _rpg.getPlayer().getItem("WoodPlanks").increaseQuantity(15); + _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(3)); } } diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 033be48..ba98daa 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -121,7 +121,7 @@ public void createBuyWindow() @SuppressWarnings({ "rawtypes", "unchecked" }) final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from - comboBox.setBounds(61, 141, 131, 20); + comboBox.setBounds(61, 141, 250, 20); contentPane.add(comboBox); final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into @@ -208,7 +208,7 @@ public void createSellWindow() contentPane.add(lblYouHave); final JComboBox comboBox = new JComboBox(itemList); //create a drop-down menu for the player to choose from - comboBox.setBounds(61, 141, 131, 20); + comboBox.setBounds(61, 141, 250, 20); contentPane.add(comboBox); final JTextField textField = new JTextField(); //create a text field for the player to enter the amount desired into From 1ffa3cdc84d0ba456ee97980e9e4a0d60f4268d9 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sat, 11 Apr 2015 14:32:27 -0400 Subject: [PATCH 06/13] Added ability to advance the day by pressing a key. We will later change this so the player has to travel to a certain loaction to do so. --- MerchantRPGCSE2102/src/controller/RPGame.java | 43 ++++++++++++++++++- MerchantRPGCSE2102/src/model/Map.java | 10 ++++- .../src/sprites/PlayerSprite.java | 15 +++++++ MerchantRPGCSE2102/src/tests/MockGame.java | 3 +- MerchantRPGCSE2102/src/view/MapUI.java | 27 +++++++++++- 5 files changed, 94 insertions(+), 4 deletions(-) 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() { From a428dba0dafaaf93a04acd48619682971eb9a0b6 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 11 Apr 2015 14:35:35 -0400 Subject: [PATCH 07/13] Fixed the warning window bug in TransactionUI --- .../src/view/TransactionUI.java | 134 ++++++++++-------- 1 file changed, 75 insertions(+), 59 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index ba98daa..0230e63 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -19,12 +19,14 @@ 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; + /** * Create the frame. */ public TransactionUI(Transaction master) { MASTER = master; + _isWarning = false; setTitle("Transaction Window"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 430); @@ -42,8 +44,8 @@ public void actionPerformed(ActionEvent arg0) { public void mouseReleased(MouseEvent arg0) { System.out.println("BUY"); //temporary test code createBuyWindow(); - } - }); + } + }); btnBuy.setBounds(58, 155, 169, 105); transactionPanel.add(btnBuy); @@ -64,8 +66,8 @@ public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) { System.out.println("Cancel"); //temporary test code - MASTER.actionCancel(); - exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open + MASTER.actionCancel(); + exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open } }); btnCancel.setBounds(210, 310, 160, 50); @@ -141,10 +143,13 @@ public void createBuyWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -153,26 +158,29 @@ public void mouseReleased(MouseEvent arg0) { 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() { @Override - public void mouseReleased(MouseEvent e) { //information is consumed when player clicks the accept button - String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu - System.out.println(chosenItem); - - 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 - { - System.out.println(chosenAmount); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + public void mouseReleased(MouseEvent e) { + if(!_isWarning) + { + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + + 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 + { + System.out.println(chosenAmount); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false + } + catch(NumberFormatException exception){ + System.out.println("Must input an amount that you can afford"); + createWarning("Must input an amount that you can afford"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionBuy returns a false - } - catch(NumberFormatException exception){ - System.out.println("Must input an amount that you can afford"); - createWarning("Must input an amount that you can afford"); } } }); @@ -228,10 +236,13 @@ public void createSellWindow() btnCancel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + if(!_isWarning) + { + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } } }); btnCancel.setBounds(61, 306, 89, 23); @@ -241,40 +252,43 @@ public void mouseReleased(MouseEvent arg0) { btnAccept.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { //information is consumed when player clicks the accept button - String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu - System.out.println(chosenItem); - boolean isGood = false; + if(!_isWarning) + { + String chosenItem = comboBox.getSelectedItem().toString().split(" ")[0]; //consumes the String name of the item from drop-down menu + System.out.println(chosenItem); + boolean isGood = false; - try{ 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 - if(isGood) //will attempt to sell the specified amount of the chosen item - { - System.out.println(chosenAmount); - getContentPane().removeAll(); - add(transactionPanel); - setVisible(false); - setVisible(true); + 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 + if(isGood) //will attempt to sell the specified amount of the chosen item + { + System.out.println(chosenAmount); + getContentPane().removeAll(); + add(transactionPanel); + setVisible(false); + setVisible(true); + } + else + throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false + } + catch(MerchantNotEnoughCashException mnecexception){ + System.out.println("Merchant does not have enough cash"); + createWarning("Merchant does not have enough cash"); } - else - throw new NumberFormatException(); //will throw a NumberFormatException if actionSell returns a false } - catch(MerchantNotEnoughCashException mnecexception){ - System.out.println("Merchant does not have enough cash"); - createWarning("Merchant does not have enough cash"); + catch(NumberFormatException exception){ + System.out.println("You do not have enough of that item"); + createWarning("You do not have enough of that item"); } } - catch(NumberFormatException exception){ - System.out.println("You do not have enough of that item"); - createWarning("You do not have enough of that item"); + catch(NotInInventoryException niiexception){ + System.out.println("Merchant does not accept that item"); + createWarning("Merchant does not accept that item"); } } - catch(NotInInventoryException niiexception){ - System.out.println("Merchant does not accept that item"); - createWarning("Merchant does not accept that item"); - } } }); btnAccept.setBounds(247, 306, 89, 23); @@ -282,8 +296,9 @@ public void mouseReleased(MouseEvent e) { //information is consumed // frame.setVisible(true); // frame.setLocationRelativeTo(null); } - + public static void createWarning(String message) { + _isWarning = true; final JFrame contentFrame = new JFrame(); JPanel warningPane = new JPanel(); contentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -297,6 +312,7 @@ public static void createWarning(String message) { btnOk.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent arg0) { + _isWarning = false; contentFrame.dispose(); } }); From e6b18ae30d0cd303cabbf3c85ac944826014f548 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 11 Apr 2015 14:58:32 -0400 Subject: [PATCH 08/13] Updated ToggleMovement methods --- MerchantRPGCSE2102/src/controller/RPGame.java | 4 ++-- .../src/controller/Transaction.java | 22 +++++++++---------- .../src/view/TransactionUI.java | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index c79d02c..ffc4c07 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -130,8 +130,8 @@ public void createTransaction(Player player, Merchant targetMerchant) if(_transactionLimit > 0) { toggleMovement("OFF"); - Transaction newTransaction = new Transaction(player, targetMerchant); - toggleMovement("ON"); + System.out.println("Transaction Begining"); + Transaction newTransaction = new Transaction(player, targetMerchant, this); _transactionLimit -= 1; } else diff --git a/MerchantRPGCSE2102/src/controller/Transaction.java b/MerchantRPGCSE2102/src/controller/Transaction.java index 4e1d13f..e7b4fb9 100644 --- a/MerchantRPGCSE2102/src/controller/Transaction.java +++ b/MerchantRPGCSE2102/src/controller/Transaction.java @@ -12,11 +12,13 @@ public class Transaction private Player _player; private Merchant _targetMerchant; private TransactionUI _window; + private RPGame _game; - public Transaction(Player player, Merchant targetMerchant) + public Transaction(Player player, Merchant targetMerchant, RPGame game) { _player = player; _targetMerchant = targetMerchant; + _game = game; _window = new TransactionUI(this); _window.setVisible(true); } @@ -61,16 +63,6 @@ public boolean actionSell(String itemName, int amount) throws NotInInventoryExce return false; } - /** - * This method will push a true up to the class calling it - * incomplete method - * @return returns true - */ - public boolean actionCancel() - { - return true; - } - /** * Searches the player inventory for the item matching the item name * @param itemName name of the item @@ -91,6 +83,14 @@ public Item searchMerchantInventory(String itemName) return _targetMerchant.getItem(itemName); } + /** + * Toggles the RPGame movement back on + */ + public void endTransaction() + { + _game.toggleMovement("ON"); + } + /** * Returns the player in transaction * @return returns the player diff --git a/MerchantRPGCSE2102/src/view/TransactionUI.java b/MerchantRPGCSE2102/src/view/TransactionUI.java index 0230e63..bfc722c 100644 --- a/MerchantRPGCSE2102/src/view/TransactionUI.java +++ b/MerchantRPGCSE2102/src/view/TransactionUI.java @@ -66,7 +66,7 @@ public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) { System.out.println("Cancel"); //temporary test code - MASTER.actionCancel(); + MASTER.endTransaction(); exitWindow(); //Will end the transaction main screen but only if player does not have another transaction screen open } }); From be0b61eec639751da81847cd0ff4904435e2bcc5 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 11 Apr 2015 15:00:14 -0400 Subject: [PATCH 09/13] Minor Edits --- MerchantRPGCSE2102/src/controller/RPGame.java | 1 - 1 file changed, 1 deletion(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index ffc4c07..d7b7c1c 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -130,7 +130,6 @@ public void createTransaction(Player player, Merchant targetMerchant) if(_transactionLimit > 0) { toggleMovement("OFF"); - System.out.println("Transaction Begining"); Transaction newTransaction = new Transaction(player, targetMerchant, this); _transactionLimit -= 1; } From 46c56e0d7238ff326aacf38e46a321a50d3ad59d Mon Sep 17 00:00:00 2001 From: lul11003 Date: Sat, 11 Apr 2015 15:08:27 -0400 Subject: [PATCH 10/13] 1 --- MerchantRPGCSE2102/src/controller/RPGame.java | 2 +- MerchantRPGCSE2102/src/view/MapUI.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 7894242..e8b9867 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -21,7 +21,7 @@ public class RPGame { private ArrayList playerInventoryList = new ArrayList(); // the player's inventory list private Player _player; private Merchant _merchant1, _merchant2, _merchant3; - private boolean _movement; + public boolean _movement; private int _currentDay; private int _transactionLimit; diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index bb04f55..f422ac5 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -24,6 +24,7 @@ public class MapUI extends JPanel { public RPGame game; private PlayerSprite player; private ArrayList merchants = new ArrayList(); + private boolean _tranState=game._movement; public MapUI(Map map,RPGame Game) { @@ -100,11 +101,22 @@ public void addMerchantSprite(int row, int col) { public void createPlayerSprite(int row, int col) { player = new PlayerSprite(this, row, col); } + + public void changeState(){ + + } /** * Moves the PlayerSprite as well as the Map's instance of Player */ public void move() { + //check transaction + if (_tranState=false){ + + } + else{ + + player.move(); // Every 30 pixels, the player will move to a new Vertex @@ -128,6 +140,7 @@ public void move() { map.movePlayer("south"); player.setChangeInY(1); } + } } public Map getMap() { From 11f465d74b0baf77bb164099d84685605d3ff7d3 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sat, 11 Apr 2015 15:09:32 -0400 Subject: [PATCH 11/13] Committing updated version of my branch which now includes files from the master. --- MerchantRPGCSE2102/src/controller/RPGame.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index d0461c5..029a345 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -275,8 +275,7 @@ public static void main(String[] args) throws InterruptedException ArrayList playerInventory = _rpg.getMerchantInventoryList(1); playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); - _rpg.buildPlayer("test", 500, playerInventory); -<<<<<<< HEAD + _rpg.buildPlayer("test", 500, playerInventory); _rpg.getPlayer().getItem("armor").increaseQuantity(15); @@ -311,17 +310,16 @@ public static void main(String[] args) throws InterruptedException frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setLocationRelativeTo(null); + frame.setLocationRelativeTo(null); + _rpg.getPlayer().getItem("WoodPlanks").increaseQuantity(15); + // MAIN GAME LOOP while (true) { mapui.move(); mapui.repaint(); Thread.sleep(100/12); // Controls the speed of the game (currently 120 frames/second) - } -======= - _rpg.getPlayer().getItem("WoodPlanks").increaseQuantity(15); - _rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(3)); ->>>>>>> ab3b505559eadb3b59b74c449dcbbe17d32e2197 + } + } } From 01a8d5a8b1ddd8790edc8e7e89ff5036d5ff5ed1 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sat, 11 Apr 2015 15:19:23 -0400 Subject: [PATCH 12/13] Fixed bug with resetting player's location. --- MerchantRPGCSE2102/src/controller/RPGame.java | 5 ++--- MerchantRPGCSE2102/src/sprites/PlayerSprite.java | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/MerchantRPGCSE2102/src/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 029a345..0ca095d 100644 --- a/MerchantRPGCSE2102/src/controller/RPGame.java +++ b/MerchantRPGCSE2102/src/controller/RPGame.java @@ -275,11 +275,10 @@ public static void main(String[] args) throws InterruptedException ArrayList playerInventory = _rpg.getMerchantInventoryList(1); playerInventory.addAll(_rpg.getMerchantInventoryList(2)); playerInventory.addAll(_rpg.getMerchantInventoryList(3)); - _rpg.buildPlayer("test", 500, playerInventory); - _rpg.getPlayer().getItem("armor").increaseQuantity(15); + _rpg.buildPlayer("test", 500, playerInventory); - // SETTING UP PLAYER AND MERCHANT + // 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); diff --git a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java index 0eaf106..0bff0a8 100644 --- a/MerchantRPGCSE2102/src/sprites/PlayerSprite.java +++ b/MerchantRPGCSE2102/src/sprites/PlayerSprite.java @@ -177,6 +177,9 @@ public void move() { public void resetLocation() { x = initialX; y = initialY; + changeInX = 15; + changeInY = 15; + } /** From 92e353c88c734643a5a13e9e66492b7af395f035 Mon Sep 17 00:00:00 2001 From: John W Bojorquez Date: Sat, 11 Apr 2015 15:25:34 -0400 Subject: [PATCH 13/13] Removing transaction check for MapUI --- MerchantRPGCSE2102/src/view/MapUI.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index e2f597f..c4a9bd1 100644 --- a/MerchantRPGCSE2102/src/view/MapUI.java +++ b/MerchantRPGCSE2102/src/view/MapUI.java @@ -80,11 +80,7 @@ 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); - } + } /** @@ -133,13 +129,6 @@ 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; - } } /**