Skip to content

Commit 6997940

Browse files
committed
Fixed trace disappearing.
1 parent 0820803 commit 6997940

7 files changed

Lines changed: 100 additions & 104 deletions

File tree

src/hyperspace/game.clj

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,9 @@
2424
{point :center
2525
power :power
2626
angle :heading} player
27-
bullet (make-bullet point (make-vector-radial power angle))
28-
trace (make-trace bullet)]
27+
bullet (make-bullet point (make-vector-radial power angle))]
2928
(assoc world
30-
:bullets (conj (:bullets world) bullet)
31-
:traces (conj (:traces world) trace))))
32-
33-
(defn move-bullet
34-
[bullet planets]
35-
(let [acceleration (get-acceleration bullet planets)
36-
{position :center
37-
velocity :velocity} bullet]
38-
(assoc bullet
39-
:center (move-point position velocity)
40-
:velocity (vector-sum velocity acceleration))))
29+
:bullets (conj (:bullets world) bullet))))
4130

4231
(defn destroy-bullet?
4332
[bullet planets]
@@ -49,21 +38,27 @@
4938
planet-radius))
5039
planets)))
5140

41+
(defn update-bullet
42+
[bullet planets]
43+
(if (= (:status bullet) :dead)
44+
bullet
45+
(let [acceleration (get-acceleration bullet planets)
46+
{position :center
47+
velocity :velocity
48+
traces :traces} bullet]
49+
(assoc bullet
50+
:center (move-point position velocity)
51+
:velocity (vector-sum velocity acceleration)
52+
:status (if (destroy-bullet? bullet planets) :dead :alive)
53+
:traces (conj traces position)))))
54+
5255
(defn update-world
5356
"Simulates few steps for world."
5457
[world time]
5558
(let [{bullets :bullets
56-
planets :planets
57-
traces :traces} world]
58-
(if (<= time 0)
59+
planets :planets} world]
60+
(if (< time 0)
5961
world
60-
(recur
61-
(assoc world
62-
:bullets (doall
63-
(map #(move-bullet % planets)
64-
(filter #(not (destroy-bullet? % planets))
65-
bullets)))
66-
:traces (doall
67-
(map #(update-trace %1 %2)
68-
traces bullets)))
69-
(- time 1)))))
62+
(recur (assoc world
63+
:bullets (doall (map #(update-bullet % planets) bullets)))
64+
(- time 1)))))

src/hyperspace/ui.clj

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
(hyperspace.world Bullet
1111
Planet
1212
Player
13-
Trace
1413
World)))
1514

1615
(declare start-ui)
@@ -134,10 +133,10 @@
134133
display-y (:y display-point)]
135134
(draw-ellipse display-x display-y (normalize-x 5) (normalize-y 5) 30)))
136135

137-
(defmethod render Trace
138-
[trace]
136+
(defn draw-traces
137+
[bullet]
139138
(GL11/glColor3f 1 1 0)
140-
(doseq [point (:points trace)]
139+
(doseq [point (:traces bullet)]
141140
(let [center (space-point-to-display point)
142141
{center-x :x center-y :y} center
143142
x1 (- center-x (normalize-x 1))
@@ -170,22 +169,22 @@
170169

171170
(defmethod render Bullet
172171
[bullet]
173-
(GL11/glColor3f 1 0 0)
174-
(let [center (space-point-to-display (:center bullet))
175-
{center-x :x center-y :y} center
176-
x1 (- center-x (normalize-x 7))
177-
y1 (- center-y (normalize-y 7))
178-
x2 (+ center-x (normalize-x 7))
179-
y2 (+ center-y (normalize-y 7))]
180-
(GL11/glRectf x1 y1 x2 y2)))
172+
(draw-traces bullet)
173+
(when (= (:status bullet) :alive)
174+
(GL11/glColor3f 1 0 0)
175+
(let [center (space-point-to-display (:center bullet))
176+
{center-x :x center-y :y} center
177+
x1 (- center-x (normalize-x 7))
178+
y1 (- center-y (normalize-y 7))
179+
x2 (+ center-x (normalize-x 7))
180+
y2 (+ center-y (normalize-y 7))]
181+
(GL11/glRectf x1 y1 x2 y2))))
181182

182183
(defmethod render World
183184
[{planets :planets
184185
players :players
185-
bullets :bullets
186-
traces :traces}]
187-
(doseq [object (concat traces
188-
planets
186+
bullets :bullets}]
187+
(doseq [object (concat planets
189188
players
190189
bullets)]
191190
(render object)))

src/hyperspace/world.clj

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
44

55
(defrecord Planet [center radius mass])
66
(defrecord Player [center heading power name])
7-
(defrecord Bullet [center velocity])
8-
(defrecord Trace [points])
7+
(defrecord Bullet [center velocity status traces])
98

10-
(defrecord World [planets players bullets traces])
9+
(defrecord World [planets players bullets])
1110

1211
(def make-planet ->Planet)
1312
(def make-player ->Player)
14-
(def make-bullet ->Bullet)
1513

16-
(defn make-trace
17-
[bullet]
18-
(->Trace (list (:center bullet))))
19-
20-
(defn update-trace
21-
[trace bullet]
22-
(assoc trace
23-
:points (conj (:points trace)
24-
(:center bullet))))
14+
(defn make-bullet
15+
[center velocity]
16+
(->Bullet center velocity :alive []))
2517

2618
(defn make-world
2719
[planets players]
28-
(->World planets players [] []))
20+
(->World planets players []))
2921

3022
(def min-x 100)
3123
(def max-x 700)

test/hyperspace/test/cases.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
[]
77
(let [players [(make-player (make-point 100 100) 0 0 "player-1")
88
(make-player (make-point 200 200) 0 0 "player-2")]
9-
planet (make-planet (make-point 0 0) 10 100)]
9+
planet (make-planet (make-point 0 0) 100 100e12)]
1010
(make-world [planet] players)))

test/hyperspace/test/game.clj

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99

1010
;; Helper functions:
1111

12-
(defn bullet-count
12+
(defn alive-bullet-count
1313
[world]
14-
(count (:bullets world)))
14+
(count (filter #(= (:status %) :alive) (:bullets world))))
1515

16-
(defn player-count
16+
(defn dead-bullets-count
1717
[world]
18-
(count (:players world)))
18+
(count (filter #(= (:status %) :dead) (:bullets world))))
1919

2020
(defn trace-count
21+
[bullet]
22+
(count (:traces bullet)))
23+
24+
(defn alive-player-count
2125
[world]
22-
(count (:traces world)))
26+
(count (:players world)))
2327

2428
;; Simple tests:
2529

@@ -44,24 +48,39 @@
4448
(let [world1 (get-test-world)
4549
world2 (fire world1 "player-1")
4650
world3 (fire world2 "player-2")]
47-
(is (= (bullet-count world1) 0))
48-
(is (= (bullet-count world2) 1))
49-
(is (= (bullet-count world3) 2))))
51+
(is (= (alive-bullet-count world1) 0))
52+
(is (= (alive-bullet-count world2) 1))
53+
(is (= (alive-bullet-count world3) 2))))
5054

5155
(deftest bullet-move-test
5256
(let [planet1 (make-planet (make-point 0 0) 1 100)
5357
planet2 (make-planet (make-point 0 5) 1 200)
5458
planets [planet1 planet2]
55-
bullet (make-bullet (make-point 5 5) (make-vector 1 1))
56-
acceleration (get-acceleration bullet planets)
57-
new-velocity (vector-sum (:velocity bullet) acceleration)
58-
new-bullet (move-bullet bullet planets)]
59-
(is (almost= (:x (:center new-bullet)) 6))
60-
(is (almost= (:y (:center new-bullet)) 6))
61-
(is (almost= (:x (:velocity new-bullet))
62-
(:x new-velocity)))
63-
(is (almost= (:y (:velocity new-bullet))
64-
(:y new-velocity)))))
59+
bullet1 (make-bullet (make-point 5 5) (make-vector 1 1))
60+
bullet2 (update-bullet bullet1 planets)
61+
62+
acceleration (get-acceleration bullet1 planets)
63+
new-velocity (vector-sum (:velocity bullet1) acceleration)]
64+
(is (points-almost= (:center bullet2)
65+
(make-point 6 6)))
66+
(is (vectors-almost= (:velocity bullet2)
67+
new-velocity))))
68+
69+
(deftest trace-test
70+
(let [planet (make-planet (make-point 1 5) 5 100)
71+
bullet1 (make-bullet (make-point 1 30) (make-vector 1 1))
72+
bullet2 (update-bullet bullet1 [planet])
73+
bullet3 (update-bullet bullet2 [planet])]
74+
(is (= (trace-count bullet1) 0))
75+
(is (= (trace-count bullet2) 1))
76+
(is (= (trace-count bullet3) 2))
77+
78+
(is (points-almost= (first (:traces bullet2))
79+
(:center bullet1)))
80+
(is (points-almost= (first (:traces bullet3))
81+
(:center bullet1)))
82+
(is (points-almost= (last (:traces bullet3))
83+
(:center bullet2)))))
6584

6685
(deftest bullet-destroy-test
6786
(let [planet (make-planet (make-point 0 0) 5 100)
@@ -72,28 +91,17 @@
7291

7392
;; Complex tests:
7493

75-
(deftest simple-trace-test
76-
(let [world1 (get-test-world)
77-
world2 (fire world1 "player-1")
78-
bullet1 (first (:bullets world2))
79-
world3 (update-world world2 1)
80-
bullet2 (first (:bullets world3))
81-
trace (first (:traces world3))]
82-
(is (= (first (:points trace))
83-
(:center bullet1)))))
84-
85-
(deftest trace-test
94+
(deftest complex-firing-test
8695
(let [world1 (get-test-world)
8796
world2 (fire world1 "player-1")
88-
world3 (update-world world2 10000)
89-
world4 (update-world (fire world3 "player-1") 10000)]
90-
(is (= (bullet-count world3) 0))
91-
(is (= (trace-count world3) 1))
97+
world3 (update-world world2 10000)]
98+
(is (= (alive-bullet-count world2) 1))
99+
(is (= (dead-bullets-count world2) 0))
92100

93-
(is (= (bullet-count world4) 0))
94-
(is (= (trace-count world4) 2))))
101+
(is (= (alive-bullet-count world3) 0))
102+
(is (= (dead-bullets-count world3) 1))))
95103

96104
(deftest kill-test
97105
(let [world1 (get-test-world)
98106
world2 (update-world (fire world1 "player-2") 10000)]
99-
(is (= (player-count world2) 1))))
107+
(is (= (alive-player-count world2) 1))))

test/hyperspace/test/utils.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@
55
(< (Math/abs (- a b)) eps))
66

