Autosaving
This commit is contained in:
parent
a4062915fa
commit
6fa0abce47
|
@ -1,14 +1,27 @@
|
|||
inkjs = require("inkjs")
|
||||
|
||||
continueToNextChoice = (s) ->
|
||||
saveChoice = (index) ->
|
||||
window.progress.push(index)
|
||||
localStorage.setItem("progress", JSON.stringify(window.progress))
|
||||
|
||||
displayText = (s, interactive = true) ->
|
||||
while (s.canContinue)
|
||||
paragraphs = s.Continue().split("\n")
|
||||
delay = 1000
|
||||
if interactive
|
||||
delay = 1000
|
||||
for i in paragraphs
|
||||
if i != ""
|
||||
html = $.parseHTML(i)
|
||||
$("#content").append($('<p>').hide().html(html).fadeIn(delay))
|
||||
delay += 500
|
||||
block = $('<p>').html(html)
|
||||
if interactive
|
||||
block.hide()
|
||||
$("#content").append(block)
|
||||
if interactive
|
||||
block.fadeIn(delay)
|
||||
delay += 500
|
||||
|
||||
continueToNextChoice = (s) ->
|
||||
displayText(s, true)
|
||||
scrollTo = $('#options').offset().top
|
||||
if (s.currentChoices.length > 0)
|
||||
$("#options").html("").hide()
|
||||
|
@ -17,6 +30,7 @@ continueToNextChoice = (s) ->
|
|||
$("#options").fadeIn(800)
|
||||
$("#options li a").click(() ->
|
||||
s.ChooseChoiceIndex($(this).data("index"))
|
||||
saveChoice($(this).data("index"))
|
||||
continueToNextChoice(s)
|
||||
return false
|
||||
)
|
||||
|
@ -27,10 +41,25 @@ continueToNextChoice = (s) ->
|
|||
scrollTop: scrollTo
|
||||
}, 800)
|
||||
|
||||
loadGame = (s) ->
|
||||
for index in window.progress
|
||||
displayText(s, false)
|
||||
s.ChooseChoiceIndex(index)
|
||||
|
||||
clearProgress = () ->
|
||||
window.progress = []
|
||||
|
||||
$.ajax({
|
||||
url: 'fogg.ink.json',
|
||||
dataType: 'text',
|
||||
success: (data) ->
|
||||
progress = localStorage.getItem("progress")
|
||||
if progress?
|
||||
window.progress = JSON.parse(progress)
|
||||
else
|
||||
window.progress = []
|
||||
s = new inkjs.Story(data)
|
||||
if window.progress != []
|
||||
loadGame(s)
|
||||
continueToNextChoice(s)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue