Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package elec0.particleEngine;
- import static org.lwjgl.opengl.GL11.*;
- import java.util.Random;
- import org.newdawn.slick.Color;
- import elec0.utils.Timing;
- import elec0.utils.Vector3f;
- public class Particle
- {
- // Everything can be public because this class shouldn't ever be used outside of a ParticleEmitter, which might need to change things after initilization
- public Vector3f position, moveVector;
- public float speed, lifetime;
- public Color startcolor1, startcolor2, startcolor3; // Starting and ending colors, transition through life of particle
- public Color endcolor1, endcolor2, endcolor3;
- public long birthTime;
- public String behavior;
- private Behaviors enumBehaviors;
- public float size = 1;
- // Constructors
- Particle(Vector3f position)
- {
- this.position = position;
- this.birthTime = Timing.getTime();
- this.behavior = "default";
- // this.position.setY(size);
- initParticle();
- }
- Particle(Vector3f position, Color startcolor1)
- {
- this.position = position;
- this.startcolor1 = startcolor1;
- this.birthTime = Timing.getTime();
- this.behavior = "default";
- // this.position.setY(size);
- initParticle();
- }
- Particle(Vector3f position, Color startcolor1, String behavior)
- {
- // this.position = new Vector3f(0, 0, 0);
- this.position = position;
- this.startcolor1 = startcolor1;
- this.birthTime = Timing.getTime();
- this.behavior = behavior;
- // this.position.setY(size);
- initParticle();
- }
- // End constructors
- private void initParticle()
- {
- Random random = new Random();
- enumBehaviors = Behaviors.valueOf(behavior.toUpperCase());
- switch(enumBehaviors)
- {
- case FIRE: // Uses three colors
- break;
- case DIAMOND: // Default (atm) with diamonds instead of squares and no texture
- // Get a random spread and direction
- float diSpread =(float)(random.nextInt(100))/200.0f;
- float diAngle =(float)(random.nextInt(157))/10.0f; // Quarter circle
- float dixMove = (float)(Math.cos(diAngle)*diSpread)/5;
- float diyMove = (float)(Math.sin(diAngle)*diSpread)/5;
- // Calculate X and Z vectors
- moveVector = new Vector3f(dixMove, 0, diyMove);
- // Get a random initial speed
- moveVector.setY(random.nextInt(500)/1000f);
- // Get a random life and 'yellowness'
- lifetime = random.nextFloat()*10000;
- break;
- case DEFAULT: // Uses one color, random spread and speed, affected by gravity, lifespan 1-10 seconds
- // Get a random spread and direction
- float dSpread=(float)(random.nextInt(100))/200.0f;
- float dAngle=(float)(random.nextInt(157))/10.0f; // Quarter circle
- float dxMove = (float)(Math.cos(dAngle)*dSpread)/5;
- float dyMove = (float)(Math.sin(dAngle)*dSpread)/5;
- // Calculate X and Z vectors
- moveVector = new Vector3f(dxMove, 0, dyMove);
- // Get a random initial speed
- moveVector.setY(random.nextInt(500)/1000f);
- // Get a random life and 'yellowness'
- lifetime = random.nextFloat()*10000;
- break;
- }
- }
- // Pass this any non-null Colors you want it to change the internal variables for
- public void setColors(Color sc1, Color sc2, Color sc3, Color ec1, Color ec2, Color ec3)
- {
- if(sc1 != null)
- startcolor1 = sc1;
- if(sc2 != null)
- startcolor2 = sc2;
- if(sc3 != null)
- startcolor3 = sc3;
- if(ec1 != null)
- endcolor1 = ec1;
- if(ec2 != null)
- endcolor2 = ec2;
- if(ec3 != null)
- endcolor3 = ec3;
- }
- public void draw()
- {
- glPushMatrix();
- switch(enumBehaviors)
- {
- case DIAMOND:
- glColor4f(startcolor1.r, startcolor1.g, startcolor1.b, startcolor1.a); // Color the particle
- glDisable(GL_TEXTURE_2D);
- // Place the quad and rotate to face the viewer
- glTranslatef(position.getX(), position.getY(), position.getZ());
- //glRotatef(-ViewYaw,0.0f,1.0f,0.0f);
- //glRotatef(-ViewPitch,1.0f,0.0f,0.0f);
- glBegin(GL_QUADS);
- glTexCoord2f(1.0f,0.0f); glVertex3f(0, 0, 0.0f);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-size/2, size/2, 0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(0, size, 0.0f);
- glTexCoord2f(1.0f,1.0f); glVertex3f(size/2, size/2, 0.0f);
- glEnd();
- break;
- case DEFAULT:
- glColor4f(startcolor1.r, startcolor1.g, startcolor1.b, startcolor1.a); // Color the particle
- glEnable(GL_TEXTURE_2D);
- // Place the quad and rotate to face the viewer
- glTranslatef(position.getX(), position.getY(), position.getZ());
- //glRotatef(-ViewYaw,0.0f,1.0f,0.0f);
- //glRotatef(-ViewPitch,1.0f,0.0f,0.0f);
- glBegin(GL_QUADS);
- glTexCoord2f(1.0f,0.0f); glVertex3f(size, size,0.0f);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-size, size,0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(-size,-size,0.0f);
- glTexCoord2f(1.0f,1.0f); glVertex3f(size,-size,0.0f);
- glEnd();
- break;
- }
- glPopMatrix();
- }
- private enum Behaviors
- {
- DEFAULT,
- FIRE,
- DIAMOND,
- SQUARE
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment