1
0
Fork 0
mirror of https://github.com/Oreolek/shooter.git synced 2024-07-01 05:55:00 +03:00
shooter/game/main.coffee

291 lines
9.4 KiB
CoffeeScript
Raw Normal View History

2015-12-01 08:42:52 +02:00
# copyright (c) Alexander Yakovlev 2015.
# This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0
2015-12-01 08:42:52 +02:00
situation = require('raconteur')
undum = require('undum-commonjs')
oneOf = require('raconteur/lib/oneOf.js')
elements = require('raconteur/lib/elements.js')
qualities = require('raconteur/lib/qualities.js')
md = require('markdown-it')
markdown = new md({
typographer: true,
html: true
})
undumloc = require("./ru.coffee").language
undum.language["ru"] = undumloc
2015-12-01 12:42:15 +02:00
undumloc = require("./en.coffee").language
undum.language["en"] = undumloc
2015-12-02 09:20:18 +02:00
$ = require("jquery")
2015-12-01 08:42:52 +02:00
a = elements.a
span = elements.span
img = elements.img
undum.game.id = "7a1aba32-f0fd-4e3b-ba5a-59e3fa9e6012"
2015-12-01 12:42:15 +02:00
undum.game.version = "1.0"
2015-12-01 08:42:52 +02:00
way_to = (content, ref) -> a(content).class('way').ref(ref)
textlink = (content, ref) -> a(content).once().writer(ref)
2015-12-03 10:44:30 +02:00
# The next line doesn't work for whatever reason:
#textcycle = (content, ref) -> span(a(content).replacer(ref)).class("cycle").id(ref)
textcycle = (content, ref) -> "<span id='#{ref}' class='cycle'><a href='./_replacer_#{ref}'>#{content}</a></span>"
2015-12-01 08:42:52 +02:00
is_visited = (situation) -> undum.game.situations[situation].visited == 1
2015-12-02 12:56:40 +02:00
writemd = (system, text) ->
2015-12-01 08:42:52 +02:00
if typeof text is Function
text = text()
system.write(markdown.render(text))
2015-12-02 09:20:18 +02:00
Array.prototype.oneOf = () ->
oneOf.apply(null, this)
2015-12-01 08:42:52 +02:00
# This is an easy game.
# I'm thinking if you want more harder approach, you can:
# a) remove bullet counter (you don't know how many bullets left in a clip)
# b) remove canChoose restriction (you can shoot any time you want, but if you have no bullets - nothing comes out and you've lost a turn)
2015-12-03 07:06:22 +02:00
kill_enemy = (character, system) ->
if character.qualities.enemies == 0
return
character.sandbox.nicked = 0
character.sandbox.killed++
if character.qualities.enemies >= 1
system.setQuality("enemies", character.qualities.enemies - 1)
if character.qualities.enemies == 0
system.doLink("finale")
2015-12-01 08:42:52 +02:00
spend_bullet = (character, system) ->
bullets = character.sandbox.clips[character.sandbox.current_clip]
2015-12-02 10:48:59 +02:00
character.sandbox.shots++
2015-12-01 08:42:52 +02:00
if bullets >= 1
2015-12-02 09:20:18 +02:00
coin = system.rnd.randomInt(1,2)
audio = 'shot1'
if coin == 2
audio = 'shot2'
audio = document.getElementById(audio)
audio.currentTime=0
audio.play()
2015-12-01 08:42:52 +02:00
character.sandbox.clips[character.sandbox.current_clip]--
2015-12-02 09:20:18 +02:00
bullets--
system.setQuality("bullets", bullets)
$("#clip img").attr("src", "img/clip"+bullets+".png")
2015-12-01 08:42:52 +02:00
spend_clip = (character, system) ->
bullets = character.sandbox.clips[character.sandbox.current_clip]
clips = character.sandbox.clips.length
if clips < 2
2015-12-01 08:42:52 +02:00
return
2015-12-02 09:20:18 +02:00
audio = document.getElementById("reload")
audio.currentTime=0
audio.play()
2015-12-01 08:42:52 +02:00
if bullets == 0
character.sandbox.clips.splice(character.sandbox.current_clip, 1)
clips = character.sandbox.clips.length
2015-12-01 12:42:15 +02:00
writemd(system, "emptyclip".l())
system.setQuality("clips", clips)
if character.sandbox.current_clip < clips - 1
character.sandbox.current_clip++
2015-12-01 08:42:52 +02:00
else
character.sandbox.current_clip = 0
2015-12-02 09:20:18 +02:00
bullets = character.sandbox.clips[character.sandbox.current_clip]
system.setQuality("bullets", bullets)
$("#clip img").attr("src", "img/clip"+bullets+".png")
2015-12-03 10:44:30 +02:00
if character.sandbox.killed > 15 and character.sandbox.seen_pacifist == 0
system.doLink("pacifist")
character.sandbox.seen_pacifist = 1
2015-12-01 08:42:52 +02:00
situation 'start',
2015-12-01 12:42:15 +02:00
content: "intro".l(),
2015-12-01 08:42:52 +02:00
choices: ["#shoot"],
situation "hit",
content: (character, system, from) ->
2015-12-02 09:20:18 +02:00
response = "player_hit".l().oneOf().randomly(system)
2015-12-01 08:42:52 +02:00
return response()
choices: ["#shoot"]
before: (character, system, from) ->
2015-12-03 07:06:22 +02:00
kill_enemy(character, system)
2015-12-01 08:42:52 +02:00
choices: ["#shoot"]
situation "nicked",
content: (character, system, from) ->
if character.sandbox.nicked == 1
2015-12-03 07:06:22 +02:00
kill_enemy(character, system)
2015-12-02 09:20:18 +02:00
response = "player_finished".l().oneOf().randomly(system)
2015-12-01 08:42:52 +02:00
return response()
else
character.sandbox.nicked = 1
2015-12-02 09:20:18 +02:00
response = "player_nicked".l().oneOf().randomly(system)
2015-12-01 08:42:52 +02:00
return response()
choices: ["#shoot"]
situation "miss",
content: (character, system, from) ->
2015-12-02 09:20:18 +02:00
response = "player_missed".l().oneOf().randomly(system)
2015-12-01 08:42:52 +02:00
return response()
choices: ["#shoot"]
2015-12-03 07:06:22 +02:00
situation "trick",
before: (character, system, from) ->
kill_enemy(character, system)
kill_enemy(character, system)
content: (character, system, from) ->
response = "player_trickshot".l().oneOf().randomly(system)
return response()
choices: ["#shoot"]
2015-12-01 08:42:52 +02:00
situation "shoot",
tags: ["shoot"],
optionText: (character, system, from) ->
2015-12-02 09:20:18 +02:00
return "shoot".l().oneOf().randomly(system)
2015-12-01 08:42:52 +02:00
canChoose: (character, system) ->
return character.qualities.bullets > 0
before: (character, system, from) ->
spend_bullet(character, system)
system.clearContent()
after: (character, system, from) ->
roll = system.rnd.dice(1,20) # d20 roll
hit_threshold = 15
miss_threshold = 18
2015-12-01 08:42:52 +02:00
switch
when roll < hit_threshold then system.doLink("hit")
when roll > miss_threshold then system.doLink("miss")
2015-12-01 08:42:52 +02:00
else system.doLink("nicked")
2015-12-03 07:06:22 +02:00
situation "trick_shot",
tags: ["shoot"],
optionText: (character, system, from) ->
return "trick_shot".l()
canView: (character, system) ->
return character.sandbox.trick_shot == 1
canChoose: (character, system) ->
return character.qualities.bullets > 0
before: (character, system, from) ->
spend_bullet(character, system)
system.clearContent()
after: (character, system, from) ->
roll = system.rnd.dice(1,20) # d20 roll
trick_threshold = 5
hit_threshold = 12
miss_threshold = 16
switch
when roll < trick_threshold then system.doLink("trick")
when roll < hit_threshold then system.doLink("hit")
when roll > miss_threshold then system.doLink("miss")
else system.doLink("nicked")
2015-12-01 08:42:52 +02:00
situation "reload",
tags: ["shoot"],
choices: ["#shoot"],
2015-12-01 12:42:15 +02:00
optionText: "reload".l(),
2015-12-01 08:42:52 +02:00
canView: (character, system) ->
return character.sandbox.seen_reload || character.qualities.bullets < 6
canChoose: (character, system) ->
return character.qualities.bullets < 6 and character.sandbox.clips.length > 1
2015-12-01 08:42:52 +02:00
before: (character, system) ->
character.sandbox.seen_reload = 1
2015-12-01 08:42:52 +02:00
system.clearContent()
2015-12-02 12:56:40 +02:00
after: (character, system) ->
2015-12-01 08:42:52 +02:00
spend_clip(character, system)
2015-12-01 12:42:15 +02:00
writemd(system, "reload_response".l())
2015-12-03 07:06:22 +02:00
if character.sandbox.trick_shot == 0 and character.sandbox.clips.length == 4
character.sandbox.trick_shot = 1
writemd(system, "trick_shot_discover".l()(character))
2015-12-01 08:42:52 +02:00
return true
situation "search",
tags: ["shoot"],
2015-12-01 12:42:15 +02:00
optionText: "search".l(),
canView: (character, system) ->
return character.sandbox.seen_search || character.qualities.clips < 5
canChoose: (character, system) ->
return character.qualities.clips < 5
before: (character, system) ->
system.clearContent()
character.sandbox.seen_search = 1
after: (character, system) ->
2015-12-02 09:20:18 +02:00
response = "search_response".l().oneOf().randomly(system)
writemd(system, response())
roll = system.rnd.dice(1,20) # d20 roll
find_threshold = 10
if roll < find_threshold
system.doLink("found")
else
system.doLink("not_found")
situation "found",
choices: ["#shoot"],
before: (character, system, from) ->
bullets = system.rnd.randomInt(2,4)
character.sandbox.clips[character.sandbox.clips.length] = bullets
system.setQuality("clips", character.sandbox.clips.length)
content: (character, system, from) ->
2015-12-02 09:20:18 +02:00
response = "clips_found".l().oneOf().randomly(system)
return response()
situation "not_found",
choices: ["#shoot"],
content: (character, system, from) ->
2015-12-02 09:20:18 +02:00
response = "clips_not_found".l().oneOf().randomly(system)
return response()
2015-12-01 08:42:52 +02:00
situation "finale",
2015-12-02 10:48:59 +02:00
content: (character, system) ->
2015-12-02 12:56:40 +02:00
if character.sandbox.shots < 36
return "finale_perfect".l()
return "finale".l()
2015-12-01 08:42:52 +02:00
2015-12-03 10:44:30 +02:00
situation "pacifist",
choices: ["#pacifist"],
content: (character, system) ->
return "pacifist".l()
situation "shoot_pacifist",
optionText: "Убить пацифиста",
tags: "pacifist",
choices: ["#shoot"],
before: (character, system) ->
character.sandbox.shot_pacifist = 1
content: (character, system) ->
link = textcycle("head".l(), "leg")
console.log(link)
return "shoot_pacifist".l()(link)
writers:
head: textcycle("head".l(), "leg")
leg: textcycle("leg".l(), "arm")
arm: textcycle("arm".l(), "head")
situation "spare_pacifist",
optionText: "Опустить оружие",
tags: "pacifist",
before: (character, system) ->
character.sandbox.shot_pacifist = 0
choices: ["#shoot"],
content: (character, system) ->
return "spare_pacifist".l()
2015-12-01 08:42:52 +02:00
qualities
head:
2015-12-01 12:42:15 +02:00
bullets: qualities.integer("bullets".l()),
clips: qualities.integer("clips".l()),
enemies: qualities.integer("enemies".l()),
2015-12-01 08:42:52 +02:00
undum.game.init = (character, system) ->
system.setQuality("bullets", 6)
system.setQuality("clips", 6)
system.setQuality("enemies", 35)
2015-12-01 12:42:15 +02:00
character.sandbox.clips = [6,6,6,6,6,6]
2015-12-01 08:42:52 +02:00
character.sandbox.current_clip = 0
character.sandbox.nicked = 0
character.sandbox.seen_reload = 0
character.sandbox.seen_search = 0
2015-12-03 07:06:22 +02:00
character.sandbox.trick_shot = 0
2015-12-02 10:48:59 +02:00
character.sandbox.shots = 0
2015-12-03 07:06:22 +02:00
character.sandbox.killed = 0
2015-12-03 10:44:30 +02:00
character.sandbox.seen_pacifist = 0
character.sandbox.shot_pacifist = undefined
2015-12-02 09:20:18 +02:00
$("#title").click(() ->
$("#clip").fadeIn()
)
2015-12-01 08:42:52 +02:00
window.onload = undum.begin