forked from jvondermarck/dinosaur-exploder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDinosaurController.java
134 lines (118 loc) · 5.13 KB
/
DinosaurController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.dinosaur.dinosaurexploder.controller;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.dinosaur.dinosaurexploder.model.*;
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
import javafx.scene.input.KeyCode;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import static com.almasb.fxgl.dsl.FXGL.*;
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
import static javafx.util.Duration.seconds;
/**
* Summary :
* The Factory handles the Dinosaur , player controls and collision detection of all entities in the game
*/
public class DinosaurController {
private Entity player;
private Entity score;
private Entity life;
private int lives = 3;
private MediaPlayer inGameSound;
/**
* Summary :
* Detecting the player damage to decrease the lives and checking if the game is over
*/
public void damagePlayer() {
lives = life.getComponent(LifeComponent.class).decreaseLife(1);
var flash = new Rectangle(DinosaurGUI.WIDTH, DinosaurGUI.HEIGHT, Color.rgb(190, 10, 15, 0.5));
getGameScene().addUINode(flash);
runOnce(() -> getGameScene().removeUINode(flash), seconds(0.5));
if (lives <= 0) {
// Added extra line of code to sync the lives counter after death
// All hearts disappear after death
life.getComponent(LifeComponent.class).onUpdate(lives);
System.out.println("Game Over!");
gameOver();
}
else{
System.out.printf("%d lives remaining ! ", lives);
}
}
/**
* Summary :
* To move the space shuttle in forward , backward , right , left directions
*/
public void initInput() {
onKey(KeyCode.UP, () -> player.getComponent(PlayerComponent.class).moveUp());
onKey(KeyCode.DOWN, () -> player.getComponent(PlayerComponent.class).moveDown());
onKey(KeyCode.LEFT, () -> player.getComponent(PlayerComponent.class).moveLeft());
onKey(KeyCode.RIGHT, () -> player.getComponent(PlayerComponent.class).moveRight());
onKeyDown(KeyCode.SPACE,() -> player.getComponent(PlayerComponent.class).shoot());
onKey(KeyCode.W, () -> player.getComponent(PlayerComponent.class).moveUp());
onKey(KeyCode.S, () -> player.getComponent(PlayerComponent.class).moveDown());
onKey(KeyCode.A, () -> player.getComponent(PlayerComponent.class).moveLeft());
onKey(KeyCode.D, () -> player.getComponent(PlayerComponent.class).moveRight());
}
/**
* Summary :
* Game Background , Spawning Dinos , Limiting Player movements are Described in the below Method
*/
public void initGame() {
getGameWorld().addEntityFactory(new GameEntityFactory());
spawn("background", 0, 0);
player = spawn("player", getAppCenter().getX() - 45, getAppHeight()-200);
SoundController.getInstance().playInGameSound(GameConstants.BACKGROUND_SOUND, 1.0);
/*
* At each second that passes, we have 2 out of 3 chances of spawning a green
* dinosaur
* This spawns dinosaurs randomly
*/
run(() -> {
if (random(0, 2) < 2)
spawn("greenDino", random(0, getAppWidth() - 80), -50);
}, seconds(0.75));
score = spawn("Score", getAppCenter().getX() -270, getAppCenter().getY() - 320);
life = spawn("Life", getAppCenter().getX() - 260, getAppCenter().getY() - 250);
}
/**
* Summary :
* Detect the collision between the game elements.
*/
public void initPhysics() {
onCollisionBegin(EntityType.PROJECTILE, EntityType.GREENDINO, (projectile, greendino) -> {
SoundController.getInstance().playSoundEffect(GameConstants.ENEMY_EXPLODE_SOUND);
projectile.removeFromWorld();
greendino.removeFromWorld();
score.getComponent(ScoreComponent.class).incrementScore(1);
});
onCollisionBegin(EntityType.ENEMYPROJECTILE, EntityType.PLAYER, (projectile, player) -> {
SoundController.getInstance().playSoundEffect(GameConstants.PLAYER_HIT_SOUND);
projectile.removeFromWorld();
System.out.println("You got hit !\n");
damagePlayer();
});
onCollisionBegin(EntityType.PLAYER, EntityType.GREENDINO, (player, greendino) -> {
SoundController.getInstance().playSoundEffect(GameConstants.PLAYER_HIT_SOUND);
greendino.removeFromWorld();
System.out.println("You touched a dino !");
damagePlayer();
});
}
/**
* Summary :
* To detect whether the player lives are empty or not
*/
public void gameOver(){
getDialogService().showConfirmationBox(getLocalizationService().getLocalizedString("Game.2"), yes ->{
if (yes){
getGameController().startNewGame();
} else {
getGameController().gotoMainMenu();
SoundController.getInstance().playInGameSound(GameConstants.MAINMENU_SOUND, 1.0);
}
});
}
}