-
Notifications
You must be signed in to change notification settings - Fork 4
/
ParticleRenderComponent.java
107 lines (95 loc) · 2.91 KB
/
ParticleRenderComponent.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
package com.deco2800.game.rendering;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.deco2800.game.entities.Entity;
import com.deco2800.game.services.GameTime;
import com.deco2800.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Render particle effect for entities.
* <p>
* Usages: ParticleRenderComponent particle =
* new ParticleRenderComponent("path.party");
* entity.addComponent(particle);
* <p>
* Start particle effect: particle.startEffect();
*/
public class ParticleRenderComponent extends RenderComponent {
private String texturePath;
private ParticleEffect pe;
private boolean EffectStart = false;
private float particlePlayTime;
private final GameTime timeSource;
private static final Logger logger = LoggerFactory.getLogger(ParticleRenderComponent.class);
/**
* Constructor of particle effect
*
* @param texturePath the path of .party file
*/
public ParticleRenderComponent(String texturePath) {
this.texturePath = texturePath;
this.timeSource = ServiceLocator.getTimeSource();
}
@Override
public void create() {
super.create();
pe = new ParticleEffect();
pe.load(Gdx.files.internal(texturePath), Gdx.files.internal("images/particle"));
}
/**
* Set the zIndex of the entity according to the y coordinate of the entity and the original zIndex.
* <p>
* If zIndex is not set originally, then The smaller the Y value, the higher the Z index, so that closer entities
* are drawn in front.
* <p>
* If zIndex has been set, zIndex will not change.
*
* @return The drawing priority of the current entity
*/
@Override
public float getZIndex() {
return 1;
}
/**
* get if the particle effect is start, for test.
* @return
*/
public boolean isEffectStart() {
return EffectStart;
}
/**
* Start the particle effect on this entity
*/
public void startEffect() {
EffectStart = true;
}
@Override
protected void draw(SpriteBatch batch) {
if (EffectStart) {
Vector2 entityPosition = entity.getPosition();
pe.update(Gdx.graphics.getDeltaTime());
pe.setPosition(entityPosition.x, entityPosition.y);
pe.draw(batch);
if (pe.isComplete()) {
pe.reset();
}
particlePlayTime += timeSource.getDeltaTime();
}
}
/**
* Get the playing time of the animation
*
* @return float The time the animation has been played (seconds).
*/
public float getParticlePlayTime() {
return this.particlePlayTime;
}
@Override
public void dispose() {
pe.dispose();
super.dispose();
}
}