changing to using protocols for interactions, mouse-in?, and overriding cursors.

This commit is contained in:
=
2014-09-10 16:29:55 -07:00
parent 8ce9959409
commit 78e12296de

View File

@@ -9,25 +9,39 @@
[advent.zone :as zone] [advent.zone :as zone]
[advent.screens.dialogue :refer [talking-screen]]) [advent.screens.dialogue :refer [talking-screen]])
(:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter] (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter]
[com.badlogic.gdx.graphics.g2d TextureRegion] )) [com.badlogic.gdx.graphics.g2d TextureRegion]
[java.lang Object]))
(def +screen-width+ 320) (def +screen-width+ 320)
(def +screen-height+ 240) (def +screen-height+ 240)
(def +num-cursors+ 4) (def +num-cursors+ 4)
(defprotocol IMouseIn
(mouse-in? [this location]))
(defprotocol ICursorOverridable
(cursor-override [this]))
(defprotocol IInteractable
(interact [this screen entities cursor location]))
(def default-interaction
(reify
IInteractable
(interact [_ screen entities cursor [x y] ]
(case cursor
:walk (assoc-in entities [:ego :actions] (actions/from-path screen entities :ego [x y]))
:look (assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "Looks pretty uninteresting to me.")])
:touch (assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "Can't do anything with it.")])
:talk (assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "It's not much of a conversationalist.")])))))
(def +next-cursor+ (into {} (map vector [:walk :touch :look :talk] (drop 1 (cycle [:walk :touch :look :talk]))))) (def +next-cursor+ (into {} (map vector [:walk :touch :look :talk] (drop 1 (cycle [:walk :touch :look :talk])))))
(def +all-cursors+ [:walk :touch :look :talk :right :down :left :up]) (def +all-cursors+ [:walk :touch :look :talk :right :down :left :up])
(def +cursor-defaults+ {:walk (fn [screen entities [x y]]
(assoc-in entities [:ego :actions] (actions/from-path screen entities :ego [x y])))
:look (fn [screen entities _]
(assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "Looks pretty uninteresting to me.")]))
:touch (fn [screen entities _]
(assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "Can't do anything with it.")]))
:talk (fn [screen entities _]
(assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego "It's not much of a conversationalist.")]))})
(defn cursor [filename which] (defn cursor [filename which]
(let [scale 2 (let [scale 2
@@ -52,18 +66,20 @@
(filter #((:mouse-in? %) x y)) (filter #((:mouse-in? %) x y))
first first
:go-to)] :go-to)]
(assoc-in entities [:ego :actions] (actions/from-path screen entities :ego target-location)))) ))
(defn left-click [screen entities] (defn left-click [screen entities]
(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)})
interaction (first (filter #((:mouse-in? %) x y) (get-in entities [:background :interactions]))) interaction (first (filter #(mouse-in? % [x y])
(get-in entities [:background :interactions])))
current-cursor (get-in entities [:cursor :current]) current-cursor (get-in entities [:cursor :current])
interaction-fn (when interaction cursor-override (get-in entities [:cursor :override])
(interaction current-cursor))] interacted (or (when cursor-override
(cond (get-in entities [:cursor :override]) (walk-override-click screen entities [x y]) (interact cursor-override screen entities current-cursor [x y]))
(and interaction interaction-fn) (interaction-fn screen entities [x y]) (when interaction
:else (interact interaction screen entities current-cursor [x y]))
((+cursor-defaults+ current-cursor) screen entities [x y])))) (interact default-interaction screen entities current-cursor [x y]))]
interacted))
(defn get-ego [screen] (defn get-ego [screen]
(let [player-sheet (texture! (texture "player.png") :split 18 36) (let [player-sheet (texture! (texture "player.png") :split 18 36)
@@ -102,8 +118,10 @@
:on-show :on-show
(fn [screen entities] (fn [screen entities]
(update! screen :renderer (stage) :camera (orthographic)) (update! screen :renderer (stage) :camera (orthographic))
(let [ (let [_ (input! :set-cursor-image (cursor "cursor.png" :walk) 0 0)
_ (input! :set-cursor-image (cursor "cursor.png" :walk) 0 0) sheep-sheet (texture! (texture "outsidehouse/sheep-anim.png") :split 33 21)
sheep (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-sheet 0 i)))
background (texture "bg5.png") background (texture "bg5.png")
background-trees (texture "background-trees.png") background-trees (texture "background-trees.png")
house (texture "house.png") house (texture "house.png")
@@ -113,34 +131,68 @@
] ]
{ {
:cursor {:id "cursor" :current :walk } :cursor {:id "cursor" :current :walk }
:sheep (assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160 :anim sheep)
:background (assoc {} :background (assoc {}
:collision (advent.pathfind/map-from-resource "pathfind-test-big.png") :collision (advent.pathfind/map-from-resource "pathfind-test-big.png")
:baseline 0 :baseline 0
:mouse-overrides [{:mouse-in? (zone/box 300 131 320 224) :mouse-overrides [(reify
:cursor-override :right IMouseIn
:go-to [319 160]} (mouse-in? [_ location]
{:mouse-in? (zone/box 60 180 224 240) (apply (zone/box 300 131 320 224) location))
:cursor-override :up IInteractable
:go-to [137 204]} (interact [_ screen entities _ location]
{:mouse-in? (zone/box 0 40 20 140) (assoc-in entities [:ego :actions] (actions/from-path screen entities :ego [319 160])))
:cursor-override :left ICursorOverridable
:go-to [0 80]}] (cursor-override [_] :right))
:interactions [{:mouse-in? (zone/box 258 100 281 160) (reify
:look (fn [screen entities _] IMouseIn
(assoc-in entities [:ego :actions] [(actions/stop :ego) (mouse-in? [_ location]
(actions/talk :ego (str "The last time I went through that door, Fangald turned me into a frog."))])) (apply (zone/box 60 180 224 240) location))
:touch (fn [screen entities _] IInteractable
(assoc-in entities [:ego :actions] (concat (interact [_ screen entities _ location]
(actions/from-path screen entities :ego [262 88]) (assoc-in entities [:ego :actions] (actions/from-path screen entities :ego [137 204])))
[(actions/talk :ego (str "Anyone home?"))])))} ICursorOverridable
{:mouse-in? (zone/box 274 55 305 88) (cursor-override [_] :up))
:look (fn [screen entities _] (reify
(assoc-in entities [:ego :actions] [(actions/stop :ego) IMouseIn
(actions/talk :ego (str "It's the coolest sword I've ever seen!"))])) (mouse-in? [_ location]
:touch (fn [screen entities _] (apply (zone/box 0 40 20 140) location))
(assoc-in entities [:ego :actions] (concat IInteractable
(actions/from-path screen entities :ego [290 66]) (interact [_ screen entities _ location]
[(actions/talk :ego (str "Maybe I can pull it out."))])))}] (assoc-in entities [:ego :actions] (actions/from-path screen entities :ego [0 80])))
ICursorOverridable
(cursor-override [_] :left))]
:interactions [(reify
IMouseIn
(mouse-in? [_ location]
(apply (zone/box 258 100 281 160) location))
IInteractable
(interact [_ screen entities cursor _]
(case cursor
:look
(assoc-in entities [:ego :actions]
[(actions/stop :ego)
(actions/talk :ego (str "The last time I went through that door, Fangald turned me into a frog."))])
:touch
(assoc-in entities [:ego :actions] (concat
(actions/from-path screen entities :ego [262 88])
[(actions/talk :ego (str "Anyone home?"))]))
nil)))
(reify
IMouseIn
(mouse-in? [_ location]
(apply (zone/box 274 55 305 88) location))
IInteractable
(interact [_ screen entities cursor _]
(case cursor
:look
(assoc-in entities [:ego :actions] [(actions/stop :ego)
(actions/talk :ego (str "It's the coolest sword I've ever seen!"))])
:touch
(assoc-in entities [:ego :actions] (concat
(actions/from-path screen entities :ego [290 66])
[(actions/talk :ego (str "Maybe I can pull it out."))]))
nil)))]
:scale-fn (scaler-fn-with-baseline 110 0.10 1.00) :scale-fn (scaler-fn-with-baseline 110 0.10 1.00)
:layers [(assoc background :x 0 :y 0 :baseline 0) :layers [(assoc background :x 0 :y 0 :baseline 0)
(assoc house :x 0 :y 0 :baseline 122) (assoc house :x 0 :y 0 :baseline 122)
@@ -154,7 +206,11 @@
(fn [screen [entities]] (fn [screen [entities]]
(clear!) (clear!)
(let [entities (update-ego screen entities (:ego entities)) (let [entities (update-ego screen entities (:ego entities))
_ (label! (:fps entities) :set-text (str (game :fps))) _ (label! (:fps entities) :set-text (str (game :fps)))
entities (if (get-in entities [:sheep :anim])
(update-in entities [:sheep] #(merge % (animation->texture screen (:anim %))))
entities)
entities (if (get-in entities [:ego :anim]) entities (if (get-in entities [:ego :anim])
(update-in entities [:ego] #(merge % (animation->texture screen (:anim %)))) (update-in entities [:ego] #(merge % (animation->texture screen (:anim %))))
entities)] entities)]
@@ -167,14 +223,14 @@
:on-mouse-moved :on-mouse-moved
(fn [screen [entities]] (fn [screen [entities]]
(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 (first (filter #((:mouse-in? %) x y) (get-in entities [:background :mouse-overrides])))] (if-let [mouse-override (first (filter #(mouse-in? % [x y]) (get-in entities [:background :mouse-overrides])))]
(when (not (get-in entities [:cursor :override])) (when (not (get-in entities [:cursor :override]))
(do (input! :set-cursor-image (cursor "cursor.png" (:cursor-override mouse-override)) 0 0) (do (input! :set-cursor-image (cursor "cursor.png" (cursor-override mouse-override)) 0 0)
(assoc-in entities [:cursor :override] true))) (assoc-in entities [:cursor :override] mouse-override)))
(when (get-in entities [:cursor :override]) (when (get-in entities [:cursor :override])
(do (input! :set-cursor-image (cursor "cursor.png" (get-in entities [:cursor :current])) 0 0) (do (input! :set-cursor-image (cursor "cursor.png" (get-in entities [:cursor :current])) 0 0)
(assoc-in entities [:cursor :override] false)))))) (assoc-in entities [:cursor :override] nil))))))
:on-touch-down (fn [screen [entities]] :on-touch-down (fn [screen [entities]]
(if (= (button-code :right) (:button screen)) (if (= (button-code :right) (:button screen))