Skip to content

Commit 74122b6

Browse files
committed
page (ui add): buttons
- game mode dropdown - new game - restart same - reveal labyrinthe
1 parent 49831b8 commit 74122b6

File tree

11 files changed

+424
-106
lines changed

11 files changed

+424
-106
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"typescript": "^3.8.3"
2828
},
2929
"dependencies": {
30-
"hammerjs": "^2.0.8",
30+
"@ui5/webcomponents": "^1.0.0-rc.7",
3131
"node-dijkstra": "^2.5.0",
3232
"parcel": "^1.12.4",
3333
"rxjs": "^7.0.0-beta.0",

src/core/core.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import {
1212
import { w } from '../util/window'
1313
import { generateLabyrinth } from './labyrinth/generateLabyrinth'
1414
import { pairEqual, pairToString, pairFromString } from '../util/pair'
15+
import { Observable } from 'rxjs'
1516

1617
export interface LoadProp {
1718
config: PonyEscapeConfig
1819
display: PonyDisplay
1920
input: PonyInput
2021
random: prng
2122
size: Pair
23+
revealLabyrinth$: Observable<Event>
24+
setRevealButtonVisibility: (value: boolean) => void
2225
}
2326

2427
export let core = (prop: LoadProp) => {
2528
let { config, display, input, size } = prop
29+
let { revealLabyrinth$, setRevealButtonVisibility } = prop
2630

2731
let {
2832
grid,
@@ -54,10 +58,7 @@ export let core = (prop: LoadProp) => {
5458
let moveCount = 0
5559

5660
let hideAllWalls = () => {
57-
oddWallList.forEach(({ wall }) => {
58-
wall.visibility = 'invisible'
59-
})
60-
evenWallList.forEach(({ wall }) => {
61+
oddWallList.concat(evenWallList).forEach(({ wall }) => {
6162
wall.visibility = 'invisible'
6263
})
6364
}
@@ -75,6 +76,11 @@ export let core = (prop: LoadProp) => {
7576
})
7677
}
7778

79+
revealLabyrinth$.subscribe(() => {
80+
oddWallList.forEach(makeWallVisible)
81+
render()
82+
})
83+
7884
/**
7985
* move
8086
* Let the player do a move

src/core/labyrinth/generateLabyrinth.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export let generateLabyrinth = (prop: LoadProp) => {
2424
let twiceSize = { x: size.x * 2, y: size.y * 2 }
2525

2626
let groundList: LocalizedGround[] = []
27+
let wallList: LocalizedWall[] = []
2728
let oddWallList: LocalizedWall[] = []
2829
let evenWallList: LocalizedWall[] = []
2930

@@ -56,10 +57,13 @@ export let generateLabyrinth = (prop: LoadProp) => {
5657
}
5758

5859
if (!isMapBorder()) {
60+
let localizedWall = { x, y, wall: me }
61+
wallList.push(localizedWall)
62+
5963
if (isOddWall()) {
60-
oddWallList.push({ x, y, wall: me })
64+
oddWallList.push(localizedWall)
6165
} else {
62-
evenWallList.push({ x, y, wall: me })
66+
evenWallList.push(localizedWall)
6367
}
6468
}
6569

@@ -86,8 +90,6 @@ export let generateLabyrinth = (prop: LoadProp) => {
8690
let selectedWallList = kruskal({
8791
linkList: oddWallList,
8892
getNodePair: (wall: LocalizedWall) => {
89-
// if (wall.y == 2 && wall.x == 1) debugger
90-
9193
if (wall.x % 2 === 0) {
9294
return [grid[wall.y][wall.x - 1], grid[wall.y][wall.x + 1]]
9395
} else if (wall.y % 2 === 0) {
@@ -133,5 +135,5 @@ export let generateLabyrinth = (prop: LoadProp) => {
133135
dijkstra.addNode(label, siblingObj)
134136
})
135137

136-
return { grid, wallGrid, oddWallList, evenWallList, dijkstra }
138+
return { grid, wallGrid, wallList, oddWallList, evenWallList, dijkstra }
137139
}

src/main.ts

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,20 @@
11
import { default as seedrandom } from 'seedrandom'
2-
32
import { core } from './core/core'
43
import { getAsset } from './display/asset'
54
import { createDisplay } from './display/display'
65
import { createInput } from './input/input'
76
import { init } from './page/init'
8-
import { PonyEscapeConfig } from './type/ponyEscapeConfig'
9-
import { randomSeed } from './util/randomSeed'
10-
import { getUrlParam, spacelessURL } from './util/urlParam'
7+
import { spacelessURL } from './util/urlParam'
118

129
export let main = async () => {
1310
spacelessURL(location)
1411

15-
let config = getUrlParam<PonyEscapeConfig>(location, {
16-
seed: () => randomSeed(),
17-
smooze: () => false,
18-
easy: () => false,
19-
justSmooze: ({ smooze }) => smooze(),
20-
hard: () => false,
21-
hide: ({ smooze }) => smooze(),
22-
hideDelay: ({ hide }) => (hide() ? 4 : -1),
23-
highlight: () => false,
24-
smoozeDelay: ({ justSmooze }) => (justSmooze() ? 5 : -1),
25-
size: ({ easy, hard, hideDelay }) => {
26-
let difficulty = easy() ? 0 : hard() ? 2 : 1
27-
return (hideDelay() >= 0 ? [7, 8, 10] : [12, 15, 21])[difficulty]
28-
},
29-
cycle: () => -1,
30-
maxCycleSize: ({ cycle, size }) => {
31-
if (cycle() >= 0) {
32-
return cycle() * cycle() * size()
33-
} else {
34-
return 0
35-
}
36-
},
37-
cycleRejectionFrequency: () => 0,
12+
let { canvas, config, revealLabyrinth$, setRevealButtonVisibility } = init({
13+
document,
14+
location,
3815
})
3916

40-
if (config.hideDelay === -1) {
41-
config.hideDelay = Infinity
42-
}
43-
if (config.smoozeDelay === -1) {
44-
config.smoozeDelay = Infinity
45-
}
46-
47-
console.info(`?seed=${config.seed}`)
48-
4917
let random = seedrandom(config.seed)
50-
51-
let { canvas } = init()
5218
let asset = await getAsset()
5319
let display = createDisplay({ asset, canvas, config })
5420
let input = createInput()
@@ -58,6 +24,8 @@ export let main = async () => {
5824
display,
5925
input,
6026
random,
27+
revealLabyrinth$,
28+
setRevealButtonVisibility,
6129
size: {
6230
x: Math.round((config.size * 8) / 6),
6331
y: config.size,

src/page.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
}
1414
</style>
1515
<link rel="stylesheet" type="text/css" href="./style.css">
16-
</style>
1716
</head>
1817

1918
<body>

src/page/buttonSet.ts

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import '@ui5/webcomponents/dist/Button'
2+
import '@ui5/webcomponents/dist/Option'
3+
import '@ui5/webcomponents/dist/Select'
4+
import { fromEvent } from 'rxjs'
5+
import { h } from './lib/hyper'
6+
7+
export interface ButtonSetProp {
8+
location: Location
9+
seed: string
10+
}
11+
12+
let removeSeedParameter = (url: string): string => {
13+
return url.replace(/\?seed=[^?]*/g, '')
14+
}
15+
16+
let removeModeParameter = (url: string): string => {
17+
let regexList = [
18+
/\?hide(=[^?]*)?/g,
19+
/\?hideDelay(=[^?]*)?/g,
20+
/\?justSmooze(=[^?]*)?/g,
21+
/\?smooze(=[^?]*)?/g,
22+
/\?smoozeDelay(=[^?]*)?/g,
23+
]
24+
regexList.forEach((regex) => {
25+
url = url.replace(regex, '')
26+
})
27+
return url
28+
}
29+
30+
let urlWithSeed = (prop: ButtonSetProp) => {
31+
let urlWithoutSeed = removeSeedParameter(prop.location.href)
32+
let destination = `${urlWithoutSeed}?seed=${prop.seed}`
33+
return destination
34+
}
35+
36+
let willMatch = (regex: RegExp) => (text: string) => !!text.match(regex)
37+
38+
let testLabyrinthMode = (url: string) => {
39+
return removeModeParameter(url) === url
40+
}
41+
42+
let BUTTON = 'ui5-button' as 'button'
43+
let SELECT = 'ui5-select'
44+
let OPTION = 'ui5-option' as 'option'
45+
46+
/**
47+
* The GameModeDropdown allows to select which mode the game should run in
48+
*
49+
* This reloads the page.
50+
*/
51+
let GameModeDropdown = (prop: ButtonSetProp) => {
52+
let { location } = prop
53+
54+
let gameModeList: [string, string, (url: string) => boolean][] = [
55+
['labyrinth', '', testLabyrinthMode],
56+
['just smooze', '?justSmooze', willMatch(/\?justSmooze(\?|$)/)],
57+
['hidden walls', '?hide', willMatch(/\?hide(\?|$)/)],
58+
['smooze', '?smooze', willMatch(/\?smooze(\?|$)/)],
59+
]
60+
61+
let elem = h<HTMLSelectElement>(
62+
SELECT,
63+
{},
64+
gameModeList.map(([name, parameter, test]) => {
65+
let optionElem = h(OPTION, { textContent: name })
66+
if (test(location.href)) {
67+
optionElem.selected = true
68+
}
69+
optionElem.dataset.parameter = parameter
70+
return optionElem
71+
}),
72+
)
73+
74+
elem.addEventListener(
75+
'change',
76+
(ev) => {
77+
console.log('ev', ev)
78+
let modeParameter = (ev as any).detail.selectedOption.dataset.parameter
79+
let urlWithSeedButNotMode = removeModeParameter(urlWithSeed(prop))
80+
location.assign(urlWithSeedButNotMode + modeParameter)
81+
},
82+
true,
83+
)
84+
85+
return { elem }
86+
}
87+
88+
/**
89+
* The NewGameButton allows to start a new game.
90+
*
91+
* This reloads the page.
92+
*/
93+
let NewGameButton = (prop: ButtonSetProp) => {
94+
let { location } = prop
95+
96+
let onclick = () => {
97+
let destination = removeSeedParameter(location.href)
98+
location.assign(destination)
99+
}
100+
101+
return { elem: h(BUTTON, { onclick, textContent: 'New Game' }) }
102+
}
103+
104+
/**
105+
* The RestartSameButton allows to replay the same game.
106+
*
107+
* This reloads the page.
108+
*/
109+
let RestartSameButton = (prop: ButtonSetProp) => {
110+
let { location } = prop
111+
112+
let onclick = () => {
113+
let destination = urlWithSeed(prop)
114+
location.assign(destination)
115+
}
116+
117+
return { elem: h(BUTTON, { onclick, textContent: 'Restart Same' }) }
118+
}
119+
120+
/**
121+
* This button is used to reveal the labyrinthe when it's hidden.
122+
*/
123+
let RevealLabyrintheButton = () => {
124+
let elem = h(BUTTON, { innerText: 'Reveal' })
125+
126+
return {
127+
elem,
128+
click$: fromEvent(elem, 'click'),
129+
}
130+
}
131+
132+
export let ButtonSet = (prop: ButtonSetProp) => {
133+
let revealButtonVisibility = false
134+
135+
let gameModeDropDown = GameModeDropdown(prop)
136+
let newGameButton = NewGameButton(prop)
137+
let restartGameButton = RestartSameButton(prop)
138+
let revealLabyrinthButton = RevealLabyrintheButton()
139+
140+
let elem = h(
141+
'span',
142+
{
143+
className: 'buttonSet',
144+
},
145+
[
146+
gameModeDropDown.elem,
147+
restartGameButton.elem,
148+
newGameButton.elem,
149+
revealLabyrinthButton.elem,
150+
],
151+
)
152+
153+
return {
154+
elem,
155+
revealLabyrinth$: revealLabyrinthButton.click$,
156+
setRevealButtonVisibility: (visibility: boolean) => {
157+
if (visibility === revealButtonVisibility) {
158+
return false
159+
}
160+
if (visibility === true) {
161+
elem.append(revealLabyrinthButton.elem)
162+
} else {
163+
revealLabyrinthButton.elem.remove()
164+
}
165+
},
166+
}
167+
}

0 commit comments

Comments
 (0)