improved worldpos to support animation

v5stable
Ondřej Hruška 10 years ago
parent 233157a443
commit 494e1a657b
  1. 2
      src/mightypork/rogue/screens/ingame/WorldLayer.java
  2. 79
      src/mightypork/rogue/world/Player.java
  3. 8
      src/mightypork/rogue/world/World.java
  4. 5
      src/mightypork/rogue/world/WorldEntity.java
  5. 81
      src/mightypork/rogue/world/WorldPos.java
  6. 2
      src/mightypork/rogue/world/map/Level.java

@ -88,7 +88,7 @@ public class WorldLayer extends ScreenLayer {
}
});
w.getPlayer().setTargetListener(new Runnable() {
w.getPlayer().setMoveListener(new Runnable() {
@Override
public void run()

@ -18,22 +18,49 @@ import mightypork.util.math.Easing;
*/
public class Player implements IonBundled, MapObserver, Updateable {
private final double walktime = 0.3; // possibly make changeable for speed potion
private final WorldPos position = new WorldPos();
private final VectAnimated walkOffset = new VectAnimated(Vect.ZERO, Easing.LINEAR);
private final WorldPos target = new WorldPos();
private Runnable targetListener;
private Runnable moveListenerCustom;
private Runnable moveListener = new Runnable() {
@Override
public void run()
{
if (moveListenerCustom != null) moveListenerCustom.run();
if (!target.equals(position)) {
int x = (target.x - position.x);
int y = (target.y - position.y);
if (Math.abs(x) >= Math.abs(y)) y = 0;
if (Math.abs(y) > Math.abs(x)) x = 0;
if (x > 0) x = 1;
if (x < 0) x = -1;
if (y > 0) y = 1;
if (y < 0) y = -1;
position.walk(x, y, walktime);
}
}
};
public Player()
{
walkOffset.setDefaultDuration(0.25);
position.setMoveListener(moveListener);
}
public Player(int x, int y, int floor)
{
this.position.setTo(x, y, floor);
this.target.setTo(position);
position.setTo(x, y, floor);
target.setTo(position);
}
@ -60,19 +87,12 @@ public class Player implements IonBundled, MapObserver, Updateable {
@Override
public WorldPos getLogicalPosition()
public WorldPos getPosition()
{
return position;
}
@Override
public Vect getVisualOffset()
{
return walkOffset;
}
@Override
public int getViewRange()
{
@ -84,49 +104,24 @@ public class Player implements IonBundled, MapObserver, Updateable {
{
position.setTo(pos);
target.setTo(pos);
walkOffset.reset();
}
public void walk(int offsetX, int offsetY)
{
this.target.setTo(position.x + offsetX, position.y + offsetY, this.position.floor);
target.setTo(position.x + offsetX, position.y + offsetY, this.position.floor);
}
@Override
public void update(double delta)
{
if (!walkOffset.isFinished()) {
walkOffset.update(delta);
if (walkOffset.isFinished()) {
position.add(walkOffset.xi(), walkOffset.yi(), position.floor);
walkOffset.reset();
targetListener.run();
}
}
if (walkOffset.isFinished() && !target.equals(position)) {
int x = (target.x - position.x);
int y = (target.y - position.y);
if (Math.abs(x) >= Math.abs(y)) y = 0;
if (Math.abs(y) > Math.abs(x)) x = 0;
if (x > 0) x = 1;
if (x < 0) x = -1;
if (y > 0) y = 1;
if (y < 0) y = -1;
walkOffset.animate(x, y, 0);
}
position.update(delta);
}
public void setTargetListener(Runnable r)
public void setMoveListener(Runnable r)
{
this.targetListener = r;
this.moveListenerCustom = r;
}
}

@ -72,7 +72,7 @@ public class World implements IonBundled, Updateable {
for (int level = 0; level < levels.size(); level++) {
for (final MapObserver observer : observers) {
if (observer.getLogicalPosition().floor == level) {
if (observer.getPosition().floor == level) {
levels.get(level).update(observer, delta);
}
}
@ -82,7 +82,7 @@ public class World implements IonBundled, Updateable {
public Level getLevelForObserver(MapObserver observer)
{
return levels.get(observer.getLogicalPosition().floor);
return levels.get(observer.getPosition().floor);
}
@ -112,8 +112,8 @@ public class World implements IonBundled, Updateable {
final VectConst vpCenter = r.center().sub(tileSize * 0.5, tileSize).freeze(); // 0.5 to center, 1 to move up (down is teh navbar)
final double playerX = player.getLogicalPosition().x + player.getVisualOffset().x();
final double playerY = player.getLogicalPosition().y + player.getVisualOffset().y();
final double playerX = player.getPosition().getXVisual();
final double playerY = player.getPosition().getYVisual();
// total map area
//@formatter:off

@ -6,8 +6,5 @@ import mightypork.util.constraints.vect.Vect;
public interface WorldEntity {
WorldPos getLogicalPosition();
Vect getVisualOffset();
WorldPos getPosition();
}

@ -3,8 +3,12 @@ package mightypork.rogue.world;
import java.io.IOException;
import mightypork.util.constraints.vect.Vect;
import mightypork.util.constraints.vect.mutable.VectAnimated;
import mightypork.util.control.timing.Updateable;
import mightypork.util.ion.IonBundle;
import mightypork.util.ion.IonBundled;
import mightypork.util.math.Easing;
/**
@ -12,9 +16,11 @@ import mightypork.util.ion.IonBundled;
*
* @author MightyPork
*/
public class WorldPos implements IonBundled {
public class WorldPos implements IonBundled, Updateable {
public int x, y, floor;
private final VectAnimated walkOffset = new VectAnimated(Vect.ZERO, Easing.LINEAR);
private Runnable moveListener;
public WorldPos(int x, int y, int floor)
@ -37,6 +43,7 @@ public class WorldPos implements IonBundled {
x = in.get("x", 0);
y = in.get("y", 0);
floor = in.get("z", 0);
walkOffset.reset();
}
@ -49,19 +56,56 @@ public class WorldPos implements IonBundled {
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getFloor()
{
return floor;
}
public double getXVisual()
{
return x + walkOffset.x();
}
public double getYVisual()
{
return y + walkOffset.y();
}
public void setTo(int x, int y)
{
this.x = x;
this.y = y;
walkOffset.reset();
}
public void setTo(int x, int y, int floor)
{
this.x = x;
this.y = y;
this.floor = floor;
walkOffset.reset();
}
public void setTo(WorldPos other)
{
this.x = other.x;
this.y = other.y;
this.floor = other.floor;
setTo(other.x, other.y, other.floor);
}
@ -91,9 +135,34 @@ public class WorldPos implements IonBundled {
}
public void add(int x, int y, int floor)
public void add(int x, int y)
{
setTo(this.x + x, this.y + y);
}
public void walk(int x, int y, double secs)
{
setTo(this.x + x, this.y + y);
walkOffset.setTo(-x, -y);
walkOffset.animate(0, 0, 0, secs);
}
@Override
public void update(double delta)
{
if (!walkOffset.isFinished()) {
walkOffset.update(delta);
}
if (walkOffset.isFinished()) moveListener.run();
}
public void setMoveListener(Runnable listener)
{
setTo(this.x + x, this.y + y, this.floor + floor);
this.moveListener = listener;
}
}

@ -180,7 +180,7 @@ public class Level implements MapAccess, IonBinary {
public void update(MapObserver observer, double delta)
{
final int viewRange = observer.getViewRange();
final WorldPos position = observer.getLogicalPosition();
final WorldPos position = observer.getPosition();
int x1 = position.x - viewRange;
int y1 = position.y - viewRange;

Loading…
Cancel
Save