diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index f9b4e076..8fc3e3d6 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -84,7 +84,7 @@ (thread (do ~@forms (change-script-state ~entities false) - (utils/save @~entities)))))))) + (utils/save @~entities :autosave "Autosave")))))))) (defmacro get-unsaved-script [entities & forms] `(fn [starting-entities#] diff --git a/desktop/src-common/advent/saves.clj b/desktop/src-common/advent/saves.clj index 53c27136..cb39d288 100644 --- a/desktop/src-common/advent/saves.clj +++ b/desktop/src-common/advent/saves.clj @@ -10,5 +10,12 @@ :after-jail "Prisoner On The Loose" :in-jail "In The Slammer"}) +(def chapters + {:chapter-1 nil + :chapter-2 nil + :chapter-3 nil + :chapter-4 nil + :chapter-5 nil}) + (def name->save (into {} (map vector (vals saves) (keys saves)))) diff --git a/desktop/src-common/advent/screens/rooms/cat_tree.clj b/desktop/src-common/advent/screens/rooms/cat_tree.clj index 05410846..e205abd8 100644 --- a/desktop/src-common/advent/screens/rooms/cat_tree.clj +++ b/desktop/src-common/advent/screens/rooms/cat_tree.clj @@ -342,8 +342,7 @@ :ego "What's this?") (actions/give entities :kiss) (actions/do-dialogue entities :ego "A kiss for an inventory item?" - :ego "Sounds like the game designer was running out of good ideas.") - (utils/snapshot-state @entities :after-cat)) + :ego "Sounds like the game designer was running out of good ideas.")) (actions/talk entities :ego "I guess I'm too far away."))) :default (actions/get-script entities (actions/talk entities :ego "Kitty seems disinterested in it."))}) cat-stand) diff --git a/desktop/src-common/advent/screens/rooms/dream.clj b/desktop/src-common/advent/screens/rooms/dream.clj index 16b01d84..b871af39 100644 --- a/desktop/src-common/advent/screens/rooms/dream.clj +++ b/desktop/src-common/advent/screens/rooms/dream.clj @@ -243,7 +243,7 @@ :fairy-godfather "Choose the broom or shovel, and cast it into the pit of fate." :ego "But..." :fairy-godfather "No buts.") - (utils/snapshot-state @entities :beginning) + (utils/save-chapter @entities :chapter-1) (actions/update-state entities #(assoc % :seen-intro? true :time :day))) (defn swing [entities] @@ -422,7 +422,7 @@ :ego "If I only really could be a knight." :ego "Then I'd be able to win Georgia McGorgeous' heart.") (actions/in-love entities) - (utils/snapshot-state @entities :after-dream)) + (utils/save-chapter @entities :chapter-2)) (do (actions/walk-to entities :ego [148 76] :face :right) (actions/do-dialogue entities :fairy-godfather "What are you doing?" :ego "Erm... Nothing.")))) diff --git a/desktop/src-common/advent/screens/rooms/inside_cafeteria.clj b/desktop/src-common/advent/screens/rooms/inside_cafeteria.clj index 0c057854..bb35c71a 100644 --- a/desktop/src-common/advent/screens/rooms/inside_cafeteria.clj +++ b/desktop/src-common/advent/screens/rooms/inside_cafeteria.clj @@ -80,8 +80,7 @@ :warriors "Congratulations, young master. Thou art worthy in might." :warriors "Take thy servant's medal of strength.") (actions/give entities :medal) - (actions/glad entities) - (utils/snapshot-state @entities :after-strength)) + (actions/glad entities)) (do (play-battle entities :lose) (actions/do-dialogue entities diff --git a/desktop/src-common/advent/screens/rooms/inside_castle.clj b/desktop/src-common/advent/screens/rooms/inside_castle.clj index 8daf0826..830c27df 100644 --- a/desktop/src-common/advent/screens/rooms/inside_castle.clj +++ b/desktop/src-common/advent/screens/rooms/inside_castle.clj @@ -585,7 +585,7 @@ (actions/remove-entity entities :trophy) (actions/glad entities) (actions/talk entities :ego "Thanks!") - (utils/snapshot-state @entities :after-wisdom)) + ) (actions/do-dialogue entities :ego "What about this?" :game-player "No, that's not the solution. Keep looking.")) (brian-get-to-work entities)) :trophy (actions/get-script entities diff --git a/desktop/src-common/advent/screens/rooms/inside_jail.clj b/desktop/src-common/advent/screens/rooms/inside_jail.clj index eece3e26..a5add012 100644 --- a/desktop/src-common/advent/screens/rooms/inside_jail.clj +++ b/desktop/src-common/advent/screens/rooms/inside_jail.clj @@ -198,7 +198,7 @@ :ego "But what now?" :ego "I have till sunrise before Bloodclot comes and destroys the town." :ego "Maybe Gandarf can help me!") - (utils/snapshot-state @entities :after-jail)) + (utils/save-chapter @entities :chapter-4)) (do (actions/do-dialogue entities :ego "Yes! I made it!" :guard "Hmm?" diff --git a/desktop/src-common/advent/screens/rooms/outside_house.clj b/desktop/src-common/advent/screens/rooms/outside_house.clj index c79bb71e..f9e49515 100644 --- a/desktop/src-common/advent/screens/rooms/outside_house.clj +++ b/desktop/src-common/advent/screens/rooms/outside_house.clj @@ -210,7 +210,7 @@ :wizard "If this is truely your quest, then I will help you. " :ego "So you're my friend now, Gandarf?" :wizard "No.") - (utils/save @entities))}]}) + (utils/save @entities :autosave "Autosave"))}]}) (defn wizard-dialogue [entities] (if (get-in @entities [:state :has-met-gandarf?]) @@ -623,7 +623,7 @@ (if (get-in @entities [:state :convinced-wizard?]) (do (actions/talk entities :wizard (str "Oh, hello there boy.")) - (utils/save @entities)) + (utils/save @entities :autosave "Autosave")) (wizard-dialogue entities))))) :cursor :right} diff --git a/desktop/src-common/advent/screens/rooms/space.clj b/desktop/src-common/advent/screens/rooms/space.clj index 4a77be4d..ffbbc741 100644 --- a/desktop/src-common/advent/screens/rooms/space.clj +++ b/desktop/src-common/advent/screens/rooms/space.clj @@ -279,7 +279,7 @@ (bloodclot-disappear entities) (common/go-to-jail entities 5.0) (actions/do-dialogue entities :ego "Hey!" :ego "What's going on? I was just about to teach Bloodclot a lesson!") - (utils/snapshot-state @entities :in-jail)))) + (utils/save-chapter @entities :chapter-3)))) :magic-slingshot (actions/get-script entities (actions/do-dialogue entities :ego "Hey Bloodclot!" diff --git a/desktop/src-common/advent/screens/scene.clj b/desktop/src-common/advent/screens/scene.clj index 30598ad1..279ae996 100644 --- a/desktop/src-common/advent/screens/scene.clj +++ b/desktop/src-common/advent/screens/scene.clj @@ -819,7 +819,7 @@ void main () (defn get-state [selected-save] (if selected-save - (utils/load-snapshot selected-save) + (assoc (:state selected-save) :active? true) {:object nil :active? true :last-room :dream @@ -1245,6 +1245,10 @@ void main () :on-show-inventory (fn [screen [entities]] (click-inventory screen entities)) + :on-save (fn [screen [entities]] + (when-not (get-in entities [:actions :script-running?]) + (utils/save entities (str (java.util.UUID/randomUUID)) (str "Save " (rand-int 20))))) + :on-menu (fn [{:keys [viewport] :as screen} [entities]] (when-not (or (get-in entities [:tweens :fade-out]) (get-in entities [:tweens :fade-in])) @@ -1373,5 +1377,8 @@ void main () (utils/intersects? (:close entities) [x y]) (screen! scene :on-menu) + (utils/intersects? (:save entities) [x y]) + (screen! scene :on-save) + :else nil))))) diff --git a/desktop/src-common/advent/screens/title.clj b/desktop/src-common/advent/screens/title.clj index 903d102c..3cf388f4 100644 --- a/desktop/src-common/advent/screens/title.clj +++ b/desktop/src-common/advent/screens/title.clj @@ -101,7 +101,7 @@ flipped)) (defn quest-label [] - (if (:seen-intro? (utils/load-snapshot :autosave)) + (if (seq (utils/snapshot-list)) "Continue quest" "Begin quest")) @@ -210,19 +210,20 @@ (.setUncheckLast true)) [cell-w cell-h] [218 161] make-cell (fn [image] [image :width cell-w :pad 4 4 4 4 :height cell-h]) - make-save-screenshot (fn [[save-name save-screenshot]] + make-save-screenshot (fn [{:keys [screenshot name id state] :as save}] (let [btn (doto (assoc (image-button (skin "ui/ui.json")) - :save save-name) - (image-button! :set-size cell-w cell-h) - (image-button! :add (doto (Group. ) - (.setSize (- cell-w 16) (- cell-h 16) ) - (.setOrigin (/ (- cell-w 16) 2) (/ (- cell-h 16) 2)) - (.addActor (:object (doto (image (texture save-screenshot)) - (image! :set-size (- cell-w 16) (- cell-h 16))))) - (.addActor (:object (make-label (str " " (subs save-name 0 (min (.length save-name) 10)))))) - )) - - save-object)] + :save save + :name name) + (image-button! :set-size cell-w cell-h) + (image-button! :add (doto (Group. ) + (.setSize (- cell-w 16) (- cell-h 16) ) + (.setOrigin (/ (- cell-w 16) 2) (/ (- cell-h 16) 2)) + (.addActor (:object (doto (image (texture screenshot)) + (image! :set-size (- cell-w 16) (- cell-h 16))))) + (.addActor (:object (make-label (str " " (subs name 0 (min (.length name) 10)))))) + )) + + save-object)] (.add group (:object btn)) (ActorEntity. (:object btn)))) save-screenshots (map make-save-screenshot (:saves-list entities)) @@ -381,14 +382,14 @@ (= :continue-or-start actor-key) (start-playing screen entities (if (= "Begin quest" (quest-label)) nil - :autosave)) + (first (utils/snapshot-list)))) (= :continue actor-key) - (start-playing screen entities (saves/name->save (:selected-save entities))) + (start-playing screen entities (:selected-save entities)) (:save e) (do - (label! (-> entities :save-menu :save-label) :set-text (:save e)) + (label! (-> entities :save-menu :save-label) :set-text (:name (:save e))) (text-button! (-> entities :save-menu :continue-button) :set-disabled false) (assoc entities :selected-save (:save e))) diff --git a/desktop/src-common/advent/utils.clj b/desktop/src-common/advent/utils.clj index b7eaba6a..65d945db 100644 --- a/desktop/src-common/advent/utils.clj +++ b/desktop/src-common/advent/utils.clj @@ -67,54 +67,48 @@ (defn snapshot-list [] (let [prefs (.getPreferences (Gdx/app) "ticks-tales-saves")] - (if (.contains prefs "snapshot-list") - (edn/read-string (.getString prefs "snapshot-list")) + (if (.contains prefs "saves") + (edn/read-string (.getString prefs "saves")) []))) (defn snapshot-screenshots [] (for [snapshot (snapshot-list)] - [snapshot (try (Pixmap. (FileHandle. (save-screenshot-file-name (saves/name->save snapshot))) ) - (catch Exception e - (Pixmap. 160 120 Pixmap$Format/RGB888)))])) + (update-in snapshot [:screenshot] + #(try (Pixmap. (FileHandle. %) ) + (catch Exception e + (Pixmap. 160 120 Pixmap$Format/RGB888)))))) -(defn snapshot-state [entities name] - (doto (.getPreferences (Gdx/app) "ticks-tales-saves") - (.putString (saves/saves name) (pr-str (entities :state))) - (.putString "snapshot-list" (pr-str (cons (saves/saves name) - (filter (complement #{(saves/saves name)}) - (snapshot-list))))) - .flush) - (on-gl (let [f (FileHandle. (save-screenshot-file-name name)) - _ (Pixmap/setFilter Pixmap$Filter/BiLinear) - viewport (-> @(resolve 'advent.screens.scene/scene) :screen deref :viewport) - [x y w h g-l g-r] [(.getScreenX viewport) (.getScreenY viewport) (.getScreenWidth viewport) (.getScreenHeight viewport)] - pm (ScreenUtils/getFrameBufferPixmap x y w h) - resized (Pixmap. 160 120 Pixmap$Format/RGB888) - _ (.drawPixmap resized pm 0 0 w h 0 0 160 120) - png (PixmapIO$PNG. (* w h 1.5))] - (.write png f resized) - (.dispose png) - ))) +(defn save-chapter [entities chapter] + (let [prefs (.getPreferences (Gdx/app) "ticks-tales-saves") + chapters (if (.contains prefs "chapters") + (edn/read-string (.getString prefs "chapters")) + saves/chapters) + chapters (assoc chapters chapter (entities :state))] + (.putString prefs "chapters" (pr-str chapters)) + (.flush prefs))) -(defn save [entities] - (snapshot-state entities :autosave)) - -(defn has-save? - ([] (has-save? :autosave)) - ([key] - (let [name (saves/saves key)] - (-> (.getPreferences (Gdx/app) "ticks-tales-saves") - (.contains name))))) - -(defn load-snapshot [key] - (let [name (saves/saves key)] - (-> (.getPreferences (Gdx/app) "ticks-tales-saves") - (.getString name) - edn/read-string - (assoc :active? true)))) - -(defn load [] - (load-snapshot :autosave)) +(defn save [entities id name] + (let [prefs (.getPreferences (Gdx/app) "ticks-tales-saves") + saves (if (.contains prefs "saves") + (edn/read-string (.getString prefs "saves")) + []) + saves (cons {:name name + :id id + :screenshot (save-screenshot-file-name id) + :state (entities :state)} + (filter (comp (complement #{id}) :id) saves))] + (.putString prefs "saves" (pr-str saves)) + (.flush prefs) + (on-gl (let [f (FileHandle. (save-screenshot-file-name id)) + _ (Pixmap/setFilter Pixmap$Filter/BiLinear) + viewport (-> @ (resolve 'advent.screens.scene/scene) :screen deref :viewport) + [x y w h g-l g-r] [(.getScreenX viewport) (.getScreenY viewport) (.getScreenWidth viewport) (.getScreenHeight viewport)] + pm (ScreenUtils/getFrameBufferPixmap x y w h) + resized (Pixmap. 160 120 Pixmap$Format/RGB888) + _ (.drawPixmap resized pm 0 0 w h 0 0 160 120) + png (PixmapIO$PNG. (* w h 1.5))] + (.write png f resized) + (.dispose png))))) (defn save-settings! [] (doto (.getPreferences (Gdx/app) "ticks-tales-saves")