From 373d703fe07356c60d9f2920fb26a44cc692b6fa Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Sun, 20 Mar 2016 19:41:47 +0700 Subject: [PATCH] Taking things (issue #4) --- game/begin.coffee | 1 + game/story.coffee | 17 ++++++++++++++++- lib/character.coffee | 8 +++++++- lib/room.coffee | 16 +++++++--------- lib/salet.coffee | 16 ++++++++++------ lib/view.coffee | 6 +++--- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/game/begin.coffee b/game/begin.coffee index 4f95086..950b61c 100644 --- a/game/begin.coffee +++ b/game/begin.coffee @@ -11,6 +11,7 @@ salet = Salet({ }) $(document).ready(() -> salet.beginGame() + salet.character.bought_lamp = false ) ### diff --git a/game/story.coffee b/game/story.coffee index 44d86ee..b7135cf 100644 --- a/game/story.coffee +++ b/game/story.coffee @@ -69,7 +69,7 @@ room "lair", salet, dsc: "You see a particularly beautiful slimy {{bugg.}}" takeable: false act: (salet) => - salet.rooms[salet.current].drop(@name) + salet.rooms[salet.current].drop("bugg") return "You eat the bugg mass. Delicious and raw. Perhaps it's a good lair to live in." ] @@ -79,6 +79,21 @@ phrase "Yes", salet, "merchant", """ dialogue "No", salet, "merchant", "merchant", """ No. """ +room "sell-lamp", salet, + ways: ["shop"] + tags: ["merchant"] + choices: ["#merchant"] + optionText: "May I buy this lamp?" + title: "Talking with merchant" + canView: (salet) -> + return salet.character.has("lamp") and salet.character.bought_lamp == false + enter: (salet) -> + salet.character.bought_lamp = true + dsc: """ + "That'll be 30 pieces of your time." + + You quickly pay the price and take the lamp as a rightful owner. + """ room "shop-inside", salet, ways: ["shop"] diff --git a/lib/character.coffee b/lib/character.coffee index 7eeb145..e8cf7e5 100644 --- a/lib/character.coffee +++ b/lib/character.coffee @@ -9,7 +9,13 @@ class Character if i.name == thing index = @objects.indexOf(thing) @inventory.splice(index, 1) - + + @has = (thing) => + for i in @inventory + if i.name == thing + return true + return false + for index, value of spec this[index] = value return this diff --git a/lib/room.coffee b/lib/room.coffee index 6af6e31..ca7091b 100644 --- a/lib/room.coffee +++ b/lib/room.coffee @@ -43,7 +43,7 @@ class SaletRoom ### @exit = (system, to) => return true - + ### I call SaletRoom.enter every time the player enters this room but before the section is opened. Unlike @before this gets called before the current section is opened. @@ -98,7 +98,7 @@ class SaletRoom if @choices system.view.writeChoices(system, system.getSituationIdChoices(@choices, @maxChoices)) - + if @afterChoices? @afterChoices.fcall(this, system, f) @@ -147,8 +147,8 @@ class SaletRoom @drop = (name) => for thing in @objects if thing.name == name - index = @objects.indexOf(thing) - @objects.splice(index, 1) + @objects.splice(@objects.indexOf(thing), 1) + return @objects ### Object action. A function or a string which comes when you click on the object link. @@ -163,11 +163,9 @@ class SaletRoom # If not, we check the "act" function. if thing.takeable system.character.take(thing) - @drop name - system.view.clearContent() - @entering.fcall(this, system, @name) + @drop link[2] return system.view.write(thing.take.fcall(thing, system).toString()) - if thing.act? + else if thing.act? return system.view.write thing.act.fcall(thing, system) # the loop is done but no return came - match not found @@ -241,7 +239,7 @@ class SaletRoom for index, value of spec this[index] = value return this - + room = (name, salet, spec) -> spec ?= {} spec.name = name diff --git a/lib/salet.coffee b/lib/salet.coffee index 189a900..70c9fa1 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -275,14 +275,18 @@ class Salet # Carry out the action if (action) room = @getCurrentRoom() - if (room and @beforeAction) - # Try the global act handler - consumed = @beforeAction(room, action) - if (consumed != true) + if room + consumed = false + + if @beforeAction + # Try the global act handler + consumed = @beforeAction(room, action) + + if consumed != true room.act(this, action) - if (@afterAction) - @afterAction(this, room, action) + if @afterAction + @afterAction(room, action) # This gets called when the user clicks a link to carry out an action. @processClick = (code) => diff --git a/lib/view.coffee b/lib/view.coffee index bb59b0c..547f3d0 100644 --- a/lib/view.coffee +++ b/lib/view.coffee @@ -30,9 +30,9 @@ class SaletView event.preventDefault() a = $(this) href = a.attr('href') - if (href.match(salet.linkRe)) - if (a.hasClass("once") || href.match(/[?&]once[=&]?/)) - salet.view.clearLinks(href) + if a.hasClass("once") || href.match(/[?&]once[=&]?/) + salet.view.clearLinks(href) + if href.match(salet.linkRe) salet.processClick(href) ) $("#inventory").on("click", "a", (event) ->