Skip to content

Commit 6c27ced

Browse files
committed
Added some items, collision, world map
1 parent e3d8a76 commit 6c27ced

38 files changed

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

3+
import java.awt.*;
34
import java.awt.image.BufferedImage;
45

56
//parent class for player, monster ect
67
public class Entity {
78
//Set default position
8-
public int x, y;
9+
public int worldX, worldY;
910
public int speed;
1011
//we store our images in this variables
11-
public BufferedImage up, up1, up2, down, down1, down2, left, left1, left2, right, right1, right2;
12+
public BufferedImage up, up1, up2, down, down1, down2,
13+
left, left1, left2, right, right1, right2;
1214
public String direction;
13-
1415
public int spriteCounter = 0;
1516
public int standCounter = 0;
1617
public int spriteNum = 1;
18+
public Rectangle solidArea;
19+
public int solidAreaDefaultX, solidAreaDefaultY;
20+
public boolean collisionOn = false;
1721
}

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

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,81 @@ public class Player extends Entity {
1313
GamePanel gamePanel;
1414
KeyHandler keyHandler;
1515

16+
private final String playerPath = "/player/";
17+
18+
public final int screenX, screenY;
19+
20+
//specific player width due to diff size of sprites
21+
//TODO maybe it's better to make this type of constant for whole characters
22+
//because they're also might be different
23+
private static final int PLAYER_WIDTH = 16;
24+
1625
public Player(GamePanel gamePanel, KeyHandler keyHandler) {
1726
this.gamePanel = gamePanel;
1827
this.keyHandler = keyHandler;
28+
//camera position
29+
screenX = GamePanel.SCREEN_WIDTH / 2 - (GamePanel.TILE_SIZE / 2);
30+
screenY = GamePanel.SCREEN_HEIGHT / 2 - (GamePanel.TILE_SIZE / 2);
31+
32+
solidArea = new Rectangle();
33+
solidArea.x = 8;
34+
solidArea.y = 16;
35+
solidAreaDefaultX = solidArea.x;
36+
solidAreaDefaultY = solidArea.y;
37+
//boundaries of player
38+
//TODO adjust it if needed
39+
solidArea.width = 26 - PLAYER_WIDTH;
40+
solidArea.height = 26;
41+
1942
setDefaultValues();
2043
getPlayerImage();
2144
}
2245

2346
public void setDefaultValues() {
24-
x = 100;
25-
y = 100;
26-
speed = 3;
47+
//player position of player
48+
worldX = GamePanel.TILE_SIZE * 23;
49+
worldY = GamePanel.TILE_SIZE * 21;
50+
speed = 4;
2751
direction = "down";
2852
}
2953

3054
public void getPlayerImage() {
3155
try {
3256
up = ImageIO.read(Objects.requireNonNull(
33-
getClass().getResourceAsStream("/player/boy_up.png")));
57+
getClass().getResourceAsStream(playerPath + "boy_up.png")));
3458

3559
up1 = ImageIO.read(Objects.requireNonNull(
36-
getClass().getResourceAsStream("/player/boy_up_1.png")));
60+
getClass().getResourceAsStream(playerPath + "boy_up_1.png")));
3761

3862
up2 = ImageIO.read(Objects.requireNonNull(
39-
getClass().getResourceAsStream("/player/boy_up_2.png")));
63+
getClass().getResourceAsStream(playerPath + "boy_up_2.png")));
4064

4165
down = ImageIO.read(Objects.requireNonNull(
42-
getClass().getResourceAsStream("/player/boy_down.png")));
66+
getClass().getResourceAsStream(playerPath + "boy_down.png")));
4367

4468
down1 = ImageIO.read(Objects.requireNonNull(
45-
getClass().getResourceAsStream("/player/boy_down_1.png")));
69+
getClass().getResourceAsStream(playerPath + "boy_down_1.png")));
4670

4771
down2 = ImageIO.read(Objects.requireNonNull(
48-
getClass().getResourceAsStream("/player/boy_down_2.png")));
72+
getClass().getResourceAsStream(playerPath + "boy_down_2.png")));
4973

5074
left = ImageIO.read(Objects.requireNonNull(
51-
getClass().getResourceAsStream("/player/boy_left.png")));
75+
getClass().getResourceAsStream(playerPath + "boy_left.png")));
5276

5377
left1 = ImageIO.read(Objects.requireNonNull(
54-
getClass().getResourceAsStream("/player/boy_left_1.png")));
78+
getClass().getResourceAsStream(playerPath + "boy_left_1.png")));
5579

5680
left2 = ImageIO.read(Objects.requireNonNull(
57-
getClass().getResourceAsStream("/player/boy_left_2.png")));
81+
getClass().getResourceAsStream(playerPath + "boy_left.png")));
5882

5983
right = ImageIO.read(Objects.requireNonNull(
60-
getClass().getResourceAsStream("/player/boy_right.png")));
84+
getClass().getResourceAsStream(playerPath + "boy_right.png")));
6185

