Skip to content

Commit

Permalink
Comitting changes so I can move workspace over to my laptop. Currently
Browse files Browse the repository at this point in the history
working on collision detection.
  • Loading branch information
john committed Mar 8, 2015
1 parent ba32595 commit 40c14c3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 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
49 changes: 34 additions & 15 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public Map(int rows, int cols) {
public void initializePlayer(Player player, int x, int y) {
_player = player;
int vertexNum = y*_cols + x;
player.setX(x);
player.setY(y);
player.setCol(x);
player.setRow(y);
_mapGraph.getVertex(vertexNum).setOccupant(player);
}

Expand All @@ -37,8 +37,8 @@ public void initializePlayer(Player player, int x, int y) {
*/
public void initializeMerchant(Merchant merchant, int x, int y) {
int vertexNum = y*_cols + x;
merchant.setX(x);
merchant.setY(y);
merchant.setCol(x);
merchant.setRow(y);
_mapGraph.getVertex(vertexNum).setOccupant(merchant);
}

Expand All @@ -47,15 +47,15 @@ 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 currentX = _player.getCol();
int currentY = _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);
_player.setRow(currentY - 1);
}
}
}
Expand All @@ -65,7 +65,7 @@ public void movePlayer(String direction) {
if (!isOccupied(currentX + 1, currentY)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_cols + (currentX + 1)).setOccupant(_player);
_player.setX(currentX + 1);
_player.setCol(currentX + 1);
}
}
}
Expand All @@ -75,7 +75,7 @@ public void movePlayer(String direction) {
if (!isOccupied(currentX, currentY + 1)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex((currentY + 1)*_cols + currentX).setOccupant(_player);
_player.setY(currentY + 1);
_player.setRow(currentY + 1);
}
}
}
Expand All @@ -85,11 +85,10 @@ public void movePlayer(String direction) {
if (!isOccupied(currentX - 1, currentY)) {
_mapGraph.getVertex(currentY*_cols + currentX).setOccupant(null);
_mapGraph.getVertex(currentY*_cols + (currentX - 1)).setOccupant(_player);
_player.setX(currentX - 1);
_player.setCol(currentX - 1);
}
}
}
System.out.println("Player is at vertex (" + _player.getX() + ", " + _player.getY() + ")");
}

/**
Expand All @@ -102,12 +101,32 @@ 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());
return false;
}

public Player getPlayer() {
return _player;
}

}
12 changes: 10 additions & 2 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class PlayerSprite {
public int x, y = 0;
public int row, col;
int dx, dy = 0;
public int changeInX = 0;
public int changeInY = 0;
public int changeInX = 15;
public int changeInY = 15;
Color color = new Color(0, 0, 0);
boolean leftBeingPressed = false;
boolean rightBeingPressed = false;
Expand All @@ -27,18 +27,26 @@ public PlayerSprite(MapUI mapui) {
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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions MerchantRPGCSE2102/src/tests/MockGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.swing.JFrame;

import model.Map;
import model.Merchant;
import model.Player;
import view.MapUI;
import controller.RPGame;
Expand All @@ -12,8 +13,10 @@ 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);
Merchant merch = new Merchant("merch", 0, null);
Map map = new Map(30, 40);
map.initializePlayer(player, 0, 0);
map.initializeMerchant(merch, 10, 10);
JFrame frame = new JFrame("Merchant RPG");
MapUI mapui = new MapUI(map);
frame.add(mapui);
Expand Down
6 changes: 3 additions & 3 deletions MerchantRPGCSE2102/src/tests/TestMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void testMovePlayer() {
map.movePlayer("south");
map.movePlayer("south");
map.movePlayer("south");
assertEquals(player.getX(), 0);
assertEquals(player.getY(), 1);
assertEquals(map.isOccupied(player.getX(), player.getY()), true);
assertEquals(player.getCol(), 0);
assertEquals(player.getRow(), 1);
assertEquals(map.isOccupied(player.getCol(), player.getRow()), true);
}

}
27 changes: 19 additions & 8 deletions MerchantRPGCSE2102/src/view/MapUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,44 @@ public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < map.getGraph().getCols(); i++) {
for (int j = 0; j < map.getGraph().getRows(); j++) {
g2d.fillRect(i*30, j*30, 1, 30);
g2d.fillRect(i*30, j*30, 30, 1);
}
}
player.paint(g2d);
g2d.setColor(Color.GRAY);
g2d.setFont(new Font("Verdana", Font.BOLD, 20));
g2d.drawString("x: " + player.x + " y : " + player.y, 10, 20);
g2d.drawString("delta X: " + player.changeInX +" delta Y: " + player.changeInY, 10, 50);
g2d.drawString("Vertex: (" + map.getPlayer().getCol() + ", " + map.getPlayer().getRow() + ")", 1000, 20);
}

public void move() {
player.move();

if (player.changeInX == 30) {
if (player.changeInX == 31) {
map.movePlayer("east");
player.changeInX = 0;
player.changeInX = 1;
}
if (player.changeInX == -30) {
if (player.changeInX == 0) {
map.movePlayer("west");
player.changeInX = 0;
player.changeInX = 30;
}
if (player.changeInY == -30) {
if (player.changeInY == 0) {
map.movePlayer("north");
player.changeInY = 0;
player.changeInY = 30;
}
if (player.changeInY == 30) {
if (player.changeInY == 31) {
map.movePlayer("south");
player.changeInY = 0;
player.changeInY = 1;
}
}

public Map getMap() {
return map;
}


}

0 comments on commit 40c14c3

Please sign in to comment.