|
| 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 | +} |
0 commit comments