Skip to content

Commit 678911a

Browse files
committed
Added level progress bar. Now LevelManager creates before Game Entities.
1 parent 8c02647 commit 678911a

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.dinosaur.dinosaurexploder.components;
2+
3+
import com.almasb.fxgl.entity.component.Component;
4+
import com.dinosaur.dinosaurexploder.utils.LevelManager;
5+
import javafx.scene.shape.Rectangle;
6+
7+
/**
8+
* Summary:
9+
* This class extends Component and handles filling the level progress bar
10+
*/
11+
public class LevelProgressBarComponent extends Component {
12+
private final Rectangle fill;
13+
private LevelManager levelManager;
14+
15+
public LevelProgressBarComponent(Rectangle fill, LevelManager levelManager) {
16+
this.fill = fill;
17+
this.levelManager = levelManager;
18+
}
19+
20+
@Override
21+
public void onUpdate(double tpf) {
22+
float progress = levelManager.getLevelProgress();
23+
fill.setWidth(148 * progress);
24+
}
25+
}

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
7+
PLAYER, GREEN_DINO,RED_DINO, PROJECTILE, ENEMY_PROJECTILE, SCORE, LIFE, BOMB, COIN, HEART, LEVEL, HEALTHBAR, LEVEL_PROGRESS_BAR
88
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.almasb.fxgl.dsl.FXGL;
44
import com.almasb.fxgl.entity.Entity;
5+
import com.almasb.fxgl.entity.SpawnData;
56
import com.almasb.fxgl.time.TimerAction;
67
import com.dinosaur.dinosaurexploder.components.*;
78
import com.dinosaur.dinosaurexploder.constants.EntityType;
@@ -34,6 +35,7 @@ public class DinosaurController {
3435
private CollectedCoinsComponent collectedCoinsComponent;
3536
private Entity levelDisplay;
3637
private Entity life;
38+
private Entity levelProgressBar;
3739
private LevelManager levelManager;
3840
private TimerAction enemySpawnTimer;
3941
private boolean isSpawningPaused = false;
@@ -84,8 +86,8 @@ public void initInput() {
8486
}
8587

8688
public void initGame() {
87-
initGameEntities();
8889
levelManager = new LevelManager();
90+
initGameEntities();
8991
bossSpawner = new BossSpawner(settings,levelManager);
9092
CoinSpawner coinSpawner = new CoinSpawner(10, 1.0);
9193

@@ -110,6 +112,7 @@ private void initGameEntities(){
110112
Entity coin = spawn("Coins", getAppCenter().getX() - 260, getAppCenter().getY() - 235);
111113
collectedCoinsComponent = coin.getComponent(CollectedCoinsComponent.class);
112114
bomb.addComponent(new BombComponent());
115+
levelProgressBar = spawn("levelProgressBar", new SpawnData(getAppCenter().getX() - 170, getAppCenter().getY() + 340).put("levelManager", levelManager));
113116
}
114117

115118
/**
@@ -165,6 +168,11 @@ private void resumeEnemySpawning(){
165168
* and shows a message when the level is changed
166169
*/
167170
private void showLevelMessage(){
171+
// Hide the progress bar for boss levels
172+
if(levelManager.getCurrentLevel() % 5 == 0) {
173+
levelProgressBar.setVisible(false);
174+
}
175+
168176
//Pause game elements during level transition
169177
FXGL.getGameWorld().getEntitiesByType(EntityType.GREEN_DINO).forEach(e -> {
170178
if(e.hasComponent(GreenDinoComponent.class)) {
@@ -188,6 +196,10 @@ private void showLevelMessage(){
188196

189197
// Resume gameplay after a delay
190198
runOnce(() -> {
199+
if(levelManager.getCurrentLevel() % 5 != 0) {
200+
levelProgressBar.setVisible(true);
201+
}
202+
191203
getGameScene().removeUINode(levelText);
192204
updateLevelDisplay();
193205

src/main/java/com/dinosaur/dinosaurexploder/model/GameEntityFactory.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import com.dinosaur.dinosaurexploder.constants.EntityType;
2020
import com.dinosaur.dinosaurexploder.constants.GameConstants;
21+
import com.dinosaur.dinosaurexploder.utils.LevelManager;
2122
import javafx.geometry.Orientation;
2223
import javafx.geometry.Point2D;
24+
import javafx.scene.Group;
2325
import javafx.scene.image.Image;
2426
import javafx.scene.image.ImageView;
2527
import javafx.scene.paint.Color;
@@ -279,6 +281,30 @@ public Entity newLevel(SpawnData data) {
279281
.build();
280282
}
281283

284+
/**
285+
* Summary :
286+
* Creates level progress bar
287+
*/
288+
@Spawns("levelProgressBar")
289+
public Entity newLevelProgressBar(SpawnData data) {
290+
LevelManager levelManager = data.get("levelManager");
291+
292+
Rectangle background = new Rectangle(150, 10, Color.DARKGRAY);
293+
background.setStroke(Color.GRAY);
294+
background.setStrokeWidth(1);
295+
296+
Rectangle filled = new Rectangle(0, 8, Color.LIMEGREEN);
297+
filled.setLayoutX(1);
298+
filled.setLayoutY(1);
299+
300+
Group progressBar = new Group(background, filled);
301+
302+
return entityBuilderBase(data, EntityType.LEVEL_PROGRESS_BAR)
303+
.view(progressBar)
304+
.with(new LevelProgressBarComponent(filled, levelManager))
305+
.build();
306+
}
307+
282308
/**
283309
* Summary :
284310
* Reusable part of every entity

src/main/java/com/dinosaur/dinosaurexploder/utils/LevelManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class LevelManager {
99
private int currentLevel = 1;
10-
private int enemiesToDefeat = 5;
10+
private int enemiesToDefeat = 5;
1111
private int defeatedEnemies = 0;
1212
private double enemySpawnRate = 0.75;
1313
private double enemySpeed = 1.5;
@@ -23,6 +23,10 @@ public double getEnemySpeed() {
2323
return enemySpeed;
2424
}
2525

26+
public float getLevelProgress() {
27+
return (float) defeatedEnemies / enemiesToDefeat;
28+
}
29+
2630
public void incrementDefeatedEnemies() {
2731
defeatedEnemies++;
2832
}

0 commit comments

Comments
 (0)