Skip to content

Commit 7a999dd

Browse files
authored
Merge pull request #210 from Johann4002herti/main
Feature: added another Dino Boss,
2 parents 6b2a3c3 + 3a43fdf commit 7a999dd

File tree

11 files changed

+339
-25
lines changed

11 files changed

+339
-25
lines changed

src/main/java/com/dinosaur/dinosaurexploder/components/GreenDinoComponent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public class GreenDinoComponent extends Component implements Dinosaur {
2121
double verticalSpeed = 1.5;
2222
private final LocalTimer timer = FXGL.newLocalTimer();
2323
private boolean isPaused = false;
24-
24+
private int lives = 1;
25+
26+
public int getLives(){
27+
return lives;
28+
}
2529

2630
public void setPaused(boolean paused) {
2731
isPaused = paused;
@@ -69,4 +73,8 @@ public void shoot() {
6973
.put("direction", direction.toPoint2D() )
7074
);
7175
}
76+
77+
public void damage(int damage) {
78+
lives -= damage;
79+
}
7280
}

src/main/java/com/dinosaur/dinosaurexploder/components/HealthbarComponent.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dinosaur.dinosaurexploder.components;
22

33
import com.almasb.fxgl.entity.component.Component;
4+
import com.dinosaur.dinosaurexploder.interfaces.Dinosaur;
45
import javafx.scene.paint.Color;
56
import javafx.scene.shape.Rectangle;
67

@@ -11,18 +12,18 @@
1112
public class HealthbarComponent extends Component {
1213

1314
private int maxHealth;
14-
private RedDinoComponent redDinoComponent;
15+
private Dinosaur dinoComponent;
1516
boolean firstTime = true;
1617
double maxWidht;
1718

1819
private Rectangle healthbar;
1920

20-
public RedDinoComponent getRedDinoComponent() {
21-
return redDinoComponent;
21+
public Dinosaur getDinoComponent() {
22+
return dinoComponent;
2223
}
2324

24-
public void setRedDinoComponent(RedDinoComponent redDinoComponent) {
25-
this.redDinoComponent = redDinoComponent;
25+
public void setDinoComponent(Dinosaur dinoComponent) {
26+
this.dinoComponent = dinoComponent;
2627
}
2728

2829
@Override
@@ -40,12 +41,12 @@ public void onAdded() {
4041
* This method updates the healthbar and gets called when the boss Dino got hit
4142
*/
4243
public void updateBar() {
43-
// the first time the Dino got hit, the maxhealth is set +1 because the redDinoComponent already got hit once
44+
// the first time the Dino got hit, the maxhealth is set +1 because the dinoComponent already got hit once
4445
if(firstTime){
45-
maxHealth= redDinoComponent.getLives()+1;
46+
maxHealth= dinoComponent.getLives()+1;
4647
firstTime = false;
4748
}
48-
int currentHealth = redDinoComponent.getLives();
49+
int currentHealth = dinoComponent.getLives();
4950
double percentage = ((double) currentHealth / maxHealth);
5051
double width = (percentage * maxWidht);
5152
healthbar.setWidth(width);
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+

src/main/java/com/dinosaur/dinosaurexploder/constants/EntityType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* This handles with all the entities in the game like lives, player projectile etc.
55
*/
66
public enum EntityType {
7-
PLAYER, GREEN_DINO,RED_DINO, PROJECTILE, ENEMY_PROJECTILE, SCORE, LIFE, BOMB, COIN, HEART, LEVEL, HEALTHBAR, LEVEL_PROGRESS_BAR
7+
PLAYER, GREEN_DINO, RED_DINO, ORANGE_DINO, PROJECTILE, ENEMY_PROJECTILE, SCORE, LIFE, BOMB, COIN, HEART, LEVEL, HEALTHBAR, LEVEL_PROGRESS_BAR
88
}

src/main/java/com/dinosaur/dinosaurexploder/constants/GameConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class GameConstants {
1919
public static final String GREEN_DINO_IMAGE_FILE = "greenDino.png";
2020
public static final String RED_DINO_IMAGE_PATH = "assets/textures/redDino.png";
2121
public static final String RED_DINO_IMAGE_FILE = "redDino.png";
22+
public static final String ORANGE_DINO_IMAGE_PATH = "assets/textures/orangeDino.png";
23+
public static final String ORANGE_DINO_IMAGE_FILE = "orangeDino.png";
2224
public static final String COIN_IMAGE_PATH = "assets/textures/coin.png";
2325
public static final String COIN_IMAGE_FILE = "coin.png";
2426
public static final String HEART_IMAGE_PATH = "assets/textures/life.png";

src/main/java/com/dinosaur/dinosaurexploder/controller/BossSpawner.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import com.almasb.fxgl.entity.Entity;
44
import com.dinosaur.dinosaurexploder.components.HealthbarComponent;
5+
import com.dinosaur.dinosaurexploder.components.OrangeDinoComponent;
56
import com.dinosaur.dinosaurexploder.components.RedDinoComponent;
67
import com.dinosaur.dinosaurexploder.model.Settings;
78
import com.dinosaur.dinosaurexploder.utils.LevelManager;
8-
import com.dinosaur.dinosaurexploder.utils.AudioManager;
9+
910
import static com.almasb.fxgl.dsl.FXGL.getAppCenter;
1011
import static com.almasb.fxgl.dsl.FXGL.getAppWidth;
1112
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
@@ -16,19 +17,29 @@ public class BossSpawner {
1617
private final LevelManager levelManager;
1718
private Entity healthBar;
1819
private Entity redDino;
20+
private Entity orangeDino;
1921

2022
public BossSpawner(Settings settings, LevelManager levelManager){
2123
this.settings = settings;
2224
this.levelManager = levelManager;
2325
}
2426

25-
public void spawnNewBoss(){
26-
redDino = spawn("redDino", getAppCenter().getX() - 45, 50);
27-
28-
redDino.getComponent(RedDinoComponent.class).setLevelManager(levelManager);
27+
public void spawnNewBoss(String name){
28+
if(name.equals("red")){
29+
redDino = spawn("redDino", getAppCenter().getX() - 45, 50);
30+
31+
redDino.getComponent(RedDinoComponent.class).setLevelManager(levelManager);
32+
33+
healthBar = spawn("healthBar", getAppWidth()-215, 15);
34+
healthBar.getComponent(HealthbarComponent.class).setDinoComponent(redDino.getComponent(RedDinoComponent.class));
35+
} else if(name.equals("orange")){
36+
orangeDino = spawn("orangeDino", getAppCenter().getX() - 45, 50);
2937

30-
healthBar = spawn("healthBar", getAppWidth()-215, 15);
31-
healthBar.getComponent(HealthbarComponent.class).setRedDinoComponent(redDino.getComponent(RedDinoComponent.class));
38+
orangeDino.getComponent(OrangeDinoComponent.class).setLevelManager(levelManager);
39+
40+
healthBar = spawn("healthBar", getAppWidth()-215, 15);
41+
healthBar.getComponent(HealthbarComponent.class).setDinoComponent(orangeDino.getComponent(OrangeDinoComponent.class));
42+
}
3243
}
3344

3445
public void updateHealthBar(){
@@ -37,6 +48,12 @@ public void updateHealthBar(){
3748

3849
public void removeBossEntities(){
3950
healthBar.removeFromWorld();
40-
redDino.removeFromWorld();
51+
if(redDino != null){
52+
redDino.removeFromWorld();
53+
}
54+
if(orangeDino != null){
55+
orangeDino.removeFromWorld();
56+
}
57+
4158
}
4259
}

0 commit comments

Comments
 (0)