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 b74bf3a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 105 deletions.
13 changes: 0 additions & 13 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@
<script src="js/main.js"></script>
<div id="text">
<a href="https://github.com/narimiran/amaze" target="_blank">[source code]</a>
<!-- <h1>Welcome to aMAZE</h1> -->
<!-- <h2>How to play:</h2> -->
<!-- <ol> -->
<!-- <li>press SPACE to start generating a maze</li> -->
<!-- <li>press SPACE again to stop maze generation</li> -->
<!-- <li>navigate through the maze:</li> -->
<!-- <ul> -->
<!-- <li>starting from top-left, reach bottom-right in least amount of steps</li> -->
<!-- <li>improve score by collecting gold</li> -->
<!-- <li>if stuck, drop a bomb to clear the way</li> -->
<!-- </ul> -->
<!-- <li>can you score more than 1000 points?</li> -->
<!-- </ol> -->
</div>
</body>
</html>
74 changes: 37 additions & 37 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))))
35 changes: 16 additions & 19 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 Expand Up @@ -174,7 +171,7 @@
(assoc :bomb-loc nbs)
(assoc :bomb-time (q/millis))
(update :bombs-used inc)
(update :bombs-expls into nbs)
(update :bomb-expls into nbs)
(assoc :walls walls'))
state))))

Expand Down
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -72,7 +72,7 @@
(q/text "R restart this maze" left-margin (+ y-pos line-height))))

(defn- draw-mini-map
[{:keys [path bombs-expls] :as state}]
[{:keys [path bomb-expls] :as state}]
(q/scale (* 0.5 size))
(q/translate 2 2)
(q/fill background-color)
Expand All @@ -83,7 +83,7 @@
(doseq [[x y] path]
(q/rect x y 1 1))
(q/fill 240 80 20)
(doseq [[x y] bombs-expls]
(doseq [[x y] bomb-expls]
(q/rect (+ 0.1 x) (+ 0.1 y) 0.8 0.8))
(nav/draw-gold state false))

Expand Down

0 comments on commit b74bf3a

Please sign in to comment.