Trying to make color fade in particles but its not finished yet

This commit is contained in:
Ziver Koc 2007-04-06 13:23:47 +00:00
parent c70fb12b38
commit 3ad54c931d
3 changed files with 76 additions and 30 deletions

View file

@ -38,11 +38,10 @@ public class Particles extends Entity{
public float life = 1.0f;
public float size = 20.0f;
private static float colors[][]= // Rainbow Of Colors
private static float colors[][][]= // Rainbow Of Colors
{
{1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f},
{0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f},
{0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f}
{{1.0f,0.5f,0.5f},{1.0f,1.0f,0.5f}},
{{1.0f,1.0f,0.5f},{1.0f,0.5f,0.5f}}
};
private int maxParticles = 1000;
@ -60,8 +59,8 @@ public class Particles extends Entity{
public Particles(String name){
super(name);
this.texture = TextureLoader.getTextureLoaderInstance().getTexture("data/particle.bmp");
reset();
setDefault();
reset();
}
private void setDefault() {
@ -76,27 +75,8 @@ public class Particles extends Entity{
life = 1.0f;
size = 20.0f;
regenerate = true;
init();
}
private void init() {
for (int i=0;i<maxParticles;i++){ // Initials All The Textures
particle[i].active = true; // Make All The Particles Active
particle[i].life = life; // Give All The Particles Full Life
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Speed
particle[i].r = colors[i * (colors.length / maxParticles)][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (colors.length / maxParticles)][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (colors.length / maxParticles)][2]; // Select Red Rainbow Color
particle[i].xi = ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // Random Speed On X Axis
particle[i].yi = ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Random Speed On Y Axis
particle[i].zi = ((float)((Math.random() * MaxSpeedZ)) - (MaxSpeedZ/2)) * 10.0f; // Random Speed On Z Axis
particle[i].xg = pullForceX; // Set Horizontal Pull To Zero
particle[i].yg = pullForceY; // Set Vertical Pull Downward
particle[i].zg = pullForceZ; // Set Pull On Z Axis To Zero
}
}
/**
* Set the number of particles
* WARNING this will reset all the particles!!
@ -119,6 +99,29 @@ public class Particles extends Entity{
particle[i] = new Particle();
particle[i].life = -1.0f;
}
init();
}
private void init() {
for (int i=0;i<maxParticles;i++){ // Initials All The Textures
particle[i].active = true; // Make All The Particles Active
particle[i].life = life; // Give All The Particles Full Life
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Speed
particle[i].r = colors[i * (colors.length / maxParticles)][0][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (colors.length / maxParticles)][0][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (colors.length / maxParticles)][0][2]; // Select Red Rainbow Color
particle[i].colorId = i * (colors.length / maxParticles);
particle[i].xi = ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // Random Speed On X Axis
particle[i].yi = ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Random Speed On Y Axis
particle[i].zi = ((float)((Math.random() * MaxSpeedZ)) - (MaxSpeedZ/2)) * 10.0f; // Random Speed On Z Axis
particle[i].xg = pullForceX; // Set Horizontal Pull To Zero
particle[i].yg = pullForceY; // Set Vertical Pull Downward
particle[i].zg = pullForceZ; // Set Pull On Z Axis To Zero
particle[i].x = getLocation().getX(); // Center On X Axis
particle[i].y = getLocation().getY(); // Center On Y Axis
particle[i].z = getLocation().getZ(); // Center On Z Axis
}
}
/**
@ -181,7 +184,22 @@ public class Particles extends Entity{
particle[i].zi += particle[i].zg; // Take Pull On Z Axis Into Account
particle[i].life -= particle[i].fade; // Reduce Particles Life By 'Fade'
// Fading to another color
if(particle[i].r < colors[particle[i].colorId][1][0])
particle[i].r += 0.2f;
if(particle[i].g < colors[particle[i].colorId][1][1])
particle[i].g += 0.2f;
if(particle[i].b < colors[particle[i].colorId][1][2])
particle[i].b += 0.2f;
if(particle[i].r > colors[particle[i].colorId][1][0])
particle[i].r -= 0.2f;
if(particle[i].g > colors[particle[i].colorId][1][1])
particle[i].g -= 0.2f;
if(particle[i].b > colors[particle[i].colorId][1][2])
particle[i].b -= 0.2f;
if (particle[i].life < 0.0f) { // If Particle Is Burned Out
particle[i].active = true; // Make All The Particles Active
particle[i].life = life; // Give It New Life
@ -195,10 +213,11 @@ public class Particles extends Entity{
particle[i].yi = yspeed + ((float)((Math.random() * MaxSpeedY)) - (MaxSpeedY/2)) * 10.0f; // Y Axis Speed And Direction
particle[i].zi = ((float)((Math.random() * MaxSpeedZ)) - (MaxSpeedZ/2)) * 10.0f; // Z Axis Speed And Direction
particle[i].r = colors[col][0]; // Select Red From Color Table
particle[i].g = colors[col][1]; // Select Green From Color Table
particle[i].b = colors[col][2]; // Select Blue From Color Table
particle[i].r = colors[col][0][0]; // Select Red From Color Table
particle[i].g = colors[col][0][1]; // Select Green From Color Table
particle[i].b = colors[col][0][2]; // Select Blue From Color Table
particle[i].colorId = col;
particle[i].xg = pullForceX; // Set Horizontal Pull To Zero
particle[i].yg = pullForceY; // Set Vertical Pull Downward
particle[i].zg = pullForceZ; // Set Pull On Z Axis To Zero
@ -258,6 +277,7 @@ class Particle { // Particles Structure
public boolean active; // Active (Yes/No)
public float life; // Particle Life
public float fade; // Fade Speed
public int colorId;
public float r; // Red Value
public float g; // Green Value

View file

@ -32,6 +32,10 @@ public class ParticlesTest extends LWJGLGameWindow{
protected void update() {
super.update();
if(ptf.respawn){
p.reset();
ptf.respawn = false;
}
p.life = ptf.life.getValue();
p.MaxSpeedX = ptf.maxSpeedX.getValue();
p.MaxSpeedY = ptf.maxSpeedY.getValue();

View file

@ -7,6 +7,7 @@ import javax.swing.JCheckBox;
import java.awt.FlowLayout;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JButton;
public class ParticlesTestFrame{
@ -33,6 +34,8 @@ public class ParticlesTestFrame{
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
public JSlider size = null;
public boolean respawn = false;
private JButton jButtonRespawn = null;
/**
* This method initializes jFrame
*
@ -41,7 +44,7 @@ public class ParticlesTestFrame{
public JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(281, 627));
jFrame.setSize(new Dimension(281, 662));
jFrame.setTitle("ParticleTest");
jFrame.setContentPane(getJContentPane());
}
@ -90,6 +93,7 @@ public class ParticlesTestFrame{
jContentPane.add(getLife(), null);
jContentPane.add(jLabel5, null);
jContentPane.add(getSize(), null);
jContentPane.add(getJButtonRespawn(), null);
}
return jContentPane;
}
@ -358,4 +362,22 @@ public class ParticlesTestFrame{
return size;
}
/**
* This method initializes jButtonRespawn
*
* @return javax.swing.JButton
*/
private JButton getJButtonRespawn() {
if (jButtonRespawn == null) {
jButtonRespawn = new JButton();
jButtonRespawn.setText("Force Respawn");
jButtonRespawn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
respawn = true;
}
});
}
return jButtonRespawn;
}
}