106 lines
5.1 KiB
Clojure
106 lines
5.1 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.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 [entities]
|
|
(screen! @(resolve 'advent.screens.scene/scene) :on-reactivate)
|
|
(-> entities
|
|
(assoc :shown? false)
|
|
(assoc :start-showing? 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)
|
|
:fade (assoc (texture "black.png")
|
|
:scale-x 80
|
|
:scale-y 80
|
|
:opacity 0.7)
|
|
:all-items (texture! (texture "cursor.png") :split 16 16)
|
|
:items []
|
|
:shown? false
|
|
:start-showing? false
|
|
:highlighted-item nil
|
|
:highlighted-text highlighted-text}))
|
|
|
|
:on-render
|
|
(fn [screen [entities]]
|
|
(let [entities (if (:start-showing? entities)
|
|
(-> entities
|
|
(assoc :start-showing? false)
|
|
(assoc :shown? true))
|
|
entities)]
|
|
|
|
(when (:shown? entities)
|
|
(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} [entities]]
|
|
(assoc entities :start-showing? true
|
|
: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
|
|
:item item
|
|
:box (zone/box (- offset-x padding) (- offset-y padding) (+ offset-x item-width padding) (+ offset-y item-width padding))))))
|
|
|
|
: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)))
|
|
(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 entities)))
|
|
(close entities)))))
|
|
|
|
:on-resize (fn [screen entities]
|
|
(.update (:viewport screen) (:width screen) (:height screen) true)))
|