Project

General

Profile

Download (3.18 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / wind / WindGust.java @ e50e7ba7

1 318dd77b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 318dd77b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 318dd77b Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 318dd77b Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 318dd77b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.wind;
21
22
import java.util.Random;
23
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
26
class WindGust
27
  {
28
  private static final long TIME_PERIOD = 1000;
29
  private int mAvgWind;
30
  private boolean mBlowingNow;
31 e50e7ba7 Leszek Koltunski
  private long mLastTime, mPausedTime;
32 318dd77b Leszek Koltunski
  private static Random mRnd = new Random();
33
34
  WindGust()
35
    {
36
    mAvgWind = 0;
37
    mBlowingNow = false;
38
    mLastTime = 0;
39
    }
40
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43
  void setAverageWind(int wind)
44
    {
45
    mAvgWind = wind;
46
    }
47
48 e50e7ba7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
  void pauseWind()
51
    {
52
    mPausedTime = System.currentTimeMillis();
53
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  void resumeWind()
58
    {
59
    long gap = System.currentTimeMillis() - mPausedTime;
60
    mLastTime += gap;
61
    }
62
63 318dd77b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  boolean isWindBlowingNow(long currTime)
66
    {
67
    if( mLastTime>0 )
68
      {
69
      int lastPeriod = (int)(mLastTime/TIME_PERIOD);
70
      int currPeriod = (int)( currTime/TIME_PERIOD);
71
72
      if( currPeriod>lastPeriod ) mBlowingNow=(mRnd.nextInt(100)<=mAvgWind);
73
      }
74
75
    mLastTime = currTime;
76
77
    return mBlowingNow;
78
    }
79
  }