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
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
| 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;
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) { 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) { 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]); } } } }
|