summaryrefslogtreecommitdiff
path: root/src/tesseract/objects/emitters/ParticleEmitter.java
blob: de5a556b780327cad720710b783c2de059d0de0d (plain)
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
package tesseract.objects.emitters;

import java.util.LinkedList;
import java.util.List;

import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;

import tesseract.objects.Particle;
import tesseract.objects.PhysicalObject;

/**
 * ParticleEmitter Class.
 *
 * @author Jesse Morgan
 */
public class ParticleEmitter extends PhysicalObject {

	/**
	 * Frequency of new objects.
	 */
	private float myFrequency;
	
	/**
	 * The object to create...
	 */
	private Color3f myColor;
	
	/**
	 * Counter to keep track of how long has passed.
	 */
	private float myCount;
	
	/**
	 * Construct a new Particle Emitter.
	 * 
	 * @param position Where to put the emitter
	 * @param frequency How often to emit the object (seconds).
	 * @param color Color the particle be (null for random).
	 */
	public ParticleEmitter(final Vector3f position, final float frequency,
			final Color3f color) {
		
		super(position);
		
		myCount = 0;
		myFrequency = frequency;
		myColor = color;
	}

	/**
	 * Update State and maybe generate a new object.
	 * 
	 * @param duration The length of time that has passed.
	 * @return A list of new objects to add to the world.
	 */
	public List<PhysicalObject> updateState(final float duration) {
		List<PhysicalObject> children = super.updateState(duration);
		
		if (children == null) {
			children = new LinkedList<PhysicalObject>();
		}
		
		myCount += duration;
		if (myCount >= myFrequency) {
			children.add(new Particle(getPosition(), myColor));
			myCount = 0;
		}
		
		return children;
	}
}