001package com.fs.starfarer.api.util;
002
003import java.awt.Color;
004
005import org.lwjgl.opengl.GL11;
006import org.lwjgl.util.vector.Vector2f;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.graphics.SpriteAPI;
010
011public class CampaignEngineGlowIndividualEngine {
012
013        protected float angle;
014        protected float length;
015        protected float glowSize;
016        protected float width;
017        protected float flameTexSpanMult = 1f;
018        protected Vector2f offset;
019        
020        protected Color fringe;
021        protected Color core;
022        protected Color flameColor;
023        protected FaderUtil fader = new FaderUtil(0f, 1f, 1f);
024        
025        protected FlickerUtilV2 flicker = new FlickerUtilV2();
026        protected float texOffset;
027        
028        
029        protected CampaignEngineGlowUtil main;
030        
031        transient protected SpriteAPI glow;
032        transient protected SpriteAPI flame;
033        
034        public CampaignEngineGlowIndividualEngine(float angle, float length, float width, float glowSize, 
035                                                                                Vector2f offset,
036                                                                                CampaignEngineGlowUtil main) {
037                this.angle = angle;
038                this.length = length;
039                this.glowSize = glowSize;
040                this.width = width;
041                this.offset = offset;
042                this.main = main;
043                
044                fader.fadeIn();
045                
046                readResolve();
047        }
048
049        protected Object readResolve() {
050                glow = Global.getSettings().getSprite("campaignEntities", "campaign_engine_glow");
051                flame = Global.getSettings().getSprite("campaignEntities", "campaign_engine_flame");
052//              flame = Global.getSettings().getSprite("graphics/fx/beam_chunky_fringe.png");
053//              flame = Global.getSettings().getSprite("graphics/fx/beamfringe.png");
054//              flame = Global.getSettings().getSprite("graphics/fx/beamfringec.png");
055//              flame = Global.getSettings().getSprite("graphics/fx/engineglow32.png");
056                return this;
057        }
058        
059        public void advance(float amount) {
060                fader.advance(amount);
061                flicker.advance(amount * main.getFlickerRateMult().getCurr());
062                
063                texOffset -= amount * main.getTextureScrollMult().getCurr();
064                while (texOffset < 0) texOffset += 1f;
065        }
066        
067
068        public void render(Vector2f center, float facing, float alphaMult) {
069                alphaMult *= fader.getBrightness();
070                
071                float f = main.getFlickerMult().getCurr();
072                if (f > 0) {
073                        alphaMult *= (1f - flicker.getBrightness() * f);
074                        //alphaMult *= flicker.getBrightness() * f;
075                }
076                
077                if (alphaMult <= 0) return;
078                
079                GL11.glPushMatrix();
080                GL11.glTranslatef(center.x, center.y, 0f);
081                GL11.glRotatef(facing, 0, 0, 1);
082                GL11.glTranslatef(offset.x, offset.y, 0f);
083                
084                
085                renderFlame(alphaMult);
086                renderGlow(alphaMult);
087                
088                
089                GL11.glPopMatrix();
090        }
091        
092        protected void renderFlame(float alphaMult) {
093                float lengthMult = main.getLengthMult().getCurr();
094                float widthMult = main.getWidthMult().getCurr();
095                
096                if (lengthMult <= 0f || widthMult <= 0f || width <= 0f || length <= 0f) return;
097                
098                flame.bindTexture();
099                
100                Color base = main.getFlameColor().getCurr();
101                if (flameColor != null) base = flameColor;
102                Color dim = Misc.setAlpha(base, 0);
103                
104                //System.out.println("FC red: " + base.getRed());
105                
106                float w = width * widthMult;
107                float len = length * lengthMult; 
108                
109//              len *= 2f;
110//              w *= 1.5f;
111                
112                float bit = 0.01f;
113                float fadeInPortion = Math.min(w/2f, len/4f);
114                float fadeInTex = fadeInPortion / len;
115                float flameTexSpan = 1f;
116                //flameTexSpan = len / flame.getWidth();
117                
118                flameTexSpan = len / w * 0.25f;
119                //System.out.println("FTX: " + flameTexSpan);
120                flameTexSpan *= flameTexSpanMult;
121                //flameTexSpan *= 0.5f;
122                //flameTexSpan *= 2f;
123                
124                
125                GL11.glEnable(GL11.GL_TEXTURE_2D);
126                GL11.glEnable(GL11.GL_BLEND);
127                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
128                
129                float num = 11f;
130                //num = 6f;
131                for (float f = 1f; f <= num; f++) {
132                        float b = alphaMult * f / num; 
133                        float wMult = 1f - ((f - 1f) / (num - 1f));
134                        float lMult = f / num;
135                        lMult = 0.5f + 0.5f * lMult;
136                        //lMult = (float) Math.sqrt(lMult);
137                        
138                        float texPlus = (f - 1f) * 1.0f / num;
139                        
140                        GL11.glBegin(GL11.GL_QUAD_STRIP);
141                        Misc.setColor(dim, alphaMult * b);
142                        GL11.glTexCoord2f(texOffset + texPlus, 0 + bit);
143                        GL11.glVertex2f(0f, -w/2f * wMult);
144                        GL11.glTexCoord2f(texOffset + texPlus, 1 - bit);
145                        GL11.glVertex2f(0f, w/2f * wMult);
146                        
147                        Misc.setColor(base, alphaMult * b);
148                        GL11.glTexCoord2f(texOffset + fadeInTex + texPlus, 0 + bit);
149                        GL11.glVertex2f(-fadeInPortion * lMult, -w/2f * wMult);
150                        GL11.glTexCoord2f(texOffset + fadeInTex + texPlus, 1 - bit);
151                        GL11.glVertex2f(-fadeInPortion * lMult, w/2f * wMult);
152                        
153                        Misc.setColor(dim, alphaMult * b);
154                        GL11.glTexCoord2f(texOffset + flameTexSpan + texPlus, 0 + bit);
155                        GL11.glVertex2f(-len * lMult, -w/2f * wMult);
156                        GL11.glTexCoord2f(texOffset + flameTexSpan + texPlus, 1 - bit);
157                        GL11.glVertex2f(-len * lMult, w/2f * wMult);
158                        GL11.glEnd();
159                }
160                
161        }
162        
163        
164        protected void renderGlow(float alphaMult) {
165                float w = glowSize;
166                float h = glowSize;
167                
168                float glowScale = main.getGlowMult().getCurr();
169                //glowScale = 0.1f;
170                float fringeScale = glowScale * main.getGlowFringeMult().getCurr();
171                float coreScale = glowScale * main.getGlowCoreMult().getCurr();
172                
173                
174                if (glowScale <= 0f) return;
175                
176                Color fringeColor = main.getGlowColorFringe().getCurr();
177                if (fringe != null) fringeColor = fringe;
178                
179                Color coreColor = main.getGlowColorCore().getCurr();
180                if (core != null) coreColor = core;
181                
182                glow.setColor(fringeColor);
183                glow.setAdditiveBlend();
184                
185                glow.setAlphaMult(alphaMult * 0.5f);
186                glow.setSize(w * fringeScale, h * fringeScale);
187                glow.renderAtCenter(0f, 0f);
188                
189                glow.setColor(coreColor);
190                //for (int i = 0; i < 5; i++) {
191                for (int i = 0; i < 2; i++) {
192                        w *= 0.3f;
193                        h *= 0.3f;
194                        glow.setSize(w * coreScale, h * coreScale);
195                        glow.setAlphaMult(alphaMult * 0.67f);
196                        glow.renderAtCenter(0f, 0f);
197                }
198        }
199        
200
201        public float getAngle() {
202                return angle;
203        }
204
205        public void setAngle(float angle) {
206                this.angle = angle;
207        }
208
209        public float getLength() {
210                return length;
211        }
212
213        public void setLength(float length) {
214                this.length = length;
215        }
216
217        public float getGlowSize() {
218                return glowSize;
219        }
220
221        public void setGlowSize(float glowSize) {
222                this.glowSize = glowSize;
223        }
224
225        public float getWidth() {
226                return width;
227        }
228
229        public void setWidth(float width) {
230                this.width = width;
231        }
232
233        public Vector2f getOffset() {
234                return offset;
235        }
236
237        public void setOffset(Vector2f offset) {
238                this.offset = offset;
239        }
240
241        public Color getFringe() {
242                return fringe;
243        }
244
245        public void setFringe(Color fringe) {
246                this.fringe = fringe;
247        }
248
249        public Color getCore() {
250                return core;
251        }
252
253        public void setCore(Color core) {
254                this.core = core;
255        }
256
257        public CampaignEngineGlowUtil getMain() {
258                return main;
259        }
260
261        public void setMain(CampaignEngineGlowUtil main) {
262                this.main = main;
263        }
264
265        public SpriteAPI getGlow() {
266                return glow;
267        }
268
269        public void setGlow(SpriteAPI glow) {
270                this.glow = glow;
271        }
272
273        public FaderUtil getFader() {
274                return fader;
275        }
276
277        public Color getFlameColor() {
278                return flameColor;
279        }
280
281        public void setFlameColor(Color flameColor) {
282                this.flameColor = flameColor;
283        }
284
285        public float getFlameTexSpanMult() {
286                return flameTexSpanMult;
287        }
288
289        public void setFlameTexSpanMult(float flameTexSpanMult) {
290                this.flameTexSpanMult = flameTexSpanMult;
291        }
292        
293        
294}
295
296
297
298