diff --git a/tanki/background.hy b/tanki/background.hy index fe11536..cd7bd0f 100644 --- a/tanki/background.hy +++ b/tanki/background.hy @@ -1,5 +1,7 @@ (import pyray :as pr) +(import tanki.common :as tc) + (defclass Background [] "Background with parallax." @@ -9,9 +11,9 @@ self.scrolling-mid 0.0 self.scrolling-fore 0.0 - self.background (pr.load-texture f"assets/gfx/background-{name}.png") - self.midround (pr.load-texture f"assets/gfx/midground-{name}.png") - self.foreground (pr.load-texture f"assets/gfx/foreground-{name}.png"))) + self.background (tc.load-texture f"background-{name}.png") + self.midround (tc.load-texture f"midground-{name}.png") + self.foreground (tc.load-texture f"foreground-{name}.png"))) (defn reset [self] (setv self.scrolling-back 0.0 diff --git a/tanki/common.hy b/tanki/common.hy index 26aa16f..4f70ee2 100644 --- a/tanki/common.hy +++ b/tanki/common.hy @@ -1,7 +1,25 @@ +(import pathlib [Path]) + +(import pyray :as pr) + + (setv *resolution* #(1024 768) [*width* *height*] *resolution* *debug* False - *music* True) + *music* True + *assets-dir* (/ (. (Path __file__) parent parent) "assets")) (defn clamp [val min-val max-val] (get (sorted [min-val val max-val]) 1)) + +(defn res [#* parts] + (/ *assets-dir* #* parts)) + +(defn load-texture [#* parts] + (pr.load-texture (str (res "gfx" #* parts)))) + +(defn load-sound [#* parts] + (pr.load-sound (str (res "snd" #* parts)))) + +(defn load-music-stream [#* parts] + (pr.load-music-stream (str (res "snd" #* parts)))) diff --git a/tanki/game.hy b/tanki/game.hy index 3e7af8e..967870e 100644 --- a/tanki/game.hy +++ b/tanki/game.hy @@ -3,6 +3,7 @@ (import tanki [common] tanki.background [Background] tanki.common [*width* *height*] + tanki.common :as tc tanki.controls [InputSystem] tanki.level [Level] tanki.player [Player]) @@ -23,7 +24,7 @@ (setv self.state :in-game self.input (InputSystem) self.level (Level (get self.level-names 0)) - self.music (pr.load-music-stream "assets/snd/theme.wav"))) + self.music (tc.load-music-stream "theme.wav"))) (defn restart-level [self] (setv self.level (Level self.level.name))) diff --git a/tanki/level.hy b/tanki/level.hy index 3b04d2b..bea7bde 100644 --- a/tanki/level.hy +++ b/tanki/level.hy @@ -5,6 +5,7 @@ (import tanki.background [Background] tanki.common [*width* *height*] + tanki.common :as tc tanki.obstacles [ObstaclePool] tanki.player [Player]) @@ -64,7 +65,7 @@ #(1.6 "GO"))) self.obstacles (ObstaclePool :gap (int (+ self.player.texture.height (/ self.player.texture.height 2)))) - self.collision-sound (pr.load-sound "assets/snd/take-damage.wav")) + self.collision-sound (tc.load-sound "take-damage.wav")) (pr.set-sound-volume self.collision-sound 0.2)) ;; save to file diff --git a/tanki/player.hy b/tanki/player.hy index b814669..721ab41 100644 --- a/tanki/player.hy +++ b/tanki/player.hy @@ -1,12 +1,13 @@ (import pyray :as pr) -(import tanki [common ui]) +(import tanki [ui]) +(import tanki.common :as tc) (defclass JetpackSound [] (defn __init__ [self] - (setv self.sound (pr.load-music-stream "assets/snd/jetpack-cut.wav") + (setv self.sound (tc.load-music-stream "jetpack-cut.wav") self.length (pr.get-music-time-length self.sound)) (pr.set-music-volume self.sound 0.1) (pr.play-music-stream self.sound) @@ -42,8 +43,8 @@ self.fall-speed 4 self.jump-speed 10 self.weapon-cooldown 10 - self.texture (pr.load-texture "assets/gfx/player.png") - self.fuel-depletion-sound (pr.load-sound "assets/snd/select.wav") + self.texture (tc.load-texture "player.png") + self.fuel-depletion-sound (tc.load-sound "select.wav") self.jetpack-sound (JetpackSound) self.collision-rect (pr.Rectangle (+ pos.x 10) (+ pos.y 10) @@ -81,7 +82,7 @@ (defn update [self] (+= self.pos.y self.fall-speed) - (setv self.fuel (common.clamp (+ self.fuel 0.55) 0 100)) + (setv self.fuel (tc.clamp (+ self.fuel 0.55) 0 100)) ;; Recharched! (when (> self.fuel 25) (self.set-fuel-depleted False)) @@ -127,7 +128,7 @@ (when (> self.fuel 0) (self.jetpack-sound.resume) (-= self.pos.y self.jump-speed) - (setv self.fuel (common.clamp (- self.fuel (self.get-fuel-consumption)) 0 100))) + (setv self.fuel (tc.clamp (- self.fuel (self.get-fuel-consumption)) 0 100))) ;; TODO: add depletion sound (when (= self.fuel 0) @@ -141,5 +142,5 @@ (pr.draw-texture-ex self.texture self.pos self.rotation 1.0 pr.RAYWHITE) (self.fuel-bar.render) - (when common.*debug* + (when tc.*debug* (pr.draw-rectangle-lines-ex self.collision-rect 1 pr.RED))))