Skip to content

Commit

Permalink
Merge branch 'master' into feature/ui-console
Browse files Browse the repository at this point in the history
  • Loading branch information
neumantm authored Sep 11, 2019
2 parents 9380460 + 9dc95f9 commit a0107e1
Show file tree
Hide file tree
Showing 18 changed files with 577 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static void main(final String[] args) {
prepareUiManager(sim.getUiManager());
sim.initialize();
sim.getPlayfield().addEntity(new Position(3, 4), new Wall());
sim.getSimulationClock().start();
}

private static void prepareUiManager(UiManager manager) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This source file is part of the FIUS ICGE project.
* For more information see github.com/FIUS/ICGE2
*
* Copyright (c) 2019 the ICGE project authors.
*
* This software is available under the MIT license.
* SPDX-License-Identifier: MIT
*/
package de.unistuttgart.informatik.fius.icge.simulation;

/**
* The four fundamental directions possible in the simulation.
* <p>
* {@link #NORTH} is at the top of the screen ({@link Position#getY()} getting smaller)
* </p>
* <p>
* {@link #EAST} is at the right of the screen ({@link Position#getX()} getting bigger)
* </p>
* <p>
* {@link #SOUTH} is at the bottom of the screen ({@link Position#getY()} getting bigger)
* </p>
* <p>
* {@link #WEST} is at the left of the screen ({@link Position#getX()} getting smaller)
* </p>
*
* @see Position
*
* @author Tim Neumann
*/
public enum Direction {
/** the north direction; at the top of the screen ({@link Position#getY()} getting smaller) */
NORTH,
/** The east direction; at the right of the screen ({@link Position#getX()} getting bigger) */
EAST,
/** The south direction; at the bottom of the screen ({@link Position#getY()} getting bigger) */
SOUTH,
/** The west direction; at the left of the screen ({@link Position#getX()} getting smaller) */
WEST;

/**
* Get the direction that is next in a clock wise rotation
*
* @return The next direction
*/
public Direction clockWiseNext() {
return Direction.values()[(this.ordinal() + 1) % 4];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
public interface Playfield {

/**
* @return the simulation this playfield is part of
*/
Simulation getSimulation();

/**
* Get a list of all entities on this playfield.
*
Expand Down Expand Up @@ -90,16 +95,16 @@ public interface Playfield {
/**
* Move a entity of this simulation to a given position on this playfield.
*
* @param entity
* The entity to move; <b>must</b> be <b>on the field</b>; must <b>not</b> be <b>null</b>
* @param pos
* The position to move the entity to; must <b>not</b> be <b>null</b>
* @param entity
* The entity to add; <b>must</b> be <b>on the field</b>; must <b>not</b> be <b>null</b>
* @throws EntityNotOnFieldException
* if the given entity is not in this playfield
* @throws IllegalArgumentException
* if the given pos or entity is null
*/
void moveEntity(Position pos, Entity entity);
void moveEntity(Entity entity, Position pos);

/**
* Remove a entity of this simulation from this playfield.
Expand Down Expand Up @@ -136,4 +141,13 @@ public interface Playfield {
* if the given entity is null
*/
boolean containsEntity(Entity entity);

/**
* Check whether a solid entity is at the given position.
*
* @param pos
* The position to check
* @return true if and only if a solid entity is at that position
*/
boolean isSolidEntityAt(Position pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
* Objects of this class are immutable
* </p>
*
* <p>
* x is the row; negative towards the top ({@link Direction#NORTH}) and positive towards the bottom
* ({@link Direction#SOUTH})
* </p>
* <p>
* y is the column; negative towards the left ({@link Direction#WEST}) and positive towards the right
* ({@link Direction#EAST})
* </p>
*
* @see Direction
*
* @author Tim Neumann
*/
public class Position {
Expand Down Expand Up @@ -51,6 +62,28 @@ public int getY() {
return this.y;
}

/**
* Get the adjacent position in the given direction.
*
* @param direction
* The direction to get the adjacent position in
* @return The position adjacent to this in the given direction
*/
public Position adjacentPosition(Direction direction) {
switch (direction) {
case EAST:
return new Position(getX() + 1, getY());
case NORTH:
return new Position(getX(), getY() - 1);
case SOUTH:
return new Position(getX(), getY() + 1);
case WEST:
return new Position(getX() - 1, getY());
default:
throw new IllegalArgumentException("Unknown direction");
}
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Position)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public interface Simulation {
UiManager getUiManager();

/**
* Get the tick manager for this simulation.
* Get the simulation clock for this simulation.
*
* @return the ui manager used by this simulation
*/
SimulationClock getTickManager();
SimulationClock getSimulationClock();

/**
* Initialize the simulation and all its submodules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
*/
package de.unistuttgart.informatik.fius.icge.simulation;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;


/**
* The interface of the simulation clock, whcih produces the game and render ticks.
* The simulation clock is responsible for producing the game and render ticks.
*
* @author Tim Neumann
*/
Expand Down Expand Up @@ -61,7 +62,7 @@ public interface SimulationClock {
* Stop ticking.
* <p>
* Ticking can be started again with {@link #start()}
* <p>
* </p>
*/
public void stop();

Expand All @@ -86,4 +87,46 @@ public interface SimulationClock {
* The listener to be called.
*/
public void registerPostTickListener(Function<Long, Boolean> listener);

/**
* @return the number of the last simulation tick
*/
long getLastTickNumber();

/**
* Schedule an operation, to happen during the given tick. This method will block until that tick. Then the tick
* processing will halt until the given end of operation is completed.
* <p>
* If the given tick is in the past, the operation will be scheduled for the next tick.
* </p>
*
* @param tick
* The absolute number of the tick at which the operation will be run
* @param endOfOperation
* Tick processing will be halted until this future is completed
*/
void scheduleOperationAtTick(long tick, CompletableFuture<Void> endOfOperation);

/**
* Schedule an operation, to happen during the tick a given number of ticks in the future. This method will block
* until that tick. Then the tick processing will halt until the given end of operation is completed.
* <p>
* If the given number of ticks is less or equal to one, the operation is scheduled for the next tick.
* </p>
*
* @param ticks
* The number of ticks until the tick, for which to schedule the operation
* @param endOfOperation
* Tick processing will be halted until this future is completed
*/
void scheduleOperationInTicks(long ticks, CompletableFuture<Void> endOfOperation);

/**
* Schedule an operation, to happen during the next tick. This method will block until that tick. Then the tick
* processing will halt until the given end of operation is completed.
*
* @param endOfOperation
* Tick processing will be halted until this future is completed
*/
void scheduleOperationAtNextTick(CompletableFuture<Void> endOfOperation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

import de.unistuttgart.informatik.fius.icge.simulation.Playfield;
import de.unistuttgart.informatik.fius.icge.simulation.Position;
import de.unistuttgart.informatik.fius.icge.simulation.Simulation;
import de.unistuttgart.informatik.fius.icge.simulation.exception.EntityNotOnFieldException;
import de.unistuttgart.informatik.fius.icge.ui.Drawable;
import de.unistuttgart.informatik.fius.icge.ui.BasicDrawable;
import de.unistuttgart.informatik.fius.icge.ui.Drawable;


/**
Expand Down Expand Up @@ -70,26 +71,49 @@ public void initOnPlayfield(Playfield playfield) {
}

/**
* Get the playfield of this entity.
* Check whether this entity is on a playfield
*
* @return the playfield
* @throws EntityNotOnFieldException
* if this entity is not on a playfield
* @return true if and only if this entity is on a playfield
*/
protected Playfield getPlayfield() {
if (this.field == null) throw new EntityNotOnFieldException("This entity is not on a playfield");
public boolean isOnPlayfield() {
if (this.field == null) return false;
Playfield playfield = this.field.get();
if (playfield == null) {
// Was on playfield, but no reference to it left, so it does not have a reference to this either.
this.field = null;
throw new EntityNotOnFieldException("This entity is not on a playfield");
return false;
}
if (!playfield.containsEntity(this)) {
// Was on playfield, but no more.
this.field = null;
throw new EntityNotOnFieldException("This entity is not on a playfield");
return false;
}
return playfield;
return true;
}

/**
* Get the playfield of this entity.
*
* @return the playfield
* @throws EntityNotOnFieldException
* if this entity is not on a playfield
*/
protected Playfield getPlayfield() {
if (!isOnPlayfield()) throw new EntityNotOnFieldException("This entity is not on a playfield");
return this.field.get();
}

/**
* Get the simulation of this entity.
*
* @return the simulation
* @throws EntityNotOnFieldException
* if this entity is not on a playfield
* @throws IllegalStateException
* if the playfield of this entity is not part of any simulation
*/
protected Simulation getSimulation() {
return getPlayfield().getSimulation();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This source file is part of the FIUS ICGE project.
* For more information see github.com/FIUS/ICGE2
*
* Copyright (c) 2019 the ICGE project authors.
*
* This software is available under the MIT license.
* SPDX-License-Identifier: MIT
*/
package de.unistuttgart.informatik.fius.icge.simulation.entity;

import java.util.concurrent.CompletableFuture;

import de.unistuttgart.informatik.fius.icge.simulation.Direction;
import de.unistuttgart.informatik.fius.icge.simulation.Position;
import de.unistuttgart.informatik.fius.icge.simulation.exception.EntityNotOnFieldException;
import de.unistuttgart.informatik.fius.icge.simulation.exception.IllegalMoveException;


/**
* A movable entity
*
* @author Tim Neumann
*/
public abstract class MovableEntity extends BasicEntity {

private Direction lookingDirection = Direction.EAST;

/**
* Turn this entity for 90 degrees in clock wise direction.
*/
public void turnClockWise() {
final CompletableFuture<Void> endOfOperation = new CompletableFuture<>();
this.getSimulation().getSimulationClock().scheduleOperationAtNextTick(endOfOperation);
this.lookingDirection = this.lookingDirection.clockWiseNext();
endOfOperation.complete(null);
}

/**
* @return the current looking direction of this entity
*/
public Direction getLookingDirection() {
return this.lookingDirection;
}

private boolean isSolidEntityAt(final Position pos) {
return this.getPlayfield().isSolidEntityAt(pos);
}

/**
* Move this entity forward one field.
*
* @throws EntityNotOnFieldException
* if this entity is not on a playfield
* @throws IllegalMoveException
* if a solid entity is in the way
*/
public void move() {
final CompletableFuture<Void> endOfOperation = new CompletableFuture<>();
this.getSimulation().getSimulationClock().scheduleOperationInTicks(4, endOfOperation);
final Position nextPos = this.getPosition().adjacentPosition(this.lookingDirection);
if (this.isSolidEntityAt(nextPos)) throw new IllegalMoveException("Solid Entity in the way");
this.getPlayfield().moveEntity(this, nextPos);
endOfOperation.complete(null);
}

/**
* @return whether this entity can move forward one field.
*/
public boolean canMove() {
final Position nextPos = this.getPosition().adjacentPosition(this.lookingDirection);
return this.isOnPlayfield() && !this.isSolidEntityAt(nextPos);
}

/**
* Move this entity forward one field if that is possible.
*/
public void moveIfPossible() {
if (this.canMove()) {
this.move();
}
}
}
Loading

0 comments on commit a0107e1

Please sign in to comment.