Skip to content

Commit 6baa120

Browse files
committed
Finished the adventure game
Added new tiles, sound, timers, collision, interactive objects and quest
1 parent 6c27ced commit 6baa120

44 files changed

Lines changed: 355 additions & 72 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/com/khomsi/game/entity/Player.java

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main.java.com.khomsi.game.entity;
22

33
import main.java.com.khomsi.game.main.GamePanel;
4-
import main.java.com.khomsi.game.main.KeyHandler;
4+
import main.java.com.khomsi.game.main.tools.KeyHandler;
55

66
import javax.imageio.ImageIO;
77
import java.awt.*;
@@ -16,6 +16,7 @@ public class Player extends Entity {
1616
private final String playerPath = "/player/";
1717

1818
public final int screenX, screenY;
19+
public int hasKey = 0;
1920

2021
//specific player width due to diff size of sprites
2122
//TODO maybe it's better to make this type of constant for whole characters
@@ -36,8 +37,8 @@ public Player(GamePanel gamePanel, KeyHandler keyHandler) {
3637
solidAreaDefaultY = solidArea.y;
3738
//boundaries of player
3839
//TODO adjust it if needed
39-
solidArea.width = 26 - PLAYER_WIDTH;
40-
solidArea.height = 26;
40+
solidArea.width = 28 - PLAYER_WIDTH;
41+
solidArea.height = 28;
4142

4243
setDefaultValues();
4344
getPlayerImage();
@@ -54,40 +55,40 @@ public void setDefaultValues() {
5455
public void getPlayerImage() {
5556
try {
5657
up = ImageIO.read(Objects.requireNonNull(
57-
getClass().getResourceAsStream(playerPath + "boy_up.png")));
58+
getClass().getResourceAsStream(playerPath + "chara_up.png")));
5859

5960
up1 = ImageIO.read(Objects.requireNonNull(
60-
getClass().getResourceAsStream(playerPath + "boy_up_1.png")));
61+
getClass().getResourceAsStream(playerPath + "chara_up_1.png")));
6162

6263
up2 = ImageIO.read(Objects.requireNonNull(
63-
getClass().getResourceAsStream(playerPath + "boy_up_2.png")));
64+
getClass().getResourceAsStream(playerPath + "chara_up_2.png")));
6465

6566
down = ImageIO.read(Objects.requireNonNull(
66-
getClass().getResourceAsStream(playerPath + "boy_down.png")));
67+
getClass().getResourceAsStream(playerPath + "chara_down.png")));
6768

6869
down1 = ImageIO.read(Objects.requireNonNull(
69-
getClass().getResourceAsStream(playerPath + "boy_down_1.png")));
70+
getClass().getResourceAsStream(playerPath + "chara_down_1.png")));
7071

7172
down2 = ImageIO.read(Objects.requireNonNull(
72-
getClass().getResourceAsStream(playerPath + "boy_down_2.png")));
73+
getClass().getResourceAsStream(playerPath + "chara_down_2.png")));
7374

7475
left = ImageIO.read(Objects.requireNonNull(
75-
getClass().getResourceAsStream(playerPath + "boy_left.png")));
76+
getClass().getResourceAsStream(playerPath + "chara_left.png")));
7677

7778
left1 = ImageIO.read(Objects.requireNonNull(
78-
getClass().getResourceAsStream(playerPath + "boy_left_1.png")));
79+
getClass().getResourceAsStream(playerPath + "chara_left_1.png")));
7980

8081
left2 = ImageIO.read(Objects.requireNonNull(
81-
getClass().getResourceAsStream(playerPath + "boy_left.png")));
82+
getClass().getResourceAsStream(playerPath + "chara_left_2.png")));
8283

8384
right = ImageIO.read(Objects.requireNonNull(
84-
getClass().getResourceAsStream(playerPath + "boy_right.png")));
85+
getClass().getResourceAsStream(playerPath + "chara_right.png")));
8586

8687
right1 = ImageIO.read(Objects.requireNonNull(
87-
getClass().getResourceAsStream(playerPath + "boy_right_1.png")));
88+
getClass().getResourceAsStream(playerPath + "chara_right_1.png")));
8889

8990
right2 = ImageIO.read(Objects.requireNonNull(
90-
getClass().getResourceAsStream(playerPath + "boy_right_2.png")));
91+
getClass().getResourceAsStream(playerPath + "chara_right_2.png")));
9192

9293
} catch (IOException e) {
9394
System.err.println("Error on getting player images!");
@@ -114,7 +115,9 @@ public void update() {
114115
//Check tile collision
115116
collisionOn = false;
116117
gamePanel.checkCollision.checkTile(this);
117-
118+
//Check obj collision
119+
int objIndex = gamePanel.checkCollision.checkObject(this, true);
120+
takeObject(objIndex);
118121
//If collision false, play player move
119122
if (!collisionOn) {
120123
switch (direction) {
@@ -145,6 +148,53 @@ public void update() {
145148
}
146149
}
147150

151+
public void takeObject(int index) {
152+
//if index is not player, make a reaction on obj
153+
if (index != 999) {
154+
String objName = gamePanel.object[index].name;
155+
switch (objName) {
156+
case "Key" -> {
157+
gamePanel.playSE(2);
158+
hasKey++;
159+
gamePanel.object[index] = null;
160+
gamePanel.ui.showMessage("You got a key!");
161+
}
162+
case "Door" -> {
163+
if (hasKey > 0) {
164+
gamePanel.playSE(4);
165+
gamePanel.object[index] = null;
166+
hasKey--;
167+
gamePanel.ui.showMessage("Door opened!");
168+
} else {
169+
gamePanel.ui.showMessage("Find a key!");
170+
// gamePanel.playerSE(5);
171+
}
172+
}
173+
case "Boots" -> {
174+
gamePanel.playSE(3);
175+
speed += 1;
176+
gamePanel.object[index] = null;
177+
gamePanel.ui.showMessage("Go faster!");
178+
}
179+
case "Dog" -> {
180+
gamePanel.playSE(1);
181+
speed -= 1;
182+
gamePanel.object[index] = null;
183+
gamePanel.ui.showMessage("Big toby!");
184+
}
185+
case "Knife" -> {
186+
gamePanel.playSE(6);
187+
gamePanel.object[index] = null;
188+
}
189+
case "Chest" -> {
190+
gamePanel.ui.gameFinished = true;
191+
gamePanel.stopMusic();
192+
gamePanel.playSE(7);
193+
}
194+
}
195+
}
196+
}
197+
148198
public void draw(Graphics2D graphics2D) {
149199

150200
BufferedImage image = null;

src/main/java/com/khomsi/game/main/CheckCollision.java

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
public 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

Comments
 (0)