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 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

View File

@ -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 playGame() { function(data, textStatus, jqXHR) {
goToNextState(curState); var state = onPopulateResponse(data, textStatus, jqXHR);
setTimeout(playGame, 500); if (loop) {
setTimeout(function() { goToNextState(state, loop); }, 500);
}
},
'json'
);
} }
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>