From: Daniel Liu Date: Sat, 1 May 2021 18:32:50 +0000 (-0400) Subject: basic water update X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=b01133aa41dd617c27be81eabe33f90b69cbf618;p=ekitaihs.git basic water update --- diff --git a/Sim.hs b/Sim.hs index 8dd5819..c48dbe4 100644 --- a/Sim.hs +++ b/Sim.hs @@ -1,18 +1,18 @@ -module Sim ( initSimSpace, testSim, physStep ) where +module Sim ( initSimSpace, testSim, physStep, updateWaterChunk, validDirects ) where +import Debug.Trace import Data.Vector ((!), (//)) import qualified Data.Vector as V --- Simulation simSpace simW simH data Simulation = Simulation { simSpace :: V.Vector ChunkData , simW :: Int , simH :: Int } deriving (Show) -data ChunkType = Empty +data ChunkType = Air | Water - | Wall deriving (Show) + | Wall deriving (Show, Enum) data ChunkData = ChunkData { chunkType :: ChunkType @@ -21,29 +21,48 @@ data ChunkData = ChunkData -- vec accessors simGet Simulation{simSpace=s,simW=w} x y = s ! (y*w+x) simSet sim@Simulation{simSpace=s,simW=w,simH=h} c x y = sim { simSpace = (s // [(y*w+x,c)]) } +simGetChunkType sim x y = chunkType $ simGet sim x y initSimSpace x y = Simulation - { simSpace = V.replicate (y*x) ChunkData { chunkType=Empty } + { simSpace = V.replicate (y*x) ChunkData { chunkType=Air } , simW = x , simH = y } -testSim = simSet (initSimSpace 10 10) (ChunkData Water) 5 0 +testSim = simSet (initSimSpace 3 3) (ChunkData Water) 1 1 -physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) sim +physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] sim sim +-- _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} | trace ("" ++ show acc) False = undefined _physStep grid acc sim@Simulation{simSpace=s,simW=w,simH=h} = if null grid then acc else _physStep (tail grid) next sim where x = fst $ head grid y = snd $ head grid - next = sim + valid = validDirects x y w h + next = case simGetChunkType sim x y of + Water -> updateWaterChunk x y valid acc + _ -> acc -- takes in list of valid chunks, as well as the sim to modify --- updateWaterChunk valid sim = - +updateWaterChunk x y valid sim = + -- if the chunk below is free, fall straight down + if elem (0,-1) valid && fromEnum (simGetChunkType sim x (y-1)) == fromEnum Air + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } x (y-1) + else if elem (-1,-1) valid && fromEnum (simGetChunkType sim (x-1) (y-1)) == fromEnum Air + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } (x-1) (y-1) + else if elem (1,-1) valid && fromEnum (simGetChunkType sim (x+1) (y-1)) == fromEnum Air + then + let moved = simSet sim ChunkData { chunkType=Air } x y + in simSet moved ChunkData { chunkType=Water } (x+1) (y-1) + -- stay put + else sim -- gets chunks around a given chunk that are inside grid validDirects x y w h = filter (\q -> 0 <= (fst q) && (fst q) < w && (snd q) <= 0 && (snd q) < h) - [(a,b) | a <- [x-1..x+1], b <- [y-1..y+1], not (a==x && b==y)] + [(a-x,b-y) | a <- [x-1..x+1], b <- [y-1..y+1], not (a==x && b==y)]