Skip to content

Commit

Permalink
Updated all Classes because merge failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Apr 10, 2015
1 parent 87f4d44 commit fce685a
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 194 deletions.
18 changes: 9 additions & 9 deletions MerchantRPGCSE2102/src/model/Character.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model;

public class Character {
private int _x, _y;
private int col, row;
protected Item[] _inventory;
protected String _name;

Expand Down Expand Up @@ -43,20 +43,20 @@ public String getName()
return _name;
}

public int getX() {
return _x;
public int getCol() {
return col;
}

public void setX(int x) {
_x = x;
public void setCol(int x) {
col = x;
}

public int getY() {
return _y;
public int getRow() {
return row;
}

public void setY(int y) {
_y = y;
public void setRow(int y) {
row = y;
}


Expand Down
108 changes: 68 additions & 40 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ public Map(int rows, int cols) {
}

/**
* This method stores the sepcified instance of Player at the specified position on the map
* This method stores the specified instance of Player at the specified position on the map
* @param player The player to be initialized
* @param x The x coordinate of the player's location
* @param y The y coordinate of the player's location
* @param row The row of the player's location
* @param column The column of the player's location
*/
public void initializePlayer(Player player, int x, int y) {
public void initializePlayer(Player player, int row, int col) {
_player = player;
int vertexNum = y*_cols + x;
player.setX(x);
player.setY(y);
int vertexNum = row*_cols + col;
player.setCol(col);
player.setRow(row);
_mapGraph.getVertex(vertexNum).setOccupant(player);
}

/**
* This method stores the sepcified instance of Merchant at the specified position on the map
* @param player The merchant to be initialized
* @param x The x coordinate of the merchant's location
* @param y The y coordinate of the merchant's location
* @param row The row of the merchant's location
* @param col The column of the merchant's location
*/
public void initializeMerchant(Merchant merchant, int x, int y) {
int vertexNum = y*_cols + x;
merchant.setX(x);
merchant.setY(y);
public void initializeMerchant(Merchant merchant, int row, int col) {
int vertexNum = row*_cols + col;
merchant.setCol(col);
merchant.setRow(row);
_mapGraph.getVertex(vertexNum).setOccupant(merchant);
}

Expand All @@ -47,49 +47,49 @@ public void initializeMerchant(Merchant merchant, int x, int y) {
* @param String The direction to be moved (north, west, east, or south)
*/
public void movePlayer(String direction) {
int currentX = _player.getX();
int currentY = _player.getY();
int currentCol = _player.getCol();
int currentRow = _player.getRow();

if (direction.equals("north")) {
if (currentY != 0) {
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 (currentRow != 0) {
if (!isOccupied(currentCol, currentRow - 1)) {
_mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null);
_mapGraph.getVertex((currentRow - 1)*_cols + currentCol).setOccupant(_player);
_player.setRow(currentRow - 1);
}
}
}

if (direction.equals("east")) {
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 (currentCol != _cols - 1) {
if (!isOccupied(currentCol + 1, currentRow)) {
_mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null);
_mapGraph.getVertex(currentRow*_cols + (currentCol + 1)).setOccupant(_player);
_player.setCol(currentCol + 1);
}
}
}

if (direction.equals("south")) {
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 (currentRow != _rows - 1) {
if (!isOccupied(currentCol, currentRow + 1)) {
_mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null);
_mapGraph.getVertex((currentRow + 1)*_cols + currentCol).setOccupant(_player);
_player.setRow(currentRow + 1);
}
}
}

if (direction.equals("west")) {
if (currentX != 0) {
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 (currentCol != 0) {
if (!isOccupied(currentCol - 1, currentRow)) {
_mapGraph.getVertex(currentRow*_cols + currentCol).setOccupant(null);
_mapGraph.getVertex(currentRow*_cols + (currentCol - 1)).setOccupant(_player);
_player.setCol(currentCol - 1);
}
}
}
System.out.println("Player is at vertex (" + _player.getX() + ", " + _player.getY() + ")");

}

/**
Expand All @@ -102,12 +102,40 @@ public Graph getGraph() {

/**
* Checks if a location on the map is occupied by a merchant or player
* @param x x coordinate to check
* @param y y coordinate to check
* @param x column to check
* @param y row to check
* @return Boolean value
*/
public boolean isOccupied(int x, int y) {
return _mapGraph.getVertex(y*_cols + x).getOccupant() != null;
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;
}

public boolean collisionTo(String direction) {

if (direction.equals("north"))
return isOccupied(_player.getCol(), _player.getRow() - 1);
if (direction.equals("south"))
return isOccupied(_player.getCol(), _player.getRow() + 1);
if (direction.equals("east"))
return isOccupied(_player.getCol() + 1, _player.getRow());
if (direction.equals("west"))
return isOccupied(_player.getCol() - 1, _player.getRow());
if (direction.equals("northwest"))
return isOccupied(_player.getCol() - 1, _player.getRow() - 1);
if (direction.equals("southwest"))
return isOccupied(_player.getCol() - 1, _player.getRow() + 1);
if (direction.equals("northeast"))
return isOccupied(_player.getCol() + 1, _player.getRow() - 1);
if (direction.equals("southeast"))
return isOccupied(_player.getCol() + 1, _player.getRow() + 1);
return false;
}

public Player getPlayer() {
return _player;
}

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

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class MerchantSprite {
private int x, y;
private static final int WIDTH = 30;

public MerchantSprite(int row, int col) {
this.x = col*WIDTH;
this.y = row*WIDTH;
}

/**
* Paints the sprite at its current location
* @param g Graphics2D for painting
*/
public void paint(Graphics2D g) {
g.fillRect(x, y, WIDTH, WIDTH);
}

/**
* Gets the bounds of the sprite in the form of a Rectangle
* @return the Rectangle formed by the sprite
*/
public Rectangle getBounds() {
return new Rectangle(x, y, WIDTH, WIDTH);
}
}
Loading

0 comments on commit fce685a

Please sign in to comment.