All,
I wanted to share with you a quick class I drummed up based on examples I found on the Internet.
This class will add lightning and rain to your game.
It has several modes. You can set no rain, rainy, windy_rainy, and no storm, stormy and very stormy.
Stormy level controls the amount of lightning.
It is catered after the Slick2D interfaces…
Initialize it,
call update() in the update() loop
call render(g) in the render() loop
Hope someone finds this useful
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package slicktest;
import java.util.Random;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.opengl.SlickCallable;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
/**
*
* @author jcannonb
*/
public class StormManager
{
public static final short numdrops = 900;
public static final byte NO_RAIN = -1;
public static final byte HARD_RAIN = 0;
public static final byte HARD_WINDY_RAIN = 1;
public static final byte LIGHT_RAIN = 2;
public static final byte LIGHT_WINDY_RAIN = 3;
public static final byte NO_STORM = -1;
public static final byte RAINY = 0;
public static final byte STORMY = 1;
public static final byte VERY_STORMY = 2;
public float WINDY_RAIN_X_CHANGE=-1.0f;
public float HARD_RAIN_Y_CHNAGE = 4.0f;
public float SOFT_RAIN_X_CHANGE = 0.0f;
public float SOFT_RAIN_Y_CHANGE = 2.0f;
private LightningBoltEffect bolt;
private RainDrop[] drops;
private float rainXSpeed;
private float rainYSpeed;
private byte rainMode;
private byte stormMode;
private byte lightningChance;
public void changeStormyMode(byte mode)
{
stormMode = mode;
if (mode == NO_STORM)
{
lightningChance = (short)(0);
}
if (mode == RAINY)
{
lightningChance = (short)(255.0f*.01);
}
if (mode == STORMY)
{
lightningChance = (short)(255.0f*.10);
}
if (mode == VERY_STORMY)
{
lightningChance = (short)(255.0f*.25);
}
}
public void changeRainMode(byte mode)
{
rainMode = mode;
if (mode == NO_RAIN)
{
rainXSpeed = 0;
rainYSpeed = 0;
return;
}
if (mode == HARD_RAIN)
{
rainYSpeed = HARD_RAIN_Y_CHNAGE;
rainXSpeed = 0;
return;
}
if (mode == HARD_WINDY_RAIN)
{
rainYSpeed = HARD_RAIN_Y_CHNAGE;
rainXSpeed = WINDY_RAIN_X_CHANGE;
return;
}
if (mode == LIGHT_RAIN)
{
rainYSpeed = SOFT_RAIN_Y_CHANGE;
rainXSpeed = 0;
return;
}
if (mode == LIGHT_WINDY_RAIN)
{
rainYSpeed = SOFT_RAIN_Y_CHANGE;
rainXSpeed = WINDY_RAIN_X_CHANGE;
return;
}
}
public boolean isWeatherHappening()
{
return ((this.rainMode == NO_RAIN) && (this.stormMode == NO_STORM));
}
public StormManager()
{
this.rainMode = NO_RAIN;
this.stormMode = NO_STORM;
this.lightningChance = 0;
drops = new RainDrop[numdrops];
this.initializeRain();
System.out.printf("Add duration to this class!");
}
void initializeRain()
{
drops=new RainDrop[numdrops];
for(int i=0; i < numdrops; i++)
{
drops[i]=new RainDrop((int) (Math.random() * 10 + 4));
drops[i].setX((float) ((Math.random() * 2000)+30));
drops[i].setY((float) ((Math.random() * 750)));
}
}
public void update(int delta)
{
//Lightning Check
if (bolt != null)
{
bolt.update(delta);
}
if (this.stormMode != NO_STORM)
{
if (bolt == null)
{
if (this.lightningChance >= 0)
{
Random random = new Random();
short x =(short)random.nextInt(256);
short y =(short)random.nextInt(256);
short z =(short)random.nextInt(256);
if ( (x <= this.lightningChance) && (y <= this.lightningChance) && (z <= this.lightningChance) )
{
int duration = random.nextInt(600);
bolt = LightningBoltEffect.generateLightingBolt(new Vector2f(50, 240), new Vector2f(290, 240), duration);
}
}
}
}
if (this.rainMode != NO_RAIN)
{
//Rainy Check
for(int i=0; i <this.drops.length; i++)
{
drops[i].setY(drops[i].getY()+this.rainYSpeed);
drops[i].setX(drops[i].getX()+this.rainXSpeed);
if(drops[i].getY()>768)
{
drops[i].setX((float) ((Math.random() * 2000)+30));
drops[i].setY((float) ((Math.random() * 5)));
}
}
}
}
public void render(Graphics g)
{
if (this.stormMode != NO_STORM)
{
if (bolt != null)
{
System.out.printf("I want to draw some lightning!\n");
if (bolt.currentTime <= 0)
{
bolt = null;
}
if (bolt != null)
{
System.out.printf("I STILL want to draw some lightning!\n");
SlickCallable.enterSafeBlock();
bolt.render();
SlickCallable.leaveSafeBlock();
}
}
}
if (this.rainMode != NO_RAIN)
{
g.setColor(new Color(188.0f,227.0f,229.0f,0.3f));
for(int i=0; i <this.drops.length; i++)
{
g.draw(drops[i]);
}
}
}
}