Skip to content

Commit

Permalink
item collection
Browse files Browse the repository at this point in the history
  • Loading branch information
greshh committed Nov 2, 2024
1 parent 1190619 commit ad153b7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
57 changes: 55 additions & 2 deletions Chronomancy.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public void drawPlatforms() {
double lastvalue;
double stepsX, stepsY;

ArrayList<Item> inventory = new ArrayList<>();

// normal sprites.
BufferedImage idleImage = (BufferedImage)(loadImage("sprites/idle.png"));
BufferedImage[] walkImage = new BufferedImage[2];
Expand Down Expand Up @@ -109,6 +111,17 @@ public boolean checkCollision(Platform p) {
}
}

public boolean checkCollision(Item item) {
if ((player.hitbox.y < item.y+item.height)
&& (player.hitbox.y+player.hitbox.height > item.y)
&& (player.hitbox.x < item.x+item.width+xPush)
&& (player.hitbox.x+player.hitbox.width > item.x+xPush)) {
return true;
} else {
return false;
}
}

// checking collisions for enemies.
public boolean checkCollision(Character enemy) {
if ((player.hitbox.y < enemy.hitbox.y+enemy.hitbox.height)
Expand Down Expand Up @@ -613,6 +626,42 @@ public void drawHealthBar() {
drawRectangle(5, 60, healthBarW, healthBarH);
}

/* --- ITEM & INVENTORY --- */
ArrayList<Item> availableItems = new ArrayList<>();

public void initItems() {
availableItems.add(new Item("Apple", "Yummy apple yum", 1000.0, 350.0, 20.0, 20.0));
availableItems.add(new Item("Watch", "You need to tell the time?", 200.0, 350.0, 20.0, 20.0));
}

public void drawItems() {
changeColor(green);
for (Item a:availableItems) {
drawCircle(a.x+xPush, a.y, a.width/2);
}
}

public void updateItems(double dt) {
boolean[] isCollected = new boolean[availableItems.size()];
for (int i = 0; i < availableItems.size(); i++) {
isCollected[i] = false;
}

for (int i = 0; i < availableItems.size(); i++) {
Item a = availableItems.get(i);
if (checkCollision(a)) {
inventory.add(a);
isCollected[i] = true;
}
}

for (int i = 0; i < availableItems.size(); i++) {
if (isCollected[i]) {
availableItems.remove(i);
}
}
}

/* --- GAME --- */

public void loadSprites()
Expand Down Expand Up @@ -737,12 +786,14 @@ public void init()

initEnemy();
initPlatforms();
initItems();
}

@Override
public void update(double dt) {
if (!menu) {
updatePlayer(dt);
updateItems(dt);
for (Enemy e:enemies) {
updateEnemy(dt, e);
// if (checkCollision(e) && !e.isHit && attack) {
Expand All @@ -765,13 +816,15 @@ public void paintComponent() {
drawPlatforms();
drawEnemy();
drawHealthBar();
drawItems();

changeColor(white);
drawLine(0, GROUND, mWidth, GROUND); // DEBUG;

/* press TAB to see the debug menu */
if (debug) {
drawBoldText(5, 45, "player direction: " + player.direction + "");
drawBoldText(5, 90, "player.state: " + player.state);
drawBoldText(5, 45, "player x: " + player.x + "");
drawBoldText(5, 90, "GROUND: " + GROUND);
drawBoldText(5, 135, "dash: " + dash);
drawBoldText(5, 270, "shift: " + shift + "");
drawBoldText(5, 315, "player.vX: " + player.vX + "");
Expand Down
14 changes: 14 additions & 0 deletions Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Item {
public String name, description;
public double x, y;
public double width, height;

public Item(String name, String description, double x, double y, double width, double height) {
this.name = name;
this.description = description;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

0 comments on commit ad153b7

Please sign in to comment.