Skip to content

Commit

Permalink
cleanup PlayScreen class (#137)
Browse files Browse the repository at this point in the history
* cleanup PlayScreen class

* fixup! cleanup PlayScreen class

* fixup! cleanup PlayScreen class
  • Loading branch information
fumba authored Jul 4, 2021
1 parent f3c9473 commit d42ecbe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
33 changes: 13 additions & 20 deletions src/js/screens/PlayScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,18 @@ class PlayScreen extends me.Stage {
const gameSpeed = 500;

//update GUI elements state

let refreshHoleSleepingState = true; //initially refresh state so that the initial seed arrangement is rendered
me.timer.setInterval(() => {
const task = this.board.uiTaskQueue.dequeue();
if (task) {
//re-render all holes on board
[this.board.topPlayer, this.board.bottomPlayer].forEach((player) => {
for (const hole of player.boardHoles) {
let count = 0;
UiHelper.forEachUiSeedInHole(hole, () => {
count++;
});
hole.ui.label.setText(count);
hole.ui.renderable = hole.ui.sleepingHoleSprite;
}
// refresh all holes status after the task is complete
refreshHoleSleepingState = true;
//re-render all holes on board for every UI update
UiHelper.forEachBoardHole(this.board, (hole: Hole) => {
hole.ui.label.setText(hole.ui.seedCount());
hole.ui.renderable = hole.ui.sleepingHoleSprite;
});
console.info(`UI - Recieved task : ${task.name}`);
switch (task.name) {
case UiTaskActions.SOW_SEED_INTO_HOLE: {
const seedGroupUI: SeedGroupUI = task.seedGroupUI as SeedGroupUI;
Expand All @@ -64,15 +61,13 @@ class PlayScreen extends me.Stage {
for (const seedUI of this.board.getCurrentPlayer().ui.seedsInHand) {
seedUI.randomisePosition();
}

seedGroupUI.hole.ui.renderable =
seedGroupUI.hole.ui.startHoleSprite;
break;
}
case UiTaskActions.GRAB_ALL_SEEDS_FROM_HOLE: {
const hole: Hole = task.hole as Hole;
console.info(`UI - getting all seeds from hole ${hole.UID}`);

//remove all ui seeds from hole
UiHelper.forEachUiSeedInHole(hole, (seedUI: SeedUI) => {
const currentPlayerHandUI = this.board.getCurrentPlayer();
Expand All @@ -81,21 +76,19 @@ class PlayScreen extends me.Stage {
seedUI.id = null;
seedUI.randomisePosition();
});

hole.ui.renderable = hole.ui.startHoleSprite;
break;
}
}
} else {
const draggingSeedGroup = UiHelper.getCurrentDraggingSeedGroup(me);
//re-render all holes on board
if (!draggingSeedGroup) {
[this.board.topPlayer, this.board.bottomPlayer].forEach((player) => {
for (const hole of player.boardHoles) {
hole.ui.label.setText(hole.toString());
hole.ui.sleepStateUI();
}
if (!draggingSeedGroup && refreshHoleSleepingState == true) {
UiHelper.forEachBoardHole(this.board, (hole: Hole) => {
hole.ui.label.setText(hole.ui.seedCount());
hole.ui.sleepStateUI();
});
refreshHoleSleepingState = false;
}
}
}, gameSpeed);
Expand Down
15 changes: 12 additions & 3 deletions src/js/ui_entities/HoleUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Hole from "../core/Hole";
import Move from "../core/Move";
import me from "../me";
import SeedGroupUI from "./SeedGroupUI";
import UiHelper from "./UiHelper";

/*
* bawo.zone - <a href="https://bawo.zone">https://bawo.zone</a>
Expand Down Expand Up @@ -83,12 +84,12 @@ class HoleUI extends me.DroptargetEntity {

this.sleepStateUI();

this.label = new me.Text(this.pos.x + 15, this.pos.y - 10, {
this.label = new me.Text(this.pos.x + 15, this.pos.y - 12, {
font: "Arial",
size: 9,
size: 15,
fillStyle: this.color,
});
this.label.setText(this.hole.toString());
this.label.setText(this.seedCount());
me.game.world.addChild(this.label);
}

Expand Down Expand Up @@ -120,6 +121,14 @@ class HoleUI extends me.DroptargetEntity {
);
}
}

public seedCount(): number {
let count = 0;
UiHelper.forEachUiSeedInHole(this.hole, () => {
count++;
});
return count;
}
}

export default HoleUI;
13 changes: 13 additions & 0 deletions src/js/ui_entities/UiHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CANCELLED } from "dns";
import Board from "../core/Board";
import Hole from "../core/Hole";
import HoleUI from "./HoleUI";
import SeedGroupUI from "./SeedGroupUI";
Expand Down Expand Up @@ -47,6 +49,17 @@ class UiHelper {
);
seeds.forEach((seed) => callback(seed));
}

public static forEachBoardHole(
board: Board,
callback: CallableFunction
): void {
[board.topPlayer, board.bottomPlayer].forEach((player) => {
for (const hole of player.boardHoles) {
callback(hole);
}
});
}
}

export default UiHelper;

0 comments on commit d42ecbe

Please sign in to comment.