apecs 0.1.0.0 → 0.1.1.0
raw patch · 7 files changed
+19/−170 lines, 7 filesdep +linear
Dependencies added: linear
Files
- README.md +1/−1
- apecs.cabal +4/−5
- bench/Main.hs +2/−1
- example/RTS.hs +5/−5
- example/Simple.hs +5/−2
- src/Apecs/Vector.hs +0/−154
- tutorials/RTS.md +2/−2
README.md view
@@ -20,7 +20,7 @@ import Apecs import Apecs.Stores import Apecs.Util-import Apecs.Vector -- Optional module for basic 2D and 3D vectos+import Linear.V2 -- Component data definitions newtype Velocity = Velocity (V2 Double) deriving (Eq, Show)
apecs.cabal view
@@ -1,5 +1,5 @@ name: apecs-version: 0.1.0.0+version: 0.1.1.0 homepage: https://github.com/jonascarpay/apecs#readme license: BSD3 license-file: LICENSE@@ -17,7 +17,6 @@ src exposed-modules: Apecs,- Apecs.Vector, Apecs.Stores, Apecs.Util other-modules:@@ -40,7 +39,7 @@ main-is: Simple.hs build-depends:- base, apecs+ base, apecs, linear default-language: Haskell2010 ghc-options:@@ -53,7 +52,7 @@ main-is: RTS.hs build-depends:- base, apecs, sdl2, random+ base, apecs, sdl2, random, linear default-language: Haskell2010 ghc-options:@@ -69,7 +68,7 @@ main-is: Main.hs build-depends:- base, apecs, criterion+ base, apecs, criterion, linear default-language: Haskell2010 ghc-options:
bench/Main.hs view
@@ -7,7 +7,8 @@ import Apecs as A import Apecs.Stores import Apecs.Util-import Apecs.Vector++import Linear newtype Position = Position (V2 Float) deriving (Eq, Show) instance Component Position where
example/RTS.hs view
@@ -8,16 +8,15 @@ module Main where import Control.Monad as M-import SDL.Vect import qualified SDL import SDL (($=)) import System.Random import Data.Proxy+import SDL.Vect import Apecs as A import Apecs.Stores import Apecs.Util-import qualified Apecs.Vector as V hres, vres :: Num a => a hres = 1024@@ -93,11 +92,12 @@ SDL.present renderer step = do- let speed = 5+ let speed :: Num a => a+ speed = 5 stepPosition :: (Target, Position) -> Safe (Target, Position) stepPosition (Target t, Position p)- | V.vlength (p-t) < speed = Safe (Nothing, Just (Position t))- | otherwise = Safe (Just (Target t), Just (Position (p + V.setLength speed (t-p))))+ | norm (p-t) < speed = Safe (Nothing, Just (Position t))+ | otherwise = Safe (Just (Target t), Just (Position (p + speed * normalize (t-p)))) cmap' stepPosition
example/Simple.hs view
@@ -3,7 +3,7 @@ import Apecs import Apecs.Stores import Apecs.Util-import Apecs.Vector -- Optional module for basic 2D and 3D vectos+import Linear -- Component data definitions newtype Velocity = Velocity (V2 Double) deriving (Eq, Show)@@ -41,7 +41,7 @@ game :: System' () game = do -- Create new entities- newEntity (Position 0)+ ety <- newEntity (Position 0) -- Components can be composed using tuples newEntity (Position 0, Velocity 1) -- Tagging one as an enemy is a matter of adding the constructor@@ -51,6 +51,9 @@ liftIO$ putStrLn "Stepping velocities" -- rmap maps a pure function over all entities in its domain rmap $ \(Position p, Velocity v) -> Position (v+p)++ -- Set can be used to (over)write components+ set ety (Position 2, Enemy) -- Print the positions of all enemies cmapM_ $ \(Enemy, Position p) -> liftIO (print p)
− src/Apecs/Vector.hs
@@ -1,154 +0,0 @@--- | A lightweight version of Edward Kmett's linear, included for convenience' sake--{-# LANGUAGE TypeFamilyDependencies, ScopedTypeVariables, FlexibleContexts #-}-{-# LANGUAGE ViewPatterns #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleInstances #-}--module Apecs.Vector where--import Control.Applicative--{-# INLINE dot #-}-dot :: (Num (v a), Num a, Foldable v) => v a -> v a -> a-dot a b = sum $ a * b--{-# INLINE vlength #-}-vlength :: (Foldable v, Num (v a), Floating a) => v a -> a-vlength a = sqrt (dot a a)--{-# INLINE setLength #-}-setLength :: (Num (f b), Functor f, Floating b, Foldable f) => b -> f b -> f b-setLength r v = let l = vlength v in fmap ((*r).(/l)) v--{-# INLINE normalize #-}-normalize :: (Num (v b), Floating b, Foldable v, Functor f) => v b -> f b -> f b-normalize v = fmap (/vlength v)----- V2-data V2 a = V2 !a !a deriving (Eq, Show)--instance Functor V2 where- {-# INLINE fmap #-}- fmap f (V2 a b) = V2 (f a) (f b)--instance Applicative V2 where- {-# INLINE (<*>) #-}- V2 fx fy <*> V2 x y = V2 (fx x) (fy y)- {-# INLINE pure #-}- pure x = V2 x x--instance Num a => Num (V2 a) where- (+) = liftA2 (+)- {-# INLINE (+) #-}- (-) = liftA2 (-)- {-# INLINE (-) #-}- (*) = liftA2 (*)- {-# INLINE (*) #-}- negate = fmap negate- {-# INLINE negate #-}- abs = fmap abs- {-# INLINE abs #-}- signum = fmap signum- {-# INLINE signum #-}- fromInteger = pure . fromInteger- {-# INLINE fromInteger #-}--instance Fractional a => Fractional (V2 a) where- (/) = liftA2 (/)- {-# INLINE (/) #-}- fromRational = pure . fromRational- {-# INLINE fromRational #-}--instance Foldable V2 where- foldMap f (V2 x y) = f x `mappend` f y- foldr f seed (V2 x y) = f x (f y seed)- foldr1 f (V2 x y) = f x y- foldl f seed (V2 x y) = f (f seed x) y- foldl1 f (V2 x y) = f x y- null _ = False- length _ = 2- elem a (V2 x y) = x == a || y == a- minimum (V2 x y) = min x y- maximum (V2 x y) = max x y- sum (V2 x y) = x + y- product (V2 x y) = x * y- {-# INLINE foldMap #-}- {-# INLINE foldr #-}- {-# INLINE foldr1 #-}- {-# INLINE foldl #-}- {-# INLINE foldl1 #-}- {-# INLINE null #-}- {-# INLINE length #-}- {-# INLINE elem #-}- {-# INLINE minimum #-}- {-# INLINE maximum #-}- {-# INLINE product #-}- {-# INLINE sum #-}---- V3-data V3 a = V3 !a !a !a deriving (Eq, Show)--instance Functor V3 where- {-# INLINE fmap #-}- fmap f (V3 a b c) = V3 (f a) (f b) (f c)--instance Applicative V3 where- {-# INLINE (<*>) #-}- V3 fx fy fz <*> V3 x y z = V3 (fx x) (fy y) (fz z)- {-# INLINE pure #-}- pure x = V3 x x x--instance Num a => Num (V3 a) where- (+) = liftA2 (+)- {-# INLINE (+) #-}- (-) = liftA2 (-)- {-# INLINE (-) #-}- (*) = liftA2 (*)- {-# INLINE (*) #-}- negate = fmap negate- {-# INLINE negate #-}- abs = fmap abs- {-# INLINE abs #-}- signum = fmap signum- {-# INLINE signum #-}- fromInteger = pure . fromInteger- {-# INLINE fromInteger #-}--instance Fractional a => Fractional (V3 a) where- (/) = liftA2 (/)- {-# INLINE (/) #-}- fromRational = pure . fromRational- {-# INLINE fromRational #-}--instance Foldable V3 where- foldMap f (V3 x y z) = f x `mappend` f y `mappend` f z- foldr f seed (V3 x y z) = f x (f y (f z seed))- foldr1 f (V3 x y z) = f x (f y z)- foldl f seed (V3 x y z) = f (f (f seed x) y) z- foldl1 f (V3 x y z) = f (f x y) z- null _ = False- length _ = 3- elem a (V3 x y z) = x == a || y == a || z == a- minimum (V3 x y z) = min (min x y) z- maximum (V3 x y z) = max (max x y) z- sum (V3 x y z) = x + y + z- product (V3 x y z) = x * y * z- {-# INLINE foldMap #-}- {-# INLINE foldr #-}- {-# INLINE foldr1 #-}- {-# INLINE foldl #-}- {-# INLINE foldl1 #-}- {-# INLINE null #-}- {-# INLINE length #-}- {-# INLINE elem #-}- {-# INLINE minimum #-}- {-# INLINE maximum #-}- {-# INLINE product #-}- {-# INLINE sum #-}--{-# INLINE outer #-}-outer :: Num a => V3 a -> V3 a -> V3 a-V3 a b c `outer` V3 d e f = V3 (b*f - e*c) (c*d - a*f) (a*e - b*d)-
tutorials/RTS.md view
@@ -181,7 +181,7 @@ stepPosition :: (Target, Position) -> Safe (Target, Position) stepPosition (Target t, Position p) | V.vlength (p-t) < speed = Safe (Nothing, Just (Position t))- | otherwise = Safe (Just (Target t), Just (Position (p + V.setLength speed (t-p))))+ | otherwise = Safe (Just (Target t), Just (Position (p + speed * normalize (t-p)))) cmap' stepPosition ```@@ -237,7 +237,7 @@ How do you direct a group of units? You can't just send them all to the same location, or they'd end up overlapping. For simplicity's sake, I chose to arrange them randomly in a square, with area proportional to the number of selected units.-```+```haskell handleEvent (SDL.MouseButtonEvent (SDL.MouseButtonEventData _ SDL.Pressed _ SDL.ButtonRight _ (P (V2 px py)))) = do sl :: Slice Selected <- slice All let r = (*3) . subtract 1 . sqrt . fromIntegral$ sliceSize sl