77
(def almost= (partial compare-with-eps 1e-6))
8+
9+
(defn points-almost=
10+
[p1 p2]
11+
(and (almost= (:x p1) (:x p2))
12+
(almost= (:y p1) (:y p2))))
13+
14+
(defn vectors-almost=
15+
[v1 v2]
16+
;; Vector is almost the same as point, so we can reuse points-amlost= function here.
17+
(points-almost= v1 v2))

test/hyperspace/test/world.clj

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,9 @@
2323
(let [bullet (make-bullet (make-point 10 20)
2424
(make-vector 0 1))]
2525
(is (= (:center bullet) (make-point 10 20)))
26-
(is (= (:velocity bullet) (make-vector 0 1)))))
27-
28-
(deftest trace-test
29-
(let [bullet1 (make-bullet (make-point 1 0) (make-vector 0 0))
30-
bullet2 (make-bullet (make-point 10 20) (make-vector 0 0))
31-
trace1 (make-trace bullet1)
32-
trace2 (update-trace trace1 bullet2)]
33-
(is (= (:points trace1) [(make-point 1 0)]))
34-
(is (= (set (:points trace2))
35-
(set [(make-point 1 0)
36-
(make-point 10 20)])))))
26+
(is (= (:velocity bullet) (make-vector 0 1)))
27+
(is (= (:status bullet) :alive))
28+
(is (= (:traces bullet) []))))
3729

3830
(deftest world-test
3931
(let [planet1 (make-planet (make-point 1 0) 7000 10000)

0 commit comments

Comments
 (0)