Skip to content

Commit

Permalink
Merge pull request #16 from gal11002/GavinClone
Browse files Browse the repository at this point in the history
Gavin clone
  • Loading branch information
gal11002 committed Mar 10, 2015
2 parents 39e3042 + d511be8 commit a9aedf2
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 49 deletions.
8 changes: 7 additions & 1 deletion MerchantRPGCSE2102/src/controller/RPGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JFrame;

import model.Merchant;
import model.Player;
import view.MapUI;

public class RPGame {
public static final int WIDTH = 1206;
public static final int HEIGHT = 929;
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
Expand Down Expand Up @@ -250,8 +255,9 @@ public int getDay()
/**
* Main method used to test the GUI components since test classes do not maintain the GUI
* @param args no need for input
* @throws InterruptedException
*/
public static void main(String[] args)
public static void main(String[] args) throws InterruptedException
{
RPGame _rpg = new RPGame();
_rpg.buildMerchants();
Expand Down
40 changes: 24 additions & 16 deletions MerchantRPGCSE2102/src/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
public class Graph
{
private Vertex[] _vertexList;
private int _size;
private int _rows;
private int _cols;

public Graph(int size)
public Graph(int rows, int cols)
{
_size = size;
_vertexList = new Vertex[size*size];
_rows = rows;
_cols = cols;
_vertexList = new Vertex[rows*cols];
}

public void initializeGraph() {
Expand All @@ -17,24 +19,25 @@ public void initializeGraph() {
}

public void initializeVertices() {
for (int row = 0; row < _size; row++) {
for (int col = 0; col < _size; col++) {
int cellNum = row*_size + col;
Vertex v = new Vertex(cellNum, _size);
for (int row = 0; row < _rows; row++) {
for (int col = 0; col < _cols; col++) {
int cellNum = row*_cols + col;
Vertex v = new Vertex(cellNum, row, col);
setVertex(cellNum, v);
}
}
}

public void initializeEdges() {
for (int row = 0; row < _size; row++) {
for (int col = 0; col < _size; col++) {
for (int row = 0; row < _rows; row++) {
for (int col = 0; col < _cols; col++) {

if (row !=0)
connectWithEdge(getVertex(_size*row + col), getVertex(_size*(row - 1) + col));
if (row !=0) {
connectWithEdge(getVertex(_cols*row + col), getVertex(_cols*(row - 1) + col));
}

if (col != _size - 1)
connectWithEdge(getVertex(_size*row + col), getVertex(_size*row + col + 1));
if (col != _cols - 1)
connectWithEdge(getVertex(_cols*row + col), getVertex(_cols*row + col + 1));
}
}
}
Expand All @@ -47,9 +50,14 @@ public void connectWithEdge(Vertex v, Vertex w) {
edge.setW(w);
}

public int getSize()
public int getRows()
{
return _size;
return _rows;
}

public int getCols()
{
return _cols;
}

public Vertex getVertex(int vertexNum)
Expand Down
11 changes: 3 additions & 8 deletions MerchantRPGCSE2102/src/graph/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ public class Vertex
private LinkedList<Edge> _incidentEdges = new LinkedList<Edge>();


public Vertex(int cellNum, int n) {
public Vertex(int cellNum, int row, int col) {
_row = row;
_col = col;
_cellNum = cellNum;
_row = 0;
int temp = _cellNum;
while (temp - n >= 0) { // Calculates the row of the vertex
temp = temp - n;
_row++;
}
_col = _cellNum - _row*n; // Calculates the column of the vertex
}

public void addEdge(Edge edge) {
Expand Down
Binary file added MerchantRPGCSE2102/src/images/background.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 added MerchantRPGCSE2102/src/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 25 additions & 23 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

public class Map {

private Graph _mapGraph; // Graph representation of the map
private int _mapSize; // The n*n dimension of the map
private Graph _mapGraph; // Graph representation of the map
private int _rows, _cols; // The n*n dimension of the map
private Player _player;

public Map(int size) {
_mapSize = size;
_mapGraph = new Graph(_mapSize);
public Map(int rows, int cols) {
_rows = rows;
_cols = cols;
_mapGraph = new Graph(_rows, _cols);
_mapGraph.initializeGraph();
}

Expand All @@ -21,8 +22,8 @@ public Map(int size) {
* @param y The y coordinate of the player's location
*/
public void initializePlayer(Player player, int x, int y) {
_player = player;
int vertexNum = y*_mapSize + x;
_player = player;
int vertexNum = y*_cols + x;
player.setX(x);
player.setY(y);
_mapGraph.getVertex(vertexNum).setOccupant(player);
Expand All @@ -35,7 +36,7 @@ public void initializePlayer(Player player, int x, int y) {
* @param y The y coordinate of the merchant's location
*/
public void initializeMerchant(Merchant merchant, int x, int y) {
int vertexNum = y*_mapSize + x;
int vertexNum = y*_cols + x;
merchant.setX(x);
merchant.setY(y);
_mapGraph.getVertex(vertexNum).setOccupant(merchant);
Expand All @@ -51,43 +52,44 @@ public void movePlayer(String direction) {

if (direction.equals("north")) {
if (currentY != 0) {
if (!isOccupied(currentX, currentY - 1)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex((currentY - 1)*_mapSize + currentX).setOccupant(_player);
if (!isOccupied(currentX, currentY - 1)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex((currentY - 1)*_cols + currentX).setOccupant(_player);
_player.setY(currentY - 1);
}
}
}

if (direction.equals("east")) {
if (currentX != _mapSize - 1) {
if (!isOccupied(currentX + 1, currentY)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_mapSize + (currentX + 1)).setOccupant(_player);
if (currentX != _cols - 1) {
if (!isOccupied(currentX + 1, currentY)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_cols + (currentX + 1)).setOccupant(_player);
_player.setX(currentX + 1);
}
}
}

if (direction.equals("south")) {
if (currentY != _mapSize - 1) {
if (!isOccupied(currentX, currentY + 1)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex((currentY + 1)*_mapSize + currentX).setOccupant(_player);
if (currentY != _rows - 1) {
if (!isOccupied(currentX, currentY + 1)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex((currentY + 1)*_cols + currentX).setOccupant(_player);
_player.setY(currentY + 1);
}
}
}

if (direction.equals("west")) {
if (currentX != 0) {
if (!isOccupied(currentX - 1, currentY)) {
_mapGraph.getVertex(currentY*_mapSize + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_mapSize + (currentX - 1)).setOccupant(_player);
if (!isOccupied(currentX - 1, currentY)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_cols + (currentX - 1)).setOccupant(_player);
_player.setX(currentX - 1);
}
}
}
System.out.println("Player is at vertex (" + _player.getX() + ", " + _player.getY() + ")");
}

/**
Expand All @@ -105,7 +107,7 @@ public Graph getGraph() {
* @return Boolean value
*/
public boolean isOccupied(int x, int y) {
return _mapGraph.getVertex(y*_mapSize + x).getOccupant() != null;
return _mapGraph.getVertex(y*_cols + x).getOccupant() != null;
}

}
98 changes: 98 additions & 0 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package sprites;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import view.MapUI;

public class PlayerSprite {
private static final int WIDTH = 30;
MapUI mapui;
public int x, y = 0;
public int row, col;
int dx, dy = 0;
public int changeInX = 0;
public int changeInY = 0;
Color color = new Color(0, 0, 0);
boolean leftBeingPressed = false;
boolean rightBeingPressed = false;
boolean downBeingPressed = false;
boolean upBeingPressed = false;

public PlayerSprite(MapUI mapui) {
this.mapui = mapui;
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
dx = -1;
leftBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
dx = 1;
rightBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
dy = -1;
upBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
dy = 1;
downBeingPressed = true;
}
}

public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftBeingPressed = false;
if (rightBeingPressed == false)
dx = 0;
}

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightBeingPressed = false;
if (leftBeingPressed == false)
dx = 0;
}

if (e.getKeyCode() == KeyEvent.VK_UP) {
upBeingPressed = false;
if (downBeingPressed == false)
dy = 0;
}

if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downBeingPressed = false;
if (upBeingPressed == false)
dy = 0;
}

// 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)
dx = -1;
if(rightBeingPressed)
dx = 1;
if(downBeingPressed)
dy = 1;
if(upBeingPressed)
dy = -1;

}

public void move() {
if (x + dx >= 0 && x + dx <= mapui.getWidth() - WIDTH && y + dy >= 0 && y + dy <= mapui.getHeight() - WIDTH) {
x = x + dx;
changeInX = changeInX + dx;
y = y + dy;
changeInY = changeInY + dy;
}

}

public void paint(Graphics2D g) {
g.setColor(color);
g.fillOval(x, y, WIDTH, WIDTH);
}

}
33 changes: 33 additions & 0 deletions MerchantRPGCSE2102/src/tests/MockGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tests;

import javax.swing.JFrame;

import model.Map;
import model.Player;
import view.MapUI;
import controller.RPGame;

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);
Map map = new Map(30, 40);
map.initializePlayer(player, 0, 0);
JFrame frame = new JFrame("Merchant RPG");
MapUI mapui = new MapUI(map);
frame.add(mapui);
frame.setSize(RPGame.WIDTH, RPGame.HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

while (true) {
mapui.move();
mapui.repaint();
Thread.sleep(10);
}
}

}
2 changes: 1 addition & 1 deletion MerchantRPGCSE2102/src/tests/TestMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void test() {
}

public void setup() {
map = new Map(4);
map = new Map(4, 4);
}

public void testMapInit() {
Expand Down
Loading

0 comments on commit a9aedf2

Please sign in to comment.