Simplifying web game loop

This commit is contained in:
Dan Buch 2012-12-18 22:03:26 -05:00
parent f86855d22d
commit 72b269650b
3 changed files with 16 additions and 10 deletions

View File

@ -2,5 +2,5 @@
set -e
cd web_assets
go build .
go build -x .
./web_assets index.html | gofmt > ../web_assets.go

File diff suppressed because one or more lines are too long

View File

@ -12,13 +12,18 @@
$.getJSON('/state', {}, onPopulateResponse);
}
function goToNextState(state) {
$.post('/state', JSON.stringify({s: state}, null, 2), onPopulateResponse, 'json');
}
function playGame() {
goToNextState(curState);
setTimeout(playGame, 500);
function goToNextState(state, loop) {
$.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);
}
},
'json'
);
}
function onPopulateResponse(data, textStatus, jqXHR) {
@ -29,12 +34,13 @@
$stateImg = $('<img id="state_img" />').appendTo($('#state_container'));
}
$stateImg.attr('src', 'data:image/png;base64,' + encodeURI(data.i));
return data.s;
}
$(function() {
populateInitialState();
$('#step').click(function() { goToNextState(curState); });
$('#play').click(playGame);
$('#play').click(function() { goToNextState(curState, true); });
});
</script>
</head>