Skip to content

Commit

Permalink
Added dialog for starting a new day. Player must be near the house to
Browse files Browse the repository at this point in the history
start a new day. I have implemented the Object class as Lutong suggested
for randomly generated maps. Clusters of trees are randomly generated on
the map. Collision detection has also been fixed as documented.
Collisions now make sure that the object nearby is actually the object
the player is colliding with.
  • Loading branch information
john committed May 1, 2015
1 parent 1f1e62c commit ca0c4fa
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 86 deletions.
144 changes: 98 additions & 46 deletions MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
@@ -1,108 +1,101 @@
package controller;

import java.awt.image.BufferedImage;
import java.io.File;

import model.Object;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import model.Map;
import model.Merchant;
import model.Player;
import view.MapUI;
import view.StartScreen;
import model.Character;

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 ArrayList<String> miscList = new ArrayList<String>();


// Character instances
private Player _player;
private Merchant _merchant1;
private Merchant _merchant2;
private Merchant _merchant3;
private Merchant _misc1;

// 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 boolean _initialGameVariable;


public RPGame(int transactionLimit, int tileSize) {

// Initializing member variables
_currentDay = 1;
_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);

// Initialize the start screen
_initialGameVariable = false;

try {
createStartScreen(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while(_initialGameVariable == false)
{
System.out.println("In Loop");
}

// Initialiing Map instance and adding the player and merchants to the map

// Initialiing Map instance and adding the player, objects, and merchants to the map
map = new Map(rows, cols);
Object object = new Object(rows/2, cols/2 - 1, "house");
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);
map.initializeMerchant(_misc1, rows/3, cols/3);
map.initializeObject(object, rows/2, cols/2 - 1);


// 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.addCollison(rows/2,1);
mapui.addMerchantSprite(rows-2, cols/2);
mapui.addCollison(rows-2, cols/2);
mapui.addMerchantSprite(rows/2, cols-2);
mapui.addCollison(rows/2, cols-2);
//just the object collison
mapui.addCollison(rows/3, cols/3);

mapui.addObject(object);
buildRandomEnvironment(250);

// Creating JFrame window
JFrame frame = new JFrame("Merchant RPG");

JFrame frame = new JFrame("The Merchant RPG");
try {
frame.setIconImage(loadIcon());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Adding MapUI instance to window
frame.add(mapui);

Expand All @@ -114,6 +107,17 @@ public RPGame(int transactionLimit, int tileSize) {
frame.setLocationRelativeTo(null);

}

public BufferedImage loadIcon() throws IOException {
BufferedImage img = null;
try {
img = ImageIO.read(new File("src/images/icon.png"));
return img;
} catch (IOException e) {
System.out.println("File not found");
return null;
}
}

/**
* This method scans the inventory.txt file located in src\config. It will read lines of the format <stringname> <price> and store them in the inventory list member variables
Expand Down Expand Up @@ -161,7 +165,6 @@ public void buildMerchants()
_merchant1 = new Merchant("Cheap Merchant", 300, merchantInventoryList1, 0);
_merchant2 = new Merchant("Medium Merchant", 600, merchantInventoryList2, merchantInventoryList1.size());
_merchant3 = new Merchant("Expensive Merchant", 1000, merchantInventoryList3, merchantInventoryList1.size()+ merchantInventoryList2.size());
_misc1 = new Merchant(null, 0, miscList, 0);
}

/**
Expand All @@ -174,6 +177,48 @@ public void buildPlayer(String name, int startingCash)
_player = new Player(name, startingCash, playerInventoryList);
}

public void buildRandomEnvironment(int size) {
int clusterCount = 0;
boolean newCluster = true;

for (int i = 0; i < size; i++) {
if (clusterCount == 6) {
clusterCount = 0;
newCluster = true;
}
Random randomGenerator = new Random();
int row = randomGenerator.nextInt(map.getRows()-1);
int col = randomGenerator.nextInt(map.getCols()-1);

while (map.isOccupied(col, row) || closeToCharacter(row, col) || (!newCluster && !(map.isOccupiedByObject(col + 1, row) || map.isOccupiedByObject(col - 1, row) || map.isOccupiedByObject(col, row + 1) || map.isOccupiedByObject(col, row - 1) || map.isOccupiedByObject(col - 1, row - 1) || map.isOccupiedByObject(col + 1, row + 1) || map.isOccupiedByObject(col - 1, row + 1) || map.isOccupiedByObject(col + 1, row - 1)))) {
row = randomGenerator.nextInt(map.getRows()-1);
col = randomGenerator.nextInt(map.getCols()-1);
}
Object object = new Object(row, col, "tree");
map.initializeObject(object, row, col);
mapui.addObject(object);
clusterCount++;
newCluster = false;
}
}

public boolean closeToCharacter(int row, int col) {
Object o = new Object(row, col, "");
if (distanceBetween(o, _merchant1) < 90)
return true;
if (distanceBetween(o, _merchant2) < 90)
return true;
if (distanceBetween(o, _merchant3) < 90)
return true;
if (distanceBetween(o, _player) < 90)
return true;
return false;
}

public int distanceBetween(Character a, Character b) {
return (int) Math.sqrt(Math.pow((a.getCol()-b.getCol())*30, 2) + Math.pow((a.getRow()-b.getRow())*30, 2));
}

/**
* This method returns the specified merchant inventory list
*
Expand Down Expand Up @@ -224,11 +269,6 @@ public void createTransaction(Player player, Merchant targetMerchant)
}
}

public void createStartScreen(RPGame game) throws IOException
{
StartScreen newStartScreen = new StartScreen(game);
}

/**
* Will refresh number of transactions the Player has available
*
Expand Down Expand Up @@ -295,7 +335,7 @@ public void addAndRefreshCash (Merchant targetMerchant, int increaseAmount)
targetMerchant.addCash(increaseAmount);
targetMerchant.refreshCash();
}

/**
* Updates the MapUI to move characters and detect events
*
Expand Down Expand Up @@ -369,6 +409,18 @@ public boolean getMovement()
*/
public static void main(String[] args) throws InterruptedException, IOException
{

// Initialize the start screen
StartScreen startScreen = new StartScreen();
boolean ready = false;

while(ready == false)
{
System.out.println("");
ready = startScreen.getStatus();
}


int fps = 300; // Frames per second
RPGame rpgGame = new RPGame(3, 30);

Expand Down
Binary file modified 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.
Binary file modified 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/house.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/tree.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/water.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ca0c4fa

Please sign in to comment.