Files
gitea-docker/desktop/src-common/advent/core.clj
2014-09-03 12:49:01 -07:00

135 lines
5.7 KiB
Clojure

(ns advent.core
(:require [play-clj.core :refer :all]
[play-clj.ui :refer :all]
[play-clj.utils :refer :all]
[play-clj.g2d :refer :all]
[clojure.pprint]
[advent.pathfind])
(:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter]))
(def scale 4)
(def +screen-width+ 320)
(def +screen-height+ 240)
(def +num-cursors+ 4)
(def +next-cursor+ (into {} (map vector (range +num-cursors+) (drop 1 (cycle (range +num-cursors+))))))
(defn +next-cursor+ [x]
(first (drop 1 (drop-while #(not= x %) (cycle (range +num-cursors+))))))
(defn cursor [filename index]
(let [base-cursor (pixmap filename)
target-width (* 16 scale)
target-height (* 16 scale)
resized (Pixmap. target-width target-height (.getFormat base-cursor))]
(Pixmap/setFilter Pixmap$Filter/NearestNeighbour)
(pixmap! resized :draw-pixmap base-cursor (* index 16) 0 16 16
0 0 target-width target-height)
resized ))
(defn right-click [screen entities]
(let [entities (update-in entities [:cursor] #(assoc % :cursor-index (+next-cursor+ (:cursor-index %)) :hotspot [63 63]))]
(input! :set-cursor-image (cursor "cursor.png" (get-in entities [:cursor :cursor-index])) 0 0)
entities))
(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])))))
(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))))
:baseline 95
:origin-x 16
:x 150 :y 95 :x-velocity 1
:character-x 158 :character-y 95
: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-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) (:character-x ego))
delta-y (- (:y target) (:character-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)
(merge (as-> ego _
(update-in _ [:x] #(+ % vector-x))
(update-in _ [:y] #(+ % vector-y))
(assoc-in _ [:character-x] (+ 8 (:x _)))
(assoc-in _ [:character-y] (:y _))
(assoc-in _ [:baseline] (- 240 (:y _)))
(assoc-in _ [:anim] (if (< vector-x 0)
(:left _)
(:right _)))
(assoc-in _ [:scale-x] (/ 1 (/ 240 (- 310 (:y _)))))
(assoc-in _ [:scale-y] (/ 1 (/ 240 (- 310 (:y _))))))
(animation->texture screen (:anim ego)))))
(merge (dissoc ego :target-path) (texture (animation! (:anim ego) :get-key-frame 0.6))))))
(defscreen main-screen
:on-show
(fn [screen entities]
(update! screen :renderer (stage) :camera (orthographic))
(let [
_ (input! :set-cursor-image (cursor "cursor.png" 0) 0 0)
background (texture "bg5.png")
house (texture "house.png")
overdirt (texture "overdirt.png")
music (sound "outside-house.mp3")
_ (sound! music :loop)]
{
:cursor {:id "cursor" :cursor-index 0 :hotspot [63 63]}
:background (assoc background
:id "background" :x 0 :y 0
:collision (advent.pathfind/map-from-resource "pathfind-test-big.png")
:baseline 0)
:house (assoc house
:x 0 :y 0
:baseline 122)
:overdirt (assoc overdirt
:x 0 :y 0
:baseline 240)
:ego (get-ego screen)
:fps (assoc (label "0" (color :white) ) :x 5 :baseline 9000)}))
:on-render
(fn [screen [entities]]
(clear!)
(let [entities (assoc entities :ego (update-ego screen entities (:ego entities)))
_ (label! (:fps entities) :set-text (str (game :fps)))]
(render! screen (sort-by :baseline (vals entities)))
entities))
:on-resize (fn [screen entities]
(size! screen 320 240))
:on-touch-down (fn [screen [entities]]
(let [screen (-> screen
(update-in [:input-x] #(+ % (first (:hotspot (:cursor entities)))))
(update-in [:input-y] #(+ % (second (:hotspot (:cursor entities))))))]
(if (= (button-code :right) (:button screen))
(right-click screen entities)
(left-click screen entities)))))
(defgame advent
:on-create
(fn [this]
(set-screen! this main-screen)
#_(input! :set-cursor-catched true)))