-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamzee.coffee
148 lines (130 loc) · 3.64 KB
/
gamzee.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Gamzee.js V .01
rowCount = 0
colCount = 0
pages = []
pageChoices = []
names = []
saveNames = []
loadNames = []
#game functions
choiceEvent = ->
this
choiceEvent:: =
eventText: ""
choices: []
choice = (text, effect, next) ->
@text = text if text
@effect = effect if effect
@next = next if next
this
choice:: =
text: "==>"
effect: ->
next: ""
disp: ->
link = document.createElement("a")
parentC = this
pageChoices[pageChoices.length] = parentC
link.href = "#"
link.textContent = @text
link.set "class", "actionLink"
ltNew = lt.clone()
ltNew.appendChild link
if colCount % 3 is 0
actRow = document.createElement "div"
rowCount++
actRow.display = "table-row"
actRow.id = "row" + rowCount
actRow.appendChild ltNew
Actions.appendChild actRow
else
$("row" + rowCount).appendChild ltNew
colCount++
link.onclick = ->
pageChoices[Actions.getElements("a").indexOf(this)].effect()
events[pageChoices[Actions.getElements("a").indexOf(this)].next].eventToPages() unless pageChoices[Actions.getElements("a").indexOf(this)].next is ""
page = (text, turn) ->
@text = text
@turn = turn unless turn is `undefined`
this
page:: =
turn: [new choice(null, ->
pages.shift().disp()
)]
disp: ->
clear()
Event.innerHTML = @text
c = 0
while c < @turn.length
@turn[c].disp()
c++
templatestring = (text) ->
@raw = text if text
this
templatestring:: =
toString: ->
raw.replace /<%([\w\W]+)%>/, (("$1") -> )
clear = ->
pageChoices.empty()
rowCount = 0
colCount = 0
Event.textContent = ""
Actions.textContent = ""
String::flagReplace = ->
retString = this
retString = retString.replace("$" + flags[0], character[flags[0]])
unless @indexOf("$") is -1
i = 1
while i < flags.length
retString = retString.replace("$" + flags[i], descriptions[character[flags[i]]].getRandom()) unless character[flags[i]] is ""
i++
retString
Object::eventToPages = ->
if typeOf(this) is "function"
this()
else
@doIt() if @doIt?
txtArray = @eventText.flagReplace().split("</p>")
until txtArray.length is 0
pageText = ""
c = 0
while c < 2
prinTxt = txtArray.shift()
pageText += prinTxt + "<br /><br />" unless prinTxt is `undefined`
c++
unless txtArray.length is 0
pages.push new page(pageText.toString())
else
pages.push new page(pageText.toString(), @choices)
pages.shift().disp()
Storage::setObject = (key, value) ->
@setItem key, JSON.encode(value)
Storage::getObject = (key) ->
JSON.decode @getItem(key)
#Game set up.
preGame = ->
document.title = $("title").innerHTML = AdventureTitle
Event = $ "event"
Actions = $ "actions"
br = document.createElement "br"
lt = document.createElement "span"
lt.textContent = "> "
events["title"].eventToPages()
Actions.innerHTML = ""
namesRaw = localStorage.getObject "taStuckDataRaw"
if not namesRaw? or namesRaw is {}
$("rMenu").style.visibility = "hidden"
$("event").innerHTML += "No save data detected. Select \"New Game\" below to start."
localStorage.setObject "taStuckData", {}
else
Object.each namesRaw, (item, key) ->
names[names.length] = new choice(key, ->
character = item
, "hub")
c = names.length
while c < 7
names[c] = new choice "No data", null, null
c++
$("new").onclick = ->
events["newGame"].eventToPages()
window.addEvent "domready", preGame