Path: blob/master/SLICK_HOME/src/org/newdawn/slick/particles/Particle.java
1457 views
package org.newdawn.slick.particles;12import org.newdawn.slick.Color;3import org.newdawn.slick.Image;4import org.newdawn.slick.opengl.TextureImpl;5import org.newdawn.slick.opengl.renderer.Renderer;6import org.newdawn.slick.opengl.renderer.SGL;78/**9* A single particle within a system10*11* @author kevin12*/13public class Particle {14/** The renderer to use for all GL operations */15protected static SGL GL = Renderer.get();1617/** Indicates the particle should inherit it's use of points */18public static final int INHERIT_POINTS = 1;19/** Indicates the particle should explicitly use points */20public static final int USE_POINTS = 2;21/** Indicates the particle should explicitly not use points */22public static final int USE_QUADS = 3;2324/** The x coordinate of the particle */25protected float x;26/** The y coordinate of the particle */27protected float y;28/** The x component of the direction vector of the particle */29protected float velx;30/** The y component of the direction vector of the particle */31protected float vely;32/** The current size in pixels of the particle */33protected float size = 10;34/** The colour of the particle */35protected Color color = new Color(1f, 1f, 1f, 1f);36/** The life left in the particle */37protected float life;38/** The original life of this particle */39protected float originalLife;40/** The engine this particle belongs to */41private ParticleSystem engine;42/** The emitter controllng this particle */43private ParticleEmitter emitter;44/** The image for this particle */45protected Image image;46/** The type identifier of this particle */47protected int type;48/** How this particle should be rendered */49protected int usePoints = INHERIT_POINTS;50/** True if this particle's quad should be oritented based on it's direction */51protected boolean oriented = false;52/** The currently scalar applied on the y axis */53protected float scaleY = 1.0f;5455/**56* Create a new particle belonging to given engine57*58* @param engine59* The engine the new particle belongs to60*/61public Particle(ParticleSystem engine) {62this.engine = engine;63}6465/**66* Get the x offset of this particle67*68* @return The x offset of this particle69*/70public float getX() {71return x;72}7374/**75* Get the y offset of this particle76*77* @return The y offset of this particle78*/79public float getY() {80return y;81}8283/**84* Move this particle a fixed amount85*86* @param x The amount to move the particle on the horizontal axis87* @param y The amount to move the particle on the vertical axis88*/89public void move(float x, float y) {90this.x += x;91this.y += y;92}9394/**95* Get the size of this particle96*97* @return The size of this particle98*/99public float getSize() {100return size;101}102103/**104* Get the color of this particle105*106* @return The color of this particle107*/108public Color getColor() {109return color;110}111112/**113* Set the image used to render this particle114*115* @param image116* The image used to render this particle117*/118public void setImage(Image image) {119this.image = image;120}121122/**123* Get the original life of this particle124*125* @return The original life of this particle126*/127public float getOriginalLife() {128return originalLife;129}130131/**132* Get the life remaining in the particle in milliseconds133*134* @return The life remaining in the particle135*/136public float getLife() {137return life;138}139140/**141* Check if this particle is currently in use (i.e. is it rendering?)142*143* @return True if the particle is currently in use144*/145public boolean inUse() {146return life > 0;147}148149/**150* Render this particle151*/152public void render() {153if ((engine.usePoints() && (usePoints == INHERIT_POINTS))154|| (usePoints == USE_POINTS)) {155TextureImpl.bindNone();156GL.glEnable(SGL.GL_POINT_SMOOTH);157GL.glPointSize(size / 2);158color.bind();159GL.glBegin(SGL.GL_POINTS);160GL.glVertex2f(x, y);161GL.glEnd();162} else if (oriented || scaleY != 1.0f) {163GL.glPushMatrix();164165GL.glTranslatef(x, y, 0f);166167if (oriented) {168float angle = (float) (Math.atan2(y, x) * 180 / Math.PI);169GL.glRotatef(angle, 0f, 0f, 1.0f);170}171172// scale173GL.glScalef(1.0f, scaleY, 1.0f);174175image.draw((int) (-(size / 2)), (int) (-(size / 2)), (int) size,176(int) size, color);177GL.glPopMatrix();178} else {179color.bind();180image.drawEmbedded((int) (x - (size / 2)), (int) (y - (size / 2)),181(int) size, (int) size);182}183}184185/**186* Update the state of this particle187*188* @param delta189* The time since the last update190*/191public void update(int delta) {192emitter.updateParticle(this, delta);193life -= delta;194195if (life > 0) {196x += delta * velx;197y += delta * vely;198} else {199engine.release(this);200}201}202203/**204* Initialise the state of the particle as it's reused205*206* @param emitter207* The emitter controlling this particle208* @param life209* The life the particle should have (in milliseconds)210*/211public void init(ParticleEmitter emitter, float life) {212x = 0;213this.emitter = emitter;214y = 0;215velx = 0;216vely = 0;217size = 10;218type = 0;219this.originalLife = this.life = life;220oriented = false;221scaleY = 1.0f;222}223224/**225* Set the type of this particle226*227* @param type228* The type of this particle229*/230public void setType(int type) {231this.type = type;232}233234/**235* Indicate how this particle should be renered236*237* @param usePoints238* The indicator for rendering239* @see #USE_POINTS240* @see #USE_QUADS241* @see #INHERIT_POINTS242*/243public void setUsePoint(int usePoints) {244this.usePoints = usePoints;245}246247/**248* Get the type of this particle249*250* @return The type of this particle251*/252public int getType() {253return type;254}255256/**257* Set the size of the particle258*259* @param size260* The size of the particle (in pixels)261*/262public void setSize(float size) {263this.size = size;264}265266/**267* Adjust the size of the particle268*269* @param delta270* The amount to adjust the size by (in pixels)271*/272public void adjustSize(float delta) {273size += delta;274size = Math.max(0, size);275}276277/**278* Set the life of the particle279*280* @param life281* The life of the particle in milliseconds282*/283public void setLife(float life) {284this.life = life;285}286287/**288* Adjust the life othe particle289*290* @param delta291* The amount to adjust the particle by (in milliseconds)292*/293public void adjustLife(float delta) {294life += delta;295}296297/**298* Kill the particle, stop it rendering and send it back to the engine for299* use.300*/301public void kill() {302life = 1;303}304305/**306* Set the color of the particle307*308* @param r309* The red component of the color310* @param g311* The green component of the color312* @param b313* The blue component of the color314* @param a315* The alpha component of the color316*/317public void setColor(float r, float g, float b, float a) {318color.r = r;319color.g = g;320color.b = b;321color.a = a;322}323324/**325* Set the position of this particle326*327* @param x328* The new x position of the particle329* @param y330* The new y position of the particle331*/332public void setPosition(float x, float y) {333this.x = x;334this.y = y;335}336337/**338* Set the velocity of the particle339*340* @param dirx341* The x component of the new velocity342* @param diry343* The y component of the new velocity344* @param speed345* The speed in the given direction346*/347public void setVelocity(float dirx, float diry, float speed) {348this.velx = dirx * speed;349this.vely = diry * speed;350}351352/**353* Set the current speed of this particle354*355* @param speed The speed of this particle356*/357public void setSpeed(float speed) {358float currentSpeed = (float) Math.sqrt((velx*velx) + (vely*vely));359velx *= speed;360vely *= speed;361velx /= currentSpeed;362vely /= currentSpeed;363}364365/**366* Set the velocity of the particle367*368* @param velx The x component of the new velocity369* @param vely The y component of the new velocity370*/371public void setVelocity(float velx, float vely) {372setVelocity(velx,vely,1);373}374375/**376* Adjust (add) the position of this particle377*378* @param dx379* The amount to adjust the x component by380* @param dy381* The amount to adjust the y component by382*/383public void adjustPosition(float dx, float dy) {384x += dx;385y += dy;386}387388/**389* Adjust (add) the color of the particle390*391* @param r392* The amount to adjust the red component by393* @param g394* The amount to adjust the green component by395* @param b396* The amount to adjust the blue component by397* @param a398* The amount to adjust the alpha component by399*/400public void adjustColor(float r, float g, float b, float a) {401color.r += r;402color.g += g;403color.b += b;404color.a += a;405}406407/**408* Adjust (add) the color of the particle409*410* @param r411* The amount to adjust the red component by412* @param g413* The amount to adjust the green component by414* @param b415* The amount to adjust the blue component by416* @param a417* The amount to adjust the alpha component by418*/419public void adjustColor(int r, int g, int b, int a) {420color.r += (r / 255.0f);421color.g += (g / 255.0f);422color.b += (b / 255.0f);423color.a += (a / 255.0f);424}425426/**427* Adjust (add) the direction of this particle428*429* @param dx430* The amount to adjust the x component by431* @param dy432* The amount to adjust the y component by433*/434public void adjustVelocity(float dx, float dy) {435velx += dx;436vely += dy;437}438439/**440* Get the emitter that owns this particle441*442* @return The emitter that owns this particle443*/444public ParticleEmitter getEmitter() {445return emitter;446}447448/**449* @see java.lang.Object#toString()450*/451public String toString() {452return super.toString() + " : " + life;453}454455/**456* Check if this particle is being oriented based on it's velocity457*458* @return True if this particle being oriented based on it's velocity459*/460public boolean isOriented() {461return oriented;462}463464/**465* Indicate if this particle should be oriented based on it's velocity466*467* @param oriented True if this particle is being oriented based on it's velocity468*/469public void setOriented(boolean oriented) {470this.oriented = oriented;471}472473/**474* Get the current scalar applied on the y axis475*476* @return The scalar applied on the y axis477*/478public float getScaleY() {479return scaleY;480}481482/**483* Set the current scalar applied on the y axis484*485* @param scaleY The new scalar to apply on the y axis486*/487public void setScaleY(float scaleY) {488this.scaleY = scaleY;489}490}491492493