Files
gitea-docker/desktop/src-common/advent/screens/inventory.clj

123 lines
6.3 KiB
Clojure

(ns advent.screens.inventory
(: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.tween :as tween]
[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]))
(defn close [screen entities]
(screen! @(resolve 'advent.screens.scene/scene) :on-reactivate)
(-> entities
(assoc-in [:tweens :fade-out] (tween/tween :fade-out screen [:opacity] 1.0 0.0 0.2 :ease tween/ease-out-cubic
:finish #(assoc % :shown? false)))))
(defscreen inventory-screen
:on-show
(fn [screen entities]
(utils/setup-viewport screen 1280 960)
(let [highlighted-text (assoc (label "Hello" (style :label (utils/get-font "ego/font.fnt") (color :white))) :x 0 :y 850 :width 1280 )]
(label! highlighted-text :set-alignment Align/center)
{:overlay (assoc (texture "inventory-overlay.png" ) :x 0 :y 0 :scale-x 4 :scale-y 4 :origin-x 0 :origin-y 0 :opacity 0.0)
:fade (assoc (texture "black.png")
:scale-x 80
:scale-y 80
:opacity 0.7
:origin-x 0
:origin-y 0)
:all-items (texture! (texture (pixmap "cursor.png")) :split 16 16)
:items []
:shown? false
:highlighted-item nil
:opacity 0.0
:tweens {}
:highlighted-text highlighted-text}))
:on-render
(fn [screen [entities]]
(let [
entities (utils/apply-tweens screen entities (:tweens entities))
opacity (get-in entities [:opacity])
entities (-> entities
(assoc-in [:overlay :opacity] opacity)
(assoc-in [:fade :opacity] (* 0.6 opacity))
(assoc-in [:highlighted-text :opacity] opacity)
(update-in [:items] (fn [i]
(map #(assoc % :opacity opacity) i))))]
(when (:shown? entities)
(doto (:highlighted-text entities) (label! :set-color (color 1 1 1 opacity)))
(render! screen [(:fade entities) (:overlay entities)])
(render! screen (:items entities))
(if-let [item (:highlighted-item entities)]
(label! (:highlighted-text entities) :set-text (:name item))
(label! (:highlighted-text entities) :set-text ""))
(render! screen [(:highlighted-text entities)]))
entities))
:show-screen (fn [{items :items :as screen} [entities]]
(if (:shown? entities)
entities
(-> entities
(assoc :shown? true
:opacity 0.0
:items (for [[item index] (map vector items (range))
:let [row (int (/ index 8))
column (mod index 8)
base-x (* 79 4)
base-y (* 180 4)
x (+ base-x (* column (* 24 4)))
y (- base-y (* row (* 24 4)))
item-width 16
offset-x (+ x (/ item-width 2))
offset-y (+ y (/ item-width 2))
padding (/ item-width 2)
padding (* 4 padding)]]
(assoc (texture (aget (:all-items entities) 0 (.indexOf utils/+all-cursors+ (:cursor item))))
:x x :y y
:scale-x 4
:scale-y 4
:origin-x 0
:origin-y 0
:item item
:box (zone/box (- offset-x padding) (- offset-y padding) (+ offset-x item-width padding padding) (+ offset-y item-width padding padding)))))
(assoc-in [:tweens :fade-in] (tween/tween :fade-in screen [:opacity] 0.0 1.0 0.2 :ease tween/ease-out-cubic)))))
:on-mouse-moved (fn [screen [entities]]
(let [[x y] (utils/unproject screen)
selected-entity (first (filter #((:box %) x y) (:items entities)))]
(if selected-entity
(assoc entities :highlighted-item (:item selected-entity))
(assoc entities :highlighted-item nil))))
:on-touch-up (fn [screen [entities]]
(when (and (:shown? entities) (= (button-code :left) (:button screen)) (= 1.0 (:opacity entities)))
(let [{:keys [highlighted-item]} entities
room-entities (-> @(resolve 'advent.screens.scene/scene)
:entities
deref
first)
current-cursor (get-in room-entities [:cursor :current])]
(if highlighted-item
(if (= :main current-cursor)
(screen! @(resolve 'advent.screens.scene/scene) :on-chose-item :item highlighted-item)
(when-let [interaction-script ((or (:scripts highlighted-item) (constantly nil)) (:value current-cursor))]
(interaction-script room-entities)
(close screen entities)))
(close screen entities)))))
:on-resize (fn [screen entities]
(.update (:viewport screen) (:width screen) (:height screen) true)))