(ns advent.screens.safe (: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.zone :as zone] [advent.utils :as utils]) (:import [com.badlogic.gdx.graphics Pixmap Pixmap$Filter Texture Texture$TextureFilter] [com.badlogic.gdx.graphics.g2d TextureRegion] [com.badlogic.gdx.scenes.scene2d.utils Align] [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input InputMultiplexer InputProcessor Net Preferences Screen])) (def start-x 60) (def start-y 83) (defn close [entities] (screen! @(resolve 'advent.screens.scene/scene) :on-reactivate) (-> entities (assoc :shown? false) (assoc :start-showing? false))) (defn button-box [x y] (zone/box (+ start-x x) (+ start-y y) (+ start-x x 16) (+ start-y y 16))) (defn get-button [[x y]] (first (filter (fn [{:keys [in?] :as button}] (when (in? x y) button)) [{:in? (button-box 9 17) :sound (sound "inside-house/safe-1.ogg") :id 1 } {:in? (button-box 29 17) :sound (sound "inside-house/safe-2.ogg") :id 2} {:in? (button-box 49 17) :sound (sound "inside-house/safe-3.ogg") :id 3} {:in? (button-box 70 17) :sound (sound "inside-house/safe-4.ogg") :id 4} {:in? (button-box 91 17) :sound (sound "inside-house/safe-5.ogg") :id 5}]))) (defscreen safe-screen :on-show (fn [screen entities] (utils/setup-viewport screen 320 240) (let [inputed-key (texture "inside-house/inputed-key.png")] {:fade (assoc (texture "black.png") :scale-x 80 :scale-y 80 :opacity 0.7 :origin-x 0 :origin-y 0) :safe (assoc (texture "inside-house/safe-screen.png") :x start-x :y start-y) :entered-keys (doall (for [i (range 5)] (assoc inputed-key :x (+ start-x 10 (* i 12)) :y (+ start-y 56)))) :shown? false})) :on-render (fn [screen [entities]] (when (:shown? entities) (render! screen [(:fade entities) (:safe entities)]) (render! screen (take (count (:button-choices entities)) (:entered-keys entities)))) entities) :show-screen (fn [{:keys [success failure]} [entities]] (sound! (sound "inside-house/open-safe.ogg") :play (utils/current-sound-volume)) (assoc entities :shown? true :button-choices [] :success success :failure failure)) :on-mouse-moved (fn [screen [entities]] entities) :on-touch-up (fn [screen [entities]] (when (:shown? entities) (let [[x y] (utils/unproject screen)] (if-let [button (get-button [x y])] (do (sound! (:sound button) :play (utils/current-sound-volume)) (let [new-state (update-in entities [:button-choices] #(conj % (:id button)))] (cond (= [4 2 1 5 3] (:button-choices new-state)) (do ((:success entities) (-> @(resolve 'advent.screens.scene/scene) :entities deref first)) (close entities)) (= 5 (count (:button-choices new-state))) (do ((:failure entities) (-> @(resolve 'advent.screens.scene/scene) :entities deref first)) (close entities)) :else new-state))) (if (or (< x start-x) (> x (+ start-x 200)) (< y start-y) (> y (+ start-y 75))) (do (sound! (sound "inside-house/close-safe.ogg") :play (utils/current-sound-volume)) (close entities))))))) :on-resize (fn [{:keys [width height viewport]} entities] (.update viewport width height)))