Skip to content

Commit 5a1d3f3

Browse files
committed
Bug fixes, code refactor of UI class and new logic in game.
1 parent b610b4d commit 5a1d3f3

10 files changed

Lines changed: 185 additions & 145 deletions

File tree

config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fullscreen=Off
2-
music_volume=1
2+
music_volume=2
33
se_volume=1

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void setupSystemTray() {
5555

5656
show.addActionListener(actionEvent -> window.setVisible(true));
5757
MenuItem exit = new MenuItem("Exit");
58-
exit.addActionListener(actionEvent -> System.exit(0));
58+
exit.addActionListener(actionEvent -> handleExit(systemTray, trayIcon));
5959
popupMenu.add(show);
6060
popupMenu.add(exit);
6161
trayIcon.setPopupMenu(popupMenu);
@@ -65,4 +65,10 @@ private void setupSystemTray() {
6565
throw new RuntimeException(e);
6666
}
6767
}
68+
69+
private void handleExit(SystemTray systemTray, TrayIcon trayIcon) {
70+
systemTray.remove(trayIcon); // Remove the tray icon
71+
window.dispose(); // Dispose the window
72+
System.exit(0); // Terminate the application
73+
}
6874
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.khomsi.game.entity.player.Player;
44
import com.khomsi.game.main.GameManager;
55
import com.khomsi.game.main.tools.Tools;
6+
import com.khomsi.game.objects.interact.CoinBObject;
67
import com.khomsi.game.objects.spells.HeartObject;
78
import com.khomsi.game.objects.spells.ManaObject;
8-
import com.khomsi.game.objects.interact.CoinBObject;
99

1010
import java.awt.*;
1111
import java.awt.image.BufferedImage;
@@ -14,7 +14,7 @@
1414
import java.util.Random;
1515

