refactoring - better or worse?

This commit is contained in:
=
2014-09-04 22:52:33 -07:00
parent 1522372dda
commit cc57ab54d4

View File

@@ -31,57 +31,68 @@
(input! :set-cursor-image (cursor "cursor.png" (get-in entities [:cursor :cursor-index])) 0 0)
entities))
(defn move-to [screen entities entity [x y]]
(let [scale-fn (-> entities :background :scale-fn)
entity (assoc entity :x x
:y y
:baseline (- 240 y))]
(if (:scaled entity)
(assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y))
entity)))
(defn walk-to-fn [[x y] target-id]
(fn [screen entities]
(let [{:keys [left right anim] :as target-entity} (entities target-id)
target-loc {:x x :y y}]
(let [delta-x (- x (:x target-entity))
delta-y (- y (:y target-entity))
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))
vector-x (* 1.5 (/ delta-x mag))
vector-y (* 1.5 (/ delta-y mag))]
(if (< mag 1)
(assoc entities target-id (assoc target-entity :actions (rest (:actions target-entity)) :anim nil))
(assoc entities target-id
(assoc (move-to screen entities target-entity [(+ vector-x (:x target-entity)) (+ vector-y (:y target-entity))])
:anim (if (< vector-x 0) left right))))))))
(defn stop-fn [target-id]
(fn [screen entities]
(let [target (target-id entities)]
(assoc-in entities [target-id] (merge target
{:anim nil}
(when (:anim target)
(texture (animation! (:anim target) :get-key-frame 0.25))))))))
(defn left-click [screen entities]
(let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})
path (vec (take-nth 2 (advent.pathfind/visit-all
(:collision (:background entities))
[(int (:x (:ego entities))) (int (:y (:ego entities)))]
[(int x) (int y)])))]
(assoc-in entities [:ego :target-path] (when (seq path) (conj path [x y])))))
(assoc-in entities [:ego :actions] (when (seq path)
(concat
(vec (map #(walk-to-fn % :ego) (conj path [x y])))
[(stop-fn :ego)])))))
(defn get-ego [screen]
(let [player-sheet (texture! (texture "player.png") :split 18 36)
ego {:right (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 0 i))))
:left (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 1 i))))
:anim (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 0 i))))
:left (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 1 i))))
:baseline 95
:origin-x 9
:origin-y 0
:x 150 :y 95 :x-velocity 1
:scaled true
:actions []
:x 150 :y 95
:id "ego"}]
(merge (animation->texture screen (:anim ego)) ego)))
(merge (texture (animation! (:right ego) :get-key-frame 0.25)) ego)))
(defn update-ego [screen entities ego]
(let [ego (merge ego (animation->texture screen (:anim ego)))
target-path (:target-path ego)
[target-x target-y] (first target-path)
target {:x target-x :y target-y}]
(if (and target (seq target-path))
(let [scale-fn (-> entities :background :scale-fn)
delta-x (- (:x target) (:x ego))
delta-y (- (:y target) (:y ego))
mag (Math/sqrt (+ (* delta-x delta-x) (* delta-y delta-y)))
vector-x (* 1.5 (/ delta-x mag))
vector-y (* 1.5 (/ delta-y mag))]
(if (< mag 1)
(update-in ego [:target-path] rest)
(do
(merge (as-> ego _
(update-in _ [:x] #(+ % vector-x))
(update-in _ [:y] #(+ % vector-y))
(assoc-in _ [:baseline] (- 240 (:y _)))
(assoc-in _ [:anim] (if (< vector-x 0)
(:left _)
(:right _)))
(assoc-in _ [:scale-x] (scale-fn (:y _)))
(assoc-in _ [:scale-y] (scale-fn (:y _))))
(animation->texture screen (:anim ego))))))
(merge (dissoc ego :target-path) (texture (animation! (:anim ego) :get-key-frame 0.25))))))
(if-let [action (first (:actions ego))]
(:ego (action screen entities))
ego))
(defn scaler-fn-with-baseline [baseline minimum-size & [maximum-size]]
(let [maximum-size (or maximum-size 1.0)]
@@ -122,7 +133,7 @@
:cursor-override 7}
{:mouse-in? (box-maker-fn 0 40 30 140)
:cursor-override 6}]
:scale-fn (scaler-fn-with-baseline 110 0.10 1.10))
:scale-fn (scaler-fn-with-baseline 110 0.10 1.00))
:house (assoc house
:x 0 :y 0
:baseline 122)
@@ -140,9 +151,13 @@
(fn [screen [entities]]
(clear!)
(let [entities (assoc entities :ego (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 [:ego :anim])
(update-in entities [:ego] #(merge % (animation->texture screen (:anim %))))
entities)]
(render! screen (sort-by :baseline (vals entities)))
entities))
:on-resize (fn [screen entities]
(size! screen 320 240))
@@ -156,9 +171,7 @@
(when (get-in entities [:cursor :override])
(do (input! :set-cursor-image (cursor "cursor.png" (get-in entities [:cursor :cursor-index])) 0 0)
(assoc-in entities [:cursor :override] false))))
))
(assoc-in entities [:cursor :override] false))))))
:on-touch-down (fn [screen [entities]]
(if (= (button-code :right) (:button screen))