2021-12-01 16:24:29 +00:00
|
|
|
/// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
import { Record, Dataspace, Double, floatValue, Ref, stringify } from '@syndicate-lang/core';
|
|
|
|
import { boot as bootHtml, WindowEvent, template, Anchor } from '@syndicate-lang/html';
|
|
|
|
import { boot as bootTimer, PeriodicTick } from '@syndicate-lang/timer';
|
2021-01-29 18:56:12 +00:00
|
|
|
|
|
|
|
assertion type Position(x, y);
|
|
|
|
assertion type GameOver();
|
|
|
|
assertion type Score(count);
|
|
|
|
message type IncreaseScore();
|
|
|
|
message type Reset();
|
|
|
|
|
|
|
|
const BOARD_HEIGHT = 567;
|
|
|
|
const FLAPPY_WIDTH = 57;
|
|
|
|
const FLAPPY_HEIGHT = 41;
|
|
|
|
const FLAPPY_XPOS = 212;
|
|
|
|
const PILLAR_WIDTH = 86;
|
|
|
|
const PILLAR_GAP = 158;
|
|
|
|
const FIELD_HEIGHT = 561;
|
|
|
|
const PILLAR_HEAD_HEIGHT = 40;
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
export function main() {
|
|
|
|
Dataspace.boot(mainDs => {
|
|
|
|
bootHtml(mainDs);
|
|
|
|
bootTimer(mainDs);
|
|
|
|
spawn named 'game-factory' {
|
|
|
|
spawnGame(mainDs);
|
|
|
|
at mainDs {
|
|
|
|
during GameOver() => {
|
|
|
|
on stop spawnGame(mainDs);
|
|
|
|
on message WindowEvent('+keypress', $_e) => send message Reset();
|
|
|
|
on message WindowEvent('+click', $_e) => send message Reset();
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
2021-12-11 15:59:38 +00:00
|
|
|
});
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
function spawnGame(mainDs: Ref) {
|
|
|
|
spawn named 'GameInstance' {
|
|
|
|
const gameDs = create new Dataspace();
|
|
|
|
|
|
|
|
at gameDs {
|
|
|
|
during GameOver() => at mainDs {
|
|
|
|
assert GameOver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
at mainDs {
|
|
|
|
stop on message Reset();
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
spawn linked named 'score' {
|
2021-01-29 18:56:12 +00:00
|
|
|
let ui = new Anchor();
|
|
|
|
field score: number = 0;
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
at gameDs {
|
|
|
|
assert Score(score.value);
|
|
|
|
on message IncreaseScore() => score.value++;
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
react {
|
|
|
|
at mainDs {
|
|
|
|
assert ui.html('#board-area', template`<h1 class="score">${score.value}</h1>`);
|
|
|
|
}
|
|
|
|
at gameDs {
|
|
|
|
stop on asserted GameOver() => react {
|
|
|
|
at mainDs {
|
|
|
|
assert ui.html(
|
|
|
|
'#board-area',
|
|
|
|
template`<h1 class="score">${score.value}<br/>GAME OVER</h1>`);
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
spawn linked named 'flappy' {
|
2021-01-29 18:56:12 +00:00
|
|
|
let ui = new Anchor();
|
|
|
|
field xpos: number = 0;
|
|
|
|
field ypos: number = 312;
|
|
|
|
field yvel: number = 0;
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
at gameDs {
|
|
|
|
assert Position(Double(xpos.value), Double(ypos.value));
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
on (ypos.value > BOARD_HEIGHT - FLAPPY_HEIGHT) {
|
|
|
|
ypos.value = BOARD_HEIGHT - FLAPPY_HEIGHT;
|
|
|
|
react {
|
|
|
|
assert GameOver();
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
at mainDs {
|
|
|
|
assert ui.html(
|
|
|
|
'#board-area',
|
|
|
|
template`<div class="flappy"
|
|
|
|
style="${`transform: rotate(${2 * yvel.value}deg);
|
|
|
|
top: ${ypos.value}px`}"></div>`);
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
react {
|
|
|
|
at gameDs {
|
|
|
|
stop on asserted GameOver();
|
|
|
|
}
|
|
|
|
|
|
|
|
at mainDs {
|
|
|
|
on message WindowEvent('+keypress', $_e) => yvel.value = -10;
|
|
|
|
on message WindowEvent('+click', $_e) => yvel.value = -10;
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
const ms_per_tick = 1000.0 / 60;
|
|
|
|
const ms_per_tick_d = Double(ms_per_tick);
|
|
|
|
on message PeriodicTick(ms_per_tick_d) => {
|
|
|
|
xpos.value += 0.15 * ms_per_tick;
|
|
|
|
ypos.value = (ypos.value + yvel.value);
|
|
|
|
yvel.value += ms_per_tick * 0.05;
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
spawn linked named 'border-scroll' {
|
2021-01-29 18:56:12 +00:00
|
|
|
let ui = new Anchor();
|
|
|
|
field pos: number = 0;
|
2021-12-11 15:59:38 +00:00
|
|
|
at gameDs {
|
|
|
|
on asserted Position($xpos, _) => pos.value = floatValue(xpos) % 23;
|
|
|
|
}
|
|
|
|
at mainDs {
|
|
|
|
assert ui.html(
|
|
|
|
'#board-area',
|
|
|
|
template`<div class="scrolling-border" style="${`background-position-x: ${-pos.value}px`}"></div>`,
|
|
|
|
0);
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 15:59:38 +00:00
|
|
|
spawn linked named 'pipe-factory' {
|
2021-01-29 18:56:12 +00:00
|
|
|
field nextPipe: number = 0;
|
2021-12-11 15:59:38 +00:00
|
|
|
at gameDs {
|
2021-12-13 19:21:21 +00:00
|
|
|
on asserted Score(nextPipe.value) => {
|
|
|
|
react {
|
|
|
|
const pipeNumber = nextPipe.value++;
|
|
|
|
spawn linked named ['pipe', pipeNumber] {
|
|
|
|
runPipe(pipeNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 15:59:38 +00:00
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
function runPipe(i: number) {
|
2021-12-11 15:59:38 +00:00
|
|
|
const xlocation = (i + 1) * 324;
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
let ui = new Anchor();
|
|
|
|
field xpos: number = xlocation;
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
const upperHeight =
|
|
|
|
Math.random() * (FIELD_HEIGHT - PILLAR_GAP - PILLAR_HEAD_HEIGHT * 6)
|
|
|
|
+ PILLAR_HEAD_HEIGHT * 3;
|
|
|
|
const lowerHeight = FIELD_HEIGHT - upperHeight - PILLAR_GAP;
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
stop on (xpos.value < -(PILLAR_WIDTH + FLAPPY_XPOS));
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
at gameDs {
|
|
|
|
react {
|
|
|
|
stop on (xpos.value <= 0) send message IncreaseScore();
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
on asserted Position($flappyXpos, _) =>
|
|
|
|
xpos.value = xlocation - floatValue(flappyXpos);
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
on asserted Position($xpos, $ypos) => {
|
|
|
|
if (touchingPillar(floatValue(xpos), floatValue(ypos))) {
|
|
|
|
react {
|
|
|
|
assert GameOver();
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 19:21:21 +00:00
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
at mainDs {
|
|
|
|
assert ui.html(
|
|
|
|
'#board-area',
|
|
|
|
template`<div class="pillars">
|
|
|
|
<div class="pillar pillar-upper"
|
|
|
|
style="${`left: ${xpos.value + FLAPPY_XPOS}px; height: ${upperHeight}px;`}"></div>
|
|
|
|
<div class="pillar pillar-lower"
|
|
|
|
style="${`left: ${xpos.value + FLAPPY_XPOS}px; height: ${lowerHeight}px;`}"></div>
|
|
|
|
</div>`);
|
|
|
|
}
|
2021-01-29 18:56:12 +00:00
|
|
|
|
2021-12-13 19:21:21 +00:00
|
|
|
function touchingPillar(xpos: number, ypos: number): boolean {
|
|
|
|
const inHorizontalRange =
|
|
|
|
(xpos + FLAPPY_WIDTH >= xlocation) && (xpos <= xlocation + PILLAR_WIDTH);
|
|
|
|
const aboveGapTop = (ypos <= upperHeight);
|
|
|
|
const belowGapBottom = (ypos + FLAPPY_HEIGHT >= upperHeight + PILLAR_GAP);
|
|
|
|
return inHorizontalRange && (aboveGapTop || belowGapBottom);
|
2021-01-29 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|