diff options
author | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-30 15:58:00 +0200 |
---|---|---|
committer | Marc Sunet <jeannekamikaze@gmail.com> | 2012-08-30 15:58:00 +0200 |
commit | 5abde1cb5279c193573a8d612cdbbb4f5fb35254 (patch) | |
tree | 27aa41fa5291f0f4267fc91930aeb1b0b3f9590a | |
parent | 4f5d2769cbf308f17a33ca6576d95b4b3478ca72 (diff) |
Added support for multiple collisioners in GameObject
-rw-r--r-- | Spear/Scene/GameObject.hs | 58 | ||||
-rw-r--r-- | Spear/Scene/Loader.hs | 4 |
2 files changed, 38 insertions, 24 deletions
diff --git a/Spear/Scene/GameObject.hs b/Spear/Scene/GameObject.hs index dbe7f00..266d1ca 100644 --- a/Spear/Scene/GameObject.hs +++ b/Spear/Scene/GameObject.hs | |||
@@ -5,12 +5,15 @@ module Spear.Scene.GameObject | |||
5 | , AM.AnimationSpeed | 5 | , AM.AnimationSpeed |
6 | -- * Construction | 6 | -- * Construction |
7 | , goNew | 7 | , goNew |
8 | -- * Accessors | ||
9 | , currentAnimation | ||
10 | , numCollisioners | ||
11 | , goAABB | ||
8 | -- * Manipulation | 12 | -- * Manipulation |
9 | , goUpdate | 13 | , goUpdate |
10 | , currentAnimation | ||
11 | , setAnimation | 14 | , setAnimation |
12 | , setAnimationSpeed | 15 | , setAnimationSpeed |
13 | , goAABB | 16 | , withCollisioners |
14 | -- * Rendering | 17 | -- * Rendering |
15 | , goRender | 18 | , goRender |
16 | -- * Collision | 19 | -- * Collision |
@@ -46,17 +49,17 @@ data GameStyle | |||
46 | 49 | ||
47 | -- | An object in the game scene. | 50 | -- | An object in the game scene. |
48 | data GameObject = GameObject | 51 | data GameObject = GameObject |
49 | { gameStyle :: GameStyle | 52 | { gameStyle :: !GameStyle |
50 | , renderer :: !(Either StaticModelRenderer AM.AnimatedModelRenderer) | 53 | , renderer :: !(Either StaticModelRenderer AM.AnimatedModelRenderer) |
51 | , collisioner :: !Collisioner | 54 | , collisioners :: ![Collisioner] |
52 | , transform :: M3.Matrix3 | 55 | , transform :: !M3.Matrix3 |
53 | } | 56 | } |
54 | 57 | ||
55 | 58 | ||
56 | instance S2.Spatial2 GameObject where | 59 | instance S2.Spatial2 GameObject where |
57 | 60 | ||
58 | move v go = go | 61 | move v go = go |
59 | { collisioner = Col.move v $ collisioner go | 62 | { collisioners = fmap (Col.move v) $ collisioners go |
60 | , transform = M3.translv v * transform go | 63 | , transform = M3.translv v * transform go |
61 | } | 64 | } |
62 | 65 | ||
@@ -64,7 +67,7 @@ instance S2.Spatial2 GameObject where | |||
64 | let m = transform go | 67 | let m = transform go |
65 | v = V2.scale s $ M3.forward m | 68 | v = V2.scale s $ M3.forward m |
66 | in go | 69 | in go |
67 | { collisioner = Col.move v $ collisioner go | 70 | { collisioners = fmap (Col.move v) $ collisioners go |
68 | , transform = M3.translv v * m | 71 | , transform = M3.translv v * m |
69 | } | 72 | } |
70 | 73 | ||
@@ -72,7 +75,7 @@ instance S2.Spatial2 GameObject where | |||
72 | let m = transform go | 75 | let m = transform go |
73 | v = V2.scale (-s) $ M3.forward m | 76 | v = V2.scale (-s) $ M3.forward m |
74 | in go | 77 | in go |
75 | { collisioner = Col.move v $ collisioner go | 78 | { collisioners = fmap (Col.move v) $ collisioners go |
76 | , transform = M3.translv v * m | 79 | , transform = M3.translv v * m |
77 | } | 80 | } |
78 | 81 | ||
@@ -80,7 +83,7 @@ instance S2.Spatial2 GameObject where | |||
80 | let m = transform go | 83 | let m = transform go |
81 | v = V2.scale (-s) $ M3.right m | 84 | v = V2.scale (-s) $ M3.right m |
82 | in go | 85 | in go |
83 | { collisioner = Col.move v $ collisioner go | 86 | { collisioners = fmap (Col.move v) $ collisioners go |
84 | , transform = M3.translv v * m | 87 | , transform = M3.translv v * m |
85 | } | 88 | } |
86 | 89 | ||
@@ -88,7 +91,7 @@ instance S2.Spatial2 GameObject where | |||
88 | let m = transform go | 91 | let m = transform go |
89 | v = V2.scale s $ M3.right m | 92 | v = V2.scale s $ M3.right m |
90 | in go | 93 | in go |
91 | { collisioner = Col.move v $ collisioner go | 94 | { collisioners = fmap (Col.move v) $ collisioners go |
92 | , transform = M3.translv v * m | 95 | , transform = M3.translv v * m |
93 | } | 96 | } |
94 | 97 | ||
@@ -114,15 +117,15 @@ instance S2.Spatial2 GameObject where | |||
114 | -- | Create a new game object. | 117 | -- | Create a new game object. |
115 | goNew :: GameStyle | 118 | goNew :: GameStyle |
116 | -> Either StaticModelResource AM.AnimatedModelResource | 119 | -> Either StaticModelResource AM.AnimatedModelResource |
117 | -> Collisioner | 120 | -> [Collisioner] |
118 | -> M3.Matrix3 | 121 | -> M3.Matrix3 |
119 | -> GameObject | 122 | -> GameObject |
120 | 123 | ||
121 | goNew style (Left smr) col transf = | 124 | goNew style (Left smr) cols transf = |
122 | GameObject style (Left $ SM.staticModelRenderer smr) col transf | 125 | GameObject style (Left $ SM.staticModelRenderer smr) cols transf |
123 | 126 | ||
124 | goNew style (Right amr) col transf = | 127 | goNew style (Right amr) cols transf = |
125 | GameObject style (Right $ AM.animatedModelRenderer 1 amr) col transf | 128 | GameObject style (Right $ AM.animatedModelRenderer 1 amr) cols transf |
126 | 129 | ||
127 | 130 | ||
128 | goUpdate :: Float -> GameObject -> GameObject | 131 | goUpdate :: Float -> GameObject -> GameObject |
@@ -157,12 +160,23 @@ setAnimationSpeed s go = case renderer go of | |||
157 | Right amr -> go { renderer = Right $ AM.setAnimationSpeed s amr } | 160 | Right amr -> go { renderer = Right $ AM.setAnimationSpeed s amr } |
158 | 161 | ||
159 | 162 | ||
160 | -- | Get the game object's bounding box. | 163 | -- | Return the game object's number of collisioners. |
161 | goAABB :: GameObject -> AABB | 164 | numCollisioners :: GameObject -> Int |
162 | goAABB go = | 165 | numCollisioners = length . collisioners |
163 | case collisioner go of | 166 | |
164 | (AABBCol box) -> box | 167 | |
165 | (CircleCol circle) -> aabbFromCircle circle | 168 | -- | Manipulate the game object's collisioners. |
169 | withCollisioners :: GameObject -> ([Collisioner] -> [Collisioner]) -> GameObject | ||
170 | withCollisioners go f = go { collisioners = f $ collisioners go } | ||
171 | |||
172 | |||
173 | -- | Get the game object's ith bounding box. | ||
174 | goAABB :: Int -> GameObject -> AABB | ||
175 | goAABB i go = goAABB' $ (collisioners go) !! i | ||
176 | |||
177 | goAABB' col = case col of | ||
178 | (AABBCol box) -> box | ||
179 | (CircleCol circle) -> aabbFromCircle circle | ||
166 | 180 | ||
167 | 181 | ||
168 | -- | Render the game object. | 182 | -- | Render the game object. |
diff --git a/Spear/Scene/Loader.hs b/Spear/Scene/Loader.hs index 64ad622..081b927 100644 --- a/Spear/Scene/Loader.hs +++ b/Spear/Scene/Loader.hs | |||
@@ -317,11 +317,11 @@ loadGO style sceneRes props transf = do | |||
317 | let animSpeed = asFloat . value "animation-speed" $ props | 317 | let animSpeed = asFloat . value "animation-speed" $ props |
318 | go <- case getAnimatedModel sceneRes modelName of | 318 | go <- case getAnimatedModel sceneRes modelName of |
319 | Just model -> | 319 | Just model -> |
320 | return $ goNew style (Right model) (AABBCol $ AM.box 0 model) transf | 320 | return $ goNew style (Right model) [] transf |
321 | Nothing -> | 321 | Nothing -> |
322 | case getStaticModel sceneRes modelName of | 322 | case getStaticModel sceneRes modelName of |
323 | Just model -> | 323 | Just model -> |
324 | return $ goNew style (Left model) (AABBCol $ SM.box 0 model) transf | 324 | return $ goNew style (Left model) [] transf |
325 | Nothing -> | 325 | Nothing -> |
326 | setupError $ "model " ++ modelName ++ " not found" | 326 | setupError $ "model " ++ modelName ++ " not found" |
327 | return $ case animSpeed of | 327 | return $ case animSpeed of |