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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| package Game;
import de.matthiasmann.twl.utils.PNGDecoder; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.logging.Level; import java.util.logging.Logger; import org.lwjgl.opengl.GL11; import static org.lwjgl.opengl.GL11.*;
public class Particle_Generator { int particleCount = 0; int generationSpeed = 0; int generationNow = 0; Particle[] particles; private float x = 0; private float y = 0; private int life = 0; private float r = 0; private float g = 0; private float b = 0; private float a = 0; private boolean emitter = false; private float speed = 0; private float angle = 0; private float range = 0; ByteBuffer bufParticle; PNGDecoder decParticle; private class Particle { private Particle_Generator daddy = null; private float x = 0.0f; private float y = 0.0f; private float speed = 1.001f;
private float r = 0; private float g = 0; private float b = 0; private float a = 1;
private float personalAngle = 0; private float lifespan; private float angleDiff = 0; private boolean dead = false; public Particle(Particle_Generator generator) { daddy = generator; r = (float) (daddy.r + (Math.random()/10)); g = (float) (daddy.g + (Math.random()/10)); b = (float) (daddy.b + (Math.random()/10)); a = (float) (daddy.a + (Math.random()/10)); x = daddy.x; y = daddy.y; lifespan = daddy.life; speed = (float) (daddy.speed + (Math.random()*daddy.speed/10)); personalAngle = daddy.angle; float rangecalc = (float) Math.random(); boolean isAboveZero = false; if(rangecalc>.5) {isAboveZero = true;} rangecalc = (float) Math.random(); if (isAboveZero==true) {angleDiff = daddy.range*rangecalc;} else {angleDiff = -range*rangecalc;} personalAngle = personalAngle + angleDiff; } private void update() { if(Math.random()>.5){ x += 1*Math.random(); } else{ x -= 1*Math.random(); } if(Math.random()>.5){ y += 1*Math.random(); } else{ y -= 1*Math.random(); } x -= Math.sin(personalAngle) * speed; y += Math.cos(personalAngle) * speed; draw(); weaken(); } private void draw() { glColor4d(r,g,b,a); glTexCoord2d(1,0); glVertex2d(-10, -10); glTexCoord2d(1,1); glVertex2d(-10, 10); glTexCoord2d(0,1); glVertex2d(10, 10); glTexCoord2d(0,0); glVertex2d(10, -10); }
private void move(float x, float y){ this.x=x; this.y=y; } private void weaken(){ lifespan--; if (lifespan<0) {dead=true;} }
} public Particle_Generator(int size, int generate, int life, float r, float g, float b, float a, boolean emit, float x, float y, float speed, float angle, int range) { if(size<=0) { return; } particleCount = size; generationSpeed = generate; this.r = r; this.g = g; this.b = b; this.a = a; this.x = x; this.y = y; this.life = life; this.speed = speed; this.angle = angle; this.range = (float) (range*Math.PI/180); emitter = emit; generate(); particleTexture(); }
private void generate() { particles = new Particle[particleCount]; for(int i = 0; i < particleCount; i++) {particles[i] = new Particle(this);} for(int i = 0; i < particleCount; i++) {particles[i].lifespan = 1;} } public void update(boolean produce) { for(int i = 0; i < particleCount; i++) { if(particles[i] != null){ if(particles[i].dead==true){ particles[i] = null; } else {particles[i].update();} } else{ if (produce==true) { particles[i] = null; if(emitter){ if (generationSpeed>generationNow) { particles[i] = new Particle(this); generationNow++; } } } } } generationNow = 0; }
public void draw() { for(int i = 0; i < particleCount; i++) { if(particles[i] != null){ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decParticle.getWidth(), decParticle.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bufParticle); glBindTexture(GL_ADD, GL_LOAD); glBindTexture(GL_TEXTURE_2D,1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glPushMatrix(); glTranslated(particles[i].x+Core.SCREEN_X/2, particles[i].y+Core.SCREEN_Y/2, 0); glBegin(GL_QUADS); particles[i].draw(); glEnd(); glPopMatrix(); } } } public void move(float x, float y){ this.x = x; this.y = y; } public void killme(){ for(int i = 0; i < particleCount; i++) { if (particles[i]!=null) { particles[i] = null; } } } public void setspeed(float Speed){ this.speed = Speed; } public void setAngle(float Angle){ this.angle=Angle; } public void setRange(float Range){ this.range=(float) (Range*Math.PI/180); }
public void particleTexture() { InputStream in = null; try { in = new FileInputStream(YOUR_PARTICLE_TEXTURE); } catch (FileNotFoundException ex) { Logger.getLogger(Models.class.getName()).log(Level.SEVERE, null, ex); } try { decParticle = new PNGDecoder(in); bufParticle = ByteBuffer.allocateDirect(4*decParticle.getWidth()*decParticle.getHeight()); decParticle.decode(bufParticle, decParticle.getWidth()*4, PNGDecoder.Format.RGBA); bufParticle.flip(); } catch (IOException ex) { Logger.getLogger(Models.class.getName()).log(Level.SEVERE, null, ex); } try { in.close(); } catch (IOException ex) { Logger.getLogger(Models.class.getName()).log(Level.SEVERE, null, ex); } } } |