Skip to content

Commit

Permalink
Added dialog bubbles to prompt player to initiate transaction with F.
Browse files Browse the repository at this point in the history
Added method isMerchantNearby() in PlayerSprite so the code would be
easier to read.
  • Loading branch information
john committed Apr 19, 2015
1 parent 3adc0a3 commit 6f67233
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 73 deletions.
8 changes: 8 additions & 0 deletions MerchantRPGCSE2102/src/model/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@ public Merchant getCurrentNearbyMerchant() {
public void setCurrentNearbyMerchant(Merchant currentNearbyMerchant) {
this.currentNearbyMerchant = currentNearbyMerchant;
}

public int getRows() {
return _rows;
}

public int getCols() {
return _cols;
}

}
44 changes: 22 additions & 22 deletions MerchantRPGCSE2102/src/sprites/PlayerSprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PlayerSprite {
private boolean rightBeingPressed = false;
private boolean downBeingPressed = false;
private boolean upBeingPressed = false;

// Images for each animation
private BufferedImage[] walkingLeft = {SpriteLoader.getSprite(0, 1), SpriteLoader.getSprite(1, 1), SpriteLoader.getSprite(2, 1)}; // Gets the upper left images of my sprite sheet
private BufferedImage[] walkingRight = {SpriteLoader.getSprite(0, 2), SpriteLoader.getSprite(1, 2), SpriteLoader.getSprite(2, 2)};
Expand Down Expand Up @@ -58,26 +58,26 @@ public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
dx = -1;
animation = walkLeft;
animation.start();
animation.start();
leftBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
dx = 1;
animation = walkRight;
animation.start();
animation.start();
rightBeingPressed = true;
}

if (e.getKeyCode() == KeyEvent.VK_UP) {
dy = -1;
animation = walkUp;
animation.start();
animation.start();
upBeingPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
dy = 1;
animation = walkDown;
animation.start();
animation.start();
downBeingPressed = true;
}
}
Expand All @@ -87,48 +87,44 @@ public void keyPressed(KeyEvent e) {
* @param e The KeyEvent detected by the system
*/
public void keyReleased(KeyEvent e) {

if(e.getKeyChar()=='n') {
mapui.nextDay();
resetLocation();
}

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

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

if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downBeingPressed = false;
if (upBeingPressed == false) // Sprite will stop only when the UP key is not being pressed
animation = stand;
dy = 0;
dy = 0;
}
if (e.getKeyCode() == KeyEvent.VK_F){
if((mapui.getMap().collisionTo("east"))||(mapui.getMap().collisionTo("west"))||(mapui.getMap().collisionTo("south"))||(mapui.getMap().collisionTo("north"))||(mapui.getMap().collisionTo("northeast"))||(mapui.getMap().collisionTo("northwest"))||(mapui.getMap().collisionTo("southeast"))||(mapui.getMap().collisionTo("southwest"))){
if (mapui.getMap().getCurrentNearbyMerchant() != null)
mapui.game.createTransaction(mapui.map.getPlayer(), mapui.map.getCurrentNearbyMerchant());//RPGame initialize Trade
}
else{
return;
}
if (e.getKeyCode() == KeyEvent.VK_F) {
if (isMerchantNearby())
mapui.game.createTransaction(mapui.map.getPlayer(), mapui.map.getCurrentNearbyMerchant()); // RPGame initializes Trade
}


// 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 Down Expand Up @@ -220,21 +216,21 @@ public void move() {
}

}

public void resetLocation() {
x = initialX;
y = initialY;
changeInX = 15;
changeInY = 15;

}

/**
* Paints the sprite at its current location
* @param g Graphics2D for painting
*/
public void paint(Graphics2D g) {

g.drawImage(animation.getSprite(), x, y, null);
animation.update();
}
Expand Down Expand Up @@ -290,4 +286,8 @@ public void setY(int y) {
this.y = y;
}

public boolean isMerchantNearby() {
return (mapui.getMap().collisionTo("east"))||(mapui.getMap().collisionTo("west"))||(mapui.getMap().collisionTo("south"))||(mapui.getMap().collisionTo("north"))||(mapui.getMap().collisionTo("northeast"))||(mapui.getMap().collisionTo("northwest"))||(mapui.getMap().collisionTo("southeast"))||(mapui.getMap().collisionTo("southwest")) && (mapui.getMap().getCurrentNearbyMerchant() != null);
}

}
109 changes: 58 additions & 51 deletions MerchantRPGCSE2102/src/view/MapUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import model.Map;
import sprites.MerchantSprite;
import sprites.PlayerSprite;
import controller.RPGame;


