Item pickup

This commit is contained in:
2025-11-27 00:11:45 +01:00
parent cc085b0045
commit f3335f9df4
6 changed files with 81 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ public class AssetSetter {
panel.obj[3] = new DoorObj(); panel.obj[3] = new DoorObj();
panel.obj[3].worldX = 10 * panel.tileSize; panel.obj[3].worldX = 10 * panel.tileSize;
panel.obj[3].worldY = 9 * panel.tileSize; panel.obj[3].worldY = 11 * panel.tileSize;
panel.obj[4] = new DoorObj(); panel.obj[4] = new DoorObj();
panel.obj[4].worldX = 8 * panel.tileSize; panel.obj[4].worldX = 8 * panel.tileSize;

View File

@@ -1,6 +1,7 @@
package de.miaurizius.jgame2d.core; package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.entity.Entity; import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.object.SuperObject;
public class CollisionHandler { public class CollisionHandler {
@@ -51,4 +52,57 @@ public class CollisionHandler {
} }
} }
public int checkObject(Entity entity, boolean player) {
int index = 999;
int c = -1;
for(SuperObject obj : panel.obj) {
c++;
if (obj != null) {
entity.solidArea.x += entity.worldX;
entity.solidArea.y += entity.worldY;
obj.solidArea.x += obj.worldX;
obj.solidArea.y += obj.worldY;
switch (entity.direction) {
case UP:
entity.solidArea.y -= entity.speed;
if (entity.solidArea.intersects(obj.solidArea)) {
if (obj.collision) entity.collisionOn = true;
if (player) index = c;
}
break;
case DOWN:
entity.solidArea.y += entity.speed;
if (entity.solidArea.intersects(obj.solidArea)) {
if (obj.collision) entity.collisionOn = true;
if (player) index = c;
}
break;
case LEFT:
entity.solidArea.x -= entity.speed;
if (entity.solidArea.intersects(obj.solidArea)) {
if (obj.collision) entity.collisionOn = true;
if (player) index = c;
}
break;
case RIGHT:
entity.solidArea.x += entity.speed;
if (entity.solidArea.intersects(obj.solidArea)) {
if (obj.collision) entity.collisionOn = true;
if (player) index = c;
}
break;
}
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
obj.solidArea.x = obj.solidAreaDefaultX;
obj.solidArea.y = obj.solidAreaDefaultY;
}
}
return index;
}
} }

View File

@@ -17,6 +17,7 @@ public class Entity {
public int spriteNum = 1; public int spriteNum = 1;
public Rectangle solidArea; public Rectangle solidArea;
public int solidAreaDefaultX, solidAreaDefaultY;
public boolean collisionOn = false; public boolean collisionOn = false;
} }

View File

@@ -16,6 +16,7 @@ public class Player extends Entity {
public final int screenX; public final int screenX;
public final int screenY; public final int screenY;
int hasKey = 0;
public Player(GamePanel panel, KeyHandler keyH) { public Player(GamePanel panel, KeyHandler keyH) {
this.panel = panel; this.panel = panel;
@@ -27,6 +28,8 @@ public class Player extends Entity {
solidArea = new Rectangle(); solidArea = new Rectangle();
solidArea.x = 12; solidArea.x = 12;
solidArea.y = 20; solidArea.y = 20;
solidAreaDefaultX = solidArea.x;
solidAreaDefaultY = solidArea.y;
solidArea.width = 24; solidArea.width = 24;
solidArea.height = 24; solidArea.height = 24;
@@ -67,6 +70,8 @@ public class Player extends Entity {
// CHECK TILE COLLISION // CHECK TILE COLLISION
collisionOn = false; collisionOn = false;
panel.collisionH.checkTile(this); panel.collisionH.checkTile(this);
int objIndex = panel.collisionH.checkObject(this, true);
pickObject(objIndex);
if(!collisionOn) { if(!collisionOn) {
switch (direction) { switch (direction) {
@@ -86,6 +91,22 @@ public class Player extends Entity {
} }
} }
public void pickObject(int index) {
if(index == 999) return;
switch(panel.obj[index].name.toLowerCase()) {
case "key":
hasKey++;
panel.obj[index] = null;
break;
case "door":
if(hasKey > 0) {
panel.obj[index] = null;
hasKey--;
}
break;
}
}
public void draw(Graphics2D graphics2d) { public void draw(Graphics2D graphics2d) {
BufferedImage image = switch (direction) { BufferedImage image = switch (direction) {
case UP -> (spriteNum == 1) ? up1 : up2; case UP -> (spriteNum == 1) ? up1 : up2;

View File

@@ -13,6 +13,7 @@ public class DoorObj extends SuperObject {
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
collision = true;
} }
} }

View File

@@ -11,6 +11,9 @@ public class SuperObject {
public String name; public String name;
public boolean collision = false; public boolean collision = false;
public int worldX, worldY; public int worldX, worldY;
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
public int solidAreaDefaultX = 0;
public int solidAreaDefaultY = 0;
public void draw(Graphics2D graphics2D, GamePanel panel) { public void draw(Graphics2D graphics2D, GamePanel panel) {
int screenX = worldX - panel.player.worldX + panel.player.screenX; int screenX = worldX - panel.player.worldX + panel.player.screenX;