1616
//parent class for player, monster etc
17-
public class Entity extends Tools{
17+
public class Entity extends Tools {
1818
//IMAGES
1919
public BufferedImage image, image2, image3;
2020
//Store our walking images in this variables
@@ -267,9 +267,17 @@ public void damagePlayer(int attack) {
267267
offBalance = true;
268268
//Return sprite to previous one, stan effect
269269
spriteCounter -= 60;
270-
} else {
271-
//Normal guard
272-
damage /= 3;
270+
}
271+
//Normal guard
272+
else {
273+
System.out.println("before damage: " + damage);
274+
// Normal guard
275+
if (damage < 1) {
276+
damage = 1; // Make sure damage is non-negative
277+
} else {
278+
damage /= 3; // Divide the damage by 3
279+
}
280+
System.out.println("after damage: " + damage);
273281
gameManager.playSE(17);
274282
}
275283
}
@@ -629,6 +637,7 @@ public void changeDirection(String direction) {
629637
}
630638

631639
public void setKnockBack(Entity target, Entity attacker, int knockBackPower) {
640+
gameManager.playerRun = false;
632641
this.attacker = attacker;
633642
target.knockBackDirection = attacker.direction;
634643
target.speed += knockBackPower;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Player(GameManager gameManager, KeyHandler keyHandler) {
3737
solidAreaDefaultY = solidArea.y;
3838
//boundaries of player
3939
//adjust it if needed
40-
solidArea.width = 31;
40+
solidArea.width = 32;
4141
solidArea.height = 32;
4242

4343
setDefaultValues();
@@ -95,7 +95,7 @@ public void setDefaultPosition() {
9595
// gameManager.currentMap = 3;
9696
// worldX = GameManager.TILE_SIZE * 25;
9797
// worldY = GameManager.TILE_SIZE * 1;
98-
direction = "down";
98+
// direction = "down";
9999
}
100100

101101
public void restoreStatus() {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import com.khomsi.game.main.logic.CutSceneManager;
1212
import com.khomsi.game.main.logic.EntityGenerator;
1313
import com.khomsi.game.main.logic.EventHandler;
14-
import com.khomsi.game.main.tools.*;
14+
import com.khomsi.game.main.tools.EntityComparator;
15+
import com.khomsi.game.main.tools.KeyHandler;
16+
import com.khomsi.game.main.tools.PlaceObjects;
17+
import com.khomsi.game.main.tools.Sound;
1518
import com.khomsi.game.main.tools.ui.UI;
1619
import com.khomsi.game.tiles.Map;
1720
import com.khomsi.game.tiles.TileManager;
@@ -47,8 +50,7 @@ public class GameManager extends JPanel implements Runnable {
4750
int screenWidthFull = SCREEN_WIDTH;
4851
int screenHeightFull = SCREEN_HEIGHT;
4952
BufferedImage tempScreen;
50-
Graphics2D g2d;
51-
53+
public Graphics2D g2d;
5254

5355
//TOOLS FOR GAME
5456
public KeyHandler keyHandler = new KeyHandler(this);
@@ -413,20 +415,22 @@ public void playSE(int i) {
413415
se.play();
414416
}
415417

416-
//FIXME check the method
417418
public void changeArea() {
418419
if (nextArea != currentArea) {
419420
stopMusic();
420-
if (nextArea == OUTSIDE)
421+
if (nextArea == OUTSIDE) {
421422
playMusic(0);
422-
if (nextArea == INDOOR)
423+
}
424+
if (nextArea == INDOOR) {
423425
playMusic(19);
426+
}
424427
if (nextArea == DUNGEON || nextArea == BOSS_DUNGEON) {
425428
playMusic(20);
426429
placeObjects.npcOnMap2();
427430
}
428431
}
429-
currentArea = nextArea;
432+
//FIXME cause the micro lag when called
430433
placeObjects.setMobs();
434+
currentArea = nextArea;
431435
}
432436
}

src/main/java/com/khomsi/game/main/logic/EventHandler.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.khomsi.game.entity.Entity;
55
import com.khomsi.game.main.GameManager;
66

7+
import java.awt.*;
8+
79
public class EventHandler {
810
GameManager gameManager;
911
Entity eventMaster;
@@ -80,7 +82,7 @@ private void handleInteractions() {
8082
gameManager.playSE(5);
8183
}
8284
//Enter Dungeon in seller house
83-
else if (interact(1, 25, 15, "up") || interact(1, 24, 15, "up")) {
85+
else if (interact(1, 25, 15, "up", 15)) {
8486
changeLocation(2, 16, 39, GameManager.DUNGEON);
8587
gameManager.playSE(4);
8688
}
@@ -108,30 +110,39 @@ else if (interact(2, 9, 14, "any")) {
108110
gameManager.playSE(4);
109111
}
110112
//Start cutscene with the boss
111-
if (interact(3, 24, 10, "any") ||
112-
interact(3, 25, 10, "any") ||
113-
interact(3, 26, 10, "any")) {
113+
if (interact(3, 25, 10, "any") ||
114+
interact(3, 26, 10, "any", 10)) {
114115
dungeonBoss();
115116
}
116117
}
117118
}
118119
}
119120

120121
public boolean interact(int map, int col, int row, String direction) {
122+
return interact(map, col, row, direction, 0);
123+
}
124+
125+
public boolean interact(int map, int col, int row, String direction, int tolerance) {
121126
boolean interact = false;
122127

123128
if (map == gameManager.currentMap) {
124-
125129
gameManager.player.solidArea.x = gameManager.player.worldX + gameManager.player.solidArea.x;
126130
gameManager.player.solidArea.y = gameManager.player.worldY + gameManager.player.solidArea.y;
127131

128132
eventRect[map][col][row].x = col * GameManager.TILE_SIZE + eventRect[map][col][row].x;
129133
eventRect[map][col][row].y = row * GameManager.TILE_SIZE + eventRect[map][col][row].y;
130-
//if player's solidArea is colliding with event's solidArea, event can be played only 1 time
131-
if (gameManager.player.solidArea.intersects(eventRect[map][col][row]) &&
132-
!eventRect[map][col][row].eventDone) {
133-
if (gameManager.player.direction.contentEquals(direction) ||
134-
direction.contentEquals("any")) {
134+
// Define a smaller rectangle for player's collision area with tolerance
135+
//This way we can adjust the solid area of interaction, so if player collide on solid area, it will interact
136+
//any way
137+
Rectangle playerCollisionArea = new Rectangle(
138+
gameManager.player.solidArea.x,
139+
gameManager.player.solidArea.y,
140+
gameManager.player.solidArea.width + tolerance,
141+
gameManager.player.solidArea.height
142+
);
143+
// If player's collision area intersects with event's solidArea, event can be played only 1 time
144+
if (playerCollisionArea.intersects(eventRect[map][col][row]) && !eventRect[map][col][row].eventDone) {
145+
if (gameManager.player.direction.contentEquals(direction) || direction.contentEquals("any")) {
135146
interact = true;
136147
previousEventX = gameManager.player.worldX;
137148
previousEventY = gameManager.player.worldY;

src/main/java/com/khomsi/game/main/tools/KeyHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ private void playerState(int code) {
214214
if (code == KeyEvent.VK_P) gameManager.gameState = GameManager.PAUSE_STATE;
215215
if (code == KeyEvent.VK_ESCAPE) gameManager.gameState = GameManager.OPTION_STATE;
216216
//Opens the map
217-
if (code == KeyEvent.VK_M) gameManager.gameState = GameManager.MAP_STATE;
218-
if (code == KeyEvent.VK_X) gameManager.map.miniMapOn = !gameManager.map.miniMapOn;
217+
if (code == KeyEvent.VK_M && gameManager.nextArea != GameManager.DUNGEON)
218+
gameManager.gameState = GameManager.MAP_STATE;
219+
if (code == KeyEvent.VK_X)
220+
gameManager.map.miniMapOn = !gameManager.map.miniMapOn;
219221
//When player pressed shift, he runs
220222
if (code == KeyEvent.VK_SHIFT && !gameManager.playerRun) {
221223
gameManager.playerRun = true;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.khomsi.game.main.tools.ui;
2+
3+
import com.khomsi.game.main.GameManager;
4+
5+
public class Settings extends UI {
6+
public Settings(GameManager gameManager) {
7+
super(gameManager);
8+
}
9+
10+
void fullScreenNotification(int frameX, int frameY) {
11+
int textX = frameX + GameManager.TILE_SIZE;
12+
int textY = frameY + GameManager.TILE_SIZE * 3;
13+
currentDialog = "The changes will be \napplied after restarting \nthe game.";
14+
splitTextInDialog(currentDialog, textX, textY);
15+
//Back text
16+
textY = frameY + GameManager.TILE_SIZE * 9;
17+
graphics2D.drawString("Back", textX, textY);
18+
//Draw cursor
19+
if (commandNum == 0) {
20+
showChooseCursor(textX, textY, 25);
21+
if (gameManager.keyHandler.isEnterPressed) subState = 0;
22+
}
23+
}
24+
25+
void showControl(int frameX, int frameY) {
26+
int textX;
27+
int textY;
28+
String text = "Control";
29+
textX = getXCenterText(text);
30+
textY = frameY + GameManager.TILE_SIZE;
31+
graphics2D.drawString(text, textX, textY);
32+
textX = frameX + GameManager.TILE_SIZE;
33+
textY += GameManager.TILE_SIZE;
34+
graphics2D.drawString("Move", textX, textY);
35+
textY += GameManager.TILE_SIZE;
36+
graphics2D.drawString("Confirm/Attack", textX, textY);
37+
textY += GameManager.TILE_SIZE;
38+
graphics2D.drawString("Shoot/Cast", textX, textY);
39+
textY += GameManager.TILE_SIZE;
40+
graphics2D.drawString("Inventory", textX, textY);
41+
textY += GameManager.TILE_SIZE;
42+
graphics2D.drawString("Pause", textX, textY);
43+
textY += GameManager.TILE_SIZE;
44+
graphics2D.drawString("Settings", textX, textY);
45+
textY += GameManager.TILE_SIZE;
46+
graphics2D.drawString("Map/MiniMap", textX, textY);
47+
textX = frameX + GameManager.TILE_SIZE * 6;
48+
textY = frameY + GameManager.TILE_SIZE * 2;
49+
50+
graphics2D.drawString("WASD", textX, textY);
51+
textY += GameManager.TILE_SIZE;
52+
graphics2D.drawString("ENTER", textX, textY);
53+
textY += GameManager.TILE_SIZE;
54+
graphics2D.drawString("CTRL", textX, textY);
55+
textY += GameManager.TILE_SIZE;
56+
graphics2D.drawString("E", textX, textY);
57+
textY += GameManager.TILE_SIZE;
58+
graphics2D.drawString("P", textX, textY);
59+
textY += GameManager.TILE_SIZE;
60+
graphics2D.drawString("ESC", textX, textY);
61+
textY += GameManager.TILE_SIZE;
62+
graphics2D.drawString("M/X", textX, textY);
63+
64+
//Back button
65+
textX = frameX + GameManager.TILE_SIZE;
66+
textY = frameY + GameManager.TILE_SIZE * 9;
67+
graphics2D.drawString("Back", textX, textY);
68+
if (commandNum == 0) {
69+
showChooseCursor(textX, textY, 25);
70+
if (gameManager.keyHandler.isEnterPressed) {
71+
subState = 0;
72+
commandNum = 3;
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)