Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhminhmr committed Oct 3, 2019
1 parent aa869af commit 75c7295
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/mrmathami/thegame/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.stage.WindowEvent;
import mrmathami.thegame.drawer.GameDrawer;
import mrmathami.thegame.field.GameField;
import mrmathami.thegame.field.characteristic.PlayerEntity;
import mrmathami.thegame.field.entity.Player;
import mrmathami.thegame.field.tile.Wall;
import mrmathami.utilities.ThreadFactoryBuilder;
Expand Down Expand Up @@ -43,7 +44,7 @@ public final class GameController extends AnimationTimer {
* The player entity. Because it can receive keyboard and/or mouse event,
* it should be put in here. If we need multi-player, use an array of Players.
*/
private Player player;
private PlayerEntity player;
/**
* Game field. Contain everything in the current game field.
* Responsible to update the field every tick.
Expand Down Expand Up @@ -81,10 +82,11 @@ public GameController(GraphicsContext graphicsContext) {
final int width = Config.TILE_HORIZONTAL;
final int height = Config.TILE_VERTICAL;

// create new player
this.player = new Player(0, 1.0f, 1.0f, 0.9f, 0.9f, 10000.0f);

// The game field. Please consider create another way to load a game field.
// I don't have much time, so, spawn some wall then :)
// TODO: I don't have much time, so, spawn some wall then :)
this.field = new GameField(width, height);
field.doSpawn(new Wall(0, 0, 0, width, 1));
field.doSpawn(new Wall(0, 0, height - 1, width, 1));
Expand Down
2 changes: 1 addition & 1 deletion src/mrmathami/thegame/drawer/EntityDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import javax.annotation.Nonnull;

public interface EntityDrawer {
void draw(@Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom);
void draw(int tickCount, @Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom);
}
39 changes: 35 additions & 4 deletions src/mrmathami/thegame/drawer/GameDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import mrmathami.thegame.Config;
import mrmathami.thegame.drawer.entity.PlayerDrawer;
import mrmathami.thegame.drawer.tile.BombDrawer;
import mrmathami.thegame.drawer.tile.FlareDrawer;
import mrmathami.thegame.drawer.Entity.PlayerDrawer;
import mrmathami.thegame.drawer.tile.WallDrawer;
import mrmathami.thegame.field.GameEntities;
import mrmathami.thegame.field.GameEntity;
Expand All @@ -22,12 +22,22 @@
import java.util.Map;

public final class GameDrawer {
/**
* TODO:
* This is a list contains Entity type that can be drawn on screen.
* Remember to add your own entity class here if it can be drawn.
*/
@Nonnull private static final List<Class<?>> ENTITY_DRAWING_ORDER = List.of(
Bomb.class,
Player.class,
Flare.class,
Wall.class
);
/**
* TODO:
* This is a map between Entity type and its drawer.
* Remember to add your entity drawer here.
*/
@Nonnull private static final Map<Class<?>, EntityDrawer> ENTITY_DRAWER_MAP = Map.of(
Bomb.class, new BombDrawer(),
Player.class, new PlayerDrawer(),
Expand All @@ -46,7 +56,15 @@ public GameDrawer(@Nonnull GraphicsContext graphicsContext, @Nonnull GameField g
this.gameField = gameField;
}

public static int entityDrawingOrderComparator(@Nonnull GameEntity entityA, @Nonnull GameEntity entityB) {
/**
* Do not touch me.
* This is a drawing order comparator, use to sort the entity list.
*
* @param entityA entity A
* @param entityB entity B
* @return order
*/
private static int entityDrawingOrderComparator(@Nonnull GameEntity entityA, @Nonnull GameEntity entityB) {
final int compareOrder = Integer.compare(
ENTITY_DRAWING_ORDER.indexOf(entityA.getClass()),
ENTITY_DRAWING_ORDER.indexOf(entityB.getClass())
Expand All @@ -61,6 +79,10 @@ public static int entityDrawingOrderComparator(@Nonnull GameEntity entityA, @Non
return Float.compare(entityA.getHeight(), entityB.getHeight());
}

/**
* @param entity entity
* @return the drawer fot that entity, or null if that entity is not drawable.
*/
@Nullable
private static EntityDrawer getEntityDrawer(@Nonnull GameEntity entity) {
return ENTITY_DRAWER_MAP.get(entity.getClass());
Expand All @@ -82,12 +104,22 @@ public final void setGameField(@Nonnull GameField gameField) {
this.gameField = gameField;
}

/**
* Set the field view region, in other words, set the region of the field that will be drawn on the screen.
*
* @param fieldStartPosX pos x
* @param fieldStartPosY pos y
* @param fieldZoom zoom
*/
public final void setFieldViewRegion(float fieldStartPosX, float fieldStartPosY, float fieldZoom) {
this.fieldStartPosX = fieldStartPosX;
this.fieldStartPosY = fieldStartPosY;
this.fieldZoom = fieldZoom;
}

/**
* Do render. Should not touch.
*/
public final void render() {
final GameField gameField = this.gameField;
final float fieldStartPosX = this.fieldStartPosX;
Expand All @@ -107,8 +139,7 @@ public final void render() {
lastEntity = entity;
final EntityDrawer drawer = getEntityDrawer(entity);
if (drawer != null) {
drawer.draw(
graphicsContext, entity,
drawer.draw(gameField.getTickCount(), graphicsContext, entity,
(entity.getPosX() - fieldStartPosX) * fieldZoom,
(entity.getPosY() - fieldStartPosY) * fieldZoom,
entity.getWidth() * fieldZoom,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mrmathami.thegame.drawer.Entity;
package mrmathami.thegame.drawer.entity;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
Expand All @@ -9,7 +9,7 @@

public final class PlayerDrawer implements EntityDrawer {
@Override
public void draw(@Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
public void draw(int tickCount, @Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
graphicsContext.setFill(Color.GREEN);
graphicsContext.fillRoundRect(screenPosX, screenPosY, screenWidth, screenHeight, zoom / 10.0f, zoom / 10.0f);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mrmathami/thegame/drawer/tile/BombDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public final class BombDrawer implements EntityDrawer {
@Override
public void draw(@Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
public void draw(int tickCount, @Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
graphicsContext.setFill(Color.YELLOW);
graphicsContext.fillOval(screenPosX, screenPosY, screenWidth, screenHeight);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mrmathami/thegame/drawer/tile/FlareDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public final class FlareDrawer implements EntityDrawer {
@Override
public void draw(@Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
public void draw(int tickCount, @Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
graphicsContext.setFill(Color.ORANGERED);
graphicsContext.fillRoundRect(screenPosX, screenPosY, screenWidth, screenHeight, 1, 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mrmathami/thegame/drawer/tile/WallDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public final class WallDrawer implements EntityDrawer {
@Override
public void draw(@Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
public void draw(int tickCount, @Nonnull GraphicsContext graphicsContext, @Nonnull GameEntity entity, float screenPosX, float screenPosY, float screenWidth, float screenHeight, float zoom) {
graphicsContext.setFill(Color.WHITE);
graphicsContext.fillRect(screenPosX, screenPosY, screenWidth, screenHeight);
}
Expand Down
19 changes: 19 additions & 0 deletions src/mrmathami/thegame/field/AbstractEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mrmathami.thegame.field;

/**
* Abstract class for game entity.
*/
public abstract class AbstractEntity implements GameEntity {
private final int createdTick;
private float posX;
Expand All @@ -25,6 +28,10 @@ public final float getPosX() {
return posX;
}

/**
* Set entity field pos x. Should only be called in doUpdate of UpdatableEntity
* @param posX field pos x
*/
protected final void setPosX(float posX) {
this.posX = posX;
}
Expand All @@ -34,6 +41,10 @@ public final float getPosY() {
return posY;
}

/**
* Set entity field pos y. Should only be called in doUpdate of UpdatableEntity
* @param posY field pos y
*/
protected final void setPosY(float posY) {
this.posY = posY;
}
Expand All @@ -43,6 +54,10 @@ public final float getWidth() {
return width;
}

/**
* Set entity field width. Should only be called in doUpdate of UpdatableEntity
* @param width field width
*/
protected final void setWidth(float width) {
this.width = width;
}
Expand All @@ -52,6 +67,10 @@ public final float getHeight() {
return height;
}

/**
* Set entity field height. Should only be called in doUpdate of UpdatableEntity
* @param height field height
*/
protected final void setHeight(float height) {
this.height = height;
}
Expand Down
3 changes: 2 additions & 1 deletion src/mrmathami/thegame/field/GameEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

public final class GameEntities {
/**
* CHANGE ME: This is a list contains Pair of Entities that can collide with each other.
* TODO:
* This is a list contains Pair of Entities that can collide with each other.
* Remember, if an entity can collide with itself, you should put that here too.
*/
private static final Set<Pair> COLLISION_PAIR_SET = new HashSet<>(Set.of(
Expand Down
2 changes: 1 addition & 1 deletion src/mrmathami/thegame/field/GameEntity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mrmathami.thegame.field;

/**
*
* A game entity
*/
public interface GameEntity {
/**
Expand Down
35 changes: 35 additions & 0 deletions src/mrmathami/thegame/field/GameField.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ public final class GameField {
@Nonnull private final Collection<GameEntity> unmodifiableEntities = Collections.unmodifiableCollection(entities);
@Nonnull private final List<GameEntity> spawnEntities = new ArrayList<>(Config.TILE_MAP_COUNT);

/**
* Field width
*/
private final float width;
/**
* Field height
*/
private final float height;
/**
* Field tick count
*/
private int tickCount;

public GameField(float width, float height) {
Expand All @@ -40,22 +49,43 @@ public final int getTickCount() {
return tickCount;
}

/**
* @return entities on the field. Read-only list.
*/
@Nonnull
public final Collection<GameEntity> getEntities() {
return unmodifiableEntities;
}

/**
* Add an Entity to spawn list. Entity will be spawned at the end of this tick.
* @param entity Entity to spawn
*/
public final void doSpawn(@Nonnull GameEntity entity) {
if (entity.isBeingOverlapped(0.0f, 0.0f, width, height)) spawnEntities.add(entity);
}

/**
* Do a tick, in other words, update the field after a fixed period of time.
* Current update sequence:
* 1. Update Entity:
* 1.1. UpdatableEntity update itself, including moving.
* 1.2. EffectEntity check collision to affect LivingEntity.
* 1.3. DestroyableEntity check and react if it is going to be destroyed.
* 2. Destroy Entity:
* 2.1. Destroy entities that are marked to be destroyed.
* 2.2. Destroy entities that are outside the field.
* 3. Spawn Entity: Add entities that are marked to be spawned.
*/
public final void tick() {
this.tickCount += 1;

// 1.1. Update UpdatableEntity
for (final GameEntity entity : entities) {
if (entity instanceof UpdatableEntity) ((UpdatableEntity) entity).doUpdate(this, tickCount);
}

// 1.2. Update EffectEntity & LivingEntity
for (final GameEntity entity : entities) {
if (entity instanceof EffectEntity) {
final EffectEntity effectEntity = (EffectEntity) entity;
Expand All @@ -68,17 +98,22 @@ public final void tick() {
}
}

// 1.3. Update DestroyableEntity
final List<GameEntity> destroyedEntities = new ArrayList<>(Config.TILE_MAP_COUNT);
for (final GameEntity entity : entities) {
if (entity instanceof DestroyableEntity && ((DestroyableEntity) entity).isDestroyed()) {
if (entity instanceof DestroyableEntity.DestroyListener) ((DestroyableEntity.DestroyListener) entity).onDestroy(this, tickCount);
destroyedEntities.add(entity);
}
}

// 2.1. Destroy entities
entities.removeAll(destroyedEntities);

// 2.2. Destroy entities
entities.removeIf(entity -> !entity.isBeingOverlapped(0.0f, 0.0f, width, height));

// 3. Spawn entities
entities.addAll(spawnEntities);
spawnEntities.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import javax.annotation.Nonnull;

/**
* Marker interface.
* Mark objects that can be removed from the field one created.
*/
public interface DestroyableEntity extends GameEntity {
void doDestroy();

Expand Down
4 changes: 0 additions & 4 deletions src/mrmathami/thegame/field/characteristic/LivingEntity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package mrmathami.thegame.field.characteristic;

/**
* Marker interface.
* Mark living objects.
*/
public interface LivingEntity extends DestroyableEntity {
float getHealth();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import javax.annotation.Nonnull;

/**
* Marker interface.
* Mark updatable objects.
*/
public interface UpdatableEntity extends GameEntity {
void doUpdate(@Nonnull GameField field, int tickCount);
}

0 comments on commit 75c7295

Please sign in to comment.