handles moving entities, needs a lot of refactoring.

This commit is contained in:
2014-10-03 16:19:01 -07:00
parent a21d0cb6f3
commit f2d915a3f0

View File

@@ -2,6 +2,7 @@
(:require [play-clj.core :refer :all] (:require [play-clj.core :refer :all]
[play-clj.ui :refer :all] [play-clj.ui :refer :all]
[play-clj.utils :refer :all] [play-clj.utils :refer :all]
[play-clj.entities :as entities]
[play-clj.g2d :refer :all] [play-clj.g2d :refer :all]
[clojure.zip :as zip] [clojure.zip :as zip]
[clojure.pprint] [clojure.pprint]
@@ -20,29 +21,18 @@
(def +screen-width+ 320) (def +screen-width+ 320)
(def +screen-height+ 240) (def +screen-height+ 240)
(defprotocol IMouseIn
(mouse-in? [this location]))
(defprotocol ICursorOverridable
(cursor-override [this]))
(defprotocol IInteractable
(get-script [this cursor location]))
(def default-interaction (def default-interaction
(reify {:get-script (fn [cursor [x y]] (if (= :main cursor)
IInteractable (actions/get-script entities
(get-script [_ cursor [x y]] (actions/walk-to entities :ego [x y] true))
(if (= :main cursor) (actions/get-script entities
(actions/get-script entities (actions/talk entities :ego "I don't know what to do with that."))))})
(actions/walk-to entities :ego [x y] true))
(actions/get-script entities
(actions/talk entities :ego "I don't know what to do with that."))))))
(defn find-override [screen entities [x y]] (defn find-override [screen entities [x y]]
(first (filter #(and (mouse-in? % [x y]) (first (filter #(and ((:mouse-in? %) entities x y)
(satisfies? ICursorOverridable %)) (:cursor %))
(get-in entities [:background :interactions])))) (get-in entities [:background :interactions]))))
(defn open-inventory [screen entities] (defn open-inventory [screen entities]
@@ -55,8 +45,12 @@
(if ((:mouse-in? (:inventory entities)) x y) (if ((:mouse-in? (:inventory entities)) x y)
(open-inventory screen entities) (open-inventory screen entities)
(let [interaction (first (filter #(mouse-in? % [x y]) (let [interaction (first (filter #((:mouse-in? %) entities x y)
(get-in entities [:background :interactions]))) (get-in entities [:background :interactions])))
interacting-entity (first (filter #(and (:mouse-in? %)
(:get-script %)
((:mouse-in? %) entities x y))
(vals (get-in entities [:background :entities]))))
current-action (get-in entities [:actions :current]) current-action (get-in entities [:actions :current])
;; TODO - hacky way of resetting queue ;; TODO - hacky way of resetting queue
@@ -71,8 +65,10 @@
(if current-action (if current-action
entities entities
((or (when interaction ((or (when interaction
(get-script interaction (get-in entities [:cursor :current]) [x y])) ((:get-script interaction) (get-in entities [:cursor :current]) [x y]))
(get-script default-interaction (get-in entities [:cursor :current]) [x y])) entities)) (when interacting-entity
((:get-script interacting-entity) (get-in entities [:cursor :current]) [x y]))
((:get-script default-interaction) (get-in entities [:cursor :current]) [x y])) entities))
entities)))) entities))))
(defn flip [anim] (defn flip [anim]
@@ -141,22 +137,29 @@
(input! :set-cursor-image (utils/cursor "cursor.png" (or override current)) 0 0)) (input! :set-cursor-image (utils/cursor "cursor.png" (or override current)) 0 0))
(assoc-in entities [:cursor :last] (or override current))) (assoc-in entities [:cursor :last] (or override current)))
(defn make-background [& {:keys [collision interactions] :as params}] (defn make-background [& {:keys [collision interactions entities] :as params}]
(let [interactions-as-list (for [[id spec] interactions] (let [interactions-as-list (for [[id spec] interactions]
(reify (merge spec {:mouse-in? (fn [_ x y]
IMouseIn ((apply zone/box (:box spec)) x y))
(mouse-in? [_ location] :get-script (fn [cursor [x y]]
(apply (apply zone/box (:box spec)) location)) (if (= :main cursor)
IInteractable (:script spec)
(get-script [_ cursor location] (get-in spec [:scripts cursor])))}))
(if (= :main cursor) entities (into {} (for [[id entity] entities]
(:script spec) [id (merge entity
(get-in spec [:scripts cursor]))) {:mouse-in? (fn [entities x y]
ICursorOverridable (let [{entity-x :x entity-y :y region :object} (get-in entities [:background :entities id])
(cursor-override [this] (:cursor spec))) width (.getRegionWidth region)
)] height (.getRegionHeight region)]
((zone/box entity-x entity-y (+ entity-x width) (+ entity-y height)) x y)))}
(when (:script entity)
{:get-script (fn [cursor [x y]]
(if (= :main cursor)
(:script entity)
(get-in entity [:scripts cursor])))}))]))]
(merge params {:collision (advent.pathfind/map-from-resource collision) (merge params {:collision (advent.pathfind/map-from-resource collision)
:interactions interactions-as-list}))) :interactions interactions-as-list
:entities entities})))
(defn do-dialogue [entities & pairs] (defn do-dialogue [entities & pairs]
@@ -314,19 +317,6 @@
(actions/talk entities :ego (str "It's the coolest sword I've ever seen!")) (actions/talk entities :ego (str "It's the coolest sword I've ever seen!"))
(actions/walk-to entities :ego [290 66]) (actions/walk-to entities :ego [290 66])
(actions/talk entities :ego "Maybe I can pull it out."))} (actions/talk entities :ego "Maybe I can pull it out."))}
:sheep {:box [38 160 71 181]
:script (actions/get-script
entities
(if ((get-in @entities [:state :inventory]) :wool)
(actions/talk entities :ego "The sheep has given me enough wool.")
(do (actions/give entities :wool)
(actions/talk entities :ego "I guess her wool is shedding."))))
:scripts {:wool (actions/get-script entities
(actions/talk entities :ego "She doesn't need it back."))
:carrot (actions/get-script entities
(actions/walk-to entities :ego [132 140])
(actions/talk entities :ego "Come on girl, get the carrot!")
(actions/walk-straight-to entities :sheep [100 150]))}}
:right-dir {:box [300 131 320 224] :right-dir {:box [300 131 320 224]
:script (actions/get-script :script (actions/get-script
entities entities
@@ -351,7 +341,20 @@
(assoc (texture "overdirt.png") :x 0 :y 0 :baseline 240) (assoc (texture "overdirt.png") :x 0 :y 0 :baseline 240)
(assoc (texture "background-trees.png") :x 0 :y 0 :baseline 44)] (assoc (texture "background-trees.png") :x 0 :y 0 :baseline 44)]
:entities {:sheep (actions/start-animation screen :entities {:sheep (actions/start-animation screen
(assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160) (assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160
:box [38 160 71 181]
:script (actions/get-script
entities
(if ((get-in @entities [:state :inventory]) :wool)
(actions/talk entities :ego "The sheep has given me enough wool.")
(do (actions/give entities :wool)
(actions/talk entities :ego "I guess her wool is shedding."))))
:scripts {:wool (actions/get-script entities
(actions/talk entities :ego "She doesn't need it back."))
:carrot (actions/get-script entities
(actions/walk-to entities :ego [132 140])
(actions/talk entities :ego "Come on girl, get the carrot!")
(actions/walk-straight-to entities :sheep [100 150]))})
sheep)} sheep)}
:collision "outsidehouse/collision.png" :collision "outsidehouse/collision.png"
:scale-fn (scaler-fn-with-baseline 110 0.10 1.00)) :scale-fn (scaler-fn-with-baseline 110 0.10 1.00))
@@ -504,7 +507,7 @@
(when (get-in entities [:state :active?]) (when (get-in entities [:state :active?])
(let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})] (let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})]
(if-let [mouse-override (find-override screen entities [x y])] (if-let [mouse-override (find-override screen entities [x y])]
(assoc-in entities [:cursor :override] (cursor-override mouse-override)) (assoc-in entities [:cursor :override] (:cursor mouse-override))
(assoc-in entities [:cursor :override] nil))))) (assoc-in entities [:cursor :override] nil)))))
:on-touch-down (fn [screen [entities]] :on-touch-down (fn [screen [entities]]