<commit_before>-- | This module contains logic to manage the spawning and updating of enemies
--   within the game.
module WhatIsThisGame.Controllers.EnemySpawner (enemies) where

--------------------
-- Global Imports --
import FRP.Elerea.Param
import Linear.V2

-------------------
-- Local Imports --
import WhatIsThisGame.Input
import WhatIsThisGame.Data

----------
-- Code --

-- | Asking for a new enemy.
newEnemy :: Float -> SignalGen p Entity
newEnemy x = do
  rY <- randomRGen (0, 480)
  rS <- randomRGen (1, 1)

  return $ Entity { getName     = "res/player/01.png"
                  , getPosition = V2 x rY
                  , getSize     = V2 (rS * 1.618) rS
                  , getHealth   = 1
                  , shouldShoot = False
                  }

-- | Stepping an entity
step :: Float -> [Entity] -> [Entity]
step dt es =
  map step' es
  where step' :: Entity -> Entity
        step' e =
          e { getPosition = getPosition e + V2 (-20 * dt) 0 }

-- | Stepping a bunch of entities.
stepEntities :: [Entity] -> SignalGen Float (Signal [Entity])
stepEntities es = stateful es step

-- | The list of simulated enemies.
enemies :: Signal World -> SignalGen Float (Signal [Entity])
enemies _ = sequence [newEnemy x | x <- replicate 20 150] >>= stepEntities<commit_msg>Added support for enemies dying.<commit_after>-- | This module contains logic to manage the spawning and updating of enemies
--   within the game.
module WhatIsThisGame.Controllers.EnemySpawner (enemies) where

--------------------
-- Global Imports --
import FRP.Elerea.Param
import Control.Lens
import Data.Maybe
import Linear.V2

-------------------
-- Local Imports --
import WhatIsThisGame.Input
import WhatIsThisGame.Utils
import WhatIsThisGame.Data

----------
-- Code --

-- | Asking for a new enemy.
newEnemy :: Float -> SignalGen p Entity
newEnemy x = do
  rY <- randomRGen (0, 480)
  rS <- randomRGen (1, 1)

  return $ Entity { getName     = "res/player/01.png"
                  , getPosition = V2 x rY
                  , getSize     = V2 (rS * 1.618) rS
                  , getHealth   = 1
                  , shouldShoot = False
                  }

-- | Stepping an entity
step :: Float -> [Maybe Entity] -> [Maybe Entity]
step dt es =
  map step' es
  where step' :: Maybe Entity -> Maybe Entity
        step' me =
          case me of
            Nothing  -> Nothing
            Just e   -> 
              let width  = getSize e ^. _x
                  newPos = getPosition e + V2 (-20 * dt) 0 in
                if (newPos ^. _x) + width < 0
                  then Nothing
                  else Just $ e { getPosition = newPos }

-- | Stepping a bunch of entities.
stepEntities :: [Maybe Entity] -> SignalGen Float (Signal [Maybe Entity])
stepEntities es = stateful es step

-- | The list of simulated enemies.
maybeEnemies :: Signal World -> SignalGen Float (Signal [Maybe Entity])
maybeEnemies _ = sequence [fmap Just $ newEnemy x | x <- replicate 20 150] >>= stepEntities

-- | The list of simulated enemies - with all of the dead ones filtered out.
enemies :: Signal World -> SignalGen Float (Signal [Entity])
enemies = sgMap catMaybes . maybeEnemies
{'fraction_non_alphanumeric': 0.12796208530805686, 'fraction_numerical': 0.016113744075829384, 'mean_word_length': 1.9561157796451913, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '406171', 'n_tokens_mistral': 985, 'n_tokens_neox': 912, 'n_words': 336}