Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Beginnings of a Slick2D weather class  (Read 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline LordChandar

Junior Member


Projects: 1



« Posted 2012-12-21 23:58:29 »

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  
/*
 * 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]);
            }
        }
    }
   
}
Offline P0jahn

Senior Member


Projects: 2



« Reply #1 - Posted 2012-12-22 15:15:34 »

this would attract more people if you actually provided some images on how this would look in game.
Offline LordChandar

Junior Member


Projects: 1



« Reply #2 - Posted 2012-12-22 23:15:38 »

Quite right...

<a href="http://www.youtube.com/v/H1JbTXJuAQM?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/H1JbTXJuAQM?version=3&amp;hl=en_US&amp;start=</a>

Here is a video of the new game play in general with the lightning and rain at the end of the video!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-12-23 04:18:18 »

Could have just skipped to the end or posted a single screen but from what I can tell it looks nice.

Next time use something like fraps or if you are on windows print screen to take better screens as that video is really bad.

Offline davedes
« Reply #4 - Posted 2012-12-23 07:47:17 »

You don't provide LightningBoltEffect...

For the curious, creating a convincing lightning bolt is actually very easy:
https://github.com/mattdesl/lwjgl-basics/wiki/LightningEffect

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (52 views)
2014-04-15 18:08:23

BurntPizza (49 views)
2014-04-15 03:46:01

UprightPath (64 views)
2014-04-14 17:39:50

UprightPath (46 views)
2014-04-14 17:35:47

Porlus (63 views)
2014-04-14 15:48:38

tom_mai78101 (88 views)
2014-04-10 04:04:31

BurntPizza (147 views)
2014-04-08 23:06:04

tom_mai78101 (244 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!