From 4de44f6207f53848ea709e428284bab9fab080aa Mon Sep 17 00:00:00 2001 From: Gottschali Date: Sat, 2 Apr 2022 22:20:23 +0200 Subject: [PATCH 1/2] OOP Hex --- .../src/client/components/WelcomeInsect.tsx | 6 +- cra-hive/src/client/lab/BackgroundCanvas.tsx | 4 +- cra-hive/src/client/pages/TutorialPage.tsx | 88 +++--- cra-hive/src/shared/hexlib.ts | 292 +++++++----------- cra-hive/src/shared/model/action.ts | 6 +- cra-hive/src/shared/model/hive.ts | 18 +- cra-hive/src/shared/model/state.ts | 6 +- 7 files changed, 172 insertions(+), 248 deletions(-) diff --git a/cra-hive/src/client/components/WelcomeInsect.tsx b/cra-hive/src/client/components/WelcomeInsect.tsx index 1ee88b0..42150a4 100644 --- a/cra-hive/src/client/components/WelcomeInsect.tsx +++ b/cra-hive/src/client/components/WelcomeInsect.tsx @@ -10,9 +10,9 @@ export default function WelcomeInsect() { + hex={new HEX.Hex(0, 0)} + stone={{ "team": "white", "insect": "bee" }} + /> ) diff --git a/cra-hive/src/client/lab/BackgroundCanvas.tsx b/cra-hive/src/client/lab/BackgroundCanvas.tsx index b51a563..9bfa911 100644 --- a/cra-hive/src/client/lab/BackgroundCanvas.tsx +++ b/cra-hive/src/client/lab/BackgroundCanvas.tsx @@ -4,7 +4,7 @@ import { State } from '../../shared/model/state' const orientation = HEX.orientation_pointy; -const layout = HEX.Layout(orientation, HEX.Point(10, 10), HEX.Point(500, 250)) +const layout = HEX.Layout(orientation, new HEX.Point(10, 10), new HEX.Point(500, 250)) export default function BackgroundCanvas() { const state = new State(); @@ -17,7 +17,7 @@ export default function BackgroundCanvas() { ctx.beginPath() const corners = HEX.polygon_corners(layout, hex); ctx.moveTo(corners[5].x + 50, corners[5].y + 50); - corners.forEach( p => { + corners.forEach(p => { ctx.lineTo(p.x + 50, p.y + 50); }) ctx.fill(); diff --git a/cra-hive/src/client/pages/TutorialPage.tsx b/cra-hive/src/client/pages/TutorialPage.tsx index 0c27aaf..3d58c79 100644 --- a/cra-hive/src/client/pages/TutorialPage.tsx +++ b/cra-hive/src/client/pages/TutorialPage.tsx @@ -6,11 +6,11 @@ import Insect from '../../shared/model/insects' import Team from '../../shared/model/teams' import { Drop } from '../../shared/model/action' import TestGame from '../lab/TestGame'; -import {Hex} from '../../shared/hexlib' -import {DropInsectMenuTeam, iconMap} from '../components/DropInsectMenu' +import { Hex } from '../../shared/hexlib' +import { DropInsectMenuTeam, iconMap } from '../components/DropInsectMenu' import { Button } from 'semantic-ui-react' -export default function TutorialPage () { +export default function TutorialPage() { // Idea: define terms // Make chapers: click to next page // Boxes for emphasis @@ -41,70 +41,70 @@ export default function TutorialPage () { } -function TutorialLayout({left, right}) { +function TutorialLayout({ left, right }) { return
-
- {left} -
-
- {right} -
+
+ {left}
+
+ {right} +
+
} function Introduction() { const state = new State(); - state.apply(new Drop(new Stone(Insect.GRASSHOPPER, Team.WHITE), Hex(0, 0))); - state.apply(new Drop(new Stone(Insect.ANT, Team.BLACK), Hex(1, 0))); + state.apply(new Drop(new Stone(Insect.GRASSHOPPER, Team.WHITE), new Hex(0, 0))); + state.apply(new Drop(new Stone(Insect.ANT, Team.BLACK), new Hex(1, 0))); const left = <>

Introduction

- Hive is a strategic boardgame for two players (red and blue). - Each turn they can either place or move a stone on the board. We will refer to placing an insect as "dropping". - We will call the stones "insects" from now on. Each of the insects - has its own set of rules how to move. - In the beginning there is void and the starting player can select any of his insects to drop.; - const right = ; + Hive is a strategic boardgame for two players (red and blue). + Each turn they can either place or move a stone on the board. We will refer to placing an insect as "dropping". + We will call the stones "insects" from now on. Each of the insects + has its own set of rules how to move. + In the beginning there is void and the starting player can select any of his insects to drop.; + const right = ; return } function Dropping() { const state = new State(); - state.apply(new Drop(new Stone(Insect.GRASSHOPPER, Team.WHITE), Hex(0, 0))); - state.apply(new Drop(new Stone(Insect.ANT, Team.BLACK), Hex(1, 0))); + state.apply(new Drop(new Stone(Insect.GRASSHOPPER, Team.WHITE), new Hex(0, 0))); + state.apply(new Drop(new Stone(Insect.ANT, Team.BLACK), new Hex(1, 0))); const left = <> -

Dropping

- - You can select an insect from this menu to drop. Note the numbers denoting how many of each type you still have available. - ; +

Dropping

+ + You can select an insect from this menu to drop. Note the numbers denoting how many of each type you still have available. + ; const right = [h, 0])} - canvasHeight="200px" interactive={false} cameraOpts={ {position: [0, -3, 4]} } />; + team={state.team} + highlighted={state.generateDrops().map(h => [h, 0])} + canvasHeight="200px" interactive={false} cameraOpts={{ position: [0, -3, 4] }} />; return } function GoalOfGame() { const hive = new HiveModel(); - hive.addStone(Hex(0, 0), new Stone(Insect.GRASSHOPPER, Team.WHITE)); - hive.addStone(Hex(1, 0), new Stone(Insect.BEE, Team.BLACK)); - hive.addStone(Hex(0, 1), new Stone(Insect.BEE, Team.WHITE)); - hive.addStone(Hex(1, 1), new Stone(Insect.ANT, Team.BLACK)); - hive.addStone(Hex(1, -1), new Stone(Insect.SPIDER, Team.WHITE)); - hive.addStone(Hex(2, 0), new Stone(Insect.BEETLE, Team.BLACK)); - hive.addStone(Hex(2, -1), new Stone(Insect.ANT, Team.WHITE)); + hive.addStone(new Hex(0, 0), new Stone(Insect.GRASSHOPPER, Team.WHITE)); + hive.addStone(new Hex(1, 0), new Stone(Insect.BEE, Team.BLACK)); + hive.addStone(new Hex(0, 1), new Stone(Insect.BEE, Team.WHITE)); + hive.addStone(new Hex(1, 1), new Stone(Insect.ANT, Team.BLACK)); + hive.addStone(new Hex(1, -1), new Stone(Insect.SPIDER, Team.WHITE)); + hive.addStone(new Hex(2, 0), new Stone(Insect.BEETLE, Team.BLACK)); + hive.addStone(new Hex(2, -1), new Stone(Insect.ANT, Team.WHITE)); const left = <> -

The Goal of the Game

-

The goal of the game is to surround the bee