Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rdlloyd;
- import java.awt.Frame;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.File;
- import java.util.Random;
- import org.lwjgl.LWJGLUtil;
- import org.lwjgl.opengl.Display;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.CanvasGameContainer;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.geom.Vector2f;
- /**
- * Simple particle stuff. It's hacky but it works :D
- *
- * @author Stefan Lange & Florian Valerius
- * @version 1.0.0
- * @since 15.08.2011
- */
- public class ParticleAction extends BasicGame {
- private static int width = 800, height = 600;
- private Random RND = new Random();
- private boolean h4c7orzBool = true, gravityRandom = false;
- private Particle particles[];
- private Vector2f temp = new Vector2f();
- private float minColor = 0.1f, maxColor = 0.6f, colorSpeed = 0.5f;
- private int state = 0;
- private Color loopyColorShit = new Color(maxColor, minColor, minColor, 1.0f);
- private int maxNum;
- public ParticleAction() {
- super("AWESOME PARTICLE LOLOLOL");
- }
- public static void main(String[] args) throws SlickException {
- System.setProperty("org.lwjgl.librarypath",
- new File(new File(System.getProperty("user.dir"), "native"), LWJGLUtil.getPlatformName()).getAbsolutePath());
- System.setProperty("net.java.games.input.librarypath", System.getProperty("org.lwjgl.librarypath"));
- // enable resizing
- CanvasGameContainer container = new CanvasGameContainer(new ParticleAction());
- Frame frame = new Frame("AWESOME PARTICLE LOLOLOL");
- frame.setSize(800,600);
- frame.add(container);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- Display.destroy();
- System.exit(0);
- }
- });
- frame.setVisible(true);
- container.start();
- container.requestFocus();
- // Old version with fixed window size
- /*
- AppGameContainer app = new AppGameContainer(new ParticleAction());
- app.setDisplayMode(width, height, false);
- app.start();
- app.setClearEachFrame(false);
- */
- }
- // yes.
- public void init(GameContainer container) throws SlickException {
- particles = new Particle[100000];
- maxNum = 50000;
- for(int i = 0; i < particles.length; i++)
- particles[i] = new Particle();
- for(Particle p : particles) {
- p.px = RND.nextFloat() * width;
- p.py = RND.nextFloat() * height;
- p.vx = 0;
- p.vy = 0;
- p.color = loopyColorShit;
- }
- }
- // stuff gets updated
- public void update(GameContainer container, int delta) throws SlickException {
- Input in = container.getInput();
- h4c7orzBool = true;
- int pressState = 0;
- if(in.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
- pressState = 1;
- else if(in.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
- pressState = 2;
- if(in.isKeyDown(Input.KEY_LEFT))
- maxNum -= 1 * delta;
- if(in.isKeyDown(Input.KEY_RIGHT))
- maxNum += 1 * delta;
- if(in.isKeyPressed(Input.KEY_S))
- gravityRandom = !gravityRandom;
- if(in.isKeyPressed(Input.KEY_ESCAPE)) {
- Display.destroy();
- System.exit(0);
- }
- width = container.getWidth();
- height = container.getHeight();
- System.out.println(width);
- if(maxNum < 100)
- maxNum = 100;
- if(maxNum > particles.length)
- maxNum = particles.length;
- if(pressState == 1)
- h4c7orzBool = false;
- cycleColor();
- for(int i = 0; i < maxNum; i++) {
- particles[i].update(delta);
- particles[i].color = loopyColorShit;
- if(pressState == 1) {
- suckHard( i, in, delta );
- }else if( pressState == 2 ) {
- suckHard( i, in, delta );
- spinHard( i, in, delta );
- }
- }
- }
- // goes trough min <-> max color values
- private void cycleColor() {
- switch (state) {
- case 0:
- loopyColorShit.g += colorSpeed * 1.0f / 255f;
- if(loopyColorShit.g >= maxColor) {
- state = 1;
- loopyColorShit.g = maxColor;
- }
- break;
- case 1:
- loopyColorShit.r -= colorSpeed * 1.0f / 255f;
- if(loopyColorShit.r <= minColor) {
- state = 2;
- loopyColorShit.r = minColor;
- }
- break;
- case 2:
- loopyColorShit.b += colorSpeed * 1.0f / 255f;
- if(loopyColorShit.b >= maxColor) {
- state = 3;
- loopyColorShit.b = maxColor;
- }
- break;
- case 3:
- loopyColorShit.g -= colorSpeed * 1.0f / 255f;
- if(loopyColorShit.g <= minColor) {
- state = 4;
- loopyColorShit.g = minColor;
- }
- break;
- case 4:
- loopyColorShit.r += colorSpeed * 1.0f / 255f;
- if(loopyColorShit.r >= maxColor) {
- state = 5;
- loopyColorShit.r = maxColor;
- }
- break;
- case 5:
- loopyColorShit.b -= colorSpeed * 1.0f / 255f;
- if(loopyColorShit.b <= minColor) {
- state = 0;
- loopyColorShit.b = minColor;
- }
- break;
- }
- }
- // sucks the dots to the mouse bro
- private void suckHard( int i, Input in, int delta ) {
- temp.x = (in.getMouseX() - particles[i].px);
- temp.y = (in.getMouseY() - particles[i].py);
- temp.normalise();
- if(gravityRandom) {
- particles[i].vx += temp.x * delta * 0.01f * Math.random();
- particles[i].vy += temp.y * delta * 0.01f * Math.random();
- }else {
- particles[i].vx += temp.x * delta * 0.01f;
- particles[i].vy += temp.y * delta * 0.01f;
- }
- }
- // spins the dots around you!
- private void spinHard( int i, Input in, int delta ) {
- temp.x = (in.getMouseX() - particles[i].px);
- temp.y = (in.getMouseY() - particles[i].py);
- temp.normalise();
- temp = temp.getPerpendicular();
- if(gravityRandom) {
- particles[i].vx += temp.x * delta * 0.004f * Math.random();
- particles[i].vy += temp.y * delta * 0.004f * Math.random();
- }else {
- particles[i].vx += temp.x * delta * 0.004f;
- particles[i].vy += temp.y * delta * 0.004f;
- }
- }
- // stuff gets rendered
- public void render(GameContainer container, Graphics g) throws SlickException {
- g.setDrawMode(Graphics.MODE_ADD);
- for(int i = 0; i < maxNum; i++)
- particles[i].render(g);
- g.setDrawMode(Graphics.MODE_NORMAL);
- g.setColor(Color.white);
- g.drawString("Number of Particles: " + maxNum, 10, 25);
- g.drawString("-> De-/Increase with Left/Right", 10, 40);
- g.drawString("Random Gravitation(S): " + gravityRandom, 10, 55);
- }
- // Single Particle
- private class Particle {
- public float px, py, vx, vy, opx, opy;
- public Color color; // REF HOLDER
- public Particle() {}
- public void update(int delta) {
- opx = px;
- opy = py;
- px += vx * delta * 0.1f;
- py += vy * delta * 0.1f;
- if(h4c7orzBool) {
- vx -= vx * delta * 0.0003f;
- vy -= vy * delta * 0.0003f;
- }else {
- vx -= vx * delta * 0.0001f;
- vy -= vy * delta * 0.0001f;
- }
- if( px < 0.0f && vx < 0.0f ) {
- px = 0.0f;
- vx *= -0.7f;
- }else
- if( px > width && vx > 0.0f ) {
- px = width;
- vx *= -0.7f;
- }
- if( py < 0.0f && vy < 0.0f ) {
- py = 0.0f;
- vy *= -0.7f;
- }else
- if( py > height && vy > 0.0f ) {
- py = height;
- vy *= -0.7f;
- }
- }
- public void render(Graphics g) {
- g.setColor(color);
- // this makes the dots disappear when slowing down, since the line is not even a pixel long and stuff
- g.drawGradientLine(px, py, color,opx, opy, new Color(0, 0, 0, 0.2f));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement