This commit is contained in:
2022-04-07 20:54:00 +01:00
parent 591cff68ff
commit 9d4b1d5d70
3 changed files with 45 additions and 63 deletions
+19 -35
View File
@@ -1,6 +1,5 @@
module Dodge.Creature.YourControl
(-- yourControl
yourControl'
( yourControl
) where
import Dodge.Data
import Dodge.Creature.Impulse.Movement
@@ -11,50 +10,36 @@ import Geometry
import Control.Lens
import qualified SDL
import qualified Data.Set as S
import qualified Data.IntMap.Strict as IM
--import qualified Data.IntMap.Strict as IM
import Data.Maybe
{- | The AI equivalent for your control. -}
yourControl
:: Creature
-> World
-> (World -> World, Creature)
yourControl cr w =
( updateUsingInput
, mouseActionsCr (_mouseButtons w) $ wasdWithAiming w speed 0 cr
)
where
speed = _mvSpeed $ _crMvType cr
yourControl' :: Creature -> World -> World
yourControl' cr w = w
yourControl :: Creature -> World -> World
yourControl cr w = w
& updateUsingInput
& creatures . ix (_crID cr) %~ (mouseActionsCr (_mouseButtons w) . wasdWithAiming w speed 0)
& creatures . ix (_crID cr) %~ (mouseActionsCr (_mouseButtons w) . wasdWithAiming w speed)
where
speed = _mvSpeed $ _crMvType cr
{- | Turn key presses into creature movement. -}
wasdWithAiming
:: World
:: World
-> Float -- ^ Base speed
-> Int -- ^ Creature id
-> Creature
-> Creature
wasdWithAiming w speed i cr
| isAiming
= addAnyTwist $ set crDir mouseDir $ theMovement cr
wasdWithAiming w speed cr
| isAiming = addAnyTwist $ set crDir mouseDir $ theMovement cr
| isReloading && SDL.ButtonRight `S.member` _mouseButtons w
= addAnyTwist $ set crDir (mouseDir + anytwist) $ theMovement cr
= addAnyTwist $ set crDir (mouseDir + anytwist) $ theMovement cr
| otherwise = theMovement $ theTurn $ removeTwist cr
where
twistamount = 1.6
removeTwist cr' = case _crTwist cr' of
t -> cr' & crDir +~ t & crTwist .~ 0
anytwist = case cr ^? crInv . ix (_crInvSel (_creatures w IM.! i)) . itUse . useAim . aimStance of
Just TwoHandTwist -> twistamount * pi
_ -> 0
addAnyTwist cr'
= case cr' ^? crInv . ix (_crInvSel (_creatures w IM.! i)) . itUse . useAim . aimStance of
Just TwoHandTwist -> cr' & crTwist .~ twistamount * pi
_ -> cr'
removeTwist cr' = cr'
& crDir +~ _crTwist cr'
& crTwist .~ 0
anytwist = case cr ^? crInv . ix (_crInvSel cr) . itUse . useAim . aimStance of
Just TwoHandTwist -> twistamount * pi
_ -> 0
addAnyTwist = crTwist .~ anytwist
theMovement
| movDir == V2 0 0 = id
| otherwise = crMvAbsolute (speed *.* movAbs) . set crMvDir dir
@@ -64,8 +49,7 @@ wasdWithAiming w speed i cr
movAbs = rotateV (_cameraRot w) $ normalizeV movDir
isAiming = _posture (_crStance cr) == Aiming
isReloading = _posture (_crStance cr) == Reloading
mouseDir = case w ^? creatures . ix i . crInv . ix (_crInvSel (_creatures w IM.! i))
. itScope . scopePos of
mouseDir = case cr ^? crInv . ix (_crInvSel cr) . itScope . scopePos of
Just p -> normalizeAngle $ argV
$ p +.+ 2 / _cameraZoom w
*.* rotateV (_cameraRot w) (_mousePos w)
@@ -77,10 +61,10 @@ wasdM scancode = case scancode of
SDL.ScancodeS -> V2 0 (-1)
SDL.ScancodeD -> V2 1 0
SDL.ScancodeA -> V2 (-1) 0
_ -> V2 0 0
_ -> V2 0 0
wasdDir :: World -> Point2
wasdDir w = foldr ((+.+) . wasdM) (V2 0 0) $ _keys w
wasdDir = foldr ((+.+) . wasdM) (V2 0 0) . _keys
{- | Set posture according to mouse presses. -}
mouseActionsCr :: S.Set SDL.MouseButton -> Creature -> Creature