6286
right1 = ImageIO.read(Objects.requireNonNull(
63-
getClass().getResourceAsStream("/player/boy_right_1.png")));
87+
getClass().getResourceAsStream(playerPath + "boy_right_1.png")));
6488

6589
right2 = ImageIO.read(Objects.requireNonNull(
66-
getClass().getResourceAsStream("/player/boy_right_2.png")));
90+
getClass().getResourceAsStream(playerPath + "boy_right_2.png")));
6791

6892
} catch (IOException e) {
6993
System.err.println("Error on getting player images!");
@@ -80,21 +104,29 @@ public void update() {
80104
//use else if to avoid diagonal movement, if it's not needed, use just if
81105
if (keyHandler.upPressed) {
82106
direction = "up";
83-
y -= speed;
84107
} else if (keyHandler.downPressed) {
85108
direction = "down";
86-
y += speed;
87109
} else if (keyHandler.leftPressed) {
88110
direction = "left";
89-
x -= speed;
90111
} else if (keyHandler.rightPressed) {
91112
direction = "right";
92-
x += speed;
93113
}
94-
95-
//changing sprites, depends on nums
114+
//Check tile collision
115+
collisionOn = false;
116+
gamePanel.checkCollision.checkTile(this);
117+
118+
//If collision false, play player move
119+
if (!collisionOn) {
120+
switch (direction) {
121+
case "up" -> worldY -= speed;
122+
case "down" -> worldY += speed;
123+
case "left" -> worldX -= speed;
124+
case "right" -> worldX += speed;
125+
}
126+
}
127+
//Changing sprites, depends on nums
96128
spriteCounter++;
97-
if (spriteCounter <= 13 - speed) {
129+
if (spriteCounter <= 13) {
98130
spriteNum = 1;
99131
}
100132
if (spriteCounter > 13 && spriteCounter <= 24) {
@@ -105,6 +137,7 @@ public void update() {
105137
}
106138
} else {
107139
standCounter++;
140+
//timer before the idle anim starts
108141
if (standCounter == 24) {
109142
spriteNum = 3; // Idle sprite
110143
standCounter = 0;
@@ -137,6 +170,7 @@ public void draw(Graphics2D graphics2D) {
137170
if (spriteNum == 3) image = right;
138171
}
139172
}
140-
graphics2D.drawImage(image, x, y, GamePanel.TILE_SIZE, GamePanel.TILE_SIZE, null);
173+
//FIXME player width = hardcode
174+
graphics2D.drawImage(image, screenX, screenY, GamePanel.TILE_SIZE - PLAYER_WIDTH, GamePanel.TILE_SIZE, null);
141175
}
142176
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main.java.com.khomsi.game.main;
2+
3+
import main.java.com.khomsi.game.entity.Entity;
4+
5+
public class CheckCollision {
6+
7+
GamePanel gamePanel;
8+
9+
public CheckCollision(GamePanel gamePanel) {
10+
this.gamePanel = gamePanel;
11+
}
12+
13+
public void checkTile(Entity entity) {
14+
//solid.x = 8, y = 16, width, height = 32
15+
int entityLeftWorldX = entity.worldX + entity.solidArea.x;
16+
int entityRightWorldX = entity.worldX + entity.solidArea.x + entity.solidArea.width;
17+
int entityTopWorldY = entity.worldY + entity.solidArea.y;
18+
int entityBottomWorldY = entity.worldY + entity.solidArea.y + entity.solidArea.height;
19+
20+
int entityLeftCol = entityLeftWorldX / GamePanel.TILE_SIZE;
21+
int entityRightCol = entityRightWorldX / GamePanel.TILE_SIZE;
22+
int entityTopRow = entityTopWorldY / GamePanel.TILE_SIZE;
23+
int entityBottomRow = entityBottomWorldY / GamePanel.TILE_SIZE;
24+
25+
int tileNum1, tileNum2;
26+
27+
//TODO maybe make it shorter
28+
switch (entity.direction) {
29+
case "up" -> {
30+
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+
}
37+
}
38+
case "down" -> {
39+
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+
}
46+
}
47+
case "left" -> {
48+
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+
}
55+
}
56+
case "right" -> {
57+
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;
63+
}
64+
}
65+
}
66+
}
67+
68+
}

src/main/java/com/khomsi/game/main/Main.java renamed to src/main/java/com/khomsi/game/main/GameApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import javax.swing.*;
44

