From 54b1f1497a9b9fe66576c44eecd2ceb41da2b106 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sat, 22 Nov 2014 13:00:40 -0800 Subject: [PATCH] first attempt at saving/loading. --- desktop/src-common/advent/actions.clj | 3 +- desktop/src-common/advent/screens/scene.clj | 56 ++++++++++++--------- desktop/src-common/advent/utils.clj | 8 ++- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index 711b9a1f..949b2284 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -33,7 +33,8 @@ (defmacro get-script [entities & forms] `(fn [starting-entities#] (let [~entities (atom starting-entities#)] - (thread ~@forms)))) + (thread ~@forms + (utils/save @~entities))))) (defn jump-to [screen entities entity [x y] update-baseline?] diff --git a/desktop/src-common/advent/screens/scene.clj b/desktop/src-common/advent/screens/scene.clj index 8eadac6a..c3558f2d 100644 --- a/desktop/src-common/advent/screens/scene.clj +++ b/desktop/src-common/advent/screens/scene.clj @@ -6,6 +6,7 @@ [play-clj.g2d :refer :all] [clojure.zip :as zip] [clojure.pprint] + [clojure.java.io :as io] [advent.pathfind] [advent.actions :as actions] [advent.zone :as zone] @@ -204,6 +205,14 @@ (defn make-music [r] (doto (music r) (music! :set-looping true))) +(defn get-state [] + (if (.exists (io/file "save.edn")) + (utils/load) + {:object nil + :active? true + :inventory [items/grass items/carrot] + :clues #{} + :mints-eaten 0})) (defscreen scene :on-show (fn [screen entities] @@ -217,31 +226,28 @@ :inside-castle (rooms.inside-castle/make screen) :inside-cafeteria (rooms.inside-cafeteria/make screen) :inside-antique (rooms.inside-antique/make screen) - :outside-castle (rooms.outside-castle/make screen)}] - {:rooms rooms - :musics {:object nil - :inside-antique (make-music "inside-antique.ogg") - :town-1 (make-music "town-music-1.ogg") - :town-2 (doto (make-music "town-music-2.ogg") play-sound) - :inside-fangald (make-music "inside-fangald.ogg")} - :state {:object nil - :active? true - :inventory [items/grass items/carrot] - :clues #{} - :mints-eaten 0} - :actions {:object nil - :channel (chan) - :current nil - :started? false} - :cursor {:id "cursor" - :current :main - :last :main - :override nil} - :room (assoc-in (:outside-house rooms) - [:entities :ego] (get-ego screen)) - :inventory (assoc (texture "inventory.png") :x 278 :y 0 :baseline 9000 - :mouse-in? (zone/box 278 0 320 42)) - :fps (assoc (label "0" (color :white) ) :x 5 :baseline 0)}))) + :outside-castle (rooms.outside-castle/make screen)} + entities {:rooms rooms + :musics {:object nil + :inside-antique (make-music "inside-antique.ogg") + :town-1 (make-music "town-music-1.ogg") + :town-2 (doto (make-music "town-music-2.ogg") play-sound) + :inside-fangald (make-music "inside-fangald.ogg")} + :state (get-state) + :actions {:object nil + :channel (chan) + :current nil + :started? false} + :cursor {:id "cursor" + :current :main + :last :main + :override nil} + :room (assoc-in (:outside-house rooms) + [:entities :ego] (get-ego screen)) + :inventory (assoc (texture "inventory.png") :x 278 :y 0 :baseline 9000 + :mouse-in? (zone/box 278 0 320 42)) + :fps (assoc (label "0" (color :white) ) :x 5 :baseline 0)}] + ((get-in entities [:room :apply-state]) entities)))) :on-render (fn [screen [entities]] diff --git a/desktop/src-common/advent/utils.clj b/desktop/src-common/advent/utils.clj index 7b4488e1..4254e56e 100644 --- a/desktop/src-common/advent/utils.clj +++ b/desktop/src-common/advent/utils.clj @@ -2,7 +2,8 @@ (:require [play-clj.core :refer :all] [play-clj.ui :refer :all] [play-clj.utils :refer :all] - [play-clj.g2d :refer :all]) + [play-clj.g2d :refer :all] + [clojure.edn :as edn]) (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter] [com.badlogic.gdx.graphics.g2d TextureRegion] [java.lang Object])) @@ -25,6 +26,11 @@ 0 0 target-width target-height) resized )) +(defn save [entities] + (spit "save.edn" (entities :state))) + +(defn load [] + (edn/read-string (slurp "save.edn"))) (defn get-font [filename] (let [font (bitmap-font filename)