@SuppressWarnings("serial")
public class MapUI extends JPanel {
Expand Down Expand Up @@ -63,26 +63,33 @@ public void keyTyped(KeyEvent e) {
@Override
public void paint(Graphics g) {
super.paint(g); // repaints the canvas

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Prevents aliasing
g2d.drawImage(loadWorld("RPG_samplemap.png"), 0, 0, null);

g2d.drawImage(loadImage("RPG_samplemap.png"), 0, 0, null);

// Paints the player onto the canvas
player.paint(g2d);
// Paints each merchant onto the canvas
for (MerchantSprite merchant : merchants)
merchant.paint(g2d);


if (player.isMerchantNearby()) {
if (map.getCurrentNearbyMerchant().getCol() > map.getCols()/2)
g2d.drawImage(loadImage("dialogbubble-reverse.png"), map.getCurrentNearbyMerchant().getCol()*tileSize - 85, map.getCurrentNearbyMerchant().getRow()*tileSize - 60, null);
else
g2d.drawImage(loadImage("dialogbubble.png"), (map.getCurrentNearbyMerchant().getCol() + 1)*tileSize, map.getCurrentNearbyMerchant().getRow()*tileSize - 60, null);
}

g2d.setColor(Color.BLUE);
g2d.setFont(new Font("Verdana", Font.BOLD, 18));
// For testing purposes. Shows the Player's current Vertex position
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("Verdana", Font.BOLD, 20));
g2d.drawString("Vertex: (" + map.getPlayer().getRow() + ", " + map.getPlayer().getCol() + ")", 1000, 20);
//g2d.drawString("Vertex: (" + map.getPlayer().getRow() + ", " + map.getPlayer().getCol() + ")", 1000, 20);
//g2d.dispose();

}

/**
* Adds a MerchantSprite to the list of MerchantSprite's to be painted on the canvas
* @param row The row of the merchant
Expand All @@ -101,9 +108,9 @@ public void addMerchantSprite(int row, int col) {
public void createPlayerSprite(int row, int col) {
player = new PlayerSprite(this, row, col, tileSize);
}

public void changeState(){

}

/**
Expand All @@ -112,50 +119,50 @@ public void changeState(){
public void move() {
//check transaction
if (game._movement==false){

}
else{
player.move();

// Every 30 pixels, the player will move to a new Vertex
// I used the values 0, 1, 30, 31 to establish a border between crossing Vertices
// For example, if the player reaches a changeInX of 31, this means they are moving to a Vertex in the east direction and their changeInX is reset to 1
// if they then decide to move west, they will move to the next Vertex when their changeInX reaches 0.
// Thus, the border shared by two vertices is inbetween 0 and 1 as well as 30 and 31.
if (player.getChangeInX() == tileSize + 1) {
map.movePlayer("east");
player.setChangeInX(1);
}
if (player.getChangeInX() == 0) {
map.movePlayer("west");
player.setChangeInX(tileSize);
}
if (player.getChangeInY() == 0) {
map.movePlayer("north");
player.setChangeInY(tileSize);
}
if (player.getChangeInY() == tileSize + 1) {
map.movePlayer("south");
player.setChangeInY(1);
}


player.move();

// Every 30 pixels, the player will move to a new Vertex
// I used the values 0, 1, 30, 31 to establish a border between crossing Vertices
// For example, if the player reaches a changeInX of 31, this means they are moving to a Vertex in the east direction and their changeInX is reset to 1
// if they then decide to move west, they will move to the next Vertex when their changeInX reaches 0.
// Thus, the border shared by two vertices is inbetween 0 and 1 as well as 30 and 31.
if (player.getChangeInX() == tileSize + 1) {
map.movePlayer("east");
player.setChangeInX(1);
}
if (player.getChangeInX() == 0) {
map.movePlayer("west");
player.setChangeInX(tileSize);
}
if (player.getChangeInY() == 0) {
map.movePlayer("north");
player.setChangeInY(tileSize);
}
if (player.getChangeInY() == tileSize + 1) {
map.movePlayer("south");
player.setChangeInY(1);
}
}
}

public static BufferedImage loadWorld(String fileName) {

BufferedImage worldImage = null;
public static BufferedImage loadImage(String fileName) {

BufferedImage image = null;

try {
worldImage = ImageIO.read(new File("src/images/" + fileName));
} catch (IOException e) {
e.printStackTrace();
}
try {
image = ImageIO.read(new File("src/images/" + fileName));
} catch (IOException e) {
e.printStackTrace();
}

return image;
}

return worldImage;
}

/**
* Calls the method for advancing the day in the instance of RPGame
* @return
Expand All @@ -164,11 +171,11 @@ public void nextDay() {
game.advanceDailyCycle();
map.resetPlayerLocation();
}

public Map getMap() {
return map;
}

public ArrayList<MerchantSprite> getMerchants() {
return merchants;
}
Expand Down

0 comments on commit 6f67233

Please sign in to comment.