Skip to content

Gavin-L #29

Merged
merged 7 commits into from
Apr 23, 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
147 changes: 87 additions & 60 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> merchantInventoryList1 = new ArrayList<String>(); // merchant 1's inventory list
private ArrayList<String> merchantInventoryList2 = new ArrayList<String>(); // merchant 2's inventory list
private ArrayList<String> merchantInventoryList3 = new ArrayList<String>(); // merchant 3's inventory list
private ArrayList<String> playerInventoryList = new ArrayList<String>(); // 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);

}

/**
Expand Down Expand Up @@ -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<String> startingInventory)
public void buildPlayer(String name, int startingCash)
{
_player = new Player(name, startingCash, startingInventory);
_player = new Player(name, startingCash, playerInventoryList);
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -195,7 +252,7 @@ private void scaleAllMerchantPrices(Merchant[] merchants)
m.scaleAllAdjustedPrices();
}
}

/**
* Will call addCash on the merchant provided then refreshes their cash
*
Expand All @@ -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
Expand Down Expand Up @@ -245,7 +311,7 @@ else if(merchantNum == 2)
else
return _merchant3;
}

/**
* Getter for the current day number
* @return The day number
Expand All @@ -254,7 +320,7 @@ public int getDay()
{
return _currentDay;
}

/**
*
* @return Movement variable
Expand All @@ -271,51 +337,12 @@ public boolean getMovement()
*/
public static void main(String[] args) throws InterruptedException
{
RPGame _rpg = new RPGame();
_rpg.buildMerchants();
ArrayList<String> 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)
}
}
}
6 changes: 3 additions & 3 deletions MerchantRPGCSE2102/src/controller/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions MerchantRPGCSE2102/src/graph/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Edge> _incidentEdges = new LinkedList<Edge>();


Expand Down Expand Up @@ -37,11 +39,11 @@ public LinkedList<Edge> getIncidentList() {
return _incidentEdges;
}

public Object getOccupant() {
public Character getOccupant() {
return _occupant;
}

public void setOccupant(Object occupant) {
public void setOccupant(Character occupant) {
_occupant = occupant;
}

Expand Down
Binary file added MerchantRPGCSE2102/src/images/Merchant-real.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MerchantRPGCSE2102/src/images/RPG_samplemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MerchantRPGCSE2102/src/images/dialogbubble.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MerchantRPGCSE2102/src/images/merchant.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MerchantRPGCSE2102/src/images/scrollbutton.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified MerchantRPGCSE2102/src/images/testsprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions MerchantRPGCSE2102/src/model/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Character {


/**
* Searches through the player's inventory for the item corresponding to the specified item name
* Searches through the character's inventory for the item corresponding to the specified item name
*
* @param itemName string containing the name of the item
* @return the item matching the specified item name
Expand All @@ -25,7 +25,7 @@ public Item getItem(String itemName)
}

/**
* Returns the merchant's item inventory
* Returns the item inventory
* @return item inventory array
*/
public Item[] getInventory()
Expand All @@ -34,9 +34,9 @@ public Item[] getInventory()
}

/**
* Returns a string containing the name of the merchant
* Returns a string containing the name of the character
*
* @return merchant name string
* @return character name string
*/
public String getName()
{
Expand Down
33 changes: 31 additions & 2 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Map {
private int _rows, _cols; // The n*n dimension of the map
private int[] initialPlayerLocation = new int[2];
private Player _player;
private Merchant currentNearbyMerchant;

public Map(int rows, int cols) {
_rows = rows;
Expand Down Expand Up @@ -112,8 +113,17 @@ public Graph getGraph() {
public boolean isOccupied(int col, int row) {
if (row < 0 || col < 0 || row >= _rows || col >= _cols)
return false;
else
return _mapGraph.getVertex(row*_cols + col).getOccupant() != null;
else {
if (_mapGraph.getVertex(row*_cols + col).getOccupant() != null) {
System.out.println(_mapGraph.getVertex(row*_cols + col).getOccupant().getClass().getName());
if (_mapGraph.getVertex(row*_cols + col).getOccupant().getClass().getName().equals("model.Merchant"))
setCurrentNearbyMerchant((Merchant) _mapGraph.getVertex(row*_cols + col).getOccupant());
return true;
}
}

setCurrentNearbyMerchant(null);
return false;
}

public boolean collisionTo(String direction) {
Expand All @@ -137,7 +147,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]);
}
Expand All @@ -146,4 +159,20 @@ public Player getPlayer() {
return _player;
}

public Merchant getCurrentNearbyMerchant() {
return currentNearbyMerchant;
}

public void setCurrentNearbyMerchant(Merchant currentNearbyMerchant) {
this.currentNearbyMerchant = currentNearbyMerchant;
}

public int getRows() {
return _rows;
}

public int getCols() {
return _cols;
}

}
12 changes: 11 additions & 1 deletion MerchantRPGCSE2102/src/model/Merchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> inventory)
public Merchant(String name, int cashLimit, ArrayList<String> inventory, int inventoryStartIndex)
{
_name = name;
_currentCash = cashLimit;
_baseCash = cashLimit;
this.setInventoryStartIndex(inventoryStartIndex);
if (inventory != null)
generateMerchantInventory(inventory);
_dailyRandomPercent = 0; //placeholder percentage
Expand Down Expand Up @@ -162,4 +164,12 @@ public int getRandomPercent()
{
return _dailyRandomPercent;
}

public int getInventoryStartIndex() {
return inventoryStartIndex;
}

public void setInventoryStartIndex(int inventoryStartIndex) {
this.inventoryStartIndex = inventoryStartIndex;
}
}
Loading