|
1 | 1 | (ns hyperspace.world |
2 | 2 | (:use [clojure.pprint :only (pprint)] |
3 | 3 | [clojure.java.io :only (reader writer)] |
4 | | - [hyperspace.misc])) |
| 4 | + [hyperspace geometry misc])) |
5 | 5 |
|
6 | 6 | (def missile-radius 5) |
7 | 7 | (def player-radius 15) |
|
11 | 11 | (def amount-of-players 2) |
12 | 12 | (def amount-of-missiles 0) |
13 | 13 |
|
| 14 | + |
| 15 | +(defn generate-without-intersection |
| 16 | + "Genereate new object (player, planet etc). fgen should be 0-arity function" |
| 17 | + [{:keys [planets] :as world} fgen] |
| 18 | + (first |
| 19 | + (remove #(circle-X-any-circle? % planets) |
| 20 | + (repeatedly fgen)))) |
| 21 | + |
14 | 22 | ;;; Planets related stuff |
15 | 23 |
|
16 | 24 | (defn make-planet |
|
21 | 29 | :mass (* (Math/sqrt radius) |
22 | 30 | (Math/pow 10 7))}) |
23 | 31 |
|
24 | | -(defn add-random-planet |
| 32 | +(defn generate-random-planet |
25 | 33 | [{planets :planets |
26 | 34 | [x, y] :position |
27 | 35 | [width, height] :size |
28 | 36 | :as world}] |
29 | 37 | (let [radius (-> (min width height) (/ 5) rand) |
30 | 38 | x (rand-range (+ x radius) (- (+ x width) radius)) |
31 | 39 | y (rand-range (+ y radius) (- (+ y height) radius))] |
32 | | - (assoc world |
33 | | - :planets (conj planets (make-planet [x, y] radius))))) |
| 40 | + (make-planet [x y] radius))) |
| 41 | + |
| 42 | +(defn add-random-planet |
| 43 | + [world] |
| 44 | + (let [p (generate-without-intersection world (partial generate-random-planet world))] |
| 45 | + (update-in world [:planets] conj p))) |
34 | 46 |
|
35 | 47 | (defn generate-planets |
36 | 48 | [world] |
|
79 | 91 | :heading heading |
80 | 92 | :radius player-radius}) |
81 | 93 |
|
82 | | -(defn add-random-player |
| 94 | +(defn generate-random-player |
83 | 95 | [{[x, y] :position |
84 | 96 | [width, height] :size |
85 | 97 | players :players |
86 | 98 | :as world}] |
87 | 99 | (let [x (rand-range (+ x player-radius) (- (+ x width) player-radius)) |
88 | 100 | y (rand-range (+ y player-radius) (- (+ y height) player-radius))] |
89 | | - (assoc world |
90 | | - :players (conj players (make-player [x, y] [0, 3.0]))))) |
| 101 | + (make-player [x y] [0 3.0]))) |
| 102 | + |
| 103 | +(defn add-random-player |
| 104 | + [world] |
| 105 | + (let [player (generate-without-intersection world (partial generate-random-player world))] |
| 106 | + (update-in world [:players] conj player))) |
91 | 107 |
|
92 | 108 | (defn generate-players |
93 | 109 | [world] |
|
0 commit comments