Starsector API
Loading...
Searching...
No Matches
ValueShifterUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2
3import java.util.Iterator;
4import java.util.LinkedHashMap;
5import java.util.Map;
6
7public class ValueShifterUtil implements ValueShifterAPI {
8
9 public static class ShiftData2 {
10 public float to;
11 public FaderUtil fader;
12 public float shift;
13 public boolean nudged;
14 }
15
16 protected float base;
17 protected float curr;
18 protected boolean useSquareOfProgress = true;
19 transient protected float averageShift;
21
22 public ValueShifterUtil(float base) {
23 this.base = base;
24 this.curr = base;
25 }
26
27 public boolean isUseSquareOfProgress() {
29 }
30
32 this.useSquareOfProgress = useSquareOfProgress;
33 }
34
35
36 public boolean isShifted() {
37 //return base != curr;
38 return !data.isEmpty();
39 }
40
41 public float getBase() {
42 return base;
43 }
44
45 public void setBase(float base) {
46 this.base = base;
47 }
48
49 public float getCurr() {
50 return curr;
51 }
52
53 public void shift(Object source, float to, float durIn, float durOut, float shift) {
54 ShiftData2 sd = data.get(source);
55 if (sd == null) {
56 sd = new ShiftData2();
57 sd.fader = new FaderUtil(0, durIn, durOut);
58 sd.fader.setBounceDown(true);
59 data.put(source, sd);
60 }
61 sd.to = to;
62 sd.shift = shift;
63 sd.fader.setDuration(durIn, durOut);
64 sd.fader.fadeIn();
65 sd.nudged = true;
66 }
67
68 public void advance(float amount) {
69 Iterator<ShiftData2> iter = data.values().iterator();
70 while (iter.hasNext()) {
71 ShiftData2 sd = iter.next();
72 if (!sd.nudged) sd.fader.fadeOut();
73 sd.nudged = false;
74 sd.fader.advance(amount);
75 if (sd.fader.isFadedOut()) {
76 iter.remove();
77 }
78 }
79 updateCurr();
80 }
81
82 protected void updateCurr() {
83 if (data.isEmpty()) {
84 curr = base;
85 averageShift = 0f;
86 return;
87 }
88
89 float totalWeight = 0f;
90 for (ShiftData2 sd : data.values()) {
91 float progress = sd.fader.getBrightness();
92 if (useSquareOfProgress) progress *= progress;
93 totalWeight += progress;
94 }
96 //totalWeight = data.size();
97
98 if (totalWeight <= 0) {
99 curr = base;
100 return;
101 }
102
103 float result = base;
104
105 for (ShiftData2 sd : data.values()) {
106 float progress = sd.fader.getBrightness();
107 if (useSquareOfProgress) progress *= progress;
108
109 result += (sd.to - base) * sd.shift * progress / (totalWeight - progress + 1f);
110 }
111
112 curr = result;
113 }
114
115 public float getAverageShift() {
116 return averageShift;
117 }
118
119 public float getShiftProgress(Object key) {
120 if (data.containsKey(key)) {
121 return data.get(key).fader.getBrightness();
122 }
123 return 0f;
124 }
125
126 public static void main(String[] args) {
128
129 for (int i = 0; i < 10; i++) {
130 c.shift("c1", 25f, 1f, 1f, 1f);
131 c.shift("c2", 20f, 1f, 1f, 1f);
132 c.advance(0.1f);
133 }
134
135 System.out.println(c.getCurr());
136
137// c.advance(0.1f);
138// System.out.println(c.getCurr());
139 }
140
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
void setUseSquareOfProgress(boolean useSquareOfProgress)
void shift(Object source, float to, float durIn, float durOut, float shift)