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/controller/RPGame.java b/MerchantRPGCSE2102/src/controller/RPGame.java index 029a345..f9d9229 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 = true; private int _currentDay; private int _transactionLimit; @@ -140,6 +140,7 @@ public void createTransaction(Player player, Merchant targetMerchant) } } + /** * Will refresh number of transactions the Player has available * @@ -275,51 +276,46 @@ 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 - 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); - _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) - } - + // 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/images/testsprite.png b/MerchantRPGCSE2102/src/images/testsprite.png new file mode 100644 index 0000000..99d0222 Binary files /dev/null and b/MerchantRPGCSE2102/src/images/testsprite.png differ diff --git a/MerchantRPGCSE2102/src/sprites/Animation.java b/MerchantRPGCSE2102/src/sprites/Animation.java new file mode 100644 index 0000000..bc64c9d --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Animation.java @@ -0,0 +1,104 @@ +package sprites; + +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; + + +public class Animation { + + private int frameCount; // Counts ticks for change + private int frameDelay; // frame delay 1-12 (You will have to play around with this) + private int currentFrame; // animations current frame + private int animationDirection; // animation direction (i.e counting forward or backward) + private int totalFrames; // total amount of frames for your animation + + private boolean stopped; // has animations stopped + + private List 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..f74ac07 --- /dev/null +++ b/MerchantRPGCSE2102/src/sprites/Frame.java @@ -0,0 +1,31 @@ +package sprites; + +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 0eaf106..e922ba4 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 { @@ -23,6 +25,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; @@ -39,19 +58,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; } } @@ -89,6 +116,15 @@ 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((mapui.getMap().collisionTo("east"))||(mapui.getMap().collisionTo("west"))||(mapui.getMap().collisionTo("south"))||(mapui.getMap().collisionTo("north"))||(mapui.getMap().collisionTo("northeast"))||(mapui.getMap().collisionTo("northwest"))||(mapui.getMap().collisionTo("southeast"))||(mapui.getMap().collisionTo("southwest"))){ + System.out.println("Transaction starting"); // For testing purposes + mapui.game.createTransaction(null, null);//RPGame initialize Trade + } + else{ + return; + } + } // 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) @@ -177,6 +213,9 @@ public void move() { public void resetLocation() { x = initialX; y = initialY; + changeInX = 15; + changeInY = 15; + } /** @@ -184,8 +223,8 @@ public void resetLocation() { * @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); } /** 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 diff --git a/MerchantRPGCSE2102/src/view/MapUI.java b/MerchantRPGCSE2102/src/view/MapUI.java index e2f597f..1535611 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,16 +21,17 @@ @SuppressWarnings("serial") public class MapUI extends JPanel { - private RPGame game; - private Map map; + public RPGame game; + public Map map; private PlayerSprite player; - private ArrayList merchants = new ArrayList(); - private boolean transactionAvailable = false; + private ArrayList merchants = new ArrayList(); - public MapUI(Map map, RPGame game) { + + public MapUI(Map map,RPGame Game) { this.map = map; - this.game = game; + this.game= Game; + addKeyListener(new KeyListener() { @Override @@ -74,17 +77,13 @@ 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); 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); - } + } /** @@ -96,7 +95,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 @@ -105,11 +104,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 (game._movement==false){ + + } + else{ + + player.move(); // Every 30 pixels, the player will move to a new Vertex @@ -133,12 +143,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; } } diff --git a/MerchantRPGCSE2102/src/view/StartScreen.java b/MerchantRPGCSE2102/src/view/StartScreen.java index 4d900dd..47e6bc9 100644 --- a/MerchantRPGCSE2102/src/view/StartScreen.java +++ b/MerchantRPGCSE2102/src/view/StartScreen.java @@ -1,5 +1,6 @@ package view; +import java.awt.*; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Image; @@ -13,48 +14,83 @@ import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; +import javax.swing.*; public class StartScreen extends JFrame { + private static final long serialVersionUID = 1L; + JPanel startMenu = new JPanel(); + + public static void main(String[] args) throws IOException { new StartScreen(); } public StartScreen() throws IOException { + + + + + + JLabel MainMenuBG = new JLabel(new ImageIcon("src/images/background.jpg")); + MainMenuBG.setVisible(true); + + + + + + + Image icon, background; icon = loadIcon(); background = loadBackground(); BackgroundPanel panel = new BackgroundPanel(background, BackgroundPanel.ACTUAL, 1.0f, 0.5f); this.setIconImage(icon); + this.setTitle("The Merchant RPG"); this.setSize(1500, 844); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); + + JButton start = new JButton("Start"); + start.setVisible(true); + + start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println("Game started"); } }); + JButton instructions = new JButton("Instructions"); + instructions.setVisible(true); + + + instructions.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println("Instructions opened"); } + }); + this.getContentPane().setLayout(null); start.setBounds(650, 382, 200, 80); instructions.setBounds(650, 482, 200, 40); panel.setBounds(0, 0, 1500, 844); start.setBackground(null); instructions.setBackground(null); - this.add(panel); + + this.getContentPane().add(instructions); this.getContentPane().add(start); + + this.add(panel); } public BufferedImage loadIcon() throws IOException {