Changeset 844


Ignore:
Timestamp:
01/17/2011 09:22:57 PM (10 years ago)
Author:
dave
Message:

added game world persistence

Location:
scenarios/GerminationX/oak
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • scenarios/GerminationX/oak/public/index.html

    r816 r844  
    55<title>germination x</title>  
    66</head>  
    7 <body bgcolor="#000000">  
    8  
     7<body>  
    98<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
    109        width="50" 
  • scenarios/GerminationX/oak/src/oak/core.clj

    r817 r844  
    77   oak.remote-agent 
    88   oak.io 
    9    oak.island) 
    10   (:import java.util.concurrent.Executors) 
     9   oak.island 
     10   oak.game-world 
     11   oak.vec2 
     12   oak.plant 
     13   oak.tile) 
     14  (:import 
     15   java.util.concurrent.Executors) 
    1116  (:require [compojure.route :as route] 
    1217            [org.danlarkin.json :as json]))  
     18 
     19(defn game-world-save [game-world fn] 
     20  (serialise game-world fn)) 
     21 
     22(defn game-world-load [fn] 
     23  (deserialise fn)) 
     24 
     25(def state-filename "state.txt") 
    1326 
    1427(def myworld 
     
    2134             "AppleTree")))) 
    2235 
     36(def my-game-world (ref (game-world-load state-filename))) 
     37(println (deref my-game-world)) 
     38;(def my-game-world (ref (make-game-world))) 
     39 
    2340;(world-crank (deref myworld)) 
    2441 
    2542(defroutes main-routes 
     43  (GET "/get-tile/:tilex/:tiley" [tilex tiley] 
     44       (println (list tilex tiley)) 
     45       (let [tile (game-world-get-tile (deref my-game-world) 
     46                                       (make-vec2 tilex tiley))] 
     47         (if tile 
     48           (json/encode-to-str tile) 
     49           (json/encode-to-str '())))) 
     50  (GET "/make-plant/:tilex/:tiley/:posx/:posy/:type/:owner" 
     51       [tilex tiley posx posy type owner] 
     52       (println (list tilex tiley posx posy type owner)) 
     53       (dosync 
     54        (ref-set my-game-world 
     55                 (game-world-add-entity 
     56                  (deref my-game-world) 
     57                  (make-vec2 tilex tiley) 
     58                  (make-plant (make-vec2 posx posy) type owner)))) 
     59       (game-world-save (deref my-game-world) state-filename) 
     60       (println (deref my-game-world))) 
    2661  (GET "/spirit-sprites" [] 
    2762       (println (read-islands "./public/islands")) 
  • scenarios/GerminationX/oak/src/oak/io.clj

    r437 r844  
    1414 
    1515(ns oak.io 
     16  (:use 
     17   clojure.xml 
     18   clojure.contrib.io) 
    1619  (:import 
     20   java.io.File 
     21   java.io.FileInputStream 
     22   java.io.FileOutputStream 
     23   java.io.PushbackReader 
     24   java.io.FileReader 
    1725   java.net.Socket 
    1826   java.nio.ByteBuffer 
     
    2129   java.nio.charset.Charset 
    2230   java.io.IOException 
    23    java.io.ByteArrayInputStream) 
    24   (use clojure.xml)) 
     31   java.io.ByteArrayInputStream)) 
    2532 
    2633;(defn msg-waiting? [socket] 
     
    4855;     (. e printStackTrace) 
    4956;     false))) 
     57 
     58(comment 
     59(defn serialise 
     60  "Print a data structure to a file so that we may read it in later." 
     61  [data-structure #^String filename] 
     62  (with-out-writer 
     63    (java.io.File. filename) 
     64    (binding [*print-dup* true] (prn data-structure)))) 
     65 
     66;; This allows us to then read in the structure at a later time, like so: 
     67(defn deserialise [filename] 
     68  (with-open [r (PushbackReader. (FileReader. filename))] 
     69    (read r)))) 
     70 
     71(defn serialise [o filename] 
     72  (with-open [outp (-> (java.io.File. filename) java.io.FileOutputStream. java.io.ObjectOutputStream.)] 
     73    (.writeObject outp o))) 
     74 
     75(defn deserialise [filename] 
     76  (with-open [inp (-> (java.io.File. filename) java.io.FileInputStream. java.io.ObjectInputStream.)] 
     77    (.readObject inp))) 
    5078 
    5179(def buf (ByteBuffer/allocateDirect 4096)) 
Note: See TracChangeset for help on using the changeset viewer.