From 6f4455cbc7cd118ae0771761304b1e286b71f952 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 1 May 2021 12:51:10 -0400 Subject: [PATCH] rewrote to use record instead --- Sim.hs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/Sim.hs b/Sim.hs index 7f1688f..8dd5819 100644 --- a/Sim.hs +++ b/Sim.hs @@ -1,33 +1,47 @@ -module Sim ( initSimSpace, testSim, physStep, validDirects ) where +module Sim ( initSimSpace, testSim, physStep ) where import Data.Vector ((!), (//)) import qualified Data.Vector as V -- Simulation simSpace simW simH -data Simulation = Simulation (V.Vector ChunkData) Int Int deriving (Show) +data Simulation = Simulation + { simSpace :: V.Vector ChunkData + , simW :: Int + , simH :: Int + } deriving (Show) data ChunkType = Empty | Water | Wall deriving (Show) -data ChunkData = ChunkData ChunkType deriving (Show) +data ChunkData = ChunkData + { chunkType :: ChunkType + } deriving (Show) -- vec accessors -simGet (Simulation s w _) x y = s ! (y*w+x) -simSet (Simulation s w h) c x y = Simulation (s // [(y*w+x,c)]) w h +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)]) } -initSimSpace x y = Simulation (V.replicate (y*x) (ChunkData Empty)) x y +initSimSpace x y = Simulation + { simSpace = V.replicate (y*x) ChunkData { chunkType=Empty } + , simW = x + , simH = y + } testSim = simSet (initSimSpace 10 10) (ChunkData Water) 5 0 -physStep sim@(Simulation _ w h) = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) sim -_physStep grid acc sim@(Simulation s w h) = +physStep sim@Simulation{simW=w,simH=h} = _physStep [(x, y) | x <- [0..w-1], y <- [0..h-1]] (initSimSpace w h) sim +_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 +-- takes in list of valid chunks, as well as the sim to modify +-- updateWaterChunk valid 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) -- 2.20.1