(ns advent.screens.rooms.outside-house (:require [advent.screens.items :as items] [advent.screens.rooms :as rooms] [advent.screens.rooms.common :as common] [advent.screens.rooms.outside-castle :as outside-castle] [advent.screens.rooms.inside-castle :as inside-castle] [advent.actions :as actions] [advent.tween :as tween] [advent.utils :as utils] [clojure.zip :as zip] [play-clj.core :refer :all] [play-clj.ui :refer :all] [play-clj.math :refer :all] [play-clj.utils :refer :all] [play-clj.g2d :refer :all])) (defn dawn-fade [entities] (actions/run-action entities (begin [this screen entities] (-> entities (assoc-in [:tweens :dawn-r] (tween/tween :dawn-r screen [:time-profiles :default :r] :current 0.63 50.0 :entities entities)) (assoc-in [:tweens :dawn-g] (tween/tween :dawn-g screen [:time-profiles :default :g] :current 0.36 50.0 :entities entities)) (assoc-in [:tweens :dawn-b] (tween/tween :dawn-b screen [:time-profiles :default :b] :current 0.23 50.0 :entities entities)) (assoc-in [:tweens :hue-amount-v] (tween/tween :hue-amount-v screen [:time-profiles :default :hue-amount] :current 0.0 50.0 :entities entities)) (assoc-in [:tweens :multiply-amount-v] (tween/tween :multiply-amount-v screen [:time-profiles :default :multiply-amount] :current 0.0 50.0 :entities entities)) (assoc-in [:tweens :dawn-r-s] (tween/tween :dawn-r-s screen [:time-profiles :sprite :r] :current 0.63 50.0 :entities entities)) (assoc-in [:tweens :dawn-g-s] (tween/tween :dawn-g-s screen [:time-profiles :sprite :g] :current 0.36 50.0 :entities entities)) (assoc-in [:tweens :dawn-b-s] (tween/tween :dawn-b-s screen [:time-profiles :sprite :b] :current 0.23 50.0 :entities entities)) (assoc-in [:tweens :hue-amount-v-s] (tween/tween :hue-amount-v-s screen [:time-profiles :sprite :hue-amount] :current 0.0 50.0 :entities entities)) (assoc-in [:tweens :multiply-amount-v-s] (tween/tween :multiply-amount-v-s screen [:time-profiles :sprite :multiply-amount] :current 0.0 50.0 :entities entities))) ) (continue [this screen entities] entities) (done? [this screen entities] true) (terminate [this screen entities] entities))) (defn walk-to-castle [entities dawn?] (actions/walk-to entities :ego [0 80]) (actions/walk-straight-to entities :ego [-20 80]) (if dawn? (actions/transition-background entities :outside-castle [330 80]) (actions/transition-background entities :outside-castle [330 80])) (actions/walk-straight-to entities :ego [310 80])) (def ego-sheep-loc [132 140]) (defn dist-to-sheep [entities] (apply utils/dist (get-in entities [:room :entities :sheep :x]) (get-in entities [:room :entities :sheep :y]) ego-sheep-loc)) (defn is-sheep-close? [entities] (< (dist-to-sheep entities) 45)) (defn is-ready-for-slingshot [entities] (and (actions/has-obtained? entities :broken-clock) (not (actions/has-item? entities :broken-clock)) (actions/has-obtained? entities :money) (not (actions/has-item? entities :money)))) (defn walk-to-sheep [entities] (actions/walk-to entities :ego [154 133]) (actions/walk-straight-to entities :ego [119 134] :update-baseline? false)) (defn leave-sheep [entities] (actions/walk-straight-to entities :ego [154 133] :update-baseline? false)) (defn magic [entities] (actions/run-action entities (begin [this screen entities] (doto (get-in entities [:room :entities :magic]) (particle-effect! :reset) (particle-effect! :start)) entities) (continue [this screen entities] entities) (done? [this screen entities] (particle-effect! (get-in entities [:room :entities :magic]) :is-complete)) (terminate [this screen entities] entities))) (defn put-something-in-cauldron [item] (condp = item :money (actions/get-script entities (actions/walk-to entities :ego [141 90] :face :right) (actions/play-animation entities :ego :reach) (actions/remove-item entities :money) (actions/talk entities :ego "I guess that's what you could call 'money in the pot'.")) :slingshot (actions/get-script entities (if (is-ready-for-slingshot entities) (do (actions/walk-to entities :ego [141 90] :face :right) (dawn-fade entities) (actions/talk entities :ego "Here goes!") (actions/play-animation entities :ego :reach-start :stop? false) (magic entities) (actions/play-animation entities :ego :reach-stop :stop? true) (actions/remove-item entities :slingshot) (actions/give entities :magic-slingshot) (actions/do-dialogue entities :ego "It worked!") (actions/glad entities) (actions/do-dialogue entities :ego "I now have The Slinger's Shot." :ego "And just in time, too. It's getting light.") (actions/update-state entities #(assoc % :next-time :sunrise)) (walk-to-castle entities true) (outside-castle/go-through-gate entities) (actions/update-state entities #(assoc % :next-time :day)) (inside-castle/walk-to-blergh entities) (actions/do-dialogue entities :ego "Bloodclot!" :bloodclot-head "Oh, hello again." :bloodclot-head "I thought I had taken care of you the last time I saw you." :bloodclot-head "Any final words before I destroy you again?") (actions/play-animation entities :ego :scared :continue? true) (Thread/sleep 500) (actions/walk-straight-to entities :ego [35 45] :override-dir :right :speed 3.0)))) :broken-clock (actions/get-script entities (actions/walk-to entities :ego [141 90] :face :right) (actions/play-animation entities :ego :reach) (actions/remove-item entities :broken-clock) (actions/talk entities :ego "Just in the nick of time.")) :recipe (actions/get-script entities (actions/walk-to entities :ego [151 90] :face :right) (actions/play-animation entities :ego :squat) (actions/remove-item entities :recipe) (actions/give entities :ash) (actions/talk entities :ego "It burned up into ash.")) :spell-component (actions/get-script entities (actions/walk-to entities :ego [141 90] :face :right) (actions/play-animation entities :ego :reach) (actions/remove-item entities :spell-component) (actions/talk entities :ego "I poured it in. Now what?")) (actions/get-script entities (actions/talk entities :ego "I don't want to put something in there unless I'm sure I need to.")))) (defn wizard-dialogue [entities] (actions/do-dialogue entities :ego "Hello there Gandarf!" :wizard "Oh no, not you again!") (actions/present-choices entities {:choices ["What do you mean, \"Not you again?\"" {:run #(actions/respond entities % :wizard "I mean, you've wrecked my life and I never want to see you again.") :choices ["You mean the time I set your house on fire with a fire mint?" {:run #(do (actions/respond entities % :wizard "That was you!?" :wizard "My house was nearly destroyed!" :wizard "I spent weeks rebuilding my home!" :wizard "Leave, now, or I'll turn you into a ..." :wizard "... a ..." :wizard "... a frog!" :ego "Okay, okay! I'm leaving.") (actions/transition-background entities :outside-house [257 90] :face :left) (actions/do-dialogue entities :ego "I guess he's really upset with me." :ego "I wonder if I can convince him to let me stay..."))} "You're still cross about my stealing your magic cowboy hat?" {:run #(do (actions/respond entities % :wizard "Of course I'm cross! It's irreplaceable!" :wizard "That cowboy hat accented my facial physique." :wizard "And complemented my skin color." :wizard "And you little pipsqueak stole it from me!" :wizard "That's why I bought my Magi-Safe 5000, to keep out intruders like you." :wizard "Now leave, before I get really angry!.") (actions/transition-background entities :outside-house [257 90] :face :left) (actions/do-dialogue entities :ego "I guess he's really upset with me." :ego "I wonder if I can convince him to let me stay..."))} "Even an old hoot like you needs a kick in the pants every now and again." {:run #(actions/respond entities % :wizard "What gives you the right to try to teach me a lesson?") :choices ["My good looks?" {:run #(do (actions/respond entities % :wizard "You know, handsome looks aren't all they're chocked up to be." :wizard "Take me for example." :wizard "When you have a bod like man, you can hardly go to the grocery store without being noticed." :wizard "But no. Your looks, however good they may be, don't give you the right to teach me a lesson." :wizard "Now please leave me in peace.") (actions/transition-background entities :outside-house [257 90] :face :left) (actions/talk entities :ego "Gandarf is pretty upset, but I bet he can be reasoned with."))} "My good standing within the community?" {:run #(do (actions/respond entities % :wizard "Ha! Good standing?" :wizard "You're the neighborhood cheat and everyone knows it." :wizard "Now please leave me in peace.") (actions/transition-background entities :outside-house [257 90] :face :left) (actions/talk entities :ego "Gandarf is pretty upset, but I bet he can be reasoned with."))} "I'm going to be a knight! That counts for something doesn't it?" {:run #(do (actions/respond entities % :wizard "You are, are you?" :wizard "Pray tell, how do you, a mere reckless youth, plan on becoming a knight?")) :choices ["By pulling the Sword of Blergh from its stone!" {:run #(actions/respond entities % :wizard "Well, well. It sounds like you've turned over a new leaf." :wizard "But know this, the stone cannot be cheated easily." :wizard "You must fulfill the age-old prophecy." :wizard "'The Sword of Blergh with magic sting,\nShall yield to no earthly king'" :wizard "'To draw from hardened, weathered stone,\nHe must have valor, to the bone.'" :wizard "'Worthy in courage, wisdom and might,\nOnly then with sword he'll fight.'" :wizard "'But a hero should he prove not be,\nOnly ruin will fall on he.'" :wizard "'Fight he must, for one more test...'") :choices ["Is this almost over?" {:run #(do (actions/update-state entities (fn [state] (assoc state :convinced-wizard? true))) (actions/respond entities % :wizard "Patience, boy." :wizard "... 'He will die if not the best.'" :wizard "If this is truely your quest, boy, then I will help you. " :ego "So you're my friend now, Gandarf?" :wizard "No, but I won't kick you out of my house, as long as you behave."))} "*cough* *cough* *ahem*" {:run #(do (actions/update-state entities (fn [state] (assoc state :convinced-wizard? true))) (actions/respond entities % :wizard "Excuse you. Moving on..." :wizard "... 'He will die if not the best'" :wizard "If this is truely your quest, boy, then I will help you. " :ego "So you're my friend now, Gandarf?" :wizard "No, but I won't kick you out of my house, as long as you behave.") (utils/save @entities))}]} "By besting swamp, foe, and the occasional bizarre conversation tree." {:run #(do (actions/respond entities % :wizard "While your goal sounds noble, no amount of bizarre conversation tree searching will earn my respect." :wizard "Now please leave.") (actions/transition-background entities :outside-house [257 90] :face :left) (actions/talk entities :ego "If only Gandarf could be tricked into seeing the valiant side of me..."))}]}]}]} "You're not happy to see me, Gandarf?" {:run #(actions/respond entities % :wizard "Of course not, you little brat. You've made my life a living hell!") :choices #(-> % zip/left)} "Goodbye, Gandarf!" {:run #(do (actions/respond entities % :wizard "Now scram!") (actions/transition-background entities :outside-house [257 90] :face :left))}]})) (defn talk-to-gandarf-outside [entities] (actions/walk-to entities :ego [120 80] :face :right) (if (actions/has-item? entities :flask-2) (actions/do-dialogue entities :wizard "Have you gotten me water from the fountain yet?" :ego "No, not yet." :wizard "Hurry, boy! Time's-a-wastin'!") (do (actions/do-dialogue entities :ego "Gandarf! Boy am I glad to see you." :wizard "There is no time, boy." :wizard "We must hurry if you are to defeat Bloodclot." :ego "Wait, you knew about Bloodclot all along?" :ego "And you never told me?" :wizard "Honestly, I've always thought you were a loser." :wizard "I never thought you'd be able to pull the sword." :wizard "But we have to hurry." :wizard "Bloodclot spent his entire time in sword prison making that helmet that zapped you." :wizard "There's only one way to neutralize it:" :wizard "The Slinger's Shot." :wizard "It's a magical slingshot that has long lost its power." :wizard "Here, take it.") (actions/give entities :slingshot) (actions/do-dialogue entities :wizard "Whatever happens, do not lose that slingshot!" :wizard "You and me, boy, we're going to return it to its powerful state." :wizard "Then you can beat Bloodclot, come sunrise." :wizard "Now, time is of the essence." :wizard "Take this flask. Fill it with water from the fountain." :wizard "And remember. Don't lose the slingshot.") (actions/give entities :flask-2)))) (defn add-wizard-if-necessary [entities] (if (actions/has-obtained? entities :money) entities (update-in entities [:room :entities] #(assoc % :wizard (get-in entities [:room :wizard]))))) (defn add-note-if-necessary [entities] (if (and (actions/has-obtained? entities :money) (not (actions/has-obtained? entities :note-1))) (update-in entities [:room :entities] #(assoc % :note (get-in entities [:room :note]))) entities)) (defn make-night [entities] (-> entities (update-in [:room :entities] #(dissoc % :butterfly)) (update-in [:room :entities] #(assoc % :cauldron (get-in entities [:room :cauldron]))) add-wizard-if-necessary add-note-if-necessary)) (defn move-toward [screen entities {:keys [x y] :as e} target-x] (let [delta-x (- target-x x) speed (* 1.0 (/ (:delta-time screen) (/ 1.0 60.0))) speed (if (< delta-x 0) (- speed) speed) moved-x (if (< (Math/abs delta-x) speed) target-x (* speed (/ delta-x delta-x) ))] (if (< (Math/abs delta-x) speed) (actions/start-animation screen (dissoc e :target-x) :stand) (actions/start-animation screen (assoc (actions/jump-to screen entities e [(+ moved-x x) y] false) :facing (cond (< delta-x 0) :left (> delta-x 0) :right :else (:facing e))) :walk)))) (defn jump-around [screen entities] (if-let [target-x (get-in entities [:room :entities :lamb :target-x])] (update-in entities [:room :entities :lamb] #(move-toward screen entities % target-x)) (if (= 1 (rand-int 50)) (assoc-in entities [:room :entities :lamb :target-x] (rand-nth [30 10 60 70])) entities))) (defn make [screen] (let [sheep-stand-sheet (texture! (texture "outsidehouse/sheep-anim.png") :split 33 21) sheep-walk-sheet (texture! (texture "outsidehouse/sheep-walk.png") :split 33 21) lamb-walk-sheet (texture! (texture "outsidehouse/lamb-walk.png") :split 27 28) sheep-stand (animation 0.15 (for [i (flatten [(repeat 10 0) 1 2 3 4 5 6 7 4 5 6 7 8 9 10 (repeat 25 11) (repeat 15 12)])] (aget sheep-stand-sheet 0 i))) sheep-walk (animation 0.05 (for [i (range 6)] (aget sheep-walk-sheet 0 i))) butterfly-stand (utils/make-anim "butterfly.png" [7 7] 0.1 [0 1]) cauldron (utils/make-anim "outsidehouse/cauldron.png" [50 38] 0.15 (range 4)) scaler (utils/scaler-fn-with-baseline 110 0.10 1.00) lamb-stand (aget lamb-walk-sheet 0 0) lamb-walk (animation 0.075 (for [i (range 4)] (aget lamb-walk-sheet 0 i)))] (rooms/make :music {:day :town-2 :night :night} :update-fn jump-around :interactions {:door {:box [250 100 281 160] :script (actions/get-unsaved-script entities (actions/walk-to entities :ego [257 90] :face :right) (actions/talk entities :ego (str "Anyone home?")) (sound! (sound "door.ogg") :play (utils/current-sound-volume)) (actions/play-animation entities :ego :reach) (if (= :night (get-in @entities [:state :time])) (actions/talk entities :ego "It's locked.") (do (actions/transition-background entities :inside-house [237 0] :between (fn [s e] (if (= 1 (rand-int 10)) (assoc-in e [:room :entities :experiment] (get-in e [:room :experiment])) e))) (when (get-in @entities [:room :entities :experiment]) (actions/play-animation entities :experiment :experiment) (actions/remove-entity entities :experiment)) (if (get-in @entities [:state :convinced-wizard?]) (do (actions/talk entities :wizard (str "Oh, hello there boy.")) (utils/save @entities)) (wizard-dialogue entities))))) :cursor :right} :right-dir {:box [220 141 320 204] :script (actions/get-script entities (actions/walk-to entities :ego [244 150]) (actions/transition-background entities :behind-house [122 140]) (actions/walk-to entities :ego [172 122])) :cursor :right} :up-dir {:box [60 180 224 240] :script (actions/get-script entities (actions/walk-to entities :ego [137 204]) (actions/transition-background entities :cat-tree [223 -51]) (actions/walk-straight-to entities :ego [200 10])) :cursor :up} :left-dir {:box [0 40 20 140] :script (actions/get-script entities (walk-to-castle entities false)) :cursor :left}} :layers {:day [(assoc (texture "outsidehouse/background.png") :x 0 :y 0 :baseline 0) (assoc (texture "outsidehouse/house.png") :x 0 :y 0 :baseline 122) (assoc (texture "outsidehouse/fence.png") :x 0 :y 0 :baseline 93) (assoc (texture "outsidehouse/background-trees.png") :x 0 :y 0 :baseline 44) (assoc (texture "outsidehouse/fg1.png") :x 0 :y 0 :baseline 1000 :parallax 1.5) (assoc (texture "outsidehouse/fg2.png") :x (- 320 55) :y 0 :baseline 1000 :parallax 1.5)] :night [(assoc (texture "outsidehouse/background.png") :x 0 :y 0 :baseline 0) (assoc (texture "outsidehouse/house.png") :x 0 :y 0 :baseline 122) (assoc (texture "outsidehouse/fence.png") :x 0 :y 0 :baseline 93) (assoc (texture "outsidehouse/background-trees.png") :x 0 :y 0 :baseline 44) (assoc (texture "outsidehouse/fg1.png") :x 0 :y 0 :baseline 1000 :parallax 1.5) (assoc (texture "outsidehouse/fg2.png") :x (- 320 55) :y 0 :baseline 1000 :parallax 1.5)]} :entities {:sheep (actions/start-animation screen (assoc (animation->texture screen sheep-stand) :x 38 :y 160 :baseline 80 :scale-x (scaler [38 160]) :scale-y (scaler [38 160]) :box [38 160 71 181] :script (actions/get-script entities (if (actions/has-item? entities :wool) (actions/talk entities :ego "The sheep has given me enough wool.") (if (is-sheep-close? @entities) (do (walk-to-sheep entities) (actions/play-animation entities :ego :reach) (actions/give entities :wool) (actions/talk entities :ego "I guess her wool is shedding.") (leave-sheep entities)) (do (actions/talk entities :ego "Come here mama sheep!") (actions/play-animation entities :ego :sigh) (actions/talk entities :ego "She's too far away for me to pet her."))))) :scripts #(condp = % :wool (actions/get-script entities (actions/talk entities :ego "She doesn't need it back.")) :grass (actions/get-script entities (walk-to-sheep entities) (actions/talk entities :ego "Come on girl, get the grass!") (actions/play-animation entities :ego :reach) (actions/talk entities :ego "I think she's not interested.") (leave-sheep entities)) :carrot (actions/get-script entities (walk-to-sheep entities) (actions/talk entities :ego "Come on girl, get the carrot!") (actions/walk-straight-to entities :sheep [90 138] :update-baseline? false) (actions/play-animation entities :ego :reach) (actions/remove-item entities :carrot) (actions/update-state entities (fn [s] (assoc s :coaxed-sheep? true))) (leave-sheep entities)) :flask-1 (actions/get-script entities (if (is-sheep-close? @entities) (do (walk-to-sheep entities) (actions/play-animation entities :ego :milk) (actions/remove-item entities :flask-1) (actions/give entities :flask-1-with-milk) (actions/talk entities :ego "Sheeps milk.") (leave-sheep entities)) (actions/talk entities :ego "She's too far away."))) :flask-1-with-mushrooms (actions/get-script entities (if (is-sheep-close? @entities) (do (walk-to-sheep entities) (actions/play-animation entities :ego :milk) (items/make-cream-of-mushroom entities) (leave-sheep entities)) (actions/talk entities :ego "She's too far away."))) nil) :left {:walk (utils/flip sheep-walk) :stand (utils/flip sheep-stand)} :right {:walk sheep-walk :stand sheep-stand} :scaled true) sheep-stand) :magic (assoc (doto (particle-effect "outsidehouse/magic") ) :x 153 :y 105 :baseline 238) :lamb (assoc (texture "outsidehouse/lamb.png") :x 10 :y 163 :baseline 77 :right {:stand (animation 0.1 [lamb-stand]) :walk lamb-walk} :left {:stand (utils/flip (animation 0.1 [lamb-stand])) :walk (utils/flip lamb-walk)} :scale-x (scaler [10 163]) :scale-y (scaler [10 163]) :script (actions/get-script entities (actions/talk entities :ego "Aww, it's a newborn lamb!") ) :scripts {:carrot (actions/get-script entities (actions/talk entities :ego "I think it's still nursing.")) :grass (actions/get-script entities (actions/talk entities :ego "I think it's still nursing."))} ) :butterfly (assoc (animation->texture screen butterfly-stand) :x 161 :y 218 :baseline 240 :anim butterfly-stand :anim-start 0 :path (catmull-rom-spline (map #(apply vector-2* %) (take 10 (repeatedly #(vector (rand-int 320) (rand-int 180))))) true) :update-fn (fn [screen entities entity] (let [speed 0.009 pos-f (- (* (:total-time screen) speed) (int (* (:total-time screen) speed))) v (vector-2 0 0) a (catmull-rom-spline! (:path entity) :value-at v pos-f)] (assoc entity :x (vector-2! v :x) :y (vector-2! v :y)))))} :cauldron (rooms/make-entity :cauldron (assoc (animation->texture screen cauldron) :x 139 :y 73 :baseline 167 :anim cauldron :anim-start 0 :night-profile :none :script (actions/get-script entities (actions/talk entities :ego "That's a big cauldron!")) :scripts put-something-in-cauldron)) :wizard (rooms/make-entity :wizard (common/make-wizard screen {:x 190 :y 78 :baseline 162 :scale-x 1.2 :scale-y 1.2 :script (actions/get-script entities (talk-to-gandarf-outside entities))})) :note (rooms/make-entity :note (assoc (texture "outsidehouse/note.png") :x 277 :y 74 :baseline 160 :night-profile :none :script (actions/get-script entities (actions/walk-to entities :ego [280 80] :face :right) (actions/play-animation entities :ego :squat) (actions/remove-entity entities :note) (actions/give entities :note-1) (common/read-note-1 entities)))) :collision "outsidehouse/collision.png" :scale-fn scaler :apply-state (fn [_ entities] (as-> entities entities (if (get-in entities [:state :coaxed-sheep?]) (let [scale ((get-in entities [:room :scale-fn]) [90 138])] (update-in entities [:room :entities :sheep] #(assoc % :x 90 :y 138 :baseline 40 :scale-x scale :scale-y scale))) entities) (if (= :night (get-in entities [:state :time])) (make-night entities) entities))) :start-pos [30 80])))