001package com.fs.starfarer.api.util;
002
003/**
004 * Meant to simulate brightness pattern of a lightning strike.
005 *
006 * Copyright 2015 Fractal Softworks, LLC
007 */
008public class FlickerUtilV2 {
009
010        public static final float UP_RATE = 25f;
011        public static final float DOWN_RATE = 5f;
012        public static final float END_PROB_PER_BURST = 0.1f;
013        
014        private float brightness;
015        private float dir = 1f;
016        private float wait;
017        private float maxWait;
018        private boolean stopBursts = false;
019        private boolean stopAll = false;
020        
021        private float angle;
022        private float currMax;
023        private float currDur;
024        private int numBursts = 0;
025        
026        private boolean peakFrame = false;
027        
028        public FlickerUtilV2() {
029                this(4f);
030        }
031        public FlickerUtilV2(float maxWait) {
032                this.maxWait = maxWait;
033                angle = (float) Math.random() * 360f;
034        }
035        
036        public float getAngle() {
037                return angle;
038        }
039        
040        public void newBurst() {
041                currMax = 0.75f + (float) Math.random() * 0.5f;
042                if (currMax > 1) currMax = 1;
043                if (currMax < brightness) currMax = brightness;
044                dir = 1f;
045                //currDur = 0f + (float) Math.random() * 0.5f;
046                currDur = 0f + (float) Math.random() * 0.5f;
047                currDur *= currDur;
048                currDur += 0.05f;
049                numBursts++;
050                peakFrame = true;
051                //angle = (float) Math.random() * 360f;
052        }
053        
054        public void newWait() {
055                wait = (float) Math.random() * maxWait;
056                numBursts = 0;
057                stopBursts = false;
058                angle = (float) Math.random() * 360f;
059        }
060        
061        public void setWait(float wait) {
062                this.wait = wait;
063        }
064        public void setNumBursts(int numBursts) {
065                this.numBursts = numBursts;
066        }
067        public boolean isPeakFrame() {
068                return peakFrame;
069        }
070        
071        public int getNumBursts() {
072                return numBursts;
073        }
074        
075        public float getWait() {
076                return wait;
077        }
078        
079        public void advance(float amount) {
080                peakFrame = false;
081                if (wait > 0) {
082                        wait -= amount;
083                        if (wait > 0) {
084                                return;
085                        } else {
086                                newBurst();
087                        }
088                }
089                
090                //float timeUp = Math.min(0.1f, currDur / 5f);
091                //float timeDown = currDur - timeUp;
092                if (dir > 0) {
093                        //brightness += amount / timeUp;
094                        brightness += amount * UP_RATE;
095                } else {
096                        //brightness -= amount / timeDown;
097                        brightness -= amount * DOWN_RATE;
098                }
099                
100                if (brightness < 0) brightness = 0;
101                
102                if (brightness >= currMax) {
103                        brightness = currMax;
104                        dir = -1;
105                }
106                
107                
108                currDur -= amount;
109                if (currDur <= 0) {
110                        if (!stopBursts && !stopAll) {
111                                if ((float) Math.random() < END_PROB_PER_BURST * (float) numBursts) {
112                                        stopBursts = true;
113                                } else {
114                                        newBurst();
115                                }
116                        } else if (!stopAll && brightness <= 0) {
117                                newWait();
118                        }
119                }
120                
121        }
122        
123        public void stop() {
124                stopAll = true;
125        }
126
127        public float getBrightness() {
128                return brightness;
129        }
130        
131        public static void main(String[] args) {
132                FlickerUtilV2 test = new FlickerUtilV2();
133                
134                for (int i = 0; i < 1000; i++) {
135                        test.advance(0.016f);
136                        System.out.println(test.getBrightness());
137                        //System.out.println(test.getAngle());
138                }
139        }
140        
141}
142
143
144
145
146
147
148
149