Skip to content

Commit

Permalink
Finished inventory GUI.
Browse files Browse the repository at this point in the history
Started on a selection gui
Updated collision to handle potential "sinking" with left, right, and top collision.
Enabled player to "use" a potion
  • Loading branch information
Mark Diez committed Aug 11, 2015
1 parent ea401e9 commit 79779c6
Show file tree
Hide file tree
Showing 13 changed files with 623 additions and 93 deletions.
427 changes: 359 additions & 68 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
47 changes: 41 additions & 6 deletions src/net/codelets/javaplatformer/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import net.codelets.javaplatformer.gamestates.GameState;
import net.codelets.javaplatformer.gui.InventoryGUI;
import net.codelets.javaplatformer.gui.StatusGUI;
import net.codelets.javaplatformer.items.Bag;
import net.codelets.javaplatformer.items.Item;
import net.codelets.javaplatformer.items.Potion;
import net.codelets.javaplatformer.items.Weapon;
import net.codelets.javaplatformer.objects.Block;
import net.codelets.javaplatformer.physics.Collision;

Expand All @@ -22,6 +26,7 @@ public class Player extends Entity {

//
private int health;
private Bag inventory;
// Constructors
public Player(int posX, int posY) {
super(posX, posY);
Expand All @@ -39,9 +44,15 @@ public void init() {
this.jumpSpeed = 5;
this.fallSpeed = 0.1;
this.health = 100;
this.speed = 2;
this.speed = 2.5;
this.currentFallSpeed = this.fallSpeed;
this.currentJumpSpeed = this.jumpSpeed;
this.inventory = new Bag();

inventory.add(new Potion());
inventory.add(new Weapon());
inventory.add(new Potion());
inventory.add(new Weapon());
this.statusGUI = new StatusGUI(this);
this.inventoryGUI = new InventoryGUI(this);
}
Expand All @@ -58,7 +69,7 @@ public void update(Block[][] blocks) {
@Override
public void draw(Graphics g) {
g.setColor(Color.red);
g.fillRect((int)posX, (int)posY, 32, 32);
g.fillRect((int) posX, (int) posY, 32, 32);
statusGUI.draw(g);
inventoryGUI.draw(g);
}
Expand All @@ -83,6 +94,15 @@ public void keyPressed(int key) {
case KeyEvent.VK_I:
inventoryGUI.keyPressed(key);
break;
case KeyEvent.VK_DOWN:
inventoryGUI.keyPressed(key);
break;
case KeyEvent.VK_UP:
inventoryGUI.keyPressed(key);
break;
case KeyEvent.VK_ENTER:
inventoryGUI.keyPressed(key);
break;
}
}

Expand Down Expand Up @@ -123,9 +143,18 @@ private void movement() {
private void collision(Block[][] blocks) {
for(int i = 0; i < blocks.length; i++) {
for(int j = 0; j < blocks[i].length; j++) {
if (Collision.topCollision(this, blocks[i][j])) currentJumpSpeed = 0;
if (Collision.leftCollision(this, blocks[i][j])) moveLeft = false;
if (Collision.rightCollision(this, blocks[i][j])) moveRight = false;
if (Collision.topCollision(this, blocks[i][j])) {
currentJumpSpeed = 0;
if(Collision.isColliding(this, blocks[i][j])) GameState.yOffset++;
}
if (Collision.leftCollision(this, blocks[i][j])) {
moveLeft = false;
if(Collision.isColliding(this, blocks[i][j])) GameState.xOffset++;
}
if (Collision.rightCollision(this, blocks[i][j])) {
moveRight = false;
if(Collision.isColliding(this, blocks[i][j])) GameState.xOffset--;
}
if (Collision.botCollision(this, blocks[i][j])) {
falling = false;
botCollision = true;
Expand All @@ -142,6 +171,12 @@ private void collision(Block[][] blocks) {
}
}

public void use(Item item) {
if(item instanceof Potion) {
this.health += ((Potion) item).getValue();
}
}
// Getters & Setters
public int getHealth() {return this.health;}
public int getHealth() { return this.health; }
public Bag getInventory() { return this.inventory; }
}
56 changes: 54 additions & 2 deletions src/net/codelets/javaplatformer/gui/InventoryGUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.codelets.javaplatformer.gui;

import net.codelets.javaplatformer.GamePanel;
import net.codelets.javaplatformer.colors.PlatformColor;
import net.codelets.javaplatformer.entities.Player;
import net.codelets.javaplatformer.items.Bag;
import net.codelets.javaplatformer.items.Item;

import java.awt.*;
import java.awt.event.KeyEvent;
Expand All @@ -11,11 +14,22 @@
*/
public class InventoryGUI {
private Player player;
private Bag inventory;

private boolean open;

private int posX;
private int posY;
private int currIndex;

public InventoryGUI(Player player) {
this.player = player;
this.inventory = new Bag();
this.inventory = player.getInventory();
this.open = false;
this.posX = GamePanel.WIDTH;
this.posY = 0;
this.currIndex = 0;
}

public void update() {
Expand All @@ -24,8 +38,31 @@ public void update() {

public void draw(Graphics g) {
if(open) {
// Background
g.setColor(Color.BLACK);
g.fillRect(posX - 205, posY + 95, 210, 310);
g.setColor(PlatformColor.DARK_BROWN);
g.fillRect(posX - 200, posY + 100, 200, 300);

// Foreground
g.setColor(Color.BLACK);
g.fillRect(posX - 190, posY + 105, 100, 25);
g.fillRect(posX - 190, posY + 140, 200, 250);
g.setColor(PlatformColor.LIGHT_BROWN);
g.fillRect(100,100,100,100);
g.fillRect(posX - 189, posY + 106, 98, 23);
g.fillRect(posX - 189, posY + 141, 200, 248);

// Words
g.setColor(Color.WHITE);
g.drawString("Inventory", posX - 185, posY + 123);
for(int i = 0; i < inventory.getMaxLoad(); i++) {
Item currItem = inventory.get(i);
if(currItem != null) {
if(i == currIndex) g.setColor(Color.YELLOW);
else g.setColor(Color.WHITE);
g.drawString(currItem.getName(), posX - 185, posY + 160 + i*20);
}
}
}
}

Expand All @@ -37,7 +74,22 @@ public void keyPressed(int key) {
} else {
open = true;
}
System.out.println(open);
break;
case KeyEvent.VK_DOWN:
if(currIndex < inventory.getCurrentLoad()-1)
currIndex++;
else
currIndex = 0;
break;
case KeyEvent.VK_UP:
if(currIndex > 0)
currIndex--;
else
currIndex = inventory.getCurrentLoad()-1;
break;
case KeyEvent.VK_ENTER:
player.use(inventory.get(currIndex));
inventory.remove(currIndex);
break;
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/net/codelets/javaplatformer/gui/SelectionGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.codelets.javaplatformer.gui;

import net.codelets.javaplatformer.GamePanel;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
* Created by Mark on 8/11/2015.
*/
public class SelectionGUI {
private int posX;
private int posY;
private int width;
private int height;
private String[] choices;

public SelectionGUI(String[] choices, int width, int height) {
this.posX = GamePanel.WIDTH / 2;
this.posY = GamePanel.HEIGHT / 2;
this.width = width;
this.height = height;
this.choices = choices;
}

public void update() {

}

public void draw(Graphics g) {

}

public void keyPressed(int key) {
switch(key) {
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_ENTER:
break;
}
}
}
20 changes: 3 additions & 17 deletions src/net/codelets/javaplatformer/gui/StatusGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.codelets.javaplatformer.gui;

import net.codelets.javaplatformer.colors.PlatformColor;
import net.codelets.javaplatformer.entities.Player;

import java.awt.*;
Expand Down Expand Up @@ -35,27 +34,14 @@ public void update() {
}

public void draw(Graphics g) {
// Stroke Background
g.setColor(Color.black);
g.fillRect(posX, posY, WIDTH, HEIGHT);
// Background
g.setColor(PlatformColor.DARK_BROWN);
g.fillRect(posX, posY + 3, 297, 114);
// Stroke Portrait
g.setColor(Color.BLACK);
g.fillRoundRect(posX + 10, posY + 10, PORTRAIT_SIZE + 4, PORTRAIT_SIZE + 4,10,10);
// Actual Portrait
g.setColor(PlatformColor.LIGHT_BROWN);
g.fillRoundRect(posX + 12, posY + 12, PORTRAIT_SIZE, PORTRAIT_SIZE,10,10);

// Stroke Health
g.setColor(Color.BLACK);
g.fillRoundRect(posX + 122, posY + 17, 166, 26, 10, 10);
g.fillRoundRect(posX + 12, posY + 17, 166, 26, 10, 10);
// Health Display
g.setColor(Color.WHITE);
g.fillRoundRect(posX + 125, posY + 20, 160, 20, 10, 10);
g.fillRoundRect(posX + 15, posY + 20, 160, 20, 10, 10);
g.setColor(Color.RED);
g.fillRoundRect(posX + 125, posY + 20, (int)(160 * healthScale), 20, 10, 10);
g.fillRoundRect(posX + 15, posY + 20, (int)(160 * healthScale), 20, 10, 10);

}

Expand Down
51 changes: 51 additions & 0 deletions src/net/codelets/javaplatformer/items/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.codelets.javaplatformer.items;

/**
* Created by Mark on 8/11/2015.
*/
public class Bag extends Item{
private Item[] items;
private int maxLoad;
private int currentLoad;

public Bag() {
super();
this.maxLoad = 5;
this.currentLoad = 0;
this.items = new Item[maxLoad];
}

public Bag(String name, String description, int maxLoad) {
super(name, description);
this.maxLoad = maxLoad;
this.currentLoad = 0;
this.items = new Item[maxLoad];
}

public void add(Item newItem) {
if(currentLoad < maxLoad) {
this.items[currentLoad] = newItem;
currentLoad++;
} else {
System.out.println("Inventory Full");
}
}

public void remove(int index) {
this.items[index] = null;
// Shifts the items
for(int i = index; i < currentLoad; i++) {
this.items[i] = this.items[i+1];
}
currentLoad--;
}

public Item get(int index) {
return this.items[index];
}

// Getters & Setters
public Item[] getItems() { return this.items; }
public int getCurrentLoad() { return this.currentLoad; }
public int getMaxLoad() { return this.maxLoad; }
}
27 changes: 27 additions & 0 deletions src/net/codelets/javaplatformer/items/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.codelets.javaplatformer.items;

/**
* Created by Mark on 8/11/2015.
*/
public abstract class Item {
private String name;
private String description;

public Item() {
this.name = "Unknown";
this.description = "N/A";
}

public Item(String name, String description) {
this.name = name;
this.description = description;
}

public String inspect() {
return getName() + ", " + getDescription();
}

// getters & setters
public String getName() { return this.name; }
public String getDescription() { return this.description; }
}
22 changes: 22 additions & 0 deletions src/net/codelets/javaplatformer/items/Potion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.codelets.javaplatformer.items;

/**
* Created by Mark on 8/11/2015.
*/
public class Potion extends Item {
private int value;

public Potion() {
super("Unidentified Potion", "Drink if you dare");
this.value = 100;
}

public Potion(String name, String description, int value) {
super(name, description);
this.value = value;
}


// Setters & Getters
public int getValue() { return value; }
}
22 changes: 22 additions & 0 deletions src/net/codelets/javaplatformer/items/Weapon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.codelets.javaplatformer.items;

/**
* Created by Mark on 8/11/2015.
*/
public class Weapon extends Item {
private int attackMax;
private int attackMin;

public Weapon() {
super("Unidentified Weapon", "It doesn't look cursed...");
this.attackMax = 2;
this.attackMin = 1;
}

public Weapon(String name, String description, int attackMax, int attackMin) {
super(name, description);
this.attackMin = attackMin;
this.attackMax = attackMax;
}

}

0 comments on commit 79779c6

Please sign in to comment.