reworked phystep loop
[ekitaihs.git] / Sim.hs
1 module Sim ( initSimSpace, testSim, physStep, validDirects ) where
2
3 import           Data.Vector ((!), (//))
4 import qualified Data.Vector as V
5
6 -- Simulation simSpace simW simH
7 data Simulation = Simulation (V.Vector ChunkData) Int Int deriving (Show)
8
9 data ChunkType = Empty
10     | Water 
11     | Wall deriving (Show)
12
13 data ChunkData = ChunkData ChunkType deriving (Show)
14
15 -- vec accessors
16 simGet (Simulation s w _) x y = s ! (y*w+x)
17 simSet (Simulation s w h) c x y = Simulation (s // [(y*w+x,c)]) w h
18
19 initSimSpace x y = Simulation (V.replicate (y*x) (ChunkData Empty)) x y
20
21 testSim = simSet (initSimSpace 10 10) (ChunkData Water) 5 0
22
23 physStep sim@(Simulation _ w h) = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) sim
24 _physStep grid acc sim@(Simulation s w h) =
25     if null grid then acc
26     else _physStep (tail grid) next sim
27     where x = fst $ head grid
28           y = snd $ head grid
29           next = sim
30
31 -- gets chunks around a given chunk that are inside grid
32 validDirects x y w h = filter
33     (\q -> 0 <= (fst q) && (fst q) < w && (snd q) <= 0 && (snd q) < h)
34     [(a,b) | a <- [x-1..x+1], b <- [y-1..y+1], not (a==x && b==y)]
35