source: scenarios/GerminationX/oak/src/oak/game_world.clj @ 873

Revision 873, 2.8 KB checked in by dave, 10 years ago (diff)

larger visible world, hacked plant types, lazy stack problem possibly fixed

Line 
1;; Copyright (C) 2010 FoAM vzw
2;; This program is free software: you can redistribute it and/or modify
3;; it under the terms of the GNU Affero General Public License as
4;; published by the Free Software Foundation, either version 3 of the
5;; License, or (at your option) any later version.
6;;
7;; This program is distributed in the hope that it will be useful,
8;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10;; GNU Affero General Public License for more details.
11;;
12;; You should have received a copy of the GNU Affero General Public License
13;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15(ns oak.game-world
16  (:use
17   oak.forms
18   oak.vec2
19   oak.plant
20   oak.tile
21   oak.rand)
22  (:require
23   clojure.contrib.math))
24
25(defrecord game-world
26  [players
27   tiles
28   spirits])
29
30(defn game-world-players [game-world] (:players game-world))
31(defn game-world-tiles [game-world] (:tiles game-world))
32(defn game-world-spirits [game-world] (:spirits game-world))
33
34(defn game-world-get-tile [game-world pos]
35  (reduce
36   (fn [r t]
37     (if (and (not r) (vec2-eq? pos (:pos t)))
38       t
39       r))
40   false
41   (:tiles game-world)))
42
43(defn game-world-add-tile [game-world tile]
44  (merge game-world {:tiles (cons tile (game-world-tiles game-world))}))
45
46(defn game-world-modify-tile [game-world pos f]
47  (modify :tiles
48          (fn [tiles]
49            (map
50             (fn [t]
51               (if (vec2-eq? (:pos t) pos) (f t) t))
52             tiles))
53          game-world))
54
55(defn game-world-add-entity [game-world tile-pos entity]
56  (let [tile (game-world-get-tile game-world tile-pos)]
57    (if (not tile)
58      (game-world-add-tile game-world (make-tile tile-pos (list entity)))
59      (game-world-modify-tile
60       game-world
61       tile-pos
62       (fn [tile]
63         (tile-add-entity tile entity))))))
64
65(defn make-game-world [num-plants area]
66  (reduce
67   (fn [world plant]
68     (game-world-add-entity
69      world
70      (make-vec2
71       (Math/round (* (rand-gaussian) area))
72       (Math/round (* (rand-gaussian) area)))
73      plant))
74   (game-world. () {} ())
75   (repeatedly num-plants (fn [] (make-random-plant)))))
76
77(defn game-world-print [game-world]
78  (doseq [tile (game-world-tiles game-world)]
79    (println (format "tile %d %d" (:x (tile-pos tile)) (:y (tile-pos tile))))
80    (doseq [plant (tile-entities tile)]
81      (println (format "plant %d %d state: %s health: %d"
82                       (:x (:pos plant)) (:y (:pos plant))
83                       (:state plant) (:health plant))))))
84
85(defn game-world-update [game-world time delta]
86  (modify :tiles
87          (fn [tiles]
88            (doall (map
89                    (fn [tile]
90                      (tile-update tile time delta))
91                    tiles)))
92          game-world))
Note: See TracBrowser for help on using the repository browser.