5-
public class Main {
5+
public class GameApplication {
66
public static void main(String[] args) {
7+
new GameApplication().startGame();
8+
}
9+
10+
private void startGame() {
711
JFrame window = new JFrame();
812

913
//let window close properly, when use press close (x) button
@@ -19,6 +23,7 @@ public static void main(String[] args) {
1923
window.setLocationRelativeTo(null);
2024
//we can see the window
2125
window.setVisible(true);
26+
gamePanel.setupGame();
2227
gamePanel.startGameThread();
2328
}
2429
}

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

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

33
import main.java.com.khomsi.game.entity.Player;
4+
import main.java.com.khomsi.game.objects.SuperObject;
45
import main.java.com.khomsi.game.tiles.TileManager;
56

67
import javax.swing.*;
@@ -11,19 +12,32 @@ public class GamePanel extends JPanel implements Runnable {
1112

1213
static final int ORIGINAL_TILE_SIZE = 16; //16x16 tiles
1314

14-
//we need to scale the size of hero, because on big screens it'll be too small
15+
//we need to scale the size of hero and game, because on big screens it'll be too small
1516
static final int SCALE = 3;
1617
public static final int TILE_SIZE = ORIGINAL_TILE_SIZE * SCALE; //48x48 tiles
1718
public static final int MAX_SCREEN_COL = 16; //16 tiles horizontal
1819
public static final int MAX_SCREEN_ROW = 12; //12 tiles vertical
1920
public static final int SCREEN_WIDTH = TILE_SIZE * MAX_SCREEN_COL; //768 pixels
2021
public static final int SCREEN_HEIGHT = TILE_SIZE * MAX_SCREEN_ROW; //576 pixels
2122

23+
//World settings
24+
public static final int MAX_WORLD_COL = 50;
25+
26+
public static final int MAX_WORLD_ROW = 50;
27+
28+
//TODO для чего они?
29+
public static final int WORLD_WIDTH = TILE_SIZE * MAX_WORLD_COL;
30+
31+
public static final int WORLD_HEIGHT = TILE_SIZE * MAX_WORLD_ROW;
32+
2233
KeyHandler keyHandler = new KeyHandler();
2334
//use threads to start, stop,repeat actions.
2435
Thread gameThread;
25-
Player player = new Player(this, keyHandler);
36+
public Player player = new Player(this, keyHandler);
2637
TileManager tileManager = new TileManager(this);
38+
public CheckCollision checkCollision = new CheckCollision(this);
39+
public PlaceObjects placeObjects = new PlaceObjects(this);
40+
public SuperObject[] object = new SuperObject[10];
2741

2842
int FPS = 60;
2943

@@ -39,6 +53,10 @@ public GamePanel() {
3953
this.setFocusable(true);
4054
}
4155

56+
public void setupGame() {
57+
placeObjects.setObject();
58+
}
59+
4260
public void startGameThread() {
4361
//pass gamePanel to thread
4462
gameThread = new Thread(this);
@@ -91,8 +109,15 @@ public void paintComponent(Graphics graphics) {
91109
//extends graphic class and provide more control on geometry, color managment ect.
92110
Graphics2D graphics2D = (Graphics2D) graphics;
93111
tileManager.draw(graphics2D);
94-
112+
//object
113+
for (SuperObject superObject : object) {
114+
if (superObject != null) {
115+
superObject.draw(graphics2D, this);
116+
}
117+
}
118+
//player
95119
player.draw(graphics2D);
120+
96121
//save some memory
97122
graphics2D.dispose();
98123
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main.java.com.khomsi.game.main;
2+
3+
import main.java.com.khomsi.game.objects.*;
4+
5+
public class PlaceObjects {
6+
GamePanel gamePanel;
7+
8+
public PlaceObjects(GamePanel gamePanel) {
9+
this.gamePanel = gamePanel;
10+
}
11+
12+
public void setObject() {
13+
gamePanel.object[0] = new KnifeObject();
14+
gamePanel.object[0].worldX = GamePanel.TILE_SIZE * 23;
15+
gamePanel.object[0].worldY = GamePanel.TILE_SIZE * 7;
16+
17+
gamePanel.object[1] = new KeyObject();
18+
gamePanel.object[1].worldX = GamePanel.TILE_SIZE * 23;
19+
gamePanel.object[1].worldY = GamePanel.TILE_SIZE * 40;
20+
21+
gamePanel.object[2] = new DogObject();
22+
gamePanel.object[2].worldX = GamePanel.TILE_SIZE * 38;
23+
gamePanel.object[2].worldY = GamePanel.TILE_SIZE * 8;
24+
25+
gamePanel.object[3] = new DoorObject();
26+
gamePanel.object[3].worldX = GamePanel.TILE_SIZE * 10;
27+
gamePanel.object[3].worldY = GamePanel.TILE_SIZE * 9;
28+
29+
gamePanel.object[4] = new DoorObject();
30+
gamePanel.object[4].worldX = GamePanel.TILE_SIZE * 8;
31+
gamePanel.object[4].worldY = GamePanel.TILE_SIZE * 28;
32+
33+
gamePanel.object[5] = new DoorObject();
34+
gamePanel.object[5].worldX = GamePanel.TILE_SIZE * 12;
35+
gamePanel.object[5].worldY = GamePanel.TILE_SIZE * 2;
36+
37+
gamePanel.object[6] = new ChestObject();
38+
gamePanel.object[6].worldX = GamePanel.TILE_SIZE * 10;
39+
gamePanel.object[6].worldY = GamePanel.TILE_SIZE * 7;
40+
41+
gamePanel.object[7] = new KeyObject();
42+
gamePanel.object[7].worldX = GamePanel.TILE_SIZE * 40;
43+
gamePanel.object[7].worldY = GamePanel.TILE_SIZE * 8;
44+
}
45+
}

0 commit comments

Comments
 (0)