v0.7.5: game camera now pans instead of snapping in most cases

This commit is contained in:
Evan Debenham 2019-09-16 00:00:09 -04:00
parent 09dd4dd053
commit f808e201f3
8 changed files with 48 additions and 23 deletions

View File

@ -52,7 +52,6 @@ public class Camera extends Gizmo {
public float[] matrix;
public PointF scroll;
public Visual target;
private float shakeMagX = 10f;
private float shakeMagY = 10f;
@ -130,7 +129,7 @@ public class Camera extends Gizmo {
@Override
public void destroy() {
target = null;
panIntensity = 0f;
}
public void zoom( float value ) {
@ -145,7 +144,7 @@ public class Camera extends Gizmo {
width = (int)(screenWidth / zoom);
height = (int)(screenHeight / zoom);
focusOn( fx, fy );
snapTo( fx, fy );
}
public void resize( int width, int height ) {
@ -155,12 +154,28 @@ public class Camera extends Gizmo {
screenHeight = (int)(height * zoom);
}
Visual followTarget = null;
PointF panTarget;
//camera moves at a speed such that it will pan to its current target in 1/intensity seconds
//keep in mind though that this speed is constantly decreasing, so actual pan time is higher
float panIntensity = 0f;
@Override
public void update() {
super.update();
if (target != null) {
focusOn( target.x + target.width / 2, target.y + target.height / 2 );
if (followTarget != null){
panTarget = followTarget.center();
}
if (panIntensity > 0f){
PointF panMove = new PointF();
panMove.x = panTarget.x - (scroll.x + width/2f);
panMove.y = panTarget.y - (scroll.y + height/2f);
panMove.scale(Game.elapsed * panIntensity);
scroll.offset(panMove);
}
if ((shakeTime -= Game.elapsed) > 0) {
@ -183,16 +198,30 @@ public class Camera extends Gizmo {
return x >= this.x && y >= this.y && x < this.x + screenWidth && y < this.y + screenHeight;
}
public void focusOn( float x, float y ) {
public void shift( PointF point ){
scroll.offset(point);
panIntensity = 0f;
}
public void snapTo(float x, float y ) {
scroll.set( x - width / 2, y - height / 2 );
panIntensity = 0f;
followTarget = null;
}
public void focusOn( PointF point ) {
focusOn( point.x, point.y );
public void snapTo(PointF point ) {
snapTo( point.x, point.y );
}
public void focusOn( Visual visual ) {
focusOn( visual.center() );
public void panTo( PointF dst, float intensity ){
panTarget = dst;
panIntensity = intensity;
followTarget = null;
}
public void panFollow(Visual target, float intensity ){
followTarget = target;
panIntensity = intensity;
}
public PointF screenToCamera( int x, int y ) {

View File

@ -224,8 +224,9 @@ public class Notes {
k = (KeyRecord) records.get(records.indexOf(k));
k.quantity(k.quantity() - key.quantity());
if (k.quantity() <= 0){
return records.remove(k);
records.remove(k);
}
return true;
}
return false;
}

View File

@ -160,8 +160,6 @@ public class CellSelector extends PointerArea {
@Override
protected void onDrag( PointerEvent event ) {
camera.target = null;
if (pinching) {
@ -180,7 +178,7 @@ public class CellSelector extends PointerArea {
lastPos.set( event.current );
} else if (dragging) {
camera.scroll.offset( PointF.diff( lastPos, event.current ).invScale( camera.zoom ) );
camera.shift( PointF.diff( lastPos, event.current ).invScale( camera.zoom ) );
lastPos.set( event.current );
}
}

View File

@ -413,7 +413,7 @@ public class GameScene extends PixelScene {
Dungeon.hero.next();
Camera.main.target = hero;
Camera.main.snapTo(hero.center());
if (InterlevelScene.mode != InterlevelScene.Mode.NONE) {
if (Dungeon.depth == Statistics.deepestFloor

View File

@ -96,7 +96,7 @@ public class HeroSprite extends CharSprite {
@Override
public void place( int p ) {
super.place( p );
Camera.main.target = this;
Camera.main.panTo(center(), 5f);
}
@Override
@ -105,7 +105,7 @@ public class HeroSprite extends CharSprite {
if (ch.flying) {
play( fly );
}
Camera.main.target = this;
Camera.main.panFollow(this, 5f);
}
@Override

View File

@ -105,8 +105,7 @@ public class DangerIndicator extends Tag {
TargetHealthIndicator.instance.target(target == TargetHealthIndicator.instance.target() ? null : target);
if (Dungeon.hero.curAction == null) {
Camera.main.target = null;
Camera.main.focusOn(target.sprite);
Camera.main.panTo(target.sprite.center(), 5f);
}
}
}

View File

@ -140,7 +140,7 @@ public class ScrollPane extends Component {
Camera c = content.camera;
c.scroll.offset( PointF.diff( lastPos, event.current ).invScale( c.zoom ) );
c.shift( PointF.diff( lastPos, event.current ).invScale( c.zoom ) );
if (c.scroll.x + width > content.width()) {
c.scroll.x = content.width() - width;
}

View File

@ -86,9 +86,7 @@ public class StatusPane extends Component {
@Override
protected void onClick( PointerEvent event ) {
Image sprite = Dungeon.hero.sprite;
if (!sprite.isVisible()) {
Camera.main.focusOn( sprite );
}
Camera.main.panTo( sprite.center(), 2f );
GameScene.show( new WndHero() );
}
} );