Skip to content

Merge from Master #23

Merged
merged 5 commits into from
Apr 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.swing.JFrame;

import model.Map;
import model.Merchant;
import model.Player;
import view.MapUI;
Expand Down Expand Up @@ -274,8 +275,51 @@ public static void main(String[] args) throws InterruptedException
ArrayList<String> playerInventory = _rpg.getMerchantInventoryList(1);
playerInventory.addAll(_rpg.getMerchantInventoryList(2));
playerInventory.addAll(_rpg.getMerchantInventoryList(3));
_rpg.buildPlayer("test", 500, playerInventory);
_rpg.getPlayer().getItem("WoodPlanks").increaseQuantity(15);
_rpg.createTransaction(_rpg.getPlayer(), _rpg.getMerchant(3));
_rpg.buildPlayer("test", 500, playerInventory);
_rpg.getPlayer().getItem("armor").increaseQuantity(15);


// 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)
}

}
}
10 changes: 9 additions & 1 deletion MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -163,6 +173,11 @@ public void move() {
}

}

public void resetLocation() {
x = initialX;
y = initialY;
}

/**
* Paints the sprite at its current location
Expand Down
36 changes: 30 additions & 6 deletions MerchantRPGCSE2102/src/tests/MockGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,51 @@
import model.Player;
import view.MapUI;
import controller.RPGame;
import model.Merchant;
import model.Player;

public class MockGame {

public static void main(String[] args) throws InterruptedException {
// This sets up the window for the game which is a 300 by 300 pixels JFrame
Player player = new Player("TestPlayer", 0, null);
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);
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, 0, 0);
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");
MapUI mapui = new MapUI(map);

// 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(10);
Thread.sleep(100/12); // Controls the speed of the game (currently 120 frames/second)
}
}

Expand Down
27 changes: 26 additions & 1 deletion MerchantRPGCSE2102/src/view/MapUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MerchantSprite> merchants = new ArrayList<MerchantSprite>();
private boolean transactionAvailable = false;


public MapUI(Map map) {
public MapUI(Map map, RPGame game) {
this.map = map;
this.game = game;
addKeyListener(new KeyListener() {

@Override
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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() {
Expand Down