Use nested dataspaces to clean up restarts

This commit is contained in:
Tony Garnock-Jones 2018-11-11 17:34:59 +00:00
parent a75b48eb26
commit 18ab5db460
1 changed files with 131 additions and 136 deletions

View File

@ -17,6 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//---------------------------------------------------------------------------
import { $QuitDataspace, Inbound, Outbound, Dataspace } from "@syndicate-lang/core";
let UI = activate require("@syndicate-lang/driver-browser-ui");
// @jsx UI.html
// @jsxFrag UI.htmlFragment
@ -27,7 +29,6 @@ assertion type Position(x, y);
assertion type GameOver();
assertion type Score(count);
message type IncreaseScore();
message type Reset();
const BOARD_HEIGHT = 567;
@ -39,19 +40,37 @@ const PILLAR_GAP = 158;
const FIELD_HEIGHT = 561;
const PILLAR_HEAD_HEIGHT = 40;
function newGame() {
spawn named 'game-factory' {
on start spawnGame();
during GameOver() {
on stop spawnGame();
on message UI.WindowEvent('+keypress', $e) {
if (e.key !== ' ') {
^ Reset();
}
}
}
}
function spawnGame() {
spawn dataspace named 'GameInstance' {
spawn named 'game-instance-control' {
during GameOver() assert Outbound(GameOver());
on message Inbound(Reset()) {
^ $QuitDataspace;
}
}
spawn named 'score' {
let ui = new UI.Anchor();
field this.score = 0;
stop on message Reset();
assert Score(this.score);
on start react {
assert ui.html('#board-area', <h1 class="score">{this.score}</h1>);
assert Outbound(ui.html('#board-area', <h1 class="score">{this.score}</h1>));
stop on asserted GameOver() react {
assert ui.html('#board-area', <h1 class="score">{this.score}<br/>GAME OVER</h1>);
assert Outbound(ui.html('#board-area', <h1 class="score">{this.score}<br/>GAME OVER</h1>));
}
}
@ -66,13 +85,11 @@ function newGame() {
field this.ypos = 312;
field this.yvel = 0;
stop on message Reset();
assert Position(this.xpos, this.ypos);
assert ui.html('#board-area', <div class="flappy"
assert Outbound(ui.html('#board-area', <div class="flappy"
style={`transform: rotate(${2 * this.yvel}deg);
top: ${this.ypos}px`}></div>);
top: ${this.ypos}px`}></div>));
on (this.ypos > BOARD_HEIGHT - FLAPPY_HEIGHT) {
this.ypos = BOARD_HEIGHT - FLAPPY_HEIGHT;
@ -84,12 +101,12 @@ function newGame() {
on start react {
stop on asserted GameOver();
on message UI.WindowEvent('+keypress', $e) {
on message Inbound(UI.WindowEvent('+keypress', $e)) {
this.yvel = -10;
}
const ms_per_tick = 1000.0 / 60;
on message PeriodicTick(ms_per_tick) {
on message Inbound(PeriodicTick(ms_per_tick)) {
this.xpos += 0.15 * ms_per_tick;
this.ypos = (this.ypos + this.yvel);
this.yvel += ms_per_tick * 0.05;
@ -101,19 +118,13 @@ function newGame() {
let ui = new UI.Anchor();
field this.pos = 0;
on asserted Position($xpos, _) this.pos = xpos % 23;
assert ui.html(
assert Outbound(ui.html(
'#board-area',
<div class="scrolling-border" style={`background-position-x: ${-this.pos}px`}></div>, 0);
stop on message Reset();
<div class="scrolling-border" style={`background-position-x: ${-this.pos}px`}></div>, 0));
}
spawn named 'pipe-factory' {
on start spawnPipe(0);
field this.nextPipe = 1;
stop on message Reset();
field this.nextPipe = 0;
on asserted Score(this.nextPipe) {
spawnPipe(this.nextPipe++);
}
@ -121,8 +132,6 @@ function newGame() {
function spawnPipe(i) {
spawn named ['pipe', i] {
stop on message Reset();
let ui = new UI.Anchor();
const xlocation = (i + 1) * 324;
@ -149,14 +158,14 @@ function newGame() {
}
}
assert ui.html(
assert Outbound(ui.html(
'#board-area',
<div class="pillars">
<div class="pillar pillar-upper"
style={`left: ${this.xpos + FLAPPY_XPOS}px; height: ${upperHeight}px;`}></div>
<div class="pillar pillar-lower"
style={`left: ${this.xpos + FLAPPY_XPOS}px; height: ${lowerHeight}px;`}></div>
</div>);
</div>));
function touchingPillar(xpos, ypos) {
const inHorizontalRange =
@ -167,19 +176,5 @@ function newGame() {
}
}
}
}
spawn named 'game-factory' {
on start newGame();
// TODO: Use nested dataspaces to clean up restarts
during GameOver() {
on stop newGame();
on message UI.WindowEvent('+keypress', $e) {
if (e.key !== ' ') {
^ Reset();
}
}
}
}