1
1
Fork 0
mirror of https://gitlab.com/Oreolek/cloak-salet.git synced 2024-06-26 12:00:53 +03:00
cloak-salet/game/begin.coffee

203 lines
5.5 KiB
CoffeeScript
Raw Normal View History

2017-05-15 13:28:53 +03:00
salet.game_id = "8b0c371c-57f4-49b3-ae3c-cba07d1a9733"
salet.game_version = "1.0"
2017-07-10 04:58:19 +03:00
salet.beforeAction = (room, actionId) ->
verbRe = /^verb\_(\w+)\_(\w+)$/
2017-05-15 13:28:53 +03:00
match = verbRe.exec(actionId)
if match? and match[1] and match[2]
verb = match[1]
unit = match[2]
2017-07-10 16:32:24 +03:00
if room.has(unit)
salet.view.write(room.unitdo(unit, verb))
2017-07-10 04:58:19 +03:00
if salet.character.has(unit)
for i in salet.character.inventory
if i.name == unit
salet.view.write(i[verb].fcall(salet.rooms[room.name]))
2017-05-15 13:28:53 +03:00
return true # consume the action
return false
2017-07-10 16:32:24 +03:00
salet.afterAction = () ->
salet.character.update_sidebar()
2017-05-15 13:28:53 +03:00
$.holdReady( true )
$.getJSON('game/translations/'+i18n.lang+'.json', (data) ->
i18n.push(i18n.lang, data)
$.holdReady( false )
)
switchTab = (tabid) ->
$(".tab").removeClass("active")
$("#"+tabid).addClass("active")
$(document).ready(() ->
window.addEventListener('popstate', (event) ->
salet.goBack()
)
$("body").on("click", '#night', () ->
if (window.night)
$("body").removeClass("night")
$("#night").removeClass("active")
window.night = false
else
$("body").addClass("night")
$("#night").addClass("active")
window.night = true
)
$("body").on("click", "#storytab", (event) ->
event.preventDefault()
if not salet.here().canSave
salet.goBack()
return false
)
$("body").on("click", ".tab", (event) ->
2017-05-20 15:25:16 +03:00
if (event.target.id == "storytab")
return true
2017-05-15 13:28:53 +03:00
switchTab(event.target.id)
return true
)
salet.beginGame()
)
updateverb = (unit, verb) ->
if unit[verb]? or salet.character.displayAll
2017-07-10 04:58:19 +03:00
if verb == "take" and unit.takeable == false
return ""
2017-07-10 16:32:24 +03:00
if verb == "drop" and not salet.character.has(unit.name)
return ""
2017-07-10 04:58:19 +03:00
if verb == "wear" and salet.character.has(unit.name)
return ""
2017-05-15 13:28:53 +03:00
$("##{verb}list").append("<li><a href='./verb_#{verb}_#{unit.name}'>#{unit.display()}</a></li>")
2017-07-10 04:58:19 +03:00
pruneVerbs = () ->
for verb in document.querySelectorAll(".verb")
text = verb.innerHTML.toLowerCase()
if $("##{text}list").html() == ""
verb.style.cssText = "visibility: hidden"
else
verb.style.cssText = "visibility: visible"
2017-05-15 13:28:53 +03:00
###
Element helpers. There is no real need to build monsters like a().id("hello")
because you won't use them as is. It does not make sense in context, the
author has Markdown and all utilities to *forget* about the markup.
###
way_to = (content, ref) ->
return "<a href='#{ref}' class='way'>#{content}</a>"
textlink = (content, ref) ->
return "<a href='./_writer_#{ref}' class='once'>#{content}</a>"
actlink = (content, ref) ->
return "<a href='./#{ref}' class='once'>#{content}</a>"
sysroom = (name, options) ->
options.canSave = false
options.enter = () ->
2017-05-20 14:55:58 +03:00
$(".action").hide()
2017-05-15 13:28:53 +03:00
options.exit = () ->
2017-05-20 15:25:16 +03:00
if document.querySelector('#current-room')
salet.view.clearContent('#current-room')
2017-05-20 14:55:58 +03:00
$(".action").show()
2017-05-15 13:28:53 +03:00
options.dsc = () ->
2017-05-20 14:55:58 +03:00
return @text.fcall()
###
+"\n\n"
+"""
2017-05-15 13:28:53 +03:00
<div class="center"><a href="./exit"><button class="btn btn-lg btn-outline-primary">Go back</button></a></div>
"""
options.actions = {
exit: () ->
return salet.goBack()
}
2017-05-20 14:55:58 +03:00
###
2017-05-15 13:28:53 +03:00
return room(name, options)
croom = (name, spec) ->
spec.clear ?= true
spec.optionColor ?= ""
2017-07-10 16:32:24 +03:00
spec.has = (thing) ->
for item in this.units
if item.name == thing
return true
return false
spec.unitdo = (thing, verb) ->
for item in this.units
if item.name == thing and item[verb] != undefined
return item[verb].fcall(this)
2017-05-15 13:28:53 +03:00
spec.optionText ?= () ->
retval = """
<div class="#{spec.optionColor}">
<div class="title">#{spec.title.fcall()}</div>
"""
if (spec.subtitle?)
retval += """
<div class="subtitle">#{spec.subtitle.fcall()}</div>
"""
retval += '</div>'
spec.enter = () ->
salet.character.update_sidebar()
if @onEnter?
@onEnter()
return room(name, spec)
sysroom "inventory",
text: () ->
if salet.character.inventory.length == 0
text = "You are carrying nothing."
else
text = "You are carrying:\n\n"
for thing in salet.character.inventory
text += "* #{salet.character.listinv(thing.name)}\n"
sysroom "settings",
text: () ->
nightclass = ""
if window.night
nightclass = "active"
return "credits".l() + """
<button id="filter" class="btn btn-outline-primary}">#{"showall".l()}</button>
<button id="night" class="btn btn-outline-primary #{nightclass}">#{"night".l()}</button>
<button onclick="TogetherJS(this); return false;" class="btn btn-outline-primary">#{"multiplayer".l()}</button>
"""
2017-05-20 14:55:58 +03:00
sysroom "map",
text: () ->
return "<div id='map'></div>"
after: () ->
data = {
edges: []
nodes: []
}
edges = []
for name, room of salet.rooms
if room.canSave == false or name == "start"
continue
data.nodes.push({
"id": name
"label": room.title()
"size": 5
"color": "#000"
2017-05-20 15:25:16 +03:00
"x": Math.random()*10
"y": Math.random()*10
2017-05-20 14:55:58 +03:00
})
if room.ways? and room.ways.length > 0
for way in room.ways
id = "edge_"+name+"_"+way
# we don't want to display a two-way link twice
if edges.indexOf("edge_"+way+"_"+name) == -1
edges.push(id)
data.edges.push({
"id": id
"source": room.name
"target": way
"size": 1
"color": "#ccc"
})
console.log data
s = new sigma({
graph: data,
container: 'map'
})
console.log(s)
return ""