animations don't start in the middle.

This commit is contained in:
2014-09-18 17:14:59 -07:00
parent 40a4e54c80
commit 875c9a6af8
2 changed files with 25 additions and 14 deletions

View File

@@ -34,11 +34,18 @@
(assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y)) (assoc entity :scale-x (scale-fn y) :scale-y (scale-fn y))
entity))) entity)))
(defn start-animation [screen entity anim]
(let [new-anim (if (keyword? anim)
(anim entity)
anim)]
(if (and anim (not= new-anim (:anim entity)))
(assoc entity
:anim new-anim
:anim-start (:total-time screen))
entity)))
(defn stop [screen entities target-id] (defn stop [screen entities target-id]
(update-in entities [:background :entities target-id] #(merge % (update-in entities [:background :entities target-id] #(start-animation screen % :stand)))
{:anim nil}
(when (:anim %)
(texture (animation! (:anim %) :get-key-frame 0.25))))))
(defn dist [x1 y1 x2 y2] (defn dist [x1 y1 x2 y2]
@@ -88,10 +95,11 @@
(do (swap! targets-left rest) (do (swap! targets-left rest)
entities) entities)
(update-in entities [:background :entities target-id] (update-in entities [:background :entities target-id]
#(assoc (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)]) #(start-animation screen
:anim (cond (< delta-x 0) left (jump-to screen entities % [(+ moved-x from-x) (+ moved-y from-y)])
(> delta-x 0) right (cond (< delta-x 0) :left
:else (:anim %)))))))) (> delta-x 0) :right
:else nil)))))))
(done? [this screen entities] (done? [this screen entities]
(let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (get-in entities [:background :entities target-id])] (let [{from-x :x from-y :y :keys [left right anim] :as target-entity} (get-in entities [:background :entities target-id])]
@@ -117,7 +125,7 @@
:x (get-in entities [:background :entities target-id :x]) :y (+ (get-in entities [:background :entities target-id :y]) height) :x (get-in entities [:background :entities target-id :x]) :y (+ (get-in entities [:background :entities target-id :y]) height)
:target-id target-id :target-id target-id
:scale scale) :scale scale)
(assoc-in entities [:background :entities target-id :anim] (get-in entities [:background :entities target-id :talk])))) (update-in entities [:background :entities target-id ] #(start-animation screen % :talk))))
(continue [this screen entities] entities) (continue [this screen entities] entities)

View File

@@ -59,12 +59,15 @@
(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)
talk-sheet (texture! (texture "ego/talk.png") :split 16 36) talk-sheet (texture! (texture "ego/talk.png") :split 16 36)
stand-sheet (texture! (texture "ego/stand.png") :split 18 36)
ego {:right (animation 0.075 (for [i (range 8)] ego {:right (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 0 i)))) (texture (aget player-sheet 0 i))))
:left (animation 0.075 (for [i (range 8)] :left (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 1 i)))) (texture (aget player-sheet 1 i))))
:talk (animation 0.2 (for [i (range 8)] :talk (animation 0.2 (for [i (range 8)]
(texture (aget talk-sheet 0 i)))) (texture (aget talk-sheet 0 i))))
:stand (animation 0.1 (for [i (flatten [(repeat 6 [(repeat 10 0) (repeat 3 1) (repeat 20 0)]) 3 4 5 5 5 6 5 6 5 6 5 4 3 ])]
(texture (aget stand-sheet 0 i))))
:baseline 95 :baseline 95
:origin-x 9 :origin-x 9
@@ -165,7 +168,9 @@
(assoc (texture "house.png") :x 0 :y 0 :baseline 122) (assoc (texture "house.png") :x 0 :y 0 :baseline 122)
(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 (assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160 :anim sheep)} :entities {:sheep (actions/start-animation screen
(assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160)
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))
@@ -196,10 +201,8 @@
:scale-fn (scaler-fn-with-baseline 110 0.10 1.00))})) :scale-fn (scaler-fn-with-baseline 110 0.10 1.00))}))
(defn animate [entity screen] (defn animate [entity screen]
(if (:anim entity) (merge entity (animation->texture (update-in screen [:total-time] #(- % (:anim-start entity)))
(merge entity (animation->texture screen (:anim entity))) (:anim entity))))
entity)
)
(defscreen scene (defscreen scene
:on-show :on-show
(fn [screen entities] (fn [screen entities]