From 75c7295051e61ff2461ca084c2e0783386962068 Mon Sep 17 00:00:00 2001 From: thanhminhmr Date: Thu, 3 Oct 2019 17:06:35 +0700 Subject: [PATCH] add comments --- src/mrmathami/thegame/GameController.java | 6 ++- .../thegame/drawer/EntityDrawer.java | 2 +- src/mrmathami/thegame/drawer/GameDrawer.java | 39 +++++++++++++++++-- .../{Entity => entity}/PlayerDrawer.java | 4 +- .../thegame/drawer/tile/BombDrawer.java | 2 +- .../thegame/drawer/tile/FlareDrawer.java | 2 +- .../thegame/drawer/tile/WallDrawer.java | 2 +- .../thegame/field/AbstractEntity.java | 19 +++++++++ src/mrmathami/thegame/field/GameEntities.java | 3 +- src/mrmathami/thegame/field/GameEntity.java | 2 +- src/mrmathami/thegame/field/GameField.java | 35 +++++++++++++++++ .../characteristic/DestroyableEntity.java | 4 -- .../field/characteristic/LivingEntity.java | 4 -- .../field/characteristic/UpdatableEntity.java | 4 -- 14 files changed, 102 insertions(+), 26 deletions(-) rename src/mrmathami/thegame/drawer/{Entity => entity}/PlayerDrawer.java (64%) diff --git a/src/mrmathami/thegame/GameController.java b/src/mrmathami/thegame/GameController.java index 638cf39..5a8a9bd 100644 --- a/src/mrmathami/thegame/GameController.java +++ b/src/mrmathami/thegame/GameController.java @@ -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; @@ -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. @@ -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)); diff --git a/src/mrmathami/thegame/drawer/EntityDrawer.java b/src/mrmathami/thegame/drawer/EntityDrawer.java index 2c343c9..d128052 100644 --- a/src/mrmathami/thegame/drawer/EntityDrawer.java +++ b/src/mrmathami/thegame/drawer/EntityDrawer.java @@ -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); } diff --git a/src/mrmathami/thegame/drawer/GameDrawer.java b/src/mrmathami/thegame/drawer/GameDrawer.java index 2ec6381..a97481c 100644 --- a/src/mrmathami/thegame/drawer/GameDrawer.java +++ b/src/mrmathami/thegame/drawer/GameDrawer.java @@ -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; @@ -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> 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, EntityDrawer> ENTITY_DRAWER_MAP = Map.of( Bomb.class, new BombDrawer(), Player.class, new PlayerDrawer(), @@ -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()) @@ -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()); @@ -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; @@ -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, diff --git a/src/mrmathami/thegame/drawer/Entity/PlayerDrawer.java b/src/mrmathami/thegame/drawer/entity/PlayerDrawer.java similarity index 64% rename from src/mrmathami/thegame/drawer/Entity/PlayerDrawer.java rename to src/mrmathami/thegame/drawer/entity/PlayerDrawer.java index 52d8ea6..4b6889c 100644 --- a/src/mrmathami/thegame/drawer/Entity/PlayerDrawer.java +++ b/src/mrmathami/thegame/drawer/entity/PlayerDrawer.java @@ -1,4 +1,4 @@ -package mrmathami.thegame.drawer.Entity; +package mrmathami.thegame.drawer.entity; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; @@ -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); } diff --git a/src/mrmathami/thegame/drawer/tile/BombDrawer.java b/src/mrmathami/thegame/drawer/tile/BombDrawer.java index 4ebc171..5aed02f 100644 --- a/src/mrmathami/thegame/drawer/tile/BombDrawer.java +++ b/src/mrmathami/thegame/drawer/tile/BombDrawer.java @@ -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); } diff --git a/src/mrmathami/thegame/drawer/tile/FlareDrawer.java b/src/mrmathami/thegame/drawer/tile/FlareDrawer.java index 2608750..7cc3acb 100644 --- a/src/mrmathami/thegame/drawer/tile/FlareDrawer.java +++ b/src/mrmathami/thegame/drawer/tile/FlareDrawer.java @@ -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); } diff --git a/src/mrmathami/thegame/drawer/tile/WallDrawer.java b/src/mrmathami/thegame/drawer/tile/WallDrawer.java index 3a35cb4..abf5b52 100644 --- a/src/mrmathami/thegame/drawer/tile/WallDrawer.java +++ b/src/mrmathami/thegame/drawer/tile/WallDrawer.java @@ -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); } diff --git a/src/mrmathami/thegame/field/AbstractEntity.java b/src/mrmathami/thegame/field/AbstractEntity.java index 0fbfda0..0da534f 100644 --- a/src/mrmathami/thegame/field/AbstractEntity.java +++ b/src/mrmathami/thegame/field/AbstractEntity.java @@ -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; @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/mrmathami/thegame/field/GameEntities.java b/src/mrmathami/thegame/field/GameEntities.java index 40d40d9..58d4c27 100644 --- a/src/mrmathami/thegame/field/GameEntities.java +++ b/src/mrmathami/thegame/field/GameEntities.java @@ -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 COLLISION_PAIR_SET = new HashSet<>(Set.of( diff --git a/src/mrmathami/thegame/field/GameEntity.java b/src/mrmathami/thegame/field/GameEntity.java index 4cbab4e..41f3f07 100644 --- a/src/mrmathami/thegame/field/GameEntity.java +++ b/src/mrmathami/thegame/field/GameEntity.java @@ -1,7 +1,7 @@ package mrmathami.thegame.field; /** - * + * A game entity */ public interface GameEntity { /** diff --git a/src/mrmathami/thegame/field/GameField.java b/src/mrmathami/thegame/field/GameField.java index 10159b9..c9e6f61 100644 --- a/src/mrmathami/thegame/field/GameField.java +++ b/src/mrmathami/thegame/field/GameField.java @@ -18,8 +18,17 @@ public final class GameField { @Nonnull private final Collection unmodifiableEntities = Collections.unmodifiableCollection(entities); @Nonnull private final List 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) { @@ -40,22 +49,43 @@ public final int getTickCount() { return tickCount; } + /** + * @return entities on the field. Read-only list. + */ @Nonnull public final Collection 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; @@ -68,6 +98,7 @@ public final void tick() { } } + // 1.3. Update DestroyableEntity final List destroyedEntities = new ArrayList<>(Config.TILE_MAP_COUNT); for (final GameEntity entity : entities) { if (entity instanceof DestroyableEntity && ((DestroyableEntity) entity).isDestroyed()) { @@ -75,10 +106,14 @@ public final void tick() { 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(); } diff --git a/src/mrmathami/thegame/field/characteristic/DestroyableEntity.java b/src/mrmathami/thegame/field/characteristic/DestroyableEntity.java index 1399f96..cd29bb3 100644 --- a/src/mrmathami/thegame/field/characteristic/DestroyableEntity.java +++ b/src/mrmathami/thegame/field/characteristic/DestroyableEntity.java @@ -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(); diff --git a/src/mrmathami/thegame/field/characteristic/LivingEntity.java b/src/mrmathami/thegame/field/characteristic/LivingEntity.java index 386673a..d3efdea 100644 --- a/src/mrmathami/thegame/field/characteristic/LivingEntity.java +++ b/src/mrmathami/thegame/field/characteristic/LivingEntity.java @@ -1,9 +1,5 @@ package mrmathami.thegame.field.characteristic; -/** - * Marker interface. - * Mark living objects. - */ public interface LivingEntity extends DestroyableEntity { float getHealth(); diff --git a/src/mrmathami/thegame/field/characteristic/UpdatableEntity.java b/src/mrmathami/thegame/field/characteristic/UpdatableEntity.java index 05e17e2..f4ffe88 100644 --- a/src/mrmathami/thegame/field/characteristic/UpdatableEntity.java +++ b/src/mrmathami/thegame/field/characteristic/UpdatableEntity.java @@ -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); }