about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <git@lukasepple.de>2019-05-24 23:46:23 +0200
committersternenseemann <git@lukasepple.de>2019-05-24 23:46:23 +0200
commit83481477ee6677214ab7fd90c3537da2dab20a6a (patch)
tree802886a08fe04f2a63552e243c42de3e41488fbc
parentef8f1f5226349cd8e4f1a539a8d7555b18f706f9 (diff)
limit lifetime of projectiles
-rw-r--r--README.md3
-rw-r--r--lib/Grav2ty/Control.hs44
-rw-r--r--lib/Grav2ty/Simulation.hs31
-rw-r--r--src/Main.hs4
4 files changed, 45 insertions, 37 deletions
diff --git a/README.md b/README.md
index de4a5a9..282d903 100644
--- a/README.md
+++ b/README.md
@@ -25,7 +25,8 @@ the most realistic asteroids-like game in existence.
 - [x] rework HUD, log additional info to console
   - [ ] Add speed to HUD
 - [x] projectiles
-  - [ ] Limit firerate, make projectiles self-destruct
+  - [ ] Limit firerate
+  - [x] make projectiles self-destruct
 - [ ] multi player support
 - [ ] cosmetics (improved models, stars, …)
 - [ ] switch rendering engine
diff --git a/lib/Grav2ty/Control.hs b/lib/Grav2ty/Control.hs
index a1c27ac..120924a 100644
--- a/lib/Grav2ty/Control.hs
+++ b/lib/Grav2ty/Control.hs
@@ -56,31 +56,37 @@ data State a g
 
 makeLenses ''State
 
-projectile :: RealFloat a => (V2 a, V2 a) -> Object a -> Object a
-projectile (pos,speed) ship =
-  Dynamic (centeredCircle 1) 0 1000 pPos pSpeed 0 NoMod Nothing
+projectile :: RealFloat a => (V2 a, V2 a) -> Integer -> Object a -> Object a
+projectile (pos,speed) tick ship =
+  Dynamic (centeredCircle 1) 0 1000 pPos pSpeed 0 NoMod Nothing . Just $ tick + 5000
   where pPos = objectLoc ship + rotateV2 (objectRot ship) pos
         pSpeed = (15 * rotateV2 (objectRot ship) speed) + objectSpeed ship
 
 applyControls :: RealFloat a => ControlState a -> Object a -> [Object a]
 applyControls _ obj@Static {} = [obj]
 applyControls cs obj@Dynamic {} =
-  case objectMod obj of
-    NoMod -> [obj]
-    LocalMod -> case Map.lookup (objectMod obj) (cs^.ctrlInputs) of
-               Nothing -> [obj]
-               Just (Modification rot acc fire) ->
-                 let newObj = obj
-                      { objectRot = rot
-                      , objectAcc = angle rot ^* acc
-                      }
-                     -- Note: we are relying on laziness here: if objectCannon
-                     -- is Nothing the pObj never gets evaluated.
-                     pObj = projectile (fromJust . objectCannon $ obj) newObj
-                     pList = if cs^.ctrlTick /= fire || isNothing (objectCannon obj)
-                                then []
-                                else [pObj]
-                  in newObj : pList
+  if isNothing life || fromJust life >= cs^.ctrlTick
+     then moddedObjs
+     else []
+  where life = objectLife obj
+        moddedObjs =
+          case objectMod obj of
+            NoMod -> [obj]
+            LocalMod ->
+              case Map.lookup (objectMod obj) (cs^.ctrlInputs) of
+                Nothing -> [obj]
+                Just (Modification rot acc fire) ->
+                  let newObj = obj
+                       { objectRot = rot
+                       , objectAcc = angle rot ^* acc
+                       }
+                      -- Note: we are relying on laziness here: if objectCannon
+                      -- is Nothing the pObj never gets evaluated.
+                      pObj = projectile (fromJust . objectCannon $ obj) (cs^.ctrlTick) newObj
+                      pList = if cs^.ctrlTick /= fire || isNothing (objectCannon obj)
+                                 then []
+                                 else [pObj]
+                   in newObj : pList
 
 type ExtractFunction a b = Object a -> (State a b -> State a b)
 
diff --git a/lib/Grav2ty/Simulation.hs b/lib/Grav2ty/Simulation.hs
index 1e63352..2b61e51 100644
--- a/lib/Grav2ty/Simulation.hs
+++ b/lib/Grav2ty/Simulation.hs
@@ -161,21 +161,22 @@ type Cannon a = Maybe (V2 a, V2 a)
 
 data Object a
   = Dynamic
-  { objectHitbox :: Hitbox a  -- ^  hitbox of the object. Hitbox points at
-                              --    (V2 0 0) will always be at the center of the
-                              --    object
-  , objectRot    :: a         -- ^ Radial angle
-  , objectMass   :: a         -- ^ mass of the object in kg
-  , objectLoc    :: V2 a      -- ^ Current location of the object.
-  , objectSpeed  :: V2 a      -- ^ Current speed of the Object. Used for
-                              --   simulation approximation
-  , objectAcc    :: V2 a      -- ^ Current static Acceleration of the object.
-                              --   0 unless controlled by the player or
-                              --   projectile.
-  , objectMod    :: Modifier  -- ^ If and how the Object can be modified during
-                              --   the simulation.
-  , objectCannon :: Cannon a  -- ^ Point and Direction projectiles can or can not
-                              --   be fired from.
+  { objectHitbox :: Hitbox a      -- ^ hitbox of the object. Hitbox points at
+                                  --   (V2 0 0) will always be at the center of
+                                  --   the object
+  , objectRot    :: a             -- ^ Radial angle
+  , objectMass   :: a             -- ^ mass of the object in kg
+  , objectLoc    :: V2 a          -- ^ Current location of the object.
+  , objectSpeed  :: V2 a          -- ^ Current speed of the Object. Used for
+                                  --   simulation approximation
+  , objectAcc    :: V2 a          -- ^ Current static Acceleration of the object.
+                                  --   0 unless controlled by the player or
+                                  --   projectile.
+  , objectMod    :: Modifier      -- ^ If and how the Object can be modified
+                                  --   during the simulation.
+  , objectCannon :: Cannon a      -- ^ Point and Direction projectiles can or
+                                  --   can not be fired from.
+  , objectLife   :: Maybe Integer -- ^ Tick the Object will be destroyed at.
   }
   | Static
   { objectHitbox :: Hitbox a -- ^ See above.
diff --git a/src/Main.hs b/src/Main.hs
index 075ced3..c418171 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -113,8 +113,8 @@ initialWorld :: Fractional a => State a GlossState
 initialWorld = State
   (ControlState (Map.fromList [(LocalMod, zeroModification)]) 1 0)
   (GlossState (800, 800) (0, 0) 1 True)
-  [ Dynamic shipHitbox 0 10000 (V2 200 0) (V2 0 0) (V2 0 0) LocalMod (Just (V2 15 0, V2 1 0))
-  , Dynamic (centeredCircle 10) 0 5000 (V2 0 200) (V2 15 0) (V2 0 0) NoMod Nothing
+  [ Dynamic shipHitbox 0 10000 (V2 200 0) (V2 0 0) (V2 0 0) LocalMod (Just (V2 15 0, V2 1 0)) Nothing
+  , Dynamic (centeredCircle 10) 0 5000 (V2 0 200) (V2 15 0) (V2 0 0) NoMod Nothing Nothing
   , Static (centeredCircle 80) 0 moonMass (V2 0 0)
 --  , Static (centeredCircle 40) 0 (0.5 * moonMass) (V2 250 250)
   ]