fixed particel to be more object orjented

This commit is contained in:
Ziver Koc 2007-03-21 21:55:03 +00:00
parent 7c8dba70d1
commit e3f5017007
7 changed files with 191 additions and 137 deletions

12
log.txt
View file

@ -1,6 +1,6 @@
2007-03-21 19:59:45:093 # Loading texture: data/units/tank.png
2007-03-21 19:59:46:203 # Loading texture: data/particle.bmp
2007-03-21 19:59:46:421 # Loading sound: data/sounds/test.wav
2007-03-21 19:59:46:484 # Sound sources: quantity=0
2007-03-21 19:59:46:484 # Stoping sound: source=0
2007-03-21 19:59:46:484 # Playing sound: source=0 buffer=261490744
2007-03-21 22:49:30:390 # Loading texture: data/units/tank.png
2007-03-21 22:49:31:187 # Loading texture: data/particle.bmp
2007-03-21 22:49:31:421 # Loading sound: data/sounds/test.wav
2007-03-21 22:49:31:437 # Sound sources: quantity=0
2007-03-21 22:49:31:437 # Stoping sound: source=0
2007-03-21 22:49:31:437 # Playing sound: source=0 buffer=261490744

Binary file not shown.

View file

@ -61,6 +61,15 @@ public class LWJGLGameWindow {
// Create the display window
Display.create();
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
// Enable Smooth Shading
GL11.glShadeModel(GL11.GL_SMOOTH);
// Black Background
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
// Depth Buffer Setup
GL11.glClearDepth(1.0f);
//Select The Projection Matrix
GL11.glMatrixMode(GL11.GL_PROJECTION);
//Reset The Projection Matrix
@ -72,14 +81,13 @@ public class LWJGLGameWindow {
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
// disable the OpenGL depth test since we're rendering 2D graphics
GL11.glDisable(GL11.GL_DEPTH_TEST);
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
//Type Of Blending To Perform
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
GL11.glDisable(GL11.GL_DEPTH_TEST);
//Enable Blending
GL11.glEnable(GL11.GL_BLEND);
//Type Of Blending To Perform
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true);

View file

@ -1,5 +1,4 @@
package ei.engine.effects;
/*
* This Code Was Created By Jeff Molofee and GB Schmick 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
@ -8,6 +7,10 @@ package ei.engine.effects;
* Visit Our Sites At www.tiptup.com and nehe.gamedev.net
*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
@ -34,7 +37,7 @@ import ei.engine.util.MultiPrintStream;
* 2004-12-19: Updated to version 0.94alpha of LWJGL and to use
* DevIL for image loading.
*/
public class CopyOfLesson19 {
public class Lesson19 {
private boolean done = false;
private boolean fullscreen = false;
private final String windowTitle = "NeHe's OpenGL Lesson 19 for LWJGL (Particle Engine Using Triangle Strips)";
@ -50,7 +53,7 @@ public class CopyOfLesson19 {
float slowdown = 2.0f; // Slow Down Particles
float xspeed; // Base X Speed (To Allow Keyboard Direction Of Tail)
float yspeed; // Base Y Speed (To Allow Keyboard Direction Of Tail)
float zoom = -40.0f; // Used To Zoom Out
float zoom = -20.0f; // Used To Zoom Out
int col; // Current Color Selection
int delay; // Rainbow Effect Delay
@ -72,7 +75,7 @@ public class CopyOfLesson19 {
}
}
CopyOfLesson19 l19 = new CopyOfLesson19();
Lesson19 l19 = new Lesson19();
l19.run(fullscreen);
}
public void run(boolean fullscreen) {
@ -186,7 +189,12 @@ public class CopyOfLesson19 {
texture = loadTexture("data/particle.bmp");
}
private void initGL() { // All Setup For OpenGL Goes Here
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
GL11.glClearDepth(1.0f); // Depth Buffer Setup
// Really Nice Perspective Calculations
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
@ -197,12 +205,10 @@ public class CopyOfLesson19 {
0.1f,100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
//GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); // Type Of Blending To Perform
GL11.glEnable(GL11.GL_BLEND); // Enable Blending
for (int i=0;i<MAX_PARTICLES;i++) // Initials All The Textures
{
particle[i].active = true; // Make All The Particles Active
@ -305,22 +311,6 @@ public class CopyOfLesson19 {
return tex.getGLTarget(); // Return Image Address In Memory
}
}
/*
class Particle { // Particles Structure
public boolean active; // Active (Yes/No)
public float life; // Particle Life
public float fade; // Fade Speed
public float r; // Red Value
public float g; // Green Value
public float b; // Blue Value
public float x; // X Position
public float y; // Y Position
public float z; // Z Position
public float xi; // X Direction
public float yi; // Y Direction
public float zi; // Z Direction
public float xg; // X Gravity
public float yg; // Y Gravity
public float zg; // Z Gravity
}
*/

View file

@ -30,25 +30,37 @@ import ei.engine.texture.TextureLoader;
*
*/
public class Particles extends Entity{
private final int MAX_PARTICLES = 1000;
private Particle particle[];
boolean rainbow = true; // Rainbow Mode?
float slowdown = 1.0f; // Slow Down Particles
float xspeed = 00; // Base X Speed (To Allow Keyboard Direction Of Tail)
float yspeed = 000; // Base Y Speed (To Allow Keyboard Direction Of Tail)
float zoom = 0.0f; // Used To Zoom Out
int col; // Current Color Selection
int delay; // Rainbow Effect Delay
Texture texture; // Storage For Our Particle Texture
public boolean regenerate = true;
public boolean enabled = true;
public boolean rainbow = true; // Rainbow Mode?
public float slowdown = 1.0f; // Slow Down Particles
public float xspeed = 00; // Base X Speed (To Allow Keyboard Direction Of Tail)
public float yspeed = 000; // Base Y Speed (To Allow Keyboard Direction Of Tail)
public float MaxSpeedX = 500; // The Max Random Speed On X Axis
public float MaxSpeedY = 500; // The Max Random Speed On Y Axis
public float MaxSpeedZ = 500; // The Max Random Speed On Z Axis
public float pullForceX = 0.0f; // Set Horizontal Pull To Zero
public float pullForceY = -0.8f; // Set Vertical Pull Downward
public float pullForceZ = 0.0f; // Set Pull On Z Axis To Zero
public float life = 1.0f;
public float size = 20.0f;
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}
};
private final int MAX_PARTICLES = 1000;
private Particle particle[];
private int col; // Current Color Selection
private int delay; // Rainbow Effect Delay
private Texture texture; // Storage For Our Particle Texture
public Particles(String name){
super(name);
@ -74,119 +86,142 @@ public class Particles extends Entity{
if(rainbow && (delay > 25)) {
delay = 0; // Reset The Rainbow Color Cycling Delay
col++; // Change The Particle Color
if(col > 11) {
if(col >= colors.length) {
col = 0; // If Color Is Too High Reset It
}
}
delay++; // Increase Rainbow Mode Color Cycling Delay Counter
}
private void setExplotion() {
private void init() {
for (int i=0;i<MAX_PARTICLES;i++){ // Initials All The Textures
particle[i].active = true; // Make All The Particles Active
particle[i].regenerate = false; // set if the partical whill regenerate
particle[i].size = 20.0f; // Set Vertical size
particle[i].life = 1.0f; // Give All The Particles Full Life
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 * (12 / MAX_PARTICLES)][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (12 / MAX_PARTICLES)][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (12 / MAX_PARTICLES)][2]; // Select Red Rainbow Color
particle[i].xi = ((float)((Math.random() * 500.0)) - 260.0f) * 10.0f; // Random Speed On X Axis
particle[i].yi = ((float)((Math.random() * 500.0)) - 250.0f) * 10.0f; // Random Speed On Y Axis
particle[i].zi = ((float)((Math.random() * 500.0)) - 250.0f) * 10.0f; // Random Speed On Z Axis
particle[i].xg = 0.0f; // Set Horizontal Pull To Zero
particle[i].yg = -0.8f; // Set Vertical Pull Downward
particle[i].zg = 0.0f; // Set Pull On Z Axis To Zero
particle[i].r = colors[i * (colors.length / MAX_PARTICLES)][0]; // Select Red Rainbow Color
particle[i].g = colors[i * (colors.length / MAX_PARTICLES)][1]; // Select Red Rainbow Color
particle[i].b = colors[i * (colors.length / MAX_PARTICLES)][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
}
}
private void setExplotion() {
MaxSpeedX = 500;
MaxSpeedY = 500;
MaxSpeedZ = 500;
pullForceX = 0.0f;
pullForceY = -0.8f;
pullForceZ = 0.0f;
life = 6.0f;
size = 20.0f;
//regenerate = false;
init();
}
public void render() {
// store the current model matrix
GL11.glPushMatrix();
//Reset The Current Modelview Matrix
GL11.glLoadIdentity();
//Sets the location
super.setTranslationGL();
texture.bindGL();
for(int i=0;i<MAX_PARTICLES;i++) { // Loop Through All The Particles
if (particle[i].active) { // If The Particle Is Active
float x = particle[i].x; // Grab Our Particle X Position
float y = particle[i].y; // Grab Our Particle Y Position
float z = particle[i].z + zoom; // Particle Z Pos + Zoom
float size = particle[i].size;
// Draw The Particle Using Our RGB Values, Fade The Particle Based On It's Life
GL11.glColor4f(particle[i].r, particle[i].g, particle[i].b, particle[i].life);
//Build Quad From A Triangle Strip
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);{
//Top Right
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x + size, y + size, z);
//Top Left
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x - size, y + size, z);
//Bottom Right
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x + size, y - size, z);
//Bottom Left
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x - size, y - size, z);
//Done Building Triangle Strip
}GL11.glEnd();
particle[i].x += particle[i].xi / (slowdown * 1000);// Move On The X Axis By X Speed
particle[i].y += particle[i].yi / (slowdown * 1000);// Move On The Y Axis By Y Speed
particle[i].z += particle[i].zi / (slowdown * 1000);// Move On The Z Axis By Z Speed
particle[i].xi += particle[i].xg; // Take Pull On X Axis Into Account
particle[i].yi += particle[i].yg; // Take Pull On Y Axis Into Account
particle[i].zi += particle[i].zg; // Take Pull On Z Axis Into Account
particle[i].life -= particle[i].fade; // Reduce Particles Life By 'Fade'
if (particle[i].life < 0.0f) { // If Particle Is Burned Out
particle[i].life = 1.0f; // Give It New Life
particle[i].size = 15.0f; // Particle size
particle[i].fade = ((float)(Math.random() * 1000.0)) / 1000.0f + 0.003f; // Random Fade Value
particle[i].x = 0.0f; // Center On X Axis
particle[i].y = 0.0f; // Center On Y Axis
particle[i].z = 0.0f; // Center On Z Axis
particle[i].xi = ((float)((Math.random() * 500.0)) - 250.0f) * 10.0f; // X Axis Speed And Direction
particle[i].yi = ((float)((Math.random() * 500.0)) - 250.0f) * 10.0f; // Y Axis Speed And Direction
particle[i].zi = ((float)((Math.random() * 500.0)) - 250.0f) * 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
if(!particle[i].regenerate){
particle[i].active = false;
}
}
}
}
//Reset The Current Modelview Matrix
GL11.glLoadIdentity();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
if(enabled){
// store the current model matrix
GL11.glPushMatrix();
//Reset The Current Modelview Matrix
GL11.glLoadIdentity();
texture.bindGL();
for(int i=0;i<MAX_PARTICLES;i++) { // Loop Through All The Particles
if (particle[i].active) { // If The Particle Is Active
float x = particle[i].x; // Grab Our Particle X Position
float y = particle[i].y; // Grab Our Particle Y Position
float z = particle[i].z; // Particle Z Pos + Zoom
// Draw The Particle Using Our RGB Values, Fade The Particle Based On It's Life
GL11.glColor4f(particle[i].r, particle[i].g, particle[i].b, particle[i].life);
//Build Quad From A Triangle Strip
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);{
//Top Right
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(x + size, y + size, z);
//Top Left
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(x - size, y + size, z);
//Bottom Right
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(x + size, y - size, z);
//Bottom Left
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(x - size, y - size, z);
//Done Building Triangle Strip
}GL11.glEnd();
particle[i].x += particle[i].xi / (slowdown * 1000);// Move On The X Axis By X Speed
particle[i].y += particle[i].yi / (slowdown * 1000);// Move On The Y Axis By Y Speed
particle[i].z += particle[i].zi / (slowdown * 1000);// Move On The Z Axis By Z Speed
particle[i].xi += particle[i].xg; // Take Pull On X Axis Into Account
particle[i].yi += particle[i].yg; // Take Pull On Y Axis Into Account
particle[i].zi += particle[i].zg; // Take Pull On Z Axis Into Account
particle[i].life -= particle[i].fade; // Reduce Particles Life By 'Fade'
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
particle[i].fade = ((float)(Math.random() * 100.0)) / 1000.0f + 0.003f; // Random Fade Value
particle[i].x = getLocation().getX(); // Center On X Axis
particle[i].y = getLocation().getY(); // Center On Y Axis
particle[i].z = getZ(); // Center On Z Axis
particle[i].xi = ((float)((Math.random() * MaxSpeedX)) - (MaxSpeedX/2)) * 10.0f; // X Axis Speed And Direction
particle[i].yi = ((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].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
if(!regenerate){
particle[i].active = false;
}
}
}
}
//Reset The Current Modelview Matrix
GL11.glLoadIdentity();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
}
}
class Particle { // Particles Structure
public boolean active; // Active (Yes/No)
public boolean regenerate; // Regenerate (Yes/No)
public float size; // Particle Size
public float life; // Particle Life
public float fade; // Fade Speed
public float r; // Red Value
public float g; // Green Value
public float b; // Blue Value
public float x; // X Position
public float y; // Y Position
public float z; // Z Position
public float xi; // X Direction
public float yi; // Y Direction
public float zi; // Z Direction
public float xg; // X Gravity
public float yg; // Y Gravity
public float zg; // Z Gravity

View file

@ -27,6 +27,9 @@ public abstract class Entity {
/** The size of this entity in pixels*/
private Vector2f size;
/** The z layer of the screen*/
private int z = 0;
/**
* creates a new entity
*
@ -46,6 +49,24 @@ public abstract class Entity {
return name;
}
/**
* Get the z value of the entity
*
* @return The z value
*/
public int getZ() {
return z;
}
/**
* set the z value of the entity
*
* @param l The z value to set
*/
public void setZ(int l) {
z = l;
}
/**
* Get the Location of the entity
*

View file

@ -20,7 +20,7 @@ public class InGameState extends GameState{
sprite1 = new Sprite("tank","data/units/tank.png");
//sprite1.setScale(new Vector2f(0.5f,0.5f));
sprite1.setLocation(new Vector2f(300,300));
//sprite1.setLocation(new Vector2f(300,300));
rootNode.add(sprite1);
p = new Particles("particle");