parent
							
								
									e14be3ec1b
								
							
						
					
					
						commit
						880bcfb553
					
				@ -1,16 +0,0 @@ | 
				
			||||
package mightypork.rogue.world; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * Player observing a map represented by an observer. | 
				
			||||
 *  | 
				
			||||
 * @author MightyPork | 
				
			||||
 */ | 
				
			||||
public interface MapObserver extends WorldEntity { | 
				
			||||
	 | 
				
			||||
	/** | 
				
			||||
	 * @return observed range (in tiles) | 
				
			||||
	 */ | 
				
			||||
	public int getViewRange(); | 
				
			||||
	 | 
				
			||||
} | 
				
			||||
@ -0,0 +1,49 @@ | 
				
			||||
package mightypork.rogue.world; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import java.io.IOException; | 
				
			||||
 | 
				
			||||
import mightypork.util.ion.IonBinary; | 
				
			||||
import mightypork.util.ion.IonInput; | 
				
			||||
import mightypork.util.ion.IonOutput; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
public class PathStep implements IonBinary { | 
				
			||||
	 | 
				
			||||
	public static final int ION_MARK = 0; | 
				
			||||
	 | 
				
			||||
	public int x; | 
				
			||||
	public int y; | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public PathStep(int x, int y) { | 
				
			||||
		this.x = x < 1 ? -1 : x > 0 ? 1 : 0; | 
				
			||||
		this.y = y < 1 ? -1 : y > 0 ? 1 : 0; | 
				
			||||
		 | 
				
			||||
		y = (int) Math.signum(x); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void load(IonInput in) throws IOException | 
				
			||||
	{ | 
				
			||||
		x = in.readByte(); | 
				
			||||
		y = in.readByte(); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void save(IonOutput out) throws IOException | 
				
			||||
	{ | 
				
			||||
		out.writeByte(x); | 
				
			||||
		out.writeByte(y); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public short getIonMark() | 
				
			||||
	{ | 
				
			||||
		return ION_MARK; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
} | 
				
			||||
@ -1,140 +0,0 @@ | 
				
			||||
package mightypork.rogue.world; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import java.io.IOException; | 
				
			||||
 | 
				
			||||
import mightypork.util.ion.IonBinary; | 
				
			||||
import mightypork.util.ion.IonBundle; | 
				
			||||
import mightypork.util.ion.IonInput; | 
				
			||||
import mightypork.util.ion.IonOutput; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * Player info | 
				
			||||
 *  | 
				
			||||
 * @author MightyPork | 
				
			||||
 */ | 
				
			||||
public class Player implements IonBinary, MapObserver { | 
				
			||||
	 | 
				
			||||
	public static final short ION_MARK = 0; | 
				
			||||
	 | 
				
			||||
	private final double walktime = 0.3; // possibly make changeable for speed potion
 | 
				
			||||
	 | 
				
			||||
	private final WorldPos position = new WorldPos(); | 
				
			||||
	private final WorldPos target = new WorldPos(); | 
				
			||||
	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() | 
				
			||||
	{ | 
				
			||||
		position.setMoveListener(moveListener); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public Player(int x, int y, int floor) | 
				
			||||
	{ | 
				
			||||
		position.setTo(x, y, floor); | 
				
			||||
		target.setTo(position); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public Player(WorldPos pos) | 
				
			||||
	{ | 
				
			||||
		this(pos.x, pos.y, pos.floor); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void load(IonInput in) throws IOException | 
				
			||||
	{ | 
				
			||||
		IonBundle ib = in.readBundle(); | 
				
			||||
		ib.loadBundled("pos", position); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void save(IonOutput out) throws IOException | 
				
			||||
	{ | 
				
			||||
		IonBundle ib = new IonBundle(); | 
				
			||||
		ib.putBundled("target", target); | 
				
			||||
		out.writeBundle(ib); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public short getIonMark() | 
				
			||||
	{ | 
				
			||||
		return ION_MARK; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public WorldPos getPosition() | 
				
			||||
	{ | 
				
			||||
		return position; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public int getViewRange() | 
				
			||||
	{ | 
				
			||||
		return 15; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void teleport(WorldPos pos) | 
				
			||||
	{ | 
				
			||||
		position.setTo(pos); | 
				
			||||
		target.setTo(pos); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void walk(int offsetX, int offsetY) | 
				
			||||
	{ | 
				
			||||
		target.setTo(position.x + offsetX, position.y + offsetY, this.position.floor); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setMoveListener(Runnable r) | 
				
			||||
	{ | 
				
			||||
		this.moveListenerCustom = r; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void updateVisual(double delta) | 
				
			||||
	{ | 
				
			||||
		position.update(delta); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void updateLogic(double delta) | 
				
			||||
	{ | 
				
			||||
		// server stuffs (sleep timer etc)
 | 
				
			||||
	} | 
				
			||||
} | 
				
			||||
@ -0,0 +1,83 @@ | 
				
			||||
package mightypork.rogue.world; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import java.io.IOException; | 
				
			||||
 | 
				
			||||
import mightypork.rogue.world.map.MapObserver; | 
				
			||||
import mightypork.util.ion.IonBundle; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * Player info | 
				
			||||
 *  | 
				
			||||
 * @author MightyPork | 
				
			||||
 */ | 
				
			||||
public class PlayerEntity extends WorldEntity implements MapObserver { | 
				
			||||
	 | 
				
			||||
	public static final double PLAYER_STEP_TIME = 0.3; | 
				
			||||
	 | 
				
			||||
	public boolean connected = false; | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public PlayerEntity() { | 
				
			||||
		super(); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public PlayerEntity(ServerWorld world, int x, int y, int floor) { | 
				
			||||
		super(world, new WorldPos(x, y, floor)); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void load(IonBundle bundle) throws IOException | 
				
			||||
	{ | 
				
			||||
		super.load(bundle); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void save(IonBundle bundle) throws IOException | 
				
			||||
	{ | 
				
			||||
		super.save(bundle); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public int getViewRange() | 
				
			||||
	{ | 
				
			||||
		return 15; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	protected double getStepTime() | 
				
			||||
	{ | 
				
			||||
		return PLAYER_STEP_TIME; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public boolean isConnected() | 
				
			||||
	{ | 
				
			||||
		return connected; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setConnected(boolean connected) | 
				
			||||
	{ | 
				
			||||
		this.connected = connected; | 
				
			||||
	} | 
				
			||||
 | 
				
			||||
 | 
				
			||||
	@Override | 
				
			||||
	public WorldPos getViewPosition() | 
				
			||||
	{ | 
				
			||||
		return getPosition(); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public boolean isPhysical() | 
				
			||||
	{ | 
				
			||||
		return isConnected(); | 
				
			||||
	} | 
				
			||||
} | 
				
			||||
@ -1,10 +1,143 @@ | 
				
			||||
package mightypork.rogue.world; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import mightypork.util.constraints.vect.Vect; | 
				
			||||
import java.io.IOException; | 
				
			||||
import java.util.LinkedList; | 
				
			||||
import java.util.Queue; | 
				
			||||
 | 
				
			||||
import mightypork.util.ion.IonBundle; | 
				
			||||
import mightypork.util.ion.IonBundled; | 
				
			||||
 | 
				
			||||
public interface WorldEntity { | 
				
			||||
 | 
				
			||||
public abstract class WorldEntity implements IonBundled { | 
				
			||||
	 | 
				
			||||
	private final WorldPos position = new WorldPos(); | 
				
			||||
	private Runnable moveEndListener, pathEndListener; | 
				
			||||
	 | 
				
			||||
	private long serial_id = 0L; | 
				
			||||
	 | 
				
			||||
	private final Queue<PathStep> path = new LinkedList<>(); | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public WorldEntity(ServerWorld world, WorldPos pos) { | 
				
			||||
		this.serial_id = world.genEid(); | 
				
			||||
		this.position.setTo(pos); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public WorldEntity() { | 
				
			||||
		// for ion
 | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void save(IonBundle bundle) throws IOException | 
				
			||||
	{ | 
				
			||||
		bundle.putBundled("pos", position); | 
				
			||||
		bundle.putSequence("steps", path); | 
				
			||||
		bundle.put("eid", serial_id); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	@Override | 
				
			||||
	public void load(IonBundle bundle) throws IOException | 
				
			||||
	{ | 
				
			||||
		bundle.loadBundled("pos", position); | 
				
			||||
		bundle.loadSequence("path", path); | 
				
			||||
		serial_id = bundle.get("eid", 0L); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setMoveEndListener(Runnable moveEndListener) | 
				
			||||
	{ | 
				
			||||
		this.moveEndListener = moveEndListener; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setPathEndListener(Runnable pathEndListener) | 
				
			||||
	{ | 
				
			||||
		this.pathEndListener = pathEndListener; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public WorldPos getPosition() | 
				
			||||
	{ | 
				
			||||
		return position; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setPosition(WorldPos pos) | 
				
			||||
	{ | 
				
			||||
		position.setTo(pos); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void setPosition(int x, int y, int floor) | 
				
			||||
	{ | 
				
			||||
		position.setTo(x, y, floor); | 
				
			||||
		cancelPath(); // discard remaining steps
 | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	/** | 
				
			||||
	 * @param world the world | 
				
			||||
	 * @param delta delta time | 
				
			||||
	 */ | 
				
			||||
	public void updateLogic(WorldAccess world, double delta) | 
				
			||||
	{ | 
				
			||||
		if(!isPhysical()) return; | 
				
			||||
		 | 
				
			||||
		if (!position.isFinished()) { | 
				
			||||
			position.update(delta); | 
				
			||||
		} | 
				
			||||
		 | 
				
			||||
		if (position.isFinished()) { | 
				
			||||
			 | 
				
			||||
			if (moveEndListener != null) moveEndListener.run(); | 
				
			||||
			 | 
				
			||||
			if (!path.isEmpty()) { | 
				
			||||
				// get next step to walk
 | 
				
			||||
				PathStep step = path.poll(); | 
				
			||||
				position.walk(step.x, step.y, getStepTime()); | 
				
			||||
			} else { | 
				
			||||
				// notify AI or whatever
 | 
				
			||||
				if (pathEndListener != null) pathEndListener.run(); | 
				
			||||
			} | 
				
			||||
		} | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	/** | 
				
			||||
	 * @param world the world | 
				
			||||
	 * @param delta delta time | 
				
			||||
	 */ | 
				
			||||
	public void updateVisual(WorldAccess world, double delta) | 
				
			||||
	{ | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public boolean isPathFinished() | 
				
			||||
	{ | 
				
			||||
		return position.isFinished() && path.isEmpty(); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void addStep(PathStep step) | 
				
			||||
	{ | 
				
			||||
		path.add(step); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	public void cancelPath() | 
				
			||||
	{ | 
				
			||||
		path.clear(); | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	protected abstract double getStepTime(); | 
				
			||||
	 | 
				
			||||
	public boolean isPhysical() { | 
				
			||||
		return true; | 
				
			||||
	} | 
				
			||||
	 | 
				
			||||
	WorldPos getPosition(); | 
				
			||||
} | 
				
			||||
 | 
				
			||||
@ -0,0 +1,13 @@ | 
				
			||||
package mightypork.rogue.world.map; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
import mightypork.rogue.world.WorldPos; | 
				
			||||
 | 
				
			||||
 | 
				
			||||
public interface MapObserver { | 
				
			||||
	 | 
				
			||||
	int getViewRange(); | 
				
			||||
	 | 
				
			||||
	 | 
				
			||||
	WorldPos getViewPosition(); | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue