Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted is distributed in the hope that it will be useful,                                  //
12
// 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
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  private long mLastTime, mPausedTime;
32
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  }
(3-3/5)