v0.5.0: improved shadows, added perspective to item sprites

This commit is contained in:
Evan Debenham 2017-01-19 16:53:49 -05:00
parent 479c689196
commit f54540d7a4
2 changed files with 123 additions and 42 deletions

View File

@ -65,7 +65,17 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
private static final float MOVE_INTERVAL = 0.1f;
private static final float FLASH_INTERVAL = 0.05f;
//the amount the sprite is raised from flat when viewed in a raised perspective
protected float perspectiveRaise = 0.4f;
//the width and height of the shadow are a percentage of sprite size
//offset is the number of pixels the shadow is moved down or up (handy for some animations)
protected boolean renderShadow = false;
protected float shadowWidth = 1.2f;
protected float shadowHeight = 0.25f;
protected float shadowOffset = 0.5f;
public enum State {
BURNING, LEVITATING, INVISIBLE, PARALYSED, FROZEN, ILLUMINATED, CHILLED, DARKENED, MARKED
}
@ -116,6 +126,7 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
place( ch.pos );
turnTo( ch.pos, Random.Int( Dungeon.level.length() ) );
renderShadow = true;
ch.updateSpriteState();
}
@ -448,15 +459,16 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
}
}
//FIXME shadows are really sloppily implemented here, there is surely a neater way to do this.
private float[] shadowMatrix;
@Override
protected void updateMatrix() {
super.updateMatrix();
shadowMatrix = Matrix.clone(matrix);
Matrix.translate(shadowMatrix, -width()/13.333f, height()*0.7625f);
Matrix.scale(shadowMatrix, 1.15f, 0.25f);
Matrix.translate(shadowMatrix,
(width() * (1f - shadowWidth)) / 2f,
(height() * (1f - shadowHeight)) + shadowOffset);
Matrix.scale(shadowMatrix, shadowWidth, shadowHeight);
}
@Override
@ -464,31 +476,33 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
if (texture == null || (!dirty && buffer == null))
return;
if (dirty) {
verticesBuffer.position( 0 );
verticesBuffer.put( vertices );
if (buffer == null)
buffer = new Vertexbuffer( verticesBuffer );
else
buffer.updateVertices( verticesBuffer );
dirty = false;
if (renderShadow) {
if (dirty) {
verticesBuffer.position(0);
verticesBuffer.put(vertices);
if (buffer == null)
buffer = new Vertexbuffer(verticesBuffer);
else
buffer.updateVertices(verticesBuffer);
dirty = false;
}
NoosaScript script = script();
texture.bind();
script.camera(camera());
updateMatrix();
script.uModel.valueM4(shadowMatrix);
script.lighting(
0, 0, 0, am * .6f,
0, 0, 0, aa * .6f);
script.drawQuad(buffer);
}
NoosaScript script = script();
texture.bind();
script.camera( camera() );
updateMatrix();
script.uModel.valueM4( shadowMatrix );
script.lighting(
0, 0, 0, am*.5f,
0, 0, 0, aa*.5f );
script.drawQuad( buffer );
super.draw();
}

View File

@ -33,8 +33,11 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.Level;
import com.shatteredpixel.shatteredpixeldungeon.levels.Terrain;
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
import com.watabou.gltextures.TextureCache;
import com.watabou.glwrap.Matrix;
import com.watabou.glwrap.Vertexbuffer;
import com.watabou.noosa.Game;
import com.watabou.noosa.MovieClip;
import com.watabou.noosa.NoosaScript;
import com.watabou.noosa.TextureFilm;
import com.watabou.noosa.audio.Sample;
import com.watabou.noosa.particles.Emitter;
@ -57,6 +60,16 @@ public class ItemSprite extends MovieClip {
private boolean glowUp;
private float dropInterval;
//the amount the sprite is raised from flat when viewed in a raised perspective
protected float perspectiveRaise = 0.333f;
//the width and height of the shadow are a percentage of sprite size
//offset is the number of pixels the shadow is moved down or up (handy for some animations)
protected boolean renderShadow = false;
protected float shadowWidth = 1f;
protected float shadowHeight = 0.25f;
protected float shadowOffset = 0.5f;
public ItemSprite() {
this( ItemSpriteSheet.SOMETHING, null );
@ -85,6 +98,7 @@ public class ItemSprite extends MovieClip {
public void link( Heap heap ) {
this.heap = heap;
view( heap.image(), heap.glowing() );
renderShadow = true;
place(heap.pos);
}
@ -195,30 +209,83 @@ public class ItemSprite extends MovieClip {
emitter = null;
}
private float[] shadowMatrix;
@Override
protected void updateMatrix() {
super.updateMatrix();
shadowMatrix = Matrix.clone(matrix);
Matrix.translate(shadowMatrix,
(width() * (1f - shadowWidth)) / 2f,
(height() * (1f - shadowHeight)) + shadowOffset);
Matrix.scale(shadowMatrix, shadowWidth, shadowHeight);
}
@Override
public void draw() {
if (texture == null || (!dirty && buffer == null))
return;
if (renderShadow) {
if (dirty) {
verticesBuffer.position(0);
verticesBuffer.put(vertices);
if (buffer == null)
buffer = new Vertexbuffer(verticesBuffer);
else
buffer.updateVertices(verticesBuffer);
dirty = false;
}
NoosaScript script = script();
texture.bind();
script.camera(camera());
updateMatrix();
script.uModel.valueM4(shadowMatrix);
script.lighting(
0, 0, 0, am * .6f,
0, 0, 0, aa * .6f);
script.drawQuad(buffer);
}
super.draw();
}
@Override
public void update() {
super.update();
visible = (heap == null || heap.seen);
if (dropInterval > 0 && (dropInterval -= Game.elapsed) <= 0) {
speed.set( 0 );
acc.set( 0 );
place( heap.pos );
if (dropInterval > 0){
shadowOffset -= speed.y * Game.elapsed * 0.8f;
if (visible) {
boolean water = Level.water[heap.pos];
if ((dropInterval -= Game.elapsed) <= 0){
if (water) {
GameScene.ripple(heap.pos);
} else {
int cell = Dungeon.level.map[heap.pos];
water = (cell == Terrain.WELL || cell == Terrain.ALCHEMY);
}
speed.set(0);
acc.set(0);
shadowOffset = 0.25f;
place(heap.pos);
if (!(heap.peek() instanceof Gold)) {
Sample.INSTANCE.play(water ? Assets.SND_WATER : Assets.SND_STEP, 0.8f, 0.8f, 1.2f);
if (visible) {
boolean water = Level.water[heap.pos];
if (water) {
GameScene.ripple(heap.pos);
} else {
int cell = Dungeon.level.map[heap.pos];
water = (cell == Terrain.WELL || cell == Terrain.ALCHEMY);
}
if (!(heap.peek() instanceof Gold)) {
Sample.INSTANCE.play(water ? Assets.SND_WATER : Assets.SND_STEP, 0.8f, 0.8f, 1.2f);
}
}
}
}