added collision to NPCs
This commit is contained in:
@@ -106,4 +106,89 @@ public class CollisionHandler {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//NPC OR MONSTER COLLISION
|
||||||
|
public int checkEntity(Entity entity, Entity[] target) {
|
||||||
|
int index = 999;
|
||||||
|
int c = -1;
|
||||||
|
|
||||||
|
for(Entity e : target) {
|
||||||
|
c++;
|
||||||
|
if (e != null) {
|
||||||
|
|
||||||
|
entity.solidArea.x += entity.worldX;
|
||||||
|
entity.solidArea.y += entity.worldY;
|
||||||
|
|
||||||
|
e.solidArea.x += e.worldX;
|
||||||
|
e.solidArea.y += e.worldY;
|
||||||
|
|
||||||
|
switch (entity.direction) {
|
||||||
|
case UP:
|
||||||
|
entity.solidArea.y -= entity.speed;
|
||||||
|
if (entity.solidArea.intersects(e.solidArea)) {
|
||||||
|
entity.collisionOn = true;
|
||||||
|
index = c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
entity.solidArea.y += entity.speed;
|
||||||
|
if (entity.solidArea.intersects(e.solidArea)) {
|
||||||
|
entity.collisionOn = true;
|
||||||
|
index = c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LEFT:
|
||||||
|
entity.solidArea.x -= entity.speed;
|
||||||
|
if (entity.solidArea.intersects(e.solidArea)) {
|
||||||
|
entity.collisionOn = true;
|
||||||
|
index = c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
entity.solidArea.x += entity.speed;
|
||||||
|
if (entity.solidArea.intersects(e.solidArea)) {
|
||||||
|
entity.collisionOn = true;
|
||||||
|
index = c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
entity.solidArea.x = entity.solidAreaDefaultX;
|
||||||
|
entity.solidArea.y = entity.solidAreaDefaultY;
|
||||||
|
e.solidArea.x = e.solidAreaDefaultX;
|
||||||
|
e.solidArea.y = e.solidAreaDefaultY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkPlayer(Entity entity) {
|
||||||
|
entity.solidArea.x += entity.worldX;
|
||||||
|
entity.solidArea.y += entity.worldY;
|
||||||
|
|
||||||
|
panel.player.solidArea.x += panel.player.worldX;
|
||||||
|
panel.player.solidArea.y += panel.player.worldY;
|
||||||
|
|
||||||
|
switch (entity.direction) {
|
||||||
|
case UP:
|
||||||
|
entity.solidArea.y -= entity.speed;
|
||||||
|
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
entity.solidArea.y += entity.speed;
|
||||||
|
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
|
||||||
|
break;
|
||||||
|
case LEFT:
|
||||||
|
entity.solidArea.x -= entity.speed;
|
||||||
|
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
entity.solidArea.x += entity.speed;
|
||||||
|
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
entity.solidArea.x = entity.solidAreaDefaultX;
|
||||||
|
entity.solidArea.y = entity.solidAreaDefaultY;
|
||||||
|
panel.player.solidArea.x = panel.player.solidAreaDefaultX;
|
||||||
|
panel.player.solidArea.y = panel.player.solidAreaDefaultY;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ public class Entity {
|
|||||||
setAction();
|
setAction();
|
||||||
collisionOn = false;
|
collisionOn = false;
|
||||||
panel.collisionH.checkTile(this);
|
panel.collisionH.checkTile(this);
|
||||||
|
panel.collisionH.checkObject(this, false);
|
||||||
|
panel.collisionH.checkPlayer(this);
|
||||||
|
|
||||||
if(!collisionOn) {
|
if(!collisionOn) {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
|
|||||||
@@ -62,9 +62,15 @@ public class Player extends Entity {
|
|||||||
// CHECK TILE COLLISION
|
// CHECK TILE COLLISION
|
||||||
collisionOn = false;
|
collisionOn = false;
|
||||||
panel.collisionH.checkTile(this);
|
panel.collisionH.checkTile(this);
|
||||||
|
|
||||||
|
// CHECK OBJECT COLLISION
|
||||||
int objIndex = panel.collisionH.checkObject(this, true);
|
int objIndex = panel.collisionH.checkObject(this, true);
|
||||||
pickObject(objIndex);
|
pickObject(objIndex);
|
||||||
|
|
||||||
|
// CHECK ENTITY COLLISION
|
||||||
|
int npcIndex = panel.collisionH.checkEntity(this, panel.npc);
|
||||||
|
interactNPC(npcIndex);
|
||||||
|
|
||||||
if(!collisionOn) {
|
if(!collisionOn) {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case UP -> worldY -= speed;
|
case UP -> worldY -= speed;
|
||||||
@@ -88,6 +94,11 @@ public class Player extends Entity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void interactNPC(int index) {
|
||||||
|
if(index == 999) return;
|
||||||
|
System.out.println("npc collision detected");
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user