Draw tiles on grid, show current tile at mouse pos, load-texture once
This commit is contained in:
parent
e91dae506a
commit
0715b84e7a
62
src/game.clj
62
src/game.clj
@ -1,23 +1,51 @@
|
||||
(ns game
|
||||
(:gen-class)
|
||||
(:require
|
||||
[engine]
|
||||
[rl :as rl]))
|
||||
|
||||
(def screen-width 900)
|
||||
(def screen-height 500)
|
||||
(def ^:const screen-width 1280)
|
||||
(def ^:const screen-height 630)
|
||||
(def ^:const cell-size 16)
|
||||
(def ^:const rows (/ screen-width cell-size))
|
||||
(def ^:const cols (quot screen-height cell-size))
|
||||
|
||||
(set! *warn-on-reflection* true)
|
||||
;; (set! *unchecked-math* :warn-on-boxed)
|
||||
|
||||
;; Colors
|
||||
(def color-ground (rl/color-seg 0xE8CFA6))
|
||||
(def color-tint-gray (rl/color-seg 0 0 0 100))
|
||||
|
||||
(defonce texture-cache (atom {}))
|
||||
|
||||
(defn load-texture-once [path]
|
||||
(or (@texture-cache path)
|
||||
(let [t (rl/load-texture path)]
|
||||
(swap! texture-cache assoc path t)
|
||||
t)))
|
||||
|
||||
(defn idx [row col] (+ (* row cols) col))
|
||||
(defn tile-at [game r c] (aget ^objects (:grid game) (idx r c)))
|
||||
(defn set-tile! [game r c tile] (aset ^objects (:grid game) (idx r c) tile))
|
||||
|
||||
(defn init-game [config]
|
||||
{:editor-mode false
|
||||
:color-idx 0})
|
||||
:color-idx 0
|
||||
;; :event-queue []
|
||||
;; TODO: Once we have a proper ID system for the cells, use int-array for performance
|
||||
:grid (object-array (* rows cols))
|
||||
:texture (load-texture-once "source-assets/Sprout Lands - Sprites - premium pack/Sprout Lands - Sprites - premium pack/Tilesets/ground tiles/New tiles/Grass_Hill_Tiles_v2.png")})
|
||||
|
||||
(defn handle-game-input [config game]
|
||||
(let [game (assoc game :event-queue [])]
|
||||
(when (rl/key-pressed? rl/key-r))
|
||||
(when (rl/mouse-button-pressed? rl/mouse-button-left)
|
||||
(let [{mx :x my :y} (rl/get-mouse-position)]
|
||||
(set-tile! game (quot my cell-size) (quot mx cell-size) [3 3])))
|
||||
(cond-> game
|
||||
(rl/key-pressed? rl/key-space)
|
||||
(update :editor-mode not)))
|
||||
(update :editor-mode not))))
|
||||
|
||||
(defn update-game [config game]
|
||||
(if (:editor-mode game)
|
||||
@ -25,16 +53,33 @@
|
||||
())
|
||||
game)
|
||||
|
||||
(defn draw-tile! [game row-idx col-idx x y scale]
|
||||
(let [src-rect (rl/rect-seg (* col-idx cell-size) (* row-idx cell-size) cell-size cell-size)
|
||||
dst-x (* (quot x (* cell-size scale)) cell-size scale)
|
||||
dst-y (* (quot y (* cell-size scale)) cell-size scale)
|
||||
dst-rect (rl/rect-seg dst-x dst-y (* cell-size scale) (* cell-size scale))]
|
||||
(rl/draw-texture-pro!* (:seg (:texture game)) src-rect dst-rect (rl/vec2-seg 0 0) (float 0.0) rl/white)))
|
||||
|
||||
(def scale 4.0)
|
||||
(defn draw-game [config game]
|
||||
(rl/clear-background!* rl/cornflower-blue)
|
||||
(rl/clear-background!* color-ground)
|
||||
(doseq [row (range rows)
|
||||
col (range cols)]
|
||||
(when-let [[tx ty] (tile-at game row col)]
|
||||
(draw-tile! game tx ty (* col cell-size) (* row cell-size) scale)))
|
||||
(let [{mx :x my :y :as mouse-pos} (rl/get-mouse-position)]
|
||||
(draw-tile! game 3 3 mx my scale))
|
||||
(when (:editor-mode game)
|
||||
(rl/draw-rectangle!* 0 0 screen-width screen-height (rl/color-seg 0 0 0 100))
|
||||
(rl/draw-rectangle!* 0 0 screen-width screen-height color-tint-gray)
|
||||
(rl/draw-rectangle!* (/ screen-width 2) (/ screen-height 2) 100 100 rl/white)
|
||||
(rl/draw-text!* "EDIT MODE" (- screen-width 100) 10 16 rl/green)))
|
||||
|
||||
(defn watch-game [config game]
|
||||
{:color-idx (:color-idx game)})
|
||||
|
||||
(defn unload-game [game]
|
||||
(rl/unload-texture! (:texture game)))
|
||||
|
||||
(defn -main [& _args]
|
||||
(engine/run-game!
|
||||
{:title "Siam Farmer"
|
||||
@ -45,8 +90,11 @@
|
||||
:input #'handle-game-input
|
||||
:update #'update-game
|
||||
:draw #'draw-game
|
||||
:watch-fn #'watch-game}))
|
||||
:watch-fn #'watch-game
|
||||
:unload #'unload-game}))
|
||||
|
||||
(comment
|
||||
(future (-main))
|
||||
(reset! @#'engine/game-state (init-game {}))
|
||||
(swap! @#'engine/game-state :assoc {})
|
||||
:-)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user