Skip to content

Commit 9b8f88b

Browse files
committed
Added new mobs, game design changes, big improvement in code structure for small entities.
1 parent a658c1e commit 9b8f88b

Some content is hidden

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

41 files changed

+215
-21
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public class Entity extends Tools {
136136
public String knockBackDirection;
137137
public Entity linkedEntity;
138138

139+
public final Random random = new Random();
140+
public final String[] directions = {"up", "down", "left", "right"};
141+
139142
public Entity(GameManager gameManager) {
140143
this.gameManager = gameManager;
141144
}
@@ -343,6 +346,10 @@ public boolean inCamera() {
343346
}
344347

345348
public void draw(Graphics2D graphics2D) {
349+
draw(graphics2D, true);
350+
}
351+
352+
public void draw(Graphics2D graphics2D, boolean bigImage) {
346353
//actual coords to draw the stuff on game screen
347354
int screenX = getScreenX();
348355
int screenY = getScreenY();
@@ -362,7 +369,8 @@ public void draw(Graphics2D graphics2D) {
362369
if (spriteNum == 3) image = up3;
363370
}
364371
if (attacking) {
365-
tempScreenY = screenY - up.getHeight();
372+
if (bigImage)
373+
tempScreenY = screenY - up.getHeight();
366374
if (spriteNum == 0) image = attackUp;
367375
if (spriteNum == 1) image = attackUp1;
368376
if (spriteNum == 2) image = attackUp2;
@@ -391,7 +399,8 @@ public void draw(Graphics2D graphics2D) {
391399
if (spriteNum == 3) image = left3;
392400
}
393401
if (attacking) {
394-
tempScreenX = screenX - left.getWidth();
402+
if (bigImage)
403+
tempScreenX = screenX - left.getWidth();
395404
if (spriteNum == 0) image = attackLeft;
396405
if (spriteNum == 1) image = attackLeft1;
397406
if (spriteNum == 2) image = attackLeft2;
@@ -429,7 +438,7 @@ public void draw(Graphics2D graphics2D) {
429438
}
430439
}
431440

432-
private void dieAnimation(Graphics2D graphics2D) {
441+
protected void dieAnimation(Graphics2D graphics2D) {
433442
dieCounter++;
434443

435444
int sec = 5;
@@ -451,7 +460,7 @@ private void dieAnimation(Graphics2D graphics2D) {
451460
}
452461

453462

454-
private void changeAlfa(Graphics2D graphics2D, float alfaValue) {
463+
protected void changeAlfa(Graphics2D graphics2D, float alfaValue) {
455464
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alfaValue));
456465
}
457466

@@ -760,6 +769,10 @@ public void resetCounters() {
760769
offBalanceCounter = 0;
761770
}
762771

772+
public String getRandomDirection() {
773+
return directions[random.nextInt(directions.length)];
774+
}
775+
763776
public int getScreenX() {
764777
return worldX - gameManager.player.worldX + gameManager.player.screenX;
765778
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.khomsi.game.entity.mobs;
2+
3+
public enum Color {
4+
RED, BROWN, BLUE
5+
}

src/main/java/com/khomsi/game/entity/mobs/MobChestMimic.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.khomsi.game.entity.Entity;
44
import com.khomsi.game.main.GameManager;
55

6+
import java.awt.*;
7+
68
public class MobChestMimic extends Entity {
79
public MobChestMimic(GameManager gameManager) {
810
super(gameManager);
@@ -30,12 +32,15 @@ private void setDefaultValues() {
3032
solidArea.height = 20;
3133
solidAreaDefaultX = solidArea.x;
3234
solidAreaDefaultY = solidArea.y;
33-
attackArea.width = 40;
35+
attackArea.width = 0;
3436
attackArea.height = 40;
3537
motion1Duration = 15;
3638
motion2Duration = 20;
3739
}
38-
40+
@Override
41+
public void draw(Graphics2D graphics2D) {
42+
draw(graphics2D, false);
43+
}
3944
private void getImage() {
4045
up = setup("/mobs/mimics/mimic_chest_down");
4146
up1 = setup("/mobs/mimics/mimic_chest_down");
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.khomsi.game.entity.mobs;
2+
3+
import com.khomsi.game.entity.Entity;
4+
import com.khomsi.game.main.GameManager;
5+
6+
import java.awt.*;
7+
import java.util.Random;
8+
9+
public class MobMushroom extends Entity {
10+
public MobMushroom(GameManager gameManager, Color color) {
11+
super(gameManager);
12+
setDefaultValues();
13+
getImage(color);
14+
getAttackImage(color);
15+
}
16+
17+
private void setDefaultValues() {
18+
name = "Mushroom";
19+
type = TYPE_MOB;
20+
defaultSpeed = 1;
21+
speed = defaultSpeed;
22+
direction = getRandomDirection();
23+
maxHp = 1;
24+
hp = maxHp;
25+
attack = 6;
26+
defense = 1;
27+
xp = 2;
28+
29+
//Boundaries
30+
solidArea.x = 8;
31+
solidArea.y = 16;
32+
//boundaries of npc
33+
solidArea.width = 31;
34+
solidArea.height = 32;
35+
solidAreaDefaultX = solidArea.x;
36+
solidAreaDefaultY = solidArea.y;
37+
38+
attackArea.width = 44;
39+
attackArea.height = 44;
40+
motion1Duration = 15;
41+
motion2Duration = 20;
42+
}
43+
44+
@Override
45+
public void draw(Graphics2D graphics2D) {
46+
draw(graphics2D, false);
47+
}
48+
49+
private String getColorPath(Color color) {
50+
String basePath = "/mobs/mushroom/";
51+
switch (color) {
52+
// case RED -> basePath += "red/crab_red_";
53+
case BROWN -> basePath += "brown/mushroom_brown_";
54+
// case BLUE -> basePath += "blue/crab_blue_";
55+
default -> throw new IllegalArgumentException("Invalid color: " + color);
56+
}
57+
return basePath;
58+
}
59+
60+
public void getImage(Color color) {
61+
String basePath = getColorPath(color);
62+
// Loop from 0 to 15 to set up the images
63+
for (int i = 0; i < 16; i++) {
64+
String filename = basePath + String.format("%02d", i);
65+
switch (i) {
66+
case 0 -> down = setup(filename);
67+
case 1 -> down1 = setup(filename);
68+
case 2 -> down2 = setup(filename);
69+
case 3 -> down3 = setup(filename);
70+
71+
case 4 -> left = setup(filename);
72+
case 5 -> left1 = setup(filename);
73+
case 6 -> left2 = setup(filename);
74+
case 7 -> left3 = setup(filename);
75+
76+
case 8 -> up = setup(filename);
77+
case 9 -> up1 = setup(filename);
78+
case 10 -> up2 = setup(filename);
79+
case 11 -> up3 = setup(filename);
80+
81+
case 12 -> right = setup(filename);
82+
case 13 -> right1 = setup(filename);
83+
case 14 -> right2 = setup(filename);
84+
case 15 -> right3 = setup(filename);
85+
}
86+
}
87+
}
88+
89+
public void getAttackImage(Color color) {
90+
String basePath = getColorPath(color);
91+
// Loop to set up the images
92+
for (int i = 16; i < 32; i++) {
93+
String filename = basePath + String.format("%02d", i);
94+
switch (i) {
95+
case 16 -> attackDown = setup(filename);
96+
case 17 -> attackDown1 = setup(filename);
97+
case 18 -> attackDown2 = setup(filename);
98+
case 19 -> attackDown3 = setup(filename);
99+
100+
case 20 -> attackLeft = setup(filename);
101+
case 21 -> attackLeft1 = setup(filename);
102+
case 22 -> attackLeft2 = setup(filename);
103+
case 23 -> attackLeft3 = setup(filename);
104+
105+
case 24 -> attackUp = setup(filename);
106+
case 25 -> attackUp1 = setup(filename);
107+
case 26 -> attackUp2 = setup(filename);
108+
case 27 -> attackUp3 = setup(filename);
109+
110+
case 28 -> attackRight = setup(filename);
111+
case 29 -> attackRight1 = setup(filename);
112+
case 30 -> attackRight2 = setup(filename);
113+
case 31 -> attackRight3 = setup(filename);
114+
}
115+
}
116+
}
117+
118+
@Override
119+
public void setAction() {
120+
if (onPath) {
121+
//Check if it stops chasing
122+
checkStopChasing(gameManager.player, 3, 70);
123+
//Search the direction to go
124+
searchPath(getGoalCol(gameManager.player), getGoalRow(gameManager.player), false);
125+
} else {
126+
//Check if it starts chasing
127+
checkStartChasing(gameManager.player, 3, 70);
128+
//Get random direction
129+
getRandomDirection(120);
130+
}
131+
//Check if attacking
132+
if (!attacking) {
133+
checkAttacking(40, GameManager.TILE_SIZE * 2, GameManager.TILE_SIZE * 2);
134+
}
135+
}
136+
137+
@Override
138+
public void damageReaction() {
139+
lockCounter = 0;
140+
int chance = random.nextInt(100) + 1;
141+
if (chance <= 50)
142+
onPath = true;
143+
else
144+
//Monster runs away
145+
direction = gameManager.player.direction;
146+
}
147+
}

src/main/java/com/khomsi/game/entity/mobs/MobSlime.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private void setDefaultValues() {
2121
type = TYPE_MOB;
2222
defaultSpeed = 1;
2323
speed = defaultSpeed;
24-
direction = "down";
24+
direction = getRandomDirection();
2525
//4 = 2 hearts
2626
maxHp = 4;
2727
hp = maxHp;
@@ -88,7 +88,7 @@ public void damageReaction() {
8888

8989
@Override
9090
public void checkDrop() {
91-
int drop = new Random().nextInt(100) + 1;
91+
int drop = random.nextInt(100) + 1;
9292

9393
//Set the mob's drop, 50% chance of coin, 30 of heart and mana
9494
if (drop < 50) dropItem(new CoinBObject(gameManager));

src/main/java/com/khomsi/game/entity/npc/beach/NpcCrabs.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
import com.khomsi.game.main.GameManager;
55

66
import java.awt.*;
7-
import java.util.Random;
87

98
public class NpcCrabs extends Entity {
10-
private final Random random = new Random();
11-
private final String[] directions = {"up", "down", "left", "right"};
129

1310
public NpcCrabs(GameManager gameManager, Color color) {
1411
super(gameManager);
@@ -105,8 +102,4 @@ public void speak() {
105102
dialogueSet = 0;
106103
}
107104
}
108-
109-
private String getRandomDirection() {
110-
return directions[random.nextInt(directions.length)];
111-
}
112105
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ public void setDefaultValues() {
8686

8787
public void setDefaultPosition() {
8888
//player position of player
89-
/* gameManager.currentMap = 0;
89+
gameManager.currentMap = 0;
9090
worldX = GameManager.TILE_SIZE * 29;
9191
worldY = GameManager.TILE_SIZE * 92;
92-
direction = "down";*/
92+
direction = "down";
9393
/* gameManager.currentMap = 0;
9494
worldX = GameManager.TILE_SIZE * 15;
9595
worldY = GameManager.TILE_SIZE * 27;
9696
direction = "down";*/
9797

98-
gameManager.currentMap = 0;
98+
/* gameManager.currentMap = 0;
9999
worldX = GameManager.TILE_SIZE * 75;
100100
worldY = GameManager.TILE_SIZE * 56;
101-
direction = "down";
101+
direction = "down";*/
102102
}
103103

104104
public void restoreStatus() {

src/main/java/com/khomsi/game/objects/editor/placement/InteractiveTilePlacement.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ public void placeInteractiveTilesOnMap0() {
5050
gameManager.interactTile[mapNum][index] = new Bush(gameManager, 51, 68);
5151
index++;
5252
gameManager.interactTile[mapNum][index] = new Bush(gameManager, 52, 66);
53-
54-
53+
index++;
54+
gameManager.interactTile[mapNum][index] = new Bush(gameManager, 35, 87);
55+
index++;
56+
gameManager.interactTile[mapNum][index] = new Bush(gameManager, 45, 84);
57+
index++;
58+
gameManager.interactTile[mapNum][index] = new Bush(gameManager, 45, 85);
5559
setCastleSwitches(index, mapNum);
5660
}
5761

src/main/java/com/khomsi/game/objects/editor/placement/MobPlacement.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.khomsi.game.objects.editor.placement;
22

33
import com.khomsi.game.entity.mobs.MobChestMimic;
4+
import com.khomsi.game.entity.mobs.MobDungeonBoss;
5+
import com.khomsi.game.entity.mobs.MobMushroom;
46
import com.khomsi.game.main.GameManager;
57

8+
import static com.khomsi.game.entity.mobs.Color.BROWN;
69
import static com.khomsi.game.objects.editor.PlaceObjects.MAP_MAIN;
710

811
public class MobPlacement {
@@ -11,14 +14,38 @@ public class MobPlacement {
1114
public MobPlacement(GameManager gameManager) {
1215
this.gameManager = gameManager;
1316
}
17+
1418
public void setMobsOnMap0() {
1519
int index = 0;
1620
int mapNum = MAP_MAIN;
1721
gameManager.mobs[mapNum][index] = new MobChestMimic(gameManager);
1822
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 15;
1923
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 84;
2024
index++;
25+
gameManager.mobs[mapNum][index] = new MobMushroom(gameManager, BROWN);
26+
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 32;
27+
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 87;
28+
index++;
29+
gameManager.mobs[mapNum][index] = new MobMushroom(gameManager, BROWN);
30+
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 16;
31+
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 90;
32+
index++;
33+
gameManager.mobs[mapNum][index] = new MobMushroom(gameManager, BROWN);
34+
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 23;
35+
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 83;
36+
index++;
37+
38+
gameManager.mobs[mapNum][index] = new MobMushroom(gameManager, BROWN);
39+
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 46;
40+
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 89;
41+
index++;
42+
43+
gameManager.mobs[mapNum][index] = new MobMushroom(gameManager, BROWN);
44+
gameManager.mobs[mapNum][index].worldX = GameManager.TILE_SIZE * 40;
45+
gameManager.mobs[mapNum][index].worldY = GameManager.TILE_SIZE * 84;
46+
index++;
2147
}
48+
2249
public void setMobsOnDungeonMap1() {
2350
int index = 0;
2451
int mapNum = 3;

0 commit comments

Comments
 (0)