Simplifying web game loop
This commit is contained in:
parent
f86855d22d
commit
72b269650b
@ -2,5 +2,5 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
cd web_assets
|
cd web_assets
|
||||||
go build .
|
go build -x .
|
||||||
./web_assets index.html | gofmt > ../web_assets.go
|
./web_assets index.html | gofmt > ../web_assets.go
|
||||||
|
File diff suppressed because one or more lines are too long
@ -12,13 +12,18 @@
|
|||||||
$.getJSON('/state', {}, onPopulateResponse);
|
$.getJSON('/state', {}, onPopulateResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
function goToNextState(state) {
|
function goToNextState(state, loop) {
|
||||||
$.post('/state', JSON.stringify({s: state}, null, 2), onPopulateResponse, 'json');
|
$.post(
|
||||||
|
'/state',
|
||||||
|
JSON.stringify({s: state}, null, 2),
|
||||||
|
function(data, textStatus, jqXHR) {
|
||||||
|
var state = onPopulateResponse(data, textStatus, jqXHR);
|
||||||
|
if (loop) {
|
||||||
|
setTimeout(function() { goToNextState(state, loop); }, 500);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
function playGame() {
|
'json'
|
||||||
goToNextState(curState);
|
);
|
||||||
setTimeout(playGame, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onPopulateResponse(data, textStatus, jqXHR) {
|
function onPopulateResponse(data, textStatus, jqXHR) {
|
||||||
@ -29,12 +34,13 @@
|
|||||||
$stateImg = $('<img id="state_img" />').appendTo($('#state_container'));
|
$stateImg = $('<img id="state_img" />').appendTo($('#state_container'));
|
||||||
}
|
}
|
||||||
$stateImg.attr('src', 'data:image/png;base64,' + encodeURI(data.i));
|
$stateImg.attr('src', 'data:image/png;base64,' + encodeURI(data.i));
|
||||||
|
return data.s;
|
||||||
}
|
}
|
||||||
|
|
||||||
$(function() {
|
$(function() {
|
||||||
populateInitialState();
|
populateInitialState();
|
||||||
$('#step').click(function() { goToNextState(curState); });
|
$('#step').click(function() { goToNextState(curState); });
|
||||||
$('#play').click(playGame);
|
$('#play').click(function() { goToNextState(curState, true); });
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
Loading…
Reference in New Issue
Block a user