-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectManager.java
233 lines (211 loc) · 7.06 KB
/
ObjectManager.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
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
package objects;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import entities.Player;
import gamestates.Playing;
import levels.Level;
import main.ScoreManager;
import utilz.LoadSave;
import static utilz.Constants.ObjectConstants.*;
/**
* Gerencia os objetos no jogo, como poções, recipientes e armadilhas.
*/
public class ObjectManager {
private Playing playing;
private BufferedImage[][] potionImgs, containerImgs;
private BufferedImage spikeImg;
private ArrayList<Potion> potions;
private ArrayList<GameContainer> containers;
private ArrayList<Spike> spikes;
private ScoreManager scoreManager;
/**
* Construtor da classe ObjectManager.
*
* @param playing A instância do estado de jogo Playing.
* @param scoreManager O gerenciador de pontuação do jogo.
*/
public ObjectManager(Playing playing, ScoreManager scoreManager) {
this.playing = playing;
this.scoreManager = scoreManager;
loadImgs();
}
/**
* Verifica se o jogador tocou em alguma armadilha (Spike).
*
* @param p O jogador.
*/
public void checkSpikesTouched(Player p) {
for (Spike s : spikes) {
if (s.getHitbox().intersects(p.getHitbox())) {
p.kill();
}
}
}
/**
* Verifica se algum objeto foi tocado pelo jogador.
*
* @param hitbox A hitbox do jogador.
*/
public void checkObjectTouched(Rectangle2D.Float hitbox) {
for (Potion p : potions) {
if (p.isActive()) {
if (hitbox.intersects(p.getHitbox())) {
p.setActive(false);
applyEffectToPlayer(p);
}
}
}
}
/**
* Aplica o efeito da poção ao jogador.
*
* @param p A poção a ser aplicada.
*/
public void applyEffectToPlayer(Potion p) {
if (p.getObjType() == RED_POTION) {
playing.getPlayer().changeHealth(RED_POTION_VALUE);
} else {
playing.getPlayer().changePower(BLUE_POTION_VALUE);
}
}
/**
* Verifica se algum objeto foi atingido pela área de ataque do jogador.
*
* @param attackbox A área de ataque do jogador.
*/
public void checkObjectHit(Rectangle2D.Float attackbox) {
for (GameContainer gc : containers) {
if (gc.isActive() && !gc.doAnimation) {
if (gc.getHitbox().intersects(attackbox)) {
gc.setAnimation(true);
int type = 0;
if (gc.getObjType() == BARREL) {
type = 1;
}
potions.add(new Potion((int) (gc.getHitbox().x + gc.getHitbox().width / 2), (int) (gc.getHitbox().y - gc.getHitbox().height / 2), type));
return;
}
}
}
}
/**
* Carrega os objetos do novo nível.
*
* @param newLevel O novo nível a ser carregado.
*/
public void loadObjects(Level newLevel) {
potions = new ArrayList<>(newLevel.getPotions());
containers = new ArrayList<>(newLevel.getContainers());
spikes = newLevel.getSpikes();
}
/**
* Carrega as imagens dos objetos (poções, recipientes e armadilhas).
*/
private void loadImgs() {
// Carrega as imagens das poções.
BufferedImage potionSprite = LoadSave.GetSpriteAtlas(LoadSave.POTION_ATLAS);
potionImgs = new BufferedImage[2][7];
for (int j = 0; j < potionImgs.length; j++) {
for (int i = 0; i < potionImgs[j].length; i++) {
potionImgs[j][i] = potionSprite.getSubimage(12 * i, 16 * j, 12, 16);
}
}
// Carrega as imagens dos recipientes.
BufferedImage containerSprite = LoadSave.GetSpriteAtlas(LoadSave.CONTAINER_ATLAS);
containerImgs = new BufferedImage[2][8];
for (int j = 0; j < containerImgs.length; j++) {
for (int i = 0; i < containerImgs[j].length; i++) {
containerImgs[j][i] = containerSprite.getSubimage(40 * i, 30 * j, 40, 30);
}
}
// Carrega a imagem da armadilha (Spike).
spikeImg = LoadSave.GetSpriteAtlas(LoadSave.TRAP_ATLAS);
}
/**
* Atualiza o estado dos objetos.
*/
public void update() {
for (Potion p : potions) {
if (p.isActive()) {
p.update();
}
}
for (GameContainer gc : containers) {
if (gc.isActive()) {
gc.update();
}
}
}
/**
* Desenha os objetos na tela.
*
* @param g O objeto Graphics para desenho.
* @param xLvlOffset O deslocamento horizontal do nível.
*/
public void draw(Graphics g, int xLvlOffset) {
drawPotions(g, xLvlOffset);
drawContainers(g, xLvlOffset);
drawTraps(g, xLvlOffset);
}
/**
* Desenha as armadilhas (Spikes) na tela.
*
* @param g O objeto Graphics para desenho.
* @param xLvlOffset O deslocamento horizontal do nível.
*/
private void drawTraps(Graphics g, int xLvlOffset) {
for (Spike s : spikes) {
g.drawImage(spikeImg, (int) (s.getHitbox().x - xLvlOffset), (int) (s.getHitbox().y - s.getyDrawOffset()), SPIKE_WIDTH, SPIKE_HEIGHT, null);
}
}
/**
* Desenha os recipientes na tela.
*
* @param g O objeto Graphics para desenho.
* @param xLvlOffset O deslocamento horizontal do nível.
*/
private void drawContainers(Graphics g, int xLvlOffset) {
for (GameContainer gc : containers) {
if (gc.isActive()) {
int type = 0;
if (gc.getObjType() == BARREL) {
type = 1;
}
g.drawImage(containerImgs[type][gc.getAniIndex()], (int) (gc.getHitbox().x - gc.getxDrawOffset() - xLvlOffset), (int) (gc.getHitbox().y - gc.getyDrawOffset()), CONTAINER_WIDTH,
CONTAINER_HEIGHT, null);
}
}
}
/**
* Desenha as poções na tela.
*
* @param g O objeto Graphics para desenho.
* @param xLvlOffset O deslocamento horizontal do nível.
*/
private void drawPotions(Graphics g, int xLvlOffset) {
for (Potion p : potions) {
if (p.isActive()) {
int type = 0;
if (p.getObjType() == RED_POTION) {
type = 1;
}
g.drawImage(potionImgs[type][p.getAniIndex()], (int) (p.getHitbox().x - p.getxDrawOffset() - xLvlOffset), (int) (p.getHitbox().y - p.getyDrawOffset()), POTION_WIDTH, POTION_HEIGHT,
null);
}
}
}
/**
* Reseta todos os objetos para o estado inicial.
*/
public void resetAllObjects() {
loadObjects(playing.getLevelManager().getCurrentLevel());
for (Potion p : potions) {
p.reset();
}
for (GameContainer gc : containers) {
gc.reset();
}
}
}