summaryrefslogtreecommitdiff
path: root/src/tesseract/objects/DyingParticle.java
blob: 1426293e6e9dc22c6953bba49a90462923d15445 (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
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
package tesseract.objects;

import java.awt.Color;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Sphere;

/**
 * A dying particle object.
 * 
 * Particle.java used as code base, parts also taken from ModifyableParticle.
 * 
 * @author Phillip Cardon
 * @author Jesse Morgan
 */
public class DyingParticle extends PhysicalObject {
	/**
	 * Rendered radius of particle.
	 */
	private static final float RADIUS = .001f;
	
	/**
	 * Default mass.
	 */
	private static final float DEFAULT_MASS = .1f;
	
	/**
	 * Number of divisions in the sphere.
	 */
	private static final int DIVISIONS = 8;
	private static final int DEFAULT_LIFE = 6; 
	private TransformGroup myTop;
	private TransformGroup myBottom;

	private Shape3D myShape;

	private Color3f myColor;

	private int myLife;
	
	/**
	 * Create a new Particle.
	 * 
	 * @param position Initial position.
	 * @param color Initial color. Null for random.
	 */
	public DyingParticle(final Vector3f position, final float mass, final Color3f color, final TransformGroup top, 
			final TransformGroup bottom) {
		super(position, mass);
		myTop = top;
		myBottom = bottom;
		myBottom.addChild(createShape(color));
		myColor = color;
		setShape(myTop);
		myLife = DEFAULT_LIFE;
	}
	
	/**
	 * Create a new particle of the give color.
	 * 
	 * @param theColor The particle color or null for random.
	 * @return A sphere to visually represent the particle.
	 */
	private Shape3D createShape(final Color3f theColor) {
		
		Color3f color = theColor;
		
		ColoringAttributes cAttr;
		
		if (color == null) {
			Color randomColor = Color.getHSBColor((float) Math.random(), 1, 1);
			color = new Color3f(randomColor);
		}
		/*
		cAttr = new ColoringAttributes(color, ColoringAttributes.FASTEST);
		Appearance appearance = new Appearance();
		Material mat = new Material();
		mat.setAmbientColor(color);
		mat.setDiffuseColor(color);
		appearance.setMaterial(mat);
		appearance.setColoringAttributes(cAttr);
		
		Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING,
				DIVISIONS, appearance);
		*/
		
		Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING,
				DIVISIONS);
		Shape3D shape = sphere.getShape();
		sphere.removeAllChildren();
		Appearance meshApp = new Appearance();
		Material surface = new Material();
		surface.setDiffuseColor(color);
		meshApp.setMaterial(surface);
		meshApp.setColoringAttributes(new ColoringAttributes(color,
						ColoringAttributes.FASTEST));
		shape.setAppearance(meshApp);
		myShape = shape;
		myShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
		return shape;
	}
	
	public boolean isDead() {
		return myLife <= 0;
	}
	
	public void updateState(float duration) {
		if (!isDead()) {
			myLife--;
			super.updateState(duration);
		} else {
			super.updateState(duration);
			this.detach();
		}
			
	}
	
	public void setAcceleration(Vector3f accelerator) {
		this.velocity = accelerator;		
	}
}