aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear/Scene/GameObject.hs84
1 files changed, 45 insertions, 39 deletions
diff --git a/Spear/Scene/GameObject.hs b/Spear/Scene/GameObject.hs
index 37374cc..dbe7f00 100644
--- a/Spear/Scene/GameObject.hs
+++ b/Spear/Scene/GameObject.hs
@@ -2,12 +2,14 @@ module Spear.Scene.GameObject
2( 2(
3 GameObject 3 GameObject
4, GameStyle(..) 4, GameStyle(..)
5, AnimationSpeed 5, AM.AnimationSpeed
6 -- * Construction 6 -- * Construction
7, goNew 7, goNew
8 -- * Manipulation 8 -- * Manipulation
9, goUpdate 9, goUpdate
10, Spear.Scene.GameObject.setAnimationSpeed 10, currentAnimation
11, setAnimation
12, setAnimationSpeed
11, goAABB 13, goAABB
12 -- * Rendering 14 -- * Rendering
13, goRender 15, goRender
@@ -29,7 +31,7 @@ import Spear.Math.MatrixUtils
29import qualified Spear.Math.Spatial2 as S2 31import qualified Spear.Math.Spatial2 as S2
30import Spear.Math.Vector2 as V2 32import Spear.Math.Vector2 as V2
31import Spear.Math.Vector3 as V3 33import Spear.Math.Vector3 as V3
32import Spear.Render.AnimatedModel as AM 34import qualified Spear.Render.AnimatedModel as AM
33import Spear.Render.Program 35import Spear.Render.Program
34import Spear.Render.StaticModel as SM 36import Spear.Render.StaticModel as SM
35 37
@@ -45,10 +47,9 @@ data GameStyle
45-- | An object in the game scene. 47-- | An object in the game scene.
46data GameObject = GameObject 48data GameObject = GameObject
47 { gameStyle :: GameStyle 49 { gameStyle :: GameStyle
48 , renderer :: !(Either StaticModelRenderer AnimatedModelRenderer) 50 , renderer :: !(Either StaticModelRenderer AM.AnimatedModelRenderer)
49 , collisioner :: !Collisioner 51 , collisioner :: !Collisioner
50 , transform :: M3.Matrix3 52 , transform :: M3.Matrix3
51 , goUpdate :: Float -> GameObject
52 } 53 }
53 54
54 55
@@ -112,36 +113,56 @@ instance S2.Spatial2 GameObject where
112 113
113-- | Create a new game object. 114-- | Create a new game object.
114goNew :: GameStyle 115goNew :: GameStyle
115 -> Either StaticModelResource AnimatedModelResource 116 -> Either StaticModelResource AM.AnimatedModelResource
116 -> Collisioner 117 -> Collisioner
117 -> M3.Matrix3 118 -> M3.Matrix3
118 -> GameObject 119 -> GameObject
119 120
120goNew style (Left smr) col transf = 121goNew style (Left smr) col transf =
121 goUpdate' style (Left $ SM.staticModelRenderer smr) col transf 0 122 GameObject style (Left $ SM.staticModelRenderer smr) col transf
122 123
123goNew style (Right amr) col transf = 124goNew style (Right amr) col transf =
124 goUpdate' style (Right $ AM.animatedModelRenderer 1 amr) col transf 0 125 GameObject style (Right $ AM.animatedModelRenderer 1 amr) col transf
125 126
126 127
127goUpdate' :: GameStyle 128goUpdate :: Float -> GameObject -> GameObject
128 -> Either StaticModelRenderer AnimatedModelRenderer 129goUpdate dt go =
129 -> Collisioner 130 let rend = renderer go
130 -> M3.Matrix3 131 rend' = case rend of
131 -> Float
132 -> GameObject
133goUpdate' style rend col mat dt =
134 let rend' = case rend of
135 Left _ -> rend 132 Left _ -> rend
136 Right amr -> Right $ AM.update dt amr 133 Right amr -> Right $ AM.update dt amr
137 in 134 in go
138 GameObject 135 { renderer = rend'
139 { gameStyle = style 136 }
140 , renderer = rend 137
141 , collisioner = col 138
142 , transform = mat 139-- | Get the game object's current animation.
143 , goUpdate = goUpdate' style rend' col mat 140currentAnimation :: Enum a => GameObject -> a
144 } 141currentAnimation go = case renderer go of
142 Left _ -> toEnum 0
143 Right amr -> AM.currentAnimation amr
144
145
146-- | Set the game object's current animation.
147setAnimation :: Enum a => a -> GameObject -> GameObject
148setAnimation a go = case renderer go of
149 Left _ -> go
150 Right amr -> go { renderer = Right $ AM.setAnimation a amr }
151
152
153-- | Set the game object's animation speed.
154setAnimationSpeed :: AM.AnimationSpeed -> GameObject -> GameObject
155setAnimationSpeed s go = case renderer go of
156 Left _ -> go
157 Right amr -> go { renderer = Right $ AM.setAnimationSpeed s amr }
158
159
160-- | Get the game object's bounding box.
161goAABB :: GameObject -> AABB
162goAABB go =
163 case collisioner go of
164 (AABBCol box) -> box
165 (CircleCol circle) -> aabbFromCircle circle
145 166
146 167
147-- | Render the game object. 168-- | Render the game object.
@@ -190,18 +211,3 @@ goCollide :: [GameObject] -> GameObject -> [GameObject]
190goCollide gos go = foldl' collide' [] gos 211goCollide gos go = foldl' collide' [] gos
191 where 212 where
192 collide' gos target = target:gos 213 collide' gos target = target:gos
193
194
195-- | Set the game object's animation speed.
196setAnimationSpeed :: AnimationSpeed -> GameObject -> GameObject
197setAnimationSpeed s go = case renderer go of
198 Left _ -> go
199 Right amr -> go { renderer = Right $ AM.setAnimationSpeed s amr }
200
201
202-- | Get the game object's bounding box.
203goAABB :: GameObject -> AABB
204goAABB go =
205 case collisioner go of
206 (AABBCol box) -> box
207 (CircleCol circle) -> aabbFromCircle circle