Implement basic attention and awareness

This commit is contained in:
2021-05-08 17:34:25 +02:00
parent 1aa59cc205
commit 86faf9fd01
12 changed files with 243 additions and 135 deletions
+22 -25
View File
@@ -17,28 +17,25 @@ import Data.Maybe
import qualified Data.Set as S
import qualified Data.IntMap.Strict as IM
import System.Random
{- | The AI equivalent for your control. -}
yourControl :: World -> (World -> World,StdGen) -> Creature -> ((World -> World,StdGen), Maybe Creature)
yourControl w (f,g) cr = ( (updateUsingInput . f, g)
, Just . crAutoReload . mouseActionsCr (_mouseButtons w) $ wasdWithAiming w speed 0 cr
)
yourControl w (f,g) cr =
( (updateUsingInput . f, g)
, Just . mouseActionsCr (_mouseButtons w) $ wasdWithAiming w speed 0 cr
)
where
speed = _varMovementSpeedModifier w * equipFactor
equipFactor = product $ map equipSpeed $ IM.elems $ _crInv $ _creatures w IM.! 0
{-
Turns key presses into creature movement.
-}
wasdWithAiming :: World -> Float -> Int -> Creature -> Creature
{- | Turn key presses into creature movement. -}
wasdWithAiming
:: World
-> Float -- ^ Base speed
-> Int -- ^ Creature id
-> Creature
-> Creature
wasdWithAiming w speed i cr
| isAiming
= set crDir mouseDir
$ crMvBy (speed *.* mov)
cr
| isMoving
= crMvForward speed -- controls the base speed
$ over crDir (`fromMaybe` dir)
cr
| isAiming = set crDir mouseDir $ crMvBy (speed *.* mov) cr
| isMoving = crMvForward speed $ over crDir (`fromMaybe` dir) cr
| otherwise = cr
where
(mov',dir') = wasdComp (view keys w) w
@@ -56,16 +53,17 @@ wasdWithAiming w speed i cr
wasdM :: World -> SDL.Scancode -> Point2
wasdM w scancode
| scancode == moveUpKey (_keyConfig w) = (0,1)
| scancode == moveDownKey (_keyConfig w) = (0,-1)
| scancode == moveRightKey (_keyConfig w) = (1,0)
| scancode == moveLeftKey (_keyConfig w) = (-1,0)
wasdM _ _ = (0,0)
| scancode == moveUpKey (_keyConfig w) = ( 0, 1)
| scancode == moveDownKey (_keyConfig w) = ( 0,-1)
| scancode == moveRightKey (_keyConfig w) = ( 1, 0)
| scancode == moveLeftKey (_keyConfig w) = (-1, 0)
wasdM _ _ = (0,0)
wasdComp :: S.Set SDL.Scancode -> World -> (Point2,Maybe Float)
wasdComp ks w = f $ foldr ( (+.+) . wasdM w ) (0,0) ks
where f (0,0) = ((0,0), Nothing)
f p = (errorNormalizeV 46 p, Just $ argV p)
where
f (0,0) = ((0,0), Nothing)
f p = (errorNormalizeV 46 p, Just $ argV p)
{- | Set posture according to mouse presses. -}
mouseActionsCr :: S.Set SDL.MouseButton -> Creature -> Creature
@@ -75,4 +73,3 @@ mouseActionsCr keys
where
lbPressed = SDL.ButtonLeft `S.member` keys
rbPressed = SDL.ButtonRight `S.member` keys