Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed May 24, 2024
1 parent d4e5a10 commit 14959f7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 80 deletions.
56 changes: 28 additions & 28 deletions docs/js/main.js

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions src/amaze/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
(def generating-speed 2)
(def free-area 3)

(def free-pass
(let [[sx sy] start
[fx fy] finish]
(->> (for [dx (range free-area)
dy (range free-area)]
[[(+ sx dx) (+ sy dy)]
[(- fx dx) (- fy dy)]])
(apply concat)
set)))

(def title-size 36)
(def text-size 14)
(def line-height 20)
Expand Down
6 changes: 2 additions & 4 deletions src/amaze/intro_screen.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns amaze.intro-screen
(:require
[quil.core :as q]
[amaze.methods :refer [draw key-press]]
[amaze.methods :refer [draw key-press change-screen]]
[amaze.config :refer [title-size scene-width scene-height text-size]]))


Expand Down Expand Up @@ -29,7 +29,5 @@
(defmethod key-press :intro
[state]
(case (q/key-as-keyword)
:space (-> state
(assoc :screen-type :generation)
(assoc :scene-start (q/millis)))
:space (change-screen state :generation)
state))
43 changes: 25 additions & 18 deletions src/amaze/maze_generation.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns amaze.maze-generation
(:require
[quil.core :as q]
[amaze.methods :refer [update-state draw key-press]]
[amaze.config :refer [size width height free-pass generating-speed
[amaze.methods :refer [update-state draw key-press change-screen]]
[amaze.config :refer [size width height free-area generating-speed
text-size bottom-1 bottom-2 x1 gold-amount
background-color]]))
background-color start finish]]))


(defn- random-point []
Expand Down Expand Up @@ -34,17 +34,27 @@
(* 0.00001 (- (q/millis) start))))

(defn- create-elliptical-walls [{:keys [scene-start]}]
(let [a (quot width 6)
(let [a (quot width 6)
aa (* 2 a)
b (quot (* 3 height) 5)
x (- (rand-int aa) a)
y (inc (rand-int b))]
b (quot (* 3 height) 5)
x (- (rand-int aa) a)
y (inc (rand-int b))]
(when (< (+ (/ (* x x) (* a a))
(/ (* y y) (* b b)))
1)
(when (< (rand) (fade-out scene-start))
[[(+ x aa) y]
[(- width x aa) (- height y 1)]]))))
(when (< (rand) (fade-out scene-start))
[[(+ x aa) y]
[(- width x aa) (- height y 1)]]))))

(def free-pass
(let [[sx sy] start
[fx fy] finish]
(->> (for [dx (range free-area)
dy (range free-area)]
[[(+ sx dx) (+ sy dy)]
[(- fx dx) (- fy dy)]])
(apply concat)
set)))

(defn- create-walls [state]
(->> (reduce (fn [acc _]
Expand Down Expand Up @@ -91,12 +101,9 @@
(defmethod key-press :generation
[{:keys [walls] :as state}]
(case (q/key-as-keyword)
(:space :n) (-> state
(assoc :screen-type :navigation)
(assoc :orig-walls walls)
(assoc :gold (place-gold walls))
(assoc :scene-start (q/millis)))
:q (-> state
(assoc :screen-type :intro)
(assoc :walls #{}))
(:space :n) (change-screen state :navigation
{:orig-walls walls
:gold (place-gold walls)})
:q (change-screen state :intro
{:walls #{}})
state))
12 changes: 11 additions & 1 deletion src/amaze/methods.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns amaze.methods)
(ns amaze.methods
(:require [quil.core :as q]))


(defmulti update-state :screen-type)
Expand All @@ -14,3 +15,12 @@
(defmethod key-press :default
[state]
state)


(defn change-screen [state new-screen-type & [opts]]
(-> (reduce-kv (fn [state k v]
(assoc state k v))
state
opts)
(assoc :screen-type new-screen-type)
(assoc :scene-start (q/millis))))
33 changes: 15 additions & 18 deletions src/amaze/navigation.cljs
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
(ns amaze.navigation
(:require
[quil.core :as q]
[amaze.methods :refer [update-state draw key-press]]
[amaze.methods :refer [update-state draw key-press change-screen]]
[amaze.config :refer [size start finish gold-multi gold-amount
bomb-multi bomb-limit width height
text-size x1 x2 x3 x4 x5 bottom-1 bottom-2
background-color move-timeout]]))


(defn reset-level [state]
(-> state
(assoc :screen-type :navigation)
(assoc :walls (:orig-walls state))
(assoc :bombs-used 0)
(assoc :bombs-expls [])
(assoc :pos start)
(assoc :path [start])
(assoc :picked-gold #{})
(assoc :scene-start (q/millis))
(assoc :cnt 0)))
(change-screen state :navigation
{:walls (:orig-walls state)
:bombs-used 0
:bomb-expls []
:pos start
:path [start]
:picked-gold #{}
:cnt 0}))

(defn quit-level [state]
(-> state
reset-level
(assoc :walls #{})
(assoc :maze-best 0)
(assoc :screen-type :generation)))
(change-screen :generation
{:walls #{}
:maze-best 0})))

(defn- calc-score [{:keys [cnt walls win-time bombs-used picked-gold]}]
(- (+ (count walls) (* gold-multi (count picked-gold)))
Expand Down Expand Up @@ -65,10 +63,9 @@
(let [win-time (calc-duration scene-start)
state (assoc state :win-time win-time)
score (calc-score state)]
(-> state
(assoc :score score)
(assoc :score-shown (count walls))
(assoc :screen-type :win)))
(change-screen state :win
{:score score
:score-shown (count walls)}))

(and (gold pos)
(not (picked-gold pos)))
Expand Down
2 changes: 1 addition & 1 deletion src/amaze/win_screen.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
[y1 y11 y2 y3 y4 y5]
(range y1 600 line-height)
y6 (+ y5 line-height 10)
[y7 y8] (range (+ 50 y6) 600 line-height)]
[y7 y8] (range (+ 50 y6) 600 line-height)]
(q/text-size text-size)
(q/text-style :normal)
(q/text-align :left)
Expand Down

0 comments on commit 14959f7

Please sign in to comment.