(ns advent.screens.scene (: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] [advent.actions :as actions] [advent.zone :as zone] [advent.utils :as utils] [advent.screens.dialogue :refer [talking-screen]] [clojure.core.async :refer [put! ! chan go thread take! alts!!]]) (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter GL20 GL30] [com.badlogic.gdx.graphics.g2d TextureRegion] [java.lang Object] [com.badlogic.gdx Gdx])) (def +screen-width+ 320) (def +screen-height+ 240) (defprotocol IMouseIn (mouse-in? [this location])) (defprotocol ICursorOverridable (cursor-override [this])) (defprotocol IInteractable (get-script [this location])) (def default-interaction (reify IInteractable (get-script [_ [x y]] (actions/get-script entities (actions/walk-to entities :ego [x y]))))) (defn find-override [screen entities [x y]] (first (filter #(and (mouse-in? % [x y]) (satisfies? ICursorOverridable %)) (get-in entities [:background :interactions])))) (defn left-click [screen entities] (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]))) ;; TODO - hacky way of resetting queue entities (if-let [current-action (get-in entities [:actions :current])] (assoc (actions/terminate current-action screen entities) :actions {:channel (chan) :current nil :started? false}) entities) script (or (when interaction (get-script interaction [x y])) (get-script default-interaction [x y]))] (script entities) entities)) (defn get-ego [screen] (let [player-sheet (texture! (texture "player.png") :split 18 36) talk-sheet (texture! (texture "ego/talk.png") :split 16 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)))) :talk (animation 0.2 (for [i (range 8)] (texture (aget talk-sheet 0 i)))) :baseline 95 :origin-x 9 :origin-y 0 :scaled true :inventory #{} :x 150 :y 95 :id "ego"}] (merge (texture (animation! (:right ego) :get-key-frame 0.25)) ego))) (defn update-from-script [screen {{:keys [current started? channel]} :actions :as entities}] (if current (let [entities (if started? entities (actions/begin current screen entities)) entities (actions/continue current screen entities)] (if (actions/done? current screen entities) (let [terminated (actions/terminate current screen entities)] (put! (actions/get-channel current) terminated) (recur screen (assoc terminated :actions {:channel channel :current nil :started? false}))) (assoc-in entities [:actions :started?] true))) (let [[current _] (alts!! [channel] :default nil)] (assoc entities :actions {:channel channel :current current :started? false})))) (defn scaler-fn-with-baseline [baseline minimum-size & [maximum-size]] (let [maximum-size (or maximum-size 1.0)] (fn [y] (if (< y baseline) maximum-size (let [percent-complete (- 1.0 (/ (- y baseline) (- +screen-height+ baseline))) range (+ (* percent-complete (- maximum-size minimum-size)) minimum-size)] range))))) (defn update-cursor [screen {{:keys [current override last]} :cursor :as entities}] (when-not (= (or override current) last) (input! :set-cursor-image (utils/cursor "cursor.png" (or override current)) 0 0)) (assoc-in entities [:cursor :last] (or override current))) (defn make-background [& {:keys [collision interactions] :as params}] (let [interactions-as-list (for [[id spec] interactions] (reify IMouseIn (mouse-in? [_ location] (apply (apply zone/box (:box spec)) location)) IInteractable (get-script [_ location] (:script spec)) ICursorOverridable (cursor-override [this] (:cursor spec))) )] (merge params {:collision (advent.pathfind/map-from-resource collision) :interactions interactions-as-list}))) (defn backgrounds [screen] (let [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)))] {:outside-house (make-background :interactions {:door {:box [258 100 281 160] :script (actions/get-script entities (actions/walk-to entities :ego [262 88]) (actions/talk entities :ego (str "Anyone home?")))} :sword {:box [274 55 305 88] :script (actions/get-script entities (actions/talk entities :ego (str "It's the coolest sword I've ever seen!")) (actions/walk-to entities :ego [290 66]) (actions/talk entities :ego "Maybe I can pull it out."))} :sheep {:box [38 160 71 181] :script (actions/get-script entities (if ((get-in @entities [:ego :inventory]) :wool) (actions/talk entities :ego "The sheep has given me enough wool.") (do (actions/give entities :ego :wool) (actions/talk entities :ego "I guess his wool is shedding."))))} :right-dir {:box [300 131 320 224] :script (actions/get-script entities (actions/walk-to entities :ego [319 160])) :cursor :right} :up-dir {:box [60 180 224 240] :script (actions/get-script entities (actions/walk-to entities :ego [137 204])) :cursor :up} :left-dir {:box [0 40 20 140] :script (actions/get-script entities (actions/walk-to entities :ego [0 80]) (actions/transition-background entities :behind-house [310 80])) :cursor :left}} :layers [(assoc (texture "bg5.png") :x 0 :y 0 :baseline 0) (assoc (texture "house.png") :x 0 :y 0 :baseline 122) (assoc (texture "overdirt.png") :x 0 :y 0 :baseline 240) (assoc (texture "background-trees.png") :x 0 :y 0 :baseline 44)] :entities [(assoc (animation->texture screen sheep) :x 38 :y 160 :baseline 160 :anim sheep)] :collision "outsidehouse/collision.png" :scale-fn (scaler-fn-with-baseline 110 0.10 1.00)) :behind-house (make-background :interactions {:right-dir {:box [300 40 320 140] :script (actions/get-script entities (actions/walk-to entities :ego [310 80]) (actions/transition-background entities :outside-house [0 80])) :cursor :right} :peddler {:box [110 90 128 146] :script (actions/get-script entities (actions/walk-to entities :ego [191 90]) (actions/talk entities :ego "Hello there."))}} :layers [(assoc (texture "outside-castle/background.png") :x 0 :y 0 :baseline 0)] :entities [(assoc (texture "outside-castle/peddler.png") :x 110 :y 90 :baseline 150)] :collision "outside-castle/collision.png" :scale-fn (scaler-fn-with-baseline 110 0.10 1.00))})) (defn animate [entity screen] (if (:anim entity) (merge entity (animation->texture screen (:anim entity))) entity) ) (defscreen scene :on-show (fn [screen entities] (update! screen :renderer (stage) :camera (orthographic)) (let [_ (input! :set-cursor-image (utils/cursor "cursor.png" :main) 0 0) music (sound "town-music.mp3") ;; _ (sound! music :loop 0.20) backgrounds (backgrounds screen)] {:backgrounds backgrounds :actions {:object nil :channel (chan) :current nil :started? false} :cursor {:id "cursor" :current :main :last :main :override nil} :background (:outside-house backgrounds) :ego (get-ego screen) :fps (assoc (label "0" (color :white) ) :x 5 :baseline 9000) })) :on-render (fn [screen [entities]] (clear!) (.glEnable (.getGL20 Gdx/graphics) GL20/GL_BLEND) #_(.glEnable (.getGL30 Gdx/graphics) GL30/GL_BLEND) (let [entities (update-cursor screen entities) entities (update-from-script screen entities) entities (update-in entities [:background :entities] (fn [entities] (map #(animate % screen) entities))) entities (update-in entities [:ego] #(animate % screen)) all-entities (concat (vals entities) (get-in entities [:background :layers]) (get-in entities [:background :entities]))] (label! (:fps entities) :set-text (str (game :fps))) (render! screen (sort-by :baseline all-entities)) entities)) :on-resize (fn [screen entities] (size! screen 320 240)) :on-mouse-moved (fn [screen [entities]] (let [{:keys [x y]} (input->screen screen {:x (:input-x screen) :y (:input-y screen)})] (if-let [mouse-override (find-override screen entities [x y])] (assoc-in entities [:cursor :override] (cursor-override mouse-override)) (assoc-in entities [:cursor :override] nil)))) :on-touch-down (fn [screen [entities]] (if (= (button-code :right) (:button screen)) (assoc-in entities [:cursor :current] :main) (left-click screen entities))))