Starsector API
Loading...
Searching...
No Matches
GenericFieldItemSprite.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign;
2
3import java.awt.Color;
4
5import org.lwjgl.opengl.GL11;
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.SectorEntityToken;
10import com.fs.starfarer.api.graphics.SpriteAPI;
11import com.fs.starfarer.api.impl.campaign.ids.Tags;
12import com.fs.starfarer.api.util.FaderUtil;
13import com.fs.starfarer.api.util.Misc;
14
16
17 //public static final float CELL_SIZE = 32;
18
19 protected SpriteAPI shadowMask;
20 protected SpriteAPI sprite;
21 protected SpriteAPI glow;
22
23 protected float width;
24 protected float height;
25
26 protected SectorEntityToken entity;
27
28 protected Vector2f loc = new Vector2f();
29 protected Vector2f vel = new Vector2f();
30 protected float timeLeft = 0f;
31 protected float facing = 0f;
32 protected float angVel = 0f;
33 protected FaderUtil fader = new FaderUtil(0f, 0.2f, 0.2f); // days
34
35// protected FaderUtil glowBounce;
36// protected FaderUtil glowFader = new FaderUtil(0f, 0.1f, 0.1f);
37
38
39 public GenericFieldItemSprite(SectorEntityToken entity, String category, String key, float cellSize, float size, float spawnRadius) {
40 this.entity = entity;
41
42// glowBounce = new FaderUtil(0f, 0.1f + (float) Math.random() * 0.1f, 0.1f + (float) Math.random() * 0.1f); // days
43// glowBounce.setBounce(true, true);
44// glowBounce.fadeIn();
45
46// if ((float) Math.random() < field.getPieceGlowProbability()) {
47// glowFader.fadeIn();
48// }
49
50 sprite = Global.getSettings().getSprite(category, key);
51 //glow = Global.getSettings().getSprite("terrain", "debrisFieldGlowSheet");
52
53
54 //float sizeRangeMult = 0.5f + 0.5f * field.params.density;
55 this.width = size;
56 this.height = width;
57
58 float w = sprite.getWidth();
59 float h = sprite.getHeight();
60 int cols = (int) (w / cellSize);
61 int rows = (int) (h / cellSize);
62
63
64 float cellX = (int) (Math.random() * cols);
65 float cellY = (int) (Math.random() * rows);
66
67 float ctw = sprite.getTextureWidth() / (float) cols;
68 float cth = sprite.getTextureHeight() / (float) rows;
69
70 sprite.setTexX(cellX * ctw);
71 sprite.setTexY(cellY * cth);
72 sprite.setTexWidth(ctw);
73 sprite.setTexHeight(cth);
74
75 if (glow != null) {
76 glow.setTexX(cellX * ctw);
77 glow.setTexY(cellY * cth);
78 glow.setTexWidth(ctw);
79 glow.setTexHeight(cth);
80 glow.setSize(width, height);
81 //glow.setColor(field.getParams().glowColor);
82 }
83
84 sprite.setSize(width, height);
85
86 //glow.setColor(new Color(255,165,100,255));
87
88 shadowMask = Global.getSettings().getSprite("graphics/fx/ship_shadow_mask.png");
89 shadowMask.setSize(width * 1.5f, height * 1.5f);
90
91 fader.fadeIn();
92
93 facing = (float) Math.random() * 360f;
94 angVel = (float) Math.random() * 360f - 180f;
95
96 float r = (float) Math.random();
97 r = (float) Math.sqrt(r);
98 float dist = r * spawnRadius;
99
100 loc = Misc.getUnitVectorAtDegreeAngle((float)Math.random() * 360f);
101 loc.scale(dist);
102
103 vel = Misc.getUnitVectorAtDegreeAngle((float)Math.random() * 360f);
104
105 vel = Misc.getPerp(loc);
106 float off = 0.25f;
107 vel.x += off - (float) Math.random() * 0.5f * off;
108 vel.y += off - (float) Math.random() * 0.5f * off;
109 if ((float) Math.random() > 0.5f) {
110 vel.negate();
111 }
112 Misc.normalise(vel);
113
114 float speed = 10f + (float) Math.random() * 10f;
115 vel.scale(speed);
116
117 timeLeft = 1f + (float) Math.random();
118
119 }
120
121 public void render(float alphaMult) {
122
123 //alphaMult *= fader.getBrightness();
124 if (alphaMult <= 0) return;
125
126 SectorEntityToken lightSource = entity.getLightSource();
127 if (lightSource != null && entity.getLightColor() != null) {
128 sprite.setColor(entity.getLightColor());
129 } else {
130 sprite.setColor(Color.white);
131 }
132
133 sprite.setAngle(facing - 90);
134 sprite.setNormalBlend();
135 sprite.setAlphaMult(alphaMult * fader.getBrightness());
136 sprite.renderAtCenter(loc.x, loc.y);
137
138 if (lightSource != null && !entity.getLightSource().hasTag(Tags.AMBIENT_LS)) {
139 float w = shadowMask.getWidth() * 1.41f;
140 float h = w;
141
142 // clear out destination alpha in area we care about
143 GL11.glColorMask(false, false, false, true);
144 GL11.glPushMatrix();
145 GL11.glTranslatef(loc.x, loc.y, 0);
146 Misc.renderQuadAlpha(0 - w/2f - 1f, 0 - h/2f - 1f, w + 2f, h + 2f, Misc.zeroColor, 0f);
147 GL11.glPopMatrix();
148 sprite.setBlendFunc(GL11.GL_ONE, GL11.GL_ZERO);
149 sprite.renderAtCenter(loc.x, loc.y);
150
151 float lightDir = Misc.getAngleInDegreesStrict(entity.getLocation(), lightSource.getLocation());
152 shadowMask.setAlphaMult(alphaMult);
153 shadowMask.setAngle(lightDir);
154 shadowMask.setBlendFunc(GL11.GL_ZERO, GL11.GL_SRC_ALPHA);
155 shadowMask.renderAtCenter(loc.x, loc.y);
156
157 GL11.glColorMask(true, true, true, false);
158 shadowMask.setBlendFunc(GL11.GL_DST_ALPHA, GL11.GL_ONE_MINUS_DST_ALPHA);
159 shadowMask.renderAtCenter(loc.x, loc.y);
160 }
161
162
163// if (glow != null && glowFader.getBrightness() > 0) {
164// glow.setAngle(facing - 90);
165// glow.setAdditiveBlend();
166// glow.setAlphaMult(alphaMult * fader.getBrightness() *
167// (0.5f + 0.5f * glowBounce.getBrightness()) *
168// glowFader.getBrightness());
169// glow.renderAtCenter(loc.x, loc.y);
170// }
171 }
172
173
174 public void advance(float days) {
175 fader.advance(days);
176// glowBounce.advance(days);
177// glowFader.advance(days);
178
179 facing += angVel * days;
180
181 loc.x += vel.x * days;
182 loc.y += vel.y * days;
183
184 timeLeft -= days;
185 if (timeLeft < 0) {
186 fader.fadeOut();
187 }
188 }
189
190
191 public boolean isDone() {
192 return fader.isFadedOut();
193 }
194
195// public FaderUtil getGlowFader() {
196// return glowFader;
197// }
198
199
200}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
static SettingsAPI getSettings()
Definition Global.java:51
GenericFieldItemSprite(SectorEntityToken entity, String category, String key, float cellSize, float size, float spawnRadius)
SpriteAPI getSprite(String filename)