44
55public class CheckCollision {
66
7- GamePanel gamePanel ;
7+ GamePanel panel ;
88
9- public CheckCollision (GamePanel gamePanel ) {
10- this .gamePanel = gamePanel ;
9+ public CheckCollision (GamePanel panel ) {
10+ this .panel = panel ;
1111 }
1212
1313 public void checkTile (Entity entity ) {
@@ -24,45 +24,94 @@ public void checkTile(Entity entity) {
2424
2525 int tileNum1 , tileNum2 ;
2626
27- //TODO maybe make it shorter
2827 switch (entity .direction ) {
2928 case "up" -> {
3029 entityTopRow = (entityTopWorldY - entity .speed ) / GamePanel .TILE_SIZE ;
31- tileNum1 = gamePanel .tileManager .mapTileNum [entityLeftCol ][entityTopRow ];
32- tileNum2 = gamePanel .tileManager .mapTileNum [entityRightCol ][entityTopRow ];
33- if (gamePanel .tileManager .tiles [tileNum1 ].collision ||
34- gamePanel .tileManager .tiles [tileNum2 ].collision ) {
35- entity .collisionOn = true ;
36- }
30+ tileNum1 = panel .tileManager .mapTileNum [entityLeftCol ][entityTopRow ];
31+ tileNum2 = panel .tileManager .mapTileNum [entityRightCol ][entityTopRow ];
32+ activateCollision (entity , tileNum1 , tileNum2 );
3733 }
3834 case "down" -> {
3935 entityBottomRow = (entityBottomWorldY + entity .speed ) / GamePanel .TILE_SIZE ;
40- tileNum1 = gamePanel .tileManager .mapTileNum [entityLeftCol ][entityBottomRow ];
41- tileNum2 = gamePanel .tileManager .mapTileNum [entityRightCol ][entityBottomRow ];
42- if (gamePanel .tileManager .tiles [tileNum1 ].collision ||
43- gamePanel .tileManager .tiles [tileNum2 ].collision ) {
44- entity .collisionOn = true ;
45- }
36+ tileNum1 = panel .tileManager .mapTileNum [entityLeftCol ][entityBottomRow ];
37+ tileNum2 = panel .tileManager .mapTileNum [entityRightCol ][entityBottomRow ];
38+ activateCollision (entity , tileNum1 , tileNum2 );
4639 }
4740 case "left" -> {
4841 entityLeftCol = (entityLeftWorldX - entity .speed ) / GamePanel .TILE_SIZE ;
49- tileNum1 = gamePanel .tileManager .mapTileNum [entityLeftCol ][entityTopRow ];
50- tileNum2 = gamePanel .tileManager .mapTileNum [entityLeftCol ][entityBottomRow ];
51- if (gamePanel .tileManager .tiles [tileNum1 ].collision ||
52- gamePanel .tileManager .tiles [tileNum2 ].collision ) {
53- entity .collisionOn = true ;
54- }
42+ tileNum1 = panel .tileManager .mapTileNum [entityLeftCol ][entityTopRow ];
43+ tileNum2 = panel .tileManager .mapTileNum [entityLeftCol ][entityBottomRow ];
44+ activateCollision (entity , tileNum1 , tileNum2 );
5545 }
5646 case "right" -> {
5747 entityRightCol = (entityRightWorldX + entity .speed ) / GamePanel .TILE_SIZE ;
58- tileNum1 = gamePanel .tileManager .mapTileNum [entityRightCol ][entityTopRow ];
59- tileNum2 = gamePanel .tileManager .mapTileNum [entityRightCol ][entityBottomRow ];
60- if (gamePanel .tileManager .tiles [tileNum1 ].collision ||
61- gamePanel .tileManager .tiles [tileNum2 ].collision ) {
62- entity .collisionOn = true ;
48+ tileNum1 = panel .tileManager .mapTileNum [entityRightCol ][entityTopRow ];
49+ tileNum2 = panel .tileManager .mapTileNum [entityRightCol ][entityBottomRow ];
50+ activateCollision (entity , tileNum1 , tileNum2 );
51+ }
52+ }
53+ }
54+
55+ //Check if any side of tile has collision
56+ private void activateCollision (Entity entity , int tileNum1 , int tileNum2 ) {
57+ if (panel .tileManager .tiles [tileNum1 ].collision ||
58+ panel .tileManager .tiles [tileNum2 ].collision ) {
59+ entity .collisionOn = true ;
60+ }
61+ }
62+
63+ public int checkObject (Entity entity , boolean player ) {
64+ int index = 999 ;
65+
66+ for (int i = 0 ; i < panel .object .length ; i ++) {
67+
68+ if (panel .object [i ] != null ) {
69+ //get entity's solid area pos
70+ entity .solidArea .x = entity .worldX + entity .solidArea .x ;
71+ entity .solidArea .y = entity .worldY + entity .solidArea .y ;
72+
73+ //get the obj's solid pos
74+ panel .object [i ].solidArea .x = panel .object [i ].worldX
75+ + panel .object [i ].solidArea .x ;
76+ panel .object [i ].solidArea .y = panel .object [i ].worldY
77+ + panel .object [i ].solidArea .y ;
78+ switch (entity .direction ) {
79+ case "up" -> {
80+ entity .solidArea .y -= entity .speed ;
81+ index = checkCollisionGetIndex (entity , player , index , i );
82+ //if it's npc or someone else, they can't pick up tools
83+ }
84+ case "down" -> {
85+ entity .solidArea .y += entity .speed ;
86+ index = checkCollisionGetIndex (entity , player , index , i );
87+ }
88+ case "left" -> {
89+ entity .solidArea .x -= entity .speed ;
90+ index = checkCollisionGetIndex (entity , player , index , i );
91+ }
92+ case "right" -> {
93+ entity .solidArea .x += entity .speed ;
94+ index = checkCollisionGetIndex (entity , player , index , i );
95+ }
6396 }
97+ entity .solidArea .x = entity .solidAreaDefaultX ;
98+ entity .solidArea .y = entity .solidAreaDefaultY ;
99+ panel .object [i ].solidArea .x = panel .object [i ].solidAreaDefaultX ;
100+ panel .object [i ].solidArea .y = panel .object [i ].solidAreaDefaultY ;
64101 }
65102 }
103+ return index ;
66104 }
67105
106+ private int checkCollisionGetIndex (Entity entity , boolean player , int index , int i ) {
107+ if (entity .solidArea .intersects (panel .object [i ].solidArea )) {
108+ if (panel .object [i ].collision ) {
109+ entity .collisionOn = true ;
110+ }
111+ if (player ) {
112+ index = i ;
113+ }
114+ }
115+ return index ;
116+ }
68117}
0 commit comments