pathfinding works.

This commit is contained in:
=
2014-09-02 22:52:29 -07:00
parent 049cc6b059
commit a79651ed04
4 changed files with 98 additions and 12 deletions

View File

@@ -3,7 +3,8 @@
[play-clj.ui :refer :all]
[play-clj.utils :refer :all]
[play-clj.g2d :refer :all]
[clojure.pprint])
[clojure.pprint]
[advent.pathfind])
(:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter]))
(def scale 4)
@@ -32,7 +33,10 @@
(defn left-click [screen entities]
(let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})]
(assoc-in entities [:ego :target] {:x x :y y})))
(assoc-in entities [:ego :target-path] (take-nth 10 (advent.pathfind/visit-all
(:collision (:background entities))
[(int (:x (:ego entities))) (int (:y (:ego entities)))]
[(int x) (int y)])))))
(defn get-ego [screen]
(let [player-sheet (texture! (texture "player.png") :split 18 36)
@@ -42,27 +46,29 @@
(texture (aget player-sheet 1 i))))
:anim (animation 0.075 (for [i (range 8)]
(texture (aget player-sheet 0 i))))
:x 100 :y 75 :x-velocity 1
:x 150 :y 95 :x-velocity 1
:id "ego"}]
(merge (animation->texture screen (:anim ego)) ego)))
(defn update-ego [screen entities ego]
(let [ego (merge ego (animation->texture screen (:anim ego)))
target (:target ego)]
(if target
(let [delta-x (- (:x target) (:x 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 [
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 (* 2 (/ delta-x mag))
vector-y (* 2 (/ delta-y mag))]
vector-x (* 1.5 (/ delta-x mag))
vector-y (* 1.5 (/ delta-y mag))]
(if (< mag 1)
(dissoc ego :target)
(update-in ego [:target-path] rest)
(-> ego
(update-in [:x] #(+ % vector-x))
(update-in [:y] #(+ % vector-y)))))
ego)))
(dissoc ego :target-path))))
(defscreen main-screen
:on-show
(fn [screen entities]
@@ -71,7 +77,7 @@
_ (input! :set-cursor-image (cursor "cursor.png" 0) 0 0)
background (texture "bg5.png")]
{:cursor {:id "cursor" :cursor-index 0}
:background (assoc background :origin-y 0 :origin-x 0 :id "background" :x 0 :y 0 :scale-x 1 :scale-y 1)
:background (assoc background :origin-y 0 :origin-x 0 :id "background" :x 0 :y 0 :scale-x 1 :scale-y 1 :collision (advent.pathfind/map-from-resource "pathfind-test-big.png"))
:ego (get-ego screen)}))
:on-render