|
| 1 | +package com.dinosaur.dinosaurexploder.components; |
| 2 | + |
| 3 | +import com.almasb.fxgl.core.math.Vec2; |
| 4 | +import com.almasb.fxgl.entity.SpawnData; |
| 5 | +import com.almasb.fxgl.entity.component.Component; |
| 6 | +import com.dinosaur.dinosaurexploder.constants.GameConstants; |
| 7 | +import com.dinosaur.dinosaurexploder.interfaces.Dinosaur; |
| 8 | +import com.dinosaur.dinosaurexploder.utils.AudioManager; |
| 9 | +import com.dinosaur.dinosaurexploder.utils.GameTimer; |
| 10 | +import com.dinosaur.dinosaurexploder.utils.LevelManager; |
| 11 | +import com.dinosaur.dinosaurexploder.view.DinosaurGUI; |
| 12 | +import javafx.geometry.Point2D; |
| 13 | + |
| 14 | +import static com.almasb.fxgl.dsl.FXGLForKtKt.random; |
| 15 | +import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn; |
| 16 | +import static java.lang.Math.atan; |
| 17 | + |
| 18 | +/** |
| 19 | + * Summary : |
| 20 | + * This class extends Component and Implements the Dinosaur Classes |
| 21 | + * and Handles Updating the Dino |
| 22 | + * This Dino is an orange Boss who appears every ten levels. |
| 23 | + * He doesn't shoot but follows the player to ram him. |
| 24 | + * he has three times the lives than the current level. |
| 25 | + */ |
| 26 | +public class OrangeDinoComponent extends Component implements Dinosaur { |
| 27 | + double movementSpeed = 1.5; |
| 28 | + private int lives = 10; |
| 29 | + private final GameTimer gameTimer; |
| 30 | + private final PlayerComponent playerComponent; |
| 31 | + Point2D playerPosition; |
| 32 | + |
| 33 | + public OrangeDinoComponent(GameTimer gameTimer, PlayerComponent playerComponent) { |
| 34 | + this.gameTimer = gameTimer; |
| 35 | + this.playerComponent = playerComponent; |
| 36 | + } |
| 37 | + |
| 38 | + private boolean isPaused = false; |
| 39 | + |
| 40 | + |
| 41 | + boolean firstTime = true; |
| 42 | + private LevelManager levelManager; |
| 43 | + |
| 44 | + public void setLevelManager(LevelManager levelManager) { |
| 45 | + this.levelManager = levelManager; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public int getLives() { |
| 50 | + return lives; |
| 51 | + } |
| 52 | + |
| 53 | + public void setLives(int lives) { |
| 54 | + this.lives = lives; |
| 55 | + } |
| 56 | + |
| 57 | + public double getMovementSpeed() { |
| 58 | + return movementSpeed; |
| 59 | + } |
| 60 | + |
| 61 | + public void setMovementSpeed(double movementSpeed) { |
| 62 | + this.movementSpeed = movementSpeed; |
| 63 | + } |
| 64 | + |
| 65 | + public void setPaused(boolean paused) { |
| 66 | + isPaused = paused; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onAdded() { |
| 73 | + //Get the current enemy speed from the level manager |
| 74 | + //levelManager = FXGL.geto("levelManager"); |
| 75 | + firstTime = true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Summary : |
| 80 | + * This method runs for every frame like a continues flow , without any stop until we put stop to it. |
| 81 | + * Parameters : |
| 82 | + * double ptf |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void onUpdate(double ptf) { |
| 86 | + playerPosition = playerComponent.getEntity().getPosition(); |
| 87 | + |
| 88 | + if (isPaused) return; |
| 89 | + |
| 90 | + if (firstTime) { |
| 91 | + System.out.println("level: " + levelManager.getCurrentLevel()); |
| 92 | + movementSpeed = levelManager.getEnemySpeed()/2; |
| 93 | + lives = levelManager.getCurrentLevel() * 3; |
| 94 | + firstTime = false; |
| 95 | + } |
| 96 | + |
| 97 | + if (entity.getX() < playerPosition.getX()) { |
| 98 | + moveRight(); |
| 99 | + } |
| 100 | + if (entity.getX() > playerPosition.getX()) { |
| 101 | + moveLeft(); |
| 102 | + } |
| 103 | + if (entity.getY() < playerPosition.getY()) { |
| 104 | + moveDown(); |
| 105 | + } |
| 106 | + if (entity.getY() > playerPosition.getY()) { |
| 107 | + moveUp(); |
| 108 | + } |
| 109 | + rotateDino(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * This method calculates the rotation, |
| 114 | + * that the dino needs to always look in the direction of the player |
| 115 | + * and rotates it. |
| 116 | + */ |
| 117 | + void rotateDino() { |
| 118 | + double oppositeSide; |
| 119 | + double adjacentSide; |
| 120 | + |
| 121 | + //dino right from player |
| 122 | + if (entity.getX() > playerPosition.getX()) { |
| 123 | + oppositeSide = entity.getX()-playerPosition.getX(); |
| 124 | + } else { |
| 125 | + oppositeSide = playerPosition.getX() - entity.getX(); |
| 126 | + } |
| 127 | + //dino under player |
| 128 | + if (entity.getY() > playerPosition.getY()) { |
| 129 | + adjacentSide = entity.getY()-playerPosition.getY(); |
| 130 | + } else { |
| 131 | + adjacentSide = playerPosition.getY() - entity.getY(); |
| 132 | + } |
| 133 | + |
| 134 | + double rotation = Math.toDegrees(atan(oppositeSide / adjacentSide)); |
| 135 | + |
| 136 | + // dino left and under from player |
| 137 | + if(entity.getX() < playerPosition.getX() && entity.getY() > playerPosition.getY()){ |
| 138 | + entity.setRotation(rotation-180); |
| 139 | + //dino right and under player |
| 140 | + } else if(entity.getX() > playerPosition.getX() && entity.getY() > playerPosition.getY()){ |
| 141 | + rotation = 90-rotation; |
| 142 | + entity.setRotation(rotation+90); |
| 143 | + //dino right and above the player |
| 144 | + } else if(entity.getX() > playerPosition.getX() && entity.getY() < playerPosition.getY()){ |
| 145 | + entity.setRotation(rotation); |
| 146 | + //dino left and above player |
| 147 | + } else if(entity.getX() < playerPosition.getX() && entity.getY() < playerPosition.getY()){ |
| 148 | + rotation = 90-rotation; |
| 149 | + entity.setRotation(rotation-90); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Summary : |
| 155 | + * This handles with the shooting of the dinosaur and spawning of the new bullet |
| 156 | + */ |
| 157 | + @Override |
| 158 | + public void shoot() { |
| 159 | + |
| 160 | + AudioManager.getInstance().playSound(GameConstants.SHOOT_SOUND); |
| 161 | + |
| 162 | + Point2D center = entity.getCenter(); |
| 163 | + Vec2 direction = Vec2.fromAngle(entity.getRotation() + 90 + random(-45, 45)); |
| 164 | + spawn("basicEnemyProjectile", |
| 165 | + new SpawnData(center.getX() + 50 + 3, center.getY()) |
| 166 | + .put("direction", direction.toPoint2D()) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Summary : |
| 172 | + * This method damages the orange dino |
| 173 | + * |
| 174 | + * @param damage |
| 175 | + */ |
| 176 | + public void damage(int damage) { |
| 177 | + lives -= damage; |
| 178 | + } |
| 179 | + |
| 180 | + public void moveUp() { |
| 181 | + if (entity.getY() < 0) { |
| 182 | + System.out.println("Out of bounds"); |
| 183 | + return; |
| 184 | + } |
| 185 | + entity.translateY(-movementSpeed); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Summary : |
| 190 | + * This method is overriding the superclass method to limit the downSide |
| 191 | + * movement. |
| 192 | + */ |
| 193 | + public void moveDown() { |
| 194 | + if (!(entity.getY() < DinosaurGUI.HEIGHT - entity.getHeight())) { |
| 195 | + System.out.println("Out of bounds"); |
| 196 | + return; |
| 197 | + } |
| 198 | + entity.translateY(movementSpeed); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Summary : |
| 203 | + * This method is overriding the superclass method to limit the rightSide |
| 204 | + * movement. |
| 205 | + */ |
| 206 | + public void moveRight() { |
| 207 | + if (!(entity.getX() < DinosaurGUI.WIDTH - entity.getWidth())) { |
| 208 | + System.out.println("Out of bounds"); |
| 209 | + return; |
| 210 | + } |
| 211 | + entity.translateX(movementSpeed); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Summary : |
| 216 | + * This method is overriding the superclass method to limit the leftSide |
| 217 | + * movement. |
| 218 | + */ |
| 219 | + public void moveLeft() { |
| 220 | + if (entity.getX() < 0) { |
| 221 | + System.out.println("Out of bounds"); |
| 222 | + return; |
| 223 | + } |
| 224 | + entity.translateX(-movementSpeed); |
| 225 | + |
| 226 | + } |
| 227 | +} |
| 228 | + |
0 commit comments