Skip to content

Commit

Permalink
Added collision detection for the PlayerSprite class as well as a cla…
Browse files Browse the repository at this point in the history
…ss for MerchantSprites. Also refactored with a hope to differentiate the use of (row, col) and (x, y). Rows and columns will be used to represent the Player and Merchants' locations on the Graph. x and y variables are used to prepresent the PlayerSprite and MerchantSprite's location on the canvas in pixels. The current scale is: x = 30*column and y = 30*row where 30 is the width/height of each sprite.
  • Loading branch information
john committed Mar 9, 2015
1 parent 40c14c3 commit 8018789
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 99 deletions.
80 changes: 44 additions & 36 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ public Map(int rows, int cols) {
/**
* This method stores the sepcified 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.setCol(x);
player.setRow(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.setCol(x);
merchant.setRow(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,45 +47,45 @@ 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.getCol();
int currentY = _player.getRow();
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.setRow(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.setCol(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.setRow(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.setCol(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);
}
}
}
Expand Down Expand Up @@ -115,13 +115,21 @@ public boolean isOccupied(int col, int row) {
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("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;
}

Expand Down
205 changes: 163 additions & 42 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,84 @@

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
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 = 15;
public int changeInY = 15;
Color color = new Color(0, 0, 0);
boolean leftBeingPressed = false;
boolean rightBeingPressed = false;
boolean downBeingPressed = false;
boolean upBeingPressed = false;

public PlayerSprite(MapUI mapui) {
MapUI mapui; // Instance of MapUI which created this PlayerSprite
private static final int WIDTH = 30; // Width of the PlayerSprite
private Color color = new Color(0, 0, 0); // Color of the PlayerSprite (this will no longer be needed once we have a sprite sheet)

private int x, y; // x and y coordinates (in pixels)
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;

// Keep track of which keys are currently being pressed
private boolean leftBeingPressed = false;
private boolean rightBeingPressed = false;
private boolean downBeingPressed = false;
private boolean upBeingPressed = false;

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


/**
* Changes the sprite's velocity based on which key is being pressed
* @param e The KeyEvent detected by the system
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
dx = -1;
if (mapui.getMap().collisionTo("west") && changeInX == 15)
dx = 0;
leftBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
dx = 1;
if (mapui.getMap().collisionTo("east") && changeInX == 15)
dx = 0;
rightBeingPressed = true;
}

if (e.getKeyCode() == KeyEvent.VK_UP) {
dy = -1;
if (mapui.getMap().collisionTo("north") && changeInY == 15)
dx = 0;
upBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
dy = 1;
if (mapui.getMap().collisionTo("south") && changeInY == 15)
dx = 0;
downBeingPressed = true;
}
}


/**
* Changes the sprite's velocity based on which key is released
* @param e The KeyEvent detected by the system
*/
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftBeingPressed = false;
if (rightBeingPressed == false)
if (rightBeingPressed == false) // Sprite will stop only when the RIGHT key is not being pressed
dx = 0;
}

if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightBeingPressed = false;
if (leftBeingPressed == false)
if (leftBeingPressed == false) // Sprite will stop only when the LEFT key is not being pressed
dx = 0;
}

if (e.getKeyCode() == KeyEvent.VK_UP) {
upBeingPressed = false;
if (downBeingPressed == false)
if (downBeingPressed == false) // Sprite will stop only when the DOWN key is not being pressed
dy = 0;
}

if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downBeingPressed = false;
if (upBeingPressed == false)
if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed
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;
Expand All @@ -85,22 +89,139 @@ public void keyReleased(KeyEvent e) {
dy = 1;
if(upBeingPressed)
dy = -1;

}

/**
* Moves the sprite using the current velocity and detects for collisions
*/
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;
if (collision()) {

// If there is a collision, we check from which direction the collision occurs. During each collision the sprite is stopped and their position is reset so they are no longer collided.
if (mapui.getMap().collisionTo("east")) {
dx = 0;
setChangeInX(getChangeInX() - 1);
setX(getX() - 1);
}
if (mapui.getMap().collisionTo("west")) {
dx = 0;
setChangeInX(getChangeInX() + 1);
setX(getX() + 1);
}
if (mapui.getMap().collisionTo("north")) {
dy = 0;
setChangeInY(getChangeInY() + 1);
setY(getY() + 1);
}
if (mapui.getMap().collisionTo("south")) {
dy = 0;
setChangeInY(getChangeInY() - 1);
setY(getY() - 1);
}
if (mapui.getMap().collisionTo("northwest")) {
dy = 0;
dx = 0;
setChangeInY(getChangeInY() + 1);
setChangeInX(getChangeInX() + 1);
setY(getY() + 1);
setX(getX() + 1);
}
if (mapui.getMap().collisionTo("southwest")) {
dy = 0;
dx = 0;
setChangeInY(getChangeInY() - 1);
setChangeInX(getChangeInX() + 1);
setY(getY() - 1);
setX(getX() + 1);
}
if (mapui.getMap().collisionTo("northeast")) {
dy = 0;
dx = 0;
setChangeInY(getChangeInY() + 1);
setChangeInX(getChangeInX() - 1);
setY(getY() + 1);
setX(getX() - 1);
}
if (mapui.getMap().collisionTo("southeast")) {
dy = 0;
dx = 0;
setChangeInY(getChangeInY() - 1);
setChangeInX(getChangeInX() - 1);
setY(getY() - 1);
setX(getX() - 1);
}

}

// Checks that the sprite is moving within the bounds of the panel
if (getX() + dx >= 0 && getX() + dx <= mapui.getWidth() - WIDTH && getY() + dy >= 0 && getY() + dy <= mapui.getHeight() - WIDTH) {
setX(getX() + dx);
setChangeInX(getChangeInX() + dx);
setY(getY() + dy);
setChangeInY(getChangeInY() + dy);
}

}


/**
* Paints the sprite at its current location
* @param g Graphics2D for painting
*/
public void paint(Graphics2D g) {
g.setColor(color);
g.fillOval(x, y, WIDTH, WIDTH);
g.fillOval(getX(), getY(), 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(getX(), getY(), WIDTH, WIDTH);
}

/**
* Detects a collision between the PlayerSprite and any of the Merchantsprites
* @return
*/
public boolean collision() {
for (MerchantSprite merchant : mapui.getMerchants())
if (merchant.getBounds().intersects(getBounds()))
return true;
return false;
}

public int getChangeInX() {
return changeInX;
}

public void setChangeInX(int changeInX) {
this.changeInX = changeInX;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getChangeInY() {
return changeInY;
}

public void setChangeInY(int changeInY) {
this.changeInY = changeInY;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

}
Loading

0 comments on commit 8018789

Please sign in to comment.