001package com.fs.starfarer.api.impl.campaign; 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.campaign.SectorEntityToken; 010import com.fs.starfarer.api.graphics.SpriteAPI; 011import com.fs.starfarer.api.impl.campaign.ids.Tags; 012import com.fs.starfarer.api.util.FaderUtil; 013import com.fs.starfarer.api.util.Misc; 014 015public class GenericFieldItemSprite { 016 017 //public static final float CELL_SIZE = 32; 018 019 protected SpriteAPI shadowMask; 020 protected SpriteAPI sprite; 021 protected SpriteAPI glow; 022 023 protected float width; 024 protected float height; 025 026 protected SectorEntityToken entity; 027 028 protected Vector2f loc = new Vector2f(); 029 protected Vector2f vel = new Vector2f(); 030 protected float timeLeft = 0f; 031 protected float facing = 0f; 032 protected float angVel = 0f; 033 protected FaderUtil fader = new FaderUtil(0f, 0.2f, 0.2f); // days 034 035// protected FaderUtil glowBounce; 036// protected FaderUtil glowFader = new FaderUtil(0f, 0.1f, 0.1f); 037 038 039 public GenericFieldItemSprite(SectorEntityToken entity, String category, String key, float cellSize, float size, float spawnRadius) { 040 this.entity = entity; 041 042// glowBounce = new FaderUtil(0f, 0.1f + (float) Math.random() * 0.1f, 0.1f + (float) Math.random() * 0.1f); // days 043// glowBounce.setBounce(true, true); 044// glowBounce.fadeIn(); 045 046// if ((float) Math.random() < field.getPieceGlowProbability()) { 047// glowFader.fadeIn(); 048// } 049 050 sprite = Global.getSettings().getSprite(category, key); 051 //glow = Global.getSettings().getSprite("terrain", "debrisFieldGlowSheet"); 052 053 054 //float sizeRangeMult = 0.5f + 0.5f * field.params.density; 055 this.width = size; 056 this.height = width; 057 058 float w = sprite.getWidth(); 059 float h = sprite.getHeight(); 060 int cols = (int) (w / cellSize); 061 int rows = (int) (h / cellSize); 062 063 064 float cellX = (int) (Math.random() * cols); 065 float cellY = (int) (Math.random() * rows); 066 067 float ctw = sprite.getTextureWidth() / (float) cols; 068 float cth = sprite.getTextureHeight() / (float) rows; 069 070 sprite.setTexX(cellX * ctw); 071 sprite.setTexY(cellY * cth); 072 sprite.setTexWidth(ctw); 073 sprite.setTexHeight(cth); 074 075 if (glow != null) { 076 glow.setTexX(cellX * ctw); 077 glow.setTexY(cellY * cth); 078 glow.setTexWidth(ctw); 079 glow.setTexHeight(cth); 080 glow.setSize(width, height); 081 //glow.setColor(field.getParams().glowColor); 082 } 083 084 sprite.setSize(width, height); 085 086 //glow.setColor(new Color(255,165,100,255)); 087 088 shadowMask = Global.getSettings().getSprite("graphics/fx/ship_shadow_mask.png"); 089 shadowMask.setSize(width * 1.5f, height * 1.5f); 090 091 fader.fadeIn(); 092 093 facing = (float) Math.random() * 360f; 094 angVel = (float) Math.random() * 360f - 180f; 095 096 float r = (float) Math.random(); 097 r = (float) Math.sqrt(r); 098 float dist = r * spawnRadius; 099 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