Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added destructuring in some places.

  • Loading branch information...
commit 3c6a6e595e7a08053bc38995ed06b47dfda1a676 1 parent 77bfdf5
Alexey Kutepov authored

Showing 2 changed files with 41 additions and 49 deletions. Show diff stats Hide diff stats

  1. +21 22 src/hyperspace/game.clj
  2. +20 27 src/hyperspace/ui.clj
43 src/hyperspace/game.clj
@@ -11,37 +11,36 @@
11 11 (defn move-bullet
12 12 [bullet planets]
13 13 (let [acceleration (get-acceleration bullet planets)
14   - position (:center bullet)
15   - velocity (:velocity bullet)
16   - new-position (point-move position velocity)
17   - new-velocity (vector-sum velocity acceleration)]
18   - (bullet. new-position new-velocity)))
  14 + {position :center
  15 + velocity :velocity} bullet]
  16 + (assoc bullet
  17 + :center (point-move position velocity)
  18 + :velocity (vector-sum velocity acceleration))))
19 19
20 20 (defn destroy-bullet?
21 21 [bullet planets]
22 22 (let [bullet-center (:center bullet)]
23   - (some #(<= (point-distance bullet-center
24   - (:center %))
25   - (:radius %))
  23 + (some (fn [{planet-radius :radius
  24 + planet-center :center}]
  25 + (<= (point-distance bullet-center
  26 + planet-center)
  27 + planet-radius))
26 28 planets)))
27 29
28 30 (defn update-world
29 31 "Simulates few steps for world."
30 32 [init-world time-delta]
31 33 (loop [time time-delta
32   - world init-world]
  34 +
  35 + {bullets :bullets
  36 + planets :planets
  37 + traces :traces :as world} init-world]
33 38 (if (<= time 0)
34 39 world
35   - (let [planets (:planets world)
36   - bullets (:bullets world)
37   - traces (:traces world)
38   - new-planets planets
39   - new-players (:players world)
40   - new-bullets (map #(move-bullet % planets)
41   - (filter #(not (destroy-bullet? % planets))
42   - bullets))
43   - new-traces (concat (map #(trace. (:center %)) bullets)
44   - (take max-traces traces))
45   - new-world (world. new-planets new-players new-bullets new-traces)
46   - new-time (- time 1)]
47   - (recur new-time new-world)))))
  40 + (recur (- time 1)
  41 + (assoc world
  42 + :bullets (map #(move-bullet % planets)
  43 + (filter #(not (destroy-bullet? % planets))
  44 + bullets))
  45 + :traces (concat (map #(trace. (:center %)) bullets)
  46 + (take max-traces traces)))))))
47 src/hyperspace/ui.clj
@@ -66,31 +66,28 @@
66 66 (defn clear-display []
67 67 (GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT)))
68 68
69   -(defn render-traces [world]
70   - (doseq [trace (:traces world)]
  69 +(defn render-traces [{traces :traces}]
  70 + (doseq [trace traces]
71 71 (render-trace trace)))
72 72
73   -(defn render-planets [world]
74   - (doseq [planet (:planets world)]
  73 +(defn render-planets [{planets :planets}]
  74 + (doseq [planet planets]
75 75 (render-planet planet)))
76 76
77   -(defn render-players [world]
78   - (doseq [player (:players world)]
  77 +(defn render-players [{players :players}]
  78 + (doseq [player players]
79 79 (render-player player)))
80 80
81   -(defn render-bullets [world]
82   - (doseq [bullet (:bullets world)]
  81 +(defn render-bullets [{bullets :bullets}]
  82 + (doseq [bullet bullets]
83 83 (render-bullet bullet)))
84 84
85 85 (defn process-input [world]
86 86 (if (Keyboard/next)
87   - (let [planets (:planets world)
88   - players (:players world)
89   - bullets (conj (:bullets world)
90   - (bullet. (:center (first players)) (vector2. 1 1)))
91   - traces (:traces world)
92   - new-world (world. planets players bullets traces)]
93   - new-world)
  87 + (let [{[{player-center :center} & _] :players
  88 + bullets :bullets} world]
  89 + (assoc world
  90 + :bullets (conj bullets (bullet. player-center (vector2. 1 1)))))
94 91 world))
95 92
96 93 (defn normalize-x [x]
@@ -99,18 +96,17 @@
99 96 (defn normalize-y [y]
100 97 (/ y window-height))
101 98
102   -(defn space-point-to-display [point]
103   - (point2.
104   - (- (* 2 (normalize-x (* (:x point) scale))) 1)
105   - (- (* 2 (normalize-y (* (:y point) scale))) 1)))
  99 +(defn space-point-to-display [{x :x y :y :as point}]
  100 + (assoc point
  101 + :x (- (* 2 (normalize-x (* x scale))) 1)
  102 + :y (- (* 2 (normalize-y (* y scale))) 1)))
106 103
107 104 (defn render-trace
108 105 "Suddenly, a trace renders as a yellow point."
109 106 [trace]
110 107 (GL11/glColor3f 1 1 0)
111 108 (let [center (space-point-to-display (:center trace))
112   - center-x (:x center)
113   - center-y (:y center)
  109 + {center-x :x center-y :y} center
114 110 x1 (- center-x (normalize-x 1))
115 111 y1 (- center-y (normalize-y 1))
116 112 x2 (+ center-x (normalize-x 1))
@@ -122,8 +118,7 @@
122 118 [planet]
123 119 (GL11/glColor3f 0 1 0)
124 120 (let [center (space-point-to-display (:center planet))
125   - center-x (:x center)
126   - center-y (:y center)
  121 + {center-x :x center-y :y} center
127 122 radius-px (* (:radius planet) scale)
128 123 delta-x (normalize-x radius-px)
129 124 delta-y (normalize-y radius-px)
@@ -137,8 +132,7 @@
137 132 "A player now renders as a, well, blue rectangle."
138 133 (GL11/glColor3f 0 0 1)
139 134 (let [center (space-point-to-display (:center player))
140   - center-x (:x center)
141   - center-y (:y center)
  135 + {center-x :x center-y :y} center
142 136 x1 (- center-x (normalize-x 2))
143 137 y1 (- center-y (normalize-y 2))
144 138 x2 (+ center-x (normalize-x 2))
@@ -149,8 +143,7 @@
149 143 "A bullet now renders as a... guess what? Right, red rectangle!"
150 144 (GL11/glColor3f 1 0 0)
151 145 (let [center (space-point-to-display (:center bullet))
152   - center-x (:x center)
153   - center-y (:y center)
  146 + {center-x :x center-y :y} center
154 147 x1 (- center-x (normalize-x 1))
155 148 y1 (- center-y (normalize-y 1))
156 149 x2 (+ center-x (normalize-x 1))

0 comments on commit 3c6a6e5

Please sign in to comment.
Something went wrong with that request. Please try again.