-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameplayState.java
More file actions
244 lines (194 loc) · 7.23 KB
/
GameplayState.java
File metadata and controls
244 lines (194 loc) · 7.23 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package de.tudarmstadt.informatik.fop.breakout.ui;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import de.tudarmstadt.informatik.fop.breakout.actions.BounceSideBallAction;
import de.tudarmstadt.informatik.fop.breakout.actions.BounceTopBallAction;
import de.tudarmstadt.informatik.fop.breakout.actions.PauseAction;
import de.tudarmstadt.informatik.fop.breakout.actions.RotationToMove;
import de.tudarmstadt.informatik.fop.breakout.constants.GameParameters;
import de.tudarmstadt.informatik.fop.breakout.entities.Ball;
import de.tudarmstadt.informatik.fop.breakout.entities.Stick;
import de.tudarmstadt.informatik.fop.breakout.events.TouchLeftBorder;
import de.tudarmstadt.informatik.fop.breakout.events.TouchRightBorder;
import de.tudarmstadt.informatik.fop.breakout.events.TouchTopBorder;
import de.tudarmstadt.informatik.fop.breakout.factories.BorderFactory;
import eea.engine.action.Action;
import eea.engine.action.basicactions.ChangeStateAction;
import eea.engine.action.basicactions.ChangeStateInitAction;
import eea.engine.action.basicactions.DestroyEntityAction;
import eea.engine.action.basicactions.MoveLeftAction;
import eea.engine.action.basicactions.MoveRightAction;
import eea.engine.action.basicactions.Movement;
import eea.engine.component.Component;
import eea.engine.component.render.ImageRenderComponent;
import eea.engine.entity.Entity;
import eea.engine.entity.StateBasedEntityManager;
import eea.engine.event.ANDEvent;
import eea.engine.event.NOTEvent;
import eea.engine.event.OREvent;
import eea.engine.event.basicevents.KeyDownEvent;
import eea.engine.event.basicevents.KeyPressedEvent;
import eea.engine.event.basicevents.LeavingScreenEvent;
import eea.engine.event.basicevents.LoopEvent;
/*
* @Author Denis Andric
*/
public class GameplayState extends BasicGameState implements GameParameters {
private int idState;
private StateBasedEntityManager entityManager;
private boolean GameWin = false;
private int lives = 5;
private long time = 0;
protected List<BorderFactory> borders = new ArrayList<BorderFactory>();
private static boolean gameWon = false;
private static boolean gameLost = false;
private static boolean gameStarted = false;
private static boolean ballMoving = false;
private static boolean timeStarted = false;
public void setLives(int lives) {
this.lives = lives;
}
public int getLives() {
return lives;
}
public int addLives(int lives) {
return this.lives = lives;
}
/**
*
* @return one live lost
*/
public int loseLive() {
return lives - 1;
}
public long getTime() {
return time;
}
public void setGameStarted(boolean state) {
gameStarted = state;
}
public boolean getGameWin() {
return GameWin;
}
public StateBasedEntityManager getEntityManager() {
return entityManager;
}
public float getStickPosition() {
return entityManager.getEntity(idState, STICK_ID).getPosition().getX();
}
public GameplayState(int ID) {
idState = ID;
entityManager = StateBasedEntityManager.getInstance();
}
/*
* BorderFactory to List
*/
public void makeBorderList() {
borders.add(new BorderFactory(BorderType.LEFT));
borders.add(new BorderFactory(BorderType.RIGHT));
borders.add(new BorderFactory(BorderType.TOP));
}
/**
*
*/
public void BorderListToEntity() {
makeBorderList();
for (BorderFactory e : borders) {
Entity border = e.createEntity();
entityManager.addEntity(idState, border);
}
}
public void setBackground() throws SlickException {
// Setting up background entity
Entity background = new Entity(BACKGROUND_ID);
// setting up position
background.setPosition(new Vector2f(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2));
// adding image to entity
background.addComponent(new ImageRenderComponent(new Image(BACKGROUND_IMAGE)));
// adding entity to entityManager
entityManager.addEntity(idState, background);
}
public void Escape() {
// Setting up entity
Entity escListener = new Entity("ESC_Listener");
// Event if is Escape pressed
KeyPressedEvent esc = new KeyPressedEvent(Input.KEY_ESCAPE);
// Action switch states if esc pressed
esc.addAction(new ChangeStateAction(MAINMENU_STATE));
// adding EventComponent in entity
escListener.addComponent(esc);
// adding entity in entityManager
entityManager.addEntity(idState, escListener);
}
public void PauseIt() {
Entity PListener = new Entity("P_Listener");
KeyPressedEvent space = new KeyPressedEvent(Input.KEY_P);
PauseAction pause = new PauseAction();
space.addAction(pause);
PListener.addComponent(space);
entityManager.addEntity(idState, PListener);
}
public void StartGameAndTime(GameContainer gc, int delta){
if(gc.getInput().isKeyPressed(Input.KEY_SPACE)){
gameStarted= true;
timeStarted= true;
}
if (timeStarted)
time += delta;
}
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
ballMoving = false;
gameStarted = false;
setBackground();
BorderListToEntity();
Escape();
PauseIt();
Stick stick = new Stick(STICK_ID);
// default Position
stick.setPosition(new Vector2f(400, 580));
// adding image to entity
stick.addComponent(new ImageRenderComponent(new Image(STICK_IMAGE)));
stick.moveLeft();// method only for stick , it is in class
stick.moveRight();// method only for stick, it is in class
entityManager.addEntity(idState, stick);
Ball ball = new Ball(BALL_ID);
ball.setPosition(new Vector2f(400, 560));
ball.setRotation(120);
ball.addComponent(new ImageRenderComponent(new Image(BALL_IMAGE)));
entityManager.addEntity(idState, ball);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
entityManager.updateEntities(gc, sbg, delta);
StartGameAndTime(gc,delta);
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
entityManager.renderEntities(gc, sbg, g);
g.drawString("" + entityManager.getEntity(GAMEPLAY_STATE, STICK_ID)
.collides(entityManager.getEntity(GAMEPLAY_STATE, LEFT_BORDER_ID)), 100, 100);
g.drawString("" + entityManager.getEntity(GAMEPLAY_STATE, STICK_ID)
.collides(entityManager.getEntity(GAMEPLAY_STATE, RIGHT_BORDER_ID)), 100, 125);
g.drawString("" + entityManager.getEntity(GAMEPLAY_STATE, STICK_ID).getPosition().getX(), 100, 150);
g.drawString("" + entityManager.getEntity(GAMEPLAY_STATE, STICK_ID).getSize().getX(), 100, 175);
g.drawString((time / 1000) / 60 + ":" + (time / 1000) % 60 + ":" + time % 1000, 700, 50);
g.drawString("Lifes left: " + lives, 600, 25);
g.drawString("" + timeStarted, 300, 10);
g.drawString(" " + gc.getTime(), 700, 75);
}
@Override
public int getID() {
// TODO Auto-generated method stub
return idState;
}
}