Skip to content

Commit da6853d

Browse files
committed
Added some simple and very inefficient ship exhaust.
...sometimes it is better to look good than to feel good.
1 parent fe54916 commit da6853d

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed
39.9 KB
Loading

sigem/src/main/java/sigem/sim/ControlDriver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@
4444
*/
4545
public interface ControlDriver {
4646

47+
public void initialize( Body body );
48+
4749
public void update( double stepTime, Body body );
4850
}

sigem/src/main/java/sigem/sim/ShipDriver.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class ShipDriver implements ControlDriver {
5353

5454
static Logger log = LoggerFactory.getLogger(ShipDriver.class);
5555

56+
private Body body;
57+
5658
// Keep track of what the player has provided.
5759
private volatile Vec3d thrust = new Vec3d();
5860

@@ -62,9 +64,25 @@ public class ShipDriver implements ControlDriver {
6264
private float maxSpeed = 10;
6365
private float turnSpeed = 1;
6466
private double lateralBraking = 0.9;
67+
68+
public ShipDriver() {
69+
}
70+
71+
public Body getBody() {
72+
return body;
73+
}
74+
75+
public Vec3d getThrust() {
76+
return thrust;
77+
}
6578

6679
public void applyControlInput( ShipInput input ) {
6780
this.thrust = input.getThrust();
81+
}
82+
83+
@Override
84+
public void initialize( Body body ) {
85+
this.body = body;
6886
}
6987

7088
@Override

sigem/src/main/java/sigem/sim/ShipInputSystem.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.slf4j.*;
4242

4343
import com.simsilica.es.*;
44+
import com.simsilica.es.common.Decay;
45+
import com.simsilica.mathd.*;
4446
import com.simsilica.sim.*;
4547

4648
import sigem.es.*;
@@ -59,6 +61,9 @@ public class ShipInputSystem extends AbstractGameSystem {
5961
private SimplePhysics phys;
6062

6163
private ShipContainer ships;
64+
65+
private double nextPuff = 0;
66+
private double puffInterval = 0.05; //0.2;
6267

6368
public ShipInputSystem() {
6469
}
@@ -93,13 +98,40 @@ public void stop() {
9398

9499
@Override
95100
public void update( SimTime time ) {
96-
ships.update();
101+
ships.update();
102+
103+
// Could have done this with a separate system but
104+
// it was convenient to do it here.
105+
double t = time.getTimeInSeconds();
106+
if( t > nextPuff ) {
107+
nextPuff = t + puffInterval;
108+
109+
for( ShipDriver ship : ships.getArray() ) {
110+
if( ship.getThrust().z == 0 ) {
111+
continue;
112+
}
113+
// Else the thrusters are on and we can puff
114+
Body body = ship.getBody();
115+
// Make it appear 2 units behind the ship
116+
Vec3d loc = body.pos.subtract(body.orientation.mult(Vec3d.UNIT_Z.mult(2)));
117+
EntityId puff = ed.createEntity();
118+
ed.setComponents(puff,
119+
new Position(loc),
120+
ObjectType.create("thrust", ed),
121+
new Decay(time.getTime(), time.getFutureTime(5.0))
122+
);
123+
}
124+
}
97125
}
98126

99127
private class ShipContainer extends EntityContainer<ShipDriver> {
100128
public ShipContainer( EntityData ed ) {
101129
super(ed, ShipInput.class);
102130
}
131+
132+
public ShipDriver[] getArray() {
133+
return super.getArray();
134+
}
103135

104136
@Override
105137
protected ShipDriver addObject( Entity e ) {

sigem/src/main/java/sigem/sim/SimplePhysics.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void setControlDriver( EntityId entityId, ControlDriver driver ) {
9696
Body current = getBody(entityId);
9797
if( current != null ) {
9898
current.driver = driver;
99+
driver.initialize(current);
99100
}
100101
}
101102
}
@@ -118,6 +119,9 @@ protected Body createBody( EntityId entityId, double invMass, double radius, boo
118119

119120
// Hookup the driver if it has one waiting
120121
result.driver = driverIndex.get(entityId);
122+
if( result.driver != null ) {
123+
result.driver.initialize(result);
124+
}
121125

122126
// Give it any initial impulse that it might have
123127
Impulse imp = ed.getComponent(entityId, Impulse.class);

sigem/src/main/java/sigem/view/ModelViewState.java

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import com.simsilica.es.*;
6060
import com.simsilica.lemur.*;
61+
import com.simsilica.lemur.anim.*;
6162
import com.simsilica.lemur.style.ElementId;
6263
import com.simsilica.mathd.*;
6364
import com.simsilica.state.*;
@@ -75,6 +76,7 @@ public class ModelViewState extends BaseAppState {
7576
static Logger log = LoggerFactory.getLogger(ModelViewState.class);
7677

7778
private EntityData ed;
79+
private AssetManager assets;
7880

7981
private Node modelRoot;
8082

@@ -103,6 +105,7 @@ public Spatial getModel( EntityId id ) {
103105
@Override
104106
protected void initialize( Application app ) {
105107
modelRoot = new Node();
108+
assets = app.getAssetManager();
106109

107110
this.ed = getState(GameSystemsState.class).get(EntityData.class);
108111
}
@@ -159,11 +162,9 @@ public void update( float tpf ) {
159162

160163
protected Spatial createShip( Entity entity ) {
161164

162-
AssetManager assetManager = getApplication().getAssetManager();
163-
164-
Spatial ship = assetManager.loadModel("Models/fighter.j3o");
165+
Spatial ship = assets.loadModel("Models/fighter.j3o");
165166
ship.center();
166-
Texture texture = assetManager.loadTexture("Textures/ship1.png");
167+
Texture texture = assets.loadTexture("Textures/ship1.png");
167168
Material mat = GuiGlobals.getInstance().createMaterial(texture, false).getMaterial();
168169
mat.setTexture("ColorMap", texture);
169170
ship.setMaterial(mat);
@@ -219,13 +220,11 @@ protected Spatial createPlanet( Entity entity ) {
219220

220221
protected Spatial createAsteroid( Entity entity ) {
221222

222-
AssetManager assetManager = getApplication().getAssetManager();
223-
224-
Spatial rock = assetManager.loadModel("Models/Rock1/Rock1.j3o");
223+
Spatial rock = assets.loadModel("Models/Rock1/Rock1.j3o");
225224
rock.setName("rock");
226225
//rock.setLocalScale(50);
227226
rock.center();
228-
Texture texture = assetManager.loadTexture("Models/Rock1/textures/Rock02LV3_1_1024.jpg");
227+
Texture texture = assets.loadTexture("Models/Rock1/textures/Rock02LV3_1_1024.jpg");
229228
Material mat = GuiGlobals.getInstance().createMaterial(texture, false).getMaterial();
230229
//mat.setTexture("ColorMap", texture);
231230
rock.setMaterial(mat);
@@ -254,6 +253,36 @@ protected Spatial createAsteroid( Entity entity ) {
254253

255254
return result;
256255
}
256+
257+
protected Spatial createThrust( Entity entity ) {
258+
259+
GuiGlobals globals = GuiGlobals.getInstance();
260+
261+
Node result = new Node("thrust");
262+
263+
Quad quad = new Quad(2, 2);
264+
Geometry geom = new Geometry("quad", quad);
265+
Texture texture = globals.loadTexture("Textures/neon-puff256.png", false, false);
266+
//Material mat = globals.createMaterial(ColorRGBA.Blue, false).getMaterial();
267+
Material mat = globals.createMaterial(texture, false).getMaterial();
268+
mat.getAdditionalRenderState().setBlendMode(BlendMode.AlphaAdditive);
269+
geom.setQueueBucket(Bucket.Transparent);
270+
geom.rotate(-FastMath.HALF_PI, 0, 0);
271+
geom.setMaterial(mat);
272+
geom.center();
273+
geom.move(0, -1, 0);
274+
275+
ColorRGBA color = new ColorRGBA(2, 2, 0, 1);
276+
mat.setColor("Color", color);
277+
278+
// Fade it out over five seconds. We could have created a system to do
279+
// this based on decay, blah blah... but this is really easy.
280+
getState(AnimationState.class).add(new ColorTween(color, ColorRGBA.White, new ColorRGBA(1, 1, 1, 0), 5));
281+
282+
result.attachChild(geom);
283+
284+
return result;
285+
}
257286

258287
protected Spatial createModel( Entity entity ) {
259288
// Check to see if one already exists
@@ -271,6 +300,8 @@ protected Spatial createModel( Entity entity ) {
271300
result = createPlanet(entity);
272301
} else if( typeName.equals("asteroid") ) {
273302
result = createAsteroid(entity);
303+
} else if( typeName.equals("thrust") ) {
304+
result = createThrust(entity);
274305
} else {
275306
throw new RuntimeException("Unknown spatial type:" + typeName);
276307
}
@@ -336,7 +367,39 @@ protected void removeObject( Spatial object, Entity e ) {
336367
log.trace("removeObject(" + e + ")");
337368
}
338369
removeModel(object, e);
339-
}
370+
}
371+
}
372+
373+
/**
374+
* Lemur does not yet provide one of these so we will do it ourselves.
375+
*/
376+
private class ColorTween extends AbstractTween {
377+
378+
private final ColorRGBA target;
379+
private final ColorRGBA from;
380+
private final ColorRGBA to;
381+
private final ColorRGBA value;
382+
383+
public ColorTween( ColorRGBA target, ColorRGBA from, ColorRGBA to, double length ) {
384+
super(length);
385+
this.target = target;
386+
this.from = from.clone();
387+
this.to = to.clone();
388+
this.value = new ColorRGBA(from);
389+
}
390+
391+
@Override
392+
protected void doInterpolate( double t ) {
393+
// Interpolate
394+
value.interpolateLocal(from, to, (float)t);
395+
target.set(value);
396+
}
397+
398+
@Override
399+
public String toString() {
400+
return getClass().getSimpleName() + "[target=" + target + ", from=" + from + ", to=" + to + ", length=" + getLength() + "]";
401+
}
340402
}
403+
341404
}
342405

0 commit comments

Comments
 (0)