add rubber band for camera
This commit is contained in:
106
game.sex
106
game.sex
@@ -13,35 +13,79 @@
|
|||||||
#+emscripten (define WIN-WIDTH 960)
|
#+emscripten (define WIN-WIDTH 960)
|
||||||
#+emscripten (define WIN-HEIGHT 600)
|
#+emscripten (define WIN-HEIGHT 600)
|
||||||
|
|
||||||
(var TILE-SIZE float 1.0f
|
;; 1.0f is fallback default
|
||||||
;; 1.0f is fallback default
|
(var TILE-SIZE float 1.0f)
|
||||||
)
|
|
||||||
|
|
||||||
(defmacro (clamp-to-grid v)
|
(defmacro (clamp-to-grid v)
|
||||||
`(* (/ (cast ,v i64) TILE-SIZE) TILE-SIZE))
|
`(* (/ (cast ,v i64) TILE-SIZE) TILE-SIZE))
|
||||||
|
|
||||||
|
(pub record camera
|
||||||
|
((follow-speed float)
|
||||||
|
(ray-cam Camera2D)))
|
||||||
|
|
||||||
|
(fn fih-center ((state (* game-state))) Vector2)
|
||||||
|
|
||||||
(pub record game-state
|
(pub record game-state
|
||||||
((app-state (* app-state))
|
((app-state (* app-state))
|
||||||
(map (* tmx-map))
|
(map (* tmx-map))
|
||||||
(cam Camera2D)
|
(cam camera)
|
||||||
(fih (* TmxObject))))
|
(fih (* TmxObject))))
|
||||||
|
|
||||||
|
(ops camera
|
||||||
|
(fn init ((self (* camera))) void
|
||||||
|
(= (. self ray-cam offset x) (/ WIN-WIDTH 2.0f))
|
||||||
|
(= (. self ray-cam offset y) (/ WIN-HEIGHT 2.0f))
|
||||||
|
(= (. self ray-cam rotation) 0.0f)
|
||||||
|
(= (. self ray-cam zoom) 3.0f)
|
||||||
|
|
||||||
|
;; More = faster. 4.0f will be smooth, 12.0f fast
|
||||||
|
(= (. self follow-speed) 8.0f))
|
||||||
|
|
||||||
|
(fn clamp ((self (* camera)) (state (* game-state))) void
|
||||||
|
(var w float (/ WIN-WIDTH (. self ray-cam zoom)))
|
||||||
|
(var h float (/ WIN-HEIGHT (. self ray-cam zoom)))
|
||||||
|
(var hw _ (/ w 2.0f))
|
||||||
|
(var hh _ (/ h 2.0f))
|
||||||
|
(var map-w _ (cast (tmx-pixel-width (. state map)) float))
|
||||||
|
(var map-h _ (cast (tmx-pixel-height (. state map)) float))
|
||||||
|
|
||||||
|
(if (<= map-w w)
|
||||||
|
(= (. self ray-cam target x) (/ map-w 2.0f))
|
||||||
|
(clamp (. self ray-cam target x) hw (- map-w hw)))
|
||||||
|
|
||||||
|
(if (<= map-h h)
|
||||||
|
(= (. self ray-cam target y) (/ map-h 2.0f))
|
||||||
|
(clamp (. self ray-cam target y) hh (- map-h hh))))
|
||||||
|
|
||||||
|
(fn update ((self (* camera)) (state (* game-state))) void
|
||||||
|
(if (|| (== (. state map) NULL)
|
||||||
|
(== (. state fih) NULL))
|
||||||
|
(return))
|
||||||
|
(var target _ (fih-center state))
|
||||||
|
(var alpha _ (* (. self follow-speed) (GetFrameTime)))
|
||||||
|
;; Rubber band for camera
|
||||||
|
(clamp alpha alpha 1.0f)
|
||||||
|
(+= (. self ray-cam target x)
|
||||||
|
(* (- (. target x) (. self ray-cam target x)) alpha))
|
||||||
|
(+= (. self ray-cam target y)
|
||||||
|
(* (- (. target y) (. self ray-cam target y)) alpha))))
|
||||||
|
|
||||||
|
(fn fih-center ((state (* game-state))) Vector2
|
||||||
|
(return #(Vector2 :
|
||||||
|
(+ (. state fih x) (/ (. state fih width) 2.0f))
|
||||||
|
(+ (. state fih y) (/ (. state fih height) 2.0f)))))
|
||||||
|
|
||||||
(pub fn mk-game ((app-state (* app-state))) page
|
(pub fn mk-game ((app-state (* app-state))) page
|
||||||
(var state _ (mem-alloc game-state))
|
(var state _ (mem-alloc game-state))
|
||||||
(= (. state app-state) app-state)
|
(= (. state app-state) app-state)
|
||||||
|
|
||||||
(= (. state cam offset x) (/ WIN-WIDTH 2.0))
|
|
||||||
(= (. state cam offset y) (/ WIN-HEIGHT 2.0))
|
|
||||||
(= (. state cam rotation) 0.0f)
|
|
||||||
(= (. state cam zoom) 3.0)
|
|
||||||
|
|
||||||
(var map (* tmx-map) (load-tmx MAP-FILE))
|
(var map (* tmx-map) (load-tmx MAP-FILE))
|
||||||
(= (. state map) map)
|
(= (. state map) map)
|
||||||
|
|
||||||
(if (== map NULL)
|
(if (== map NULL)
|
||||||
(do
|
(do
|
||||||
(= (. state cam target x) 0.0)
|
(= (. state cam ray-cam target x) 0.0)
|
||||||
(= (. state cam target y) 0.0)
|
(= (. state cam ray-cam target y) 0.0)
|
||||||
(return (page/game state))))
|
(return (page/game state))))
|
||||||
|
|
||||||
;; Assume width = height (true for our map)
|
;; Assume width = height (true for our map)
|
||||||
@@ -52,9 +96,9 @@
|
|||||||
|
|
||||||
(if (== object-count 0)
|
(if (== object-count 0)
|
||||||
(do
|
(do
|
||||||
(= (. state cam target x)
|
(= (. state cam ray-cam target x)
|
||||||
(/ (cast (tmx-pixel-width map) float) 2.0))
|
(/ (cast (tmx-pixel-width map) float) 2.0))
|
||||||
(= (. state cam target y)
|
(= (. state cam ray-cam target y)
|
||||||
(/ (cast (tmx-pixel-height map) float) 2.0))
|
(/ (cast (tmx-pixel-height map) float) 2.0))
|
||||||
(return (page/game state))))
|
(return (page/game state))))
|
||||||
|
|
||||||
@@ -75,10 +119,7 @@
|
|||||||
(tmx-object-type map "FIH" 0)
|
(tmx-object-type map "FIH" 0)
|
||||||
player-x player-y player-width player-height)
|
player-x player-y player-width player-height)
|
||||||
|
|
||||||
(= (. state cam target x)
|
(.init (& (. state cam)))
|
||||||
(+ player-x (/ player-width 2.0)))
|
|
||||||
(= (. state cam target y)
|
|
||||||
(+ player-y (/ player-height 2.0)))
|
|
||||||
(return (page/game state)))
|
(return (page/game state)))
|
||||||
|
|
||||||
(pub fn rm-game ((state (* game-state))) void
|
(pub fn rm-game ((state (* game-state))) void
|
||||||
@@ -95,10 +136,10 @@
|
|||||||
(DrawText "Failed to load map" 20 20 20 RED)
|
(DrawText "Failed to load map" 20 20 20 RED)
|
||||||
(return)))
|
(return)))
|
||||||
|
|
||||||
(BeginMode2D (. state cam))
|
(BeginMode2D (. state cam ray-cam))
|
||||||
(animate-tmx (. state map))
|
(animate-tmx (. state map))
|
||||||
(draw-tmx-layer (. state map) "Background" (& (. state cam)))
|
(draw-tmx-layer (. state map) "Background" (& (. state cam ray-cam)))
|
||||||
(draw-tmx-layer (. state map) "FIH" (& (. state cam)))
|
(draw-tmx-layer (. state map) "FIH" (& (. state cam ray-cam)))
|
||||||
(EndMode2D)
|
(EndMode2D)
|
||||||
|
|
||||||
(DrawText "WASD/arrows: move wheel: zoom ESC: back to menu"
|
(DrawText "WASD/arrows: move wheel: zoom ESC: back to menu"
|
||||||
@@ -120,7 +161,7 @@
|
|||||||
(+= (. state fih x) (* dx TILE-SIZE))
|
(+= (. state fih x) (* dx TILE-SIZE))
|
||||||
(+= (. state fih y) (* dy TILE-SIZE)))
|
(+= (. state fih y) (* dy TILE-SIZE)))
|
||||||
|
|
||||||
(pub fn process-input-game ((state (* game-state))) void
|
(pub fn update-game ((state (* game-state))) void
|
||||||
(if (== (. state map) NULL)
|
(if (== (. state map) NULL)
|
||||||
(return))
|
(return))
|
||||||
|
|
||||||
@@ -135,25 +176,16 @@
|
|||||||
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
|
(if (|| (IsKeyPressed KEY-DOWN) (IsKeyPressed KEY-S))
|
||||||
(move-fih state 0 1))
|
(move-fih state 0 1))
|
||||||
|
|
||||||
(var map-width float (cast (tmx-pixel-width (. state map)) float))
|
|
||||||
(var map-height float (cast (tmx-pixel-height (. state map)) float))
|
|
||||||
(if (< (. state cam target x) 0.0)
|
|
||||||
(= (. state cam target x) 0.0))
|
|
||||||
(if (> (. state cam target x) map-width)
|
|
||||||
(= (. state cam target x) map-width))
|
|
||||||
(if (< (. state cam target y) 0.0)
|
|
||||||
(= (. state cam target y) 0.0))
|
|
||||||
(if (> (. state cam target y) map-height)
|
|
||||||
(= (. state cam target y) map-height))
|
|
||||||
|
|
||||||
(var wheel float (GetMouseWheelMove))
|
(var wheel float (GetMouseWheelMove))
|
||||||
(if (!= wheel 0.0)
|
(if (!= wheel 0.0)
|
||||||
(do
|
(do
|
||||||
(+= (. state cam zoom) (* wheel 0.5))
|
(+= (. state cam ray-cam zoom) (* wheel 0.5))
|
||||||
(if (< (. state cam zoom) 0.5)
|
(if (< (. state cam ray-cam zoom) 0.5)
|
||||||
(= (. state cam zoom) 0.5))
|
(= (. state cam ray-cam zoom) 0.5))
|
||||||
(if (> (. state cam zoom) 8.0)
|
(if (> (. state cam ray-cam zoom) 8.0)
|
||||||
(= (. state cam zoom) 8.0))))
|
(= (. state cam ray-cam zoom) 8.0))))
|
||||||
|
|
||||||
|
(.update (& (. state cam)) state)
|
||||||
|
|
||||||
(if (IsKeyReleased KEY-ESCAPE)
|
(if (IsKeyReleased KEY-ESCAPE)
|
||||||
(.change-page (. state app-state) (sum-type page menu))))
|
(.change-page (. state app-state) (sum-type page menu))))
|
||||||
|
|||||||
4
main.sex
4
main.sex
@@ -58,10 +58,10 @@
|
|||||||
(.change-page- state)
|
(.change-page- state)
|
||||||
(match (. state page)
|
(match (. state page)
|
||||||
((menu (state))
|
((menu (state))
|
||||||
(process-input-menu state)
|
(update-menu state)
|
||||||
(render-menu state))
|
(render-menu state))
|
||||||
((game (state))
|
((game (state))
|
||||||
(process-input-game state)
|
(update-game state)
|
||||||
(render-game state))))
|
(render-game state))))
|
||||||
|
|
||||||
(fn init () void
|
(fn init () void
|
||||||
|
|||||||
2
menu.sex
2
menu.sex
@@ -51,7 +51,7 @@
|
|||||||
|
|
||||||
(EndDrawing))
|
(EndDrawing))
|
||||||
|
|
||||||
(pub fn process-input-menu ((state (* menu-state))) void
|
(pub fn update-menu ((state (* menu-state))) void
|
||||||
(if (== (. state btn-to-game) 1)
|
(if (== (. state btn-to-game) 1)
|
||||||
(do
|
(do
|
||||||
(= (. state btn-to-game) 0)
|
(= (. state btn-to-game) 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user