-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainControl.java
163 lines (134 loc) · 4.57 KB
/
MainControl.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package bomberman;
import bomberman.Entities.Bomber;
import bomberman.Entities.BomberControl;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.Level;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.parser.text.TextLevelParser;
import com.almasb.fxgl.settings.GameSettings;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import java.util.Random;
public class MainControl extends GameApplication {
public static Entity getBomber() {
return bomber;
}
private static Bomber bomberControl ;
private static Entity bomber;
// private static Bomber bomberControl;
private BombermanUI bombermanUI;
private static boolean requestNewGame = false ;
private static boolean requestEndGame = false ;
/*
* CREATE GAME WINDOW
*/
@Override
protected void initSettings(GameSettings gameSettings) {
gameSettings.setTitle(GameProperties.TITLE);
gameSettings.setVersion(GameProperties.VERSION);
gameSettings.setWidth(GameProperties.WIDTH);
gameSettings.setHeight(GameProperties.HEIGHT);
}
/*
* MUSIC
*/
@Override
protected void preInit()
{
Random random = new Random();
int i = random.nextInt(5);
getAudioPlayer().loopBGM("mainScene" + i + ".wav");
}
/*
* End game and start new game
*/
@Override
protected void onPostUpdate ( double tpf){
if ( requestEndGame) getDisplay().showMessageBox("You win !", this :: exit);
if (requestNewGame ){
requestNewGame = false ;
startNewGame();
}
}
private static void getDeath(){
requestNewGame = true ;
}
private static void gameVictory(){
requestEndGame = true ;
}
/*
* LOAD BACKGROUND
*/
@Override
protected void initGame() {
// load map from text
GameFactory factory= new GameFactory();
getGameWorld().addEntityFactory(factory);
TextLevelParser levelParser = new TextLevelParser(factory);
Level level = levelParser.parse("levels/Level1.txt");
getGameWorld().setLevel(level);
loadBackGround();
// load Player
bomber = getGameWorld().spawn("Player", GameProperties.SIZE_TILES, GameProperties.SIZE_TILES);
bomberControl = bomber.getComponent(Bomber.class);
getGameScene().getViewport().setBounds(-1500,0, 1500, getHeight());
// getGameScene().getViewport().bindToEntity(bomber, getWidth()/3, getHeight()/2);
}
// ?????
private void loadBackGround(){
for(int i = 0; i < 31; i++){
for(int j = 0; j < 13; j ++){
int finalI = i;
int finalJ = j;
getGameWorld()
.getEntitiesAt(new Point2D(i * GameProperties.SIZE_TILES, j * GameProperties.SIZE_TILES))
.stream()
.forEach(e->{
if(e.isType(GameObject.ENEMY)){
getGameWorld().spawn("Grass", finalI * GameProperties.SIZE_TILES, finalJ * GameProperties.SIZE_TILES);
}
});
}
}
}
/*
* CONTROL BOMBER PLAYER
*/
@Override
protected void initInput() {
getInput().addAction(new UserAction("Move Up") {
@Override
protected void onAction() {
bomberControl.moveUp();
}
}, KeyCode.UP);
getInput().addAction(new UserAction("Move Left") {
@Override
protected void onAction() {
bomberControl.moveLeft();
}
}, KeyCode.LEFT);
getInput().addAction(new UserAction("Move Down") {
@Override
protected void onAction() {
bomberControl.moveDown();
}
}, KeyCode.DOWN);
getInput().addAction(new UserAction("Move Right") {
@Override
protected void onAction() {
bomberControl.moveRight();
}
}, KeyCode.RIGHT);
getInput().addAction(new UserAction("Place Bomb") {
@Override
protected void onAction() {
bomberControl.placeBomb();
}
}, KeyCode.SPACE);
}
public static void main(String[] args) {
launch(args);
}
}