Refactor, try to limit dependencies
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
module Dodge.Creature.Impulse.Flee
|
||||
where
|
||||
import Dodge.Data
|
||||
import Geometry
|
||||
import FoldableHelp
|
||||
module Dodge.Creature.Impulse.Flee where
|
||||
|
||||
retreatPointForFrom
|
||||
:: Float -> World -> Creature -> Point2 -> Maybe Point2
|
||||
retreatPointForFrom d _ cr p
|
||||
= safeMinimumOn (dist cpos)
|
||||
$ divideCircle 10 p d
|
||||
import Dodge.Data.World
|
||||
import FoldableHelp
|
||||
import Geometry
|
||||
|
||||
retreatPointForFrom ::
|
||||
Float -> World -> Creature -> Point2 -> Maybe Point2
|
||||
retreatPointForFrom d _ cr p =
|
||||
safeMinimumOn (dist cpos) $
|
||||
divideCircle 10 p d
|
||||
where
|
||||
cpos = _crPos cr
|
||||
|
||||
-- = head $ sortBy (compare `on` (\p -> dist p ypos < 300)) $ retreatP'' : retreatPs
|
||||
-- where
|
||||
-- retreatPs = sortBy (compare `on` dist cpos) $ map f $ nRaysRad 8 400
|
||||
@@ -18,5 +19,5 @@ retreatPointForFrom d _ cr p
|
||||
-- $ reflectPointWalls ypos (ypos +.+ p) (wallsAlongLine ypos (ypos +.+ p) w)
|
||||
-- retreatP' = cpos +.+ 300 *.* (cpos -.- ypos)
|
||||
-- retreatP'' = fromMaybe retreatP' $ fmap fst
|
||||
-- $ reflectPointWalls ypos retreatP'
|
||||
-- $ reflectPointWalls ypos retreatP'
|
||||
-- $ wallsAlongLine ypos retreatP' w
|
||||
|
||||
@@ -1,26 +1,31 @@
|
||||
module Dodge.Creature.Impulse.Movement where
|
||||
import Dodge.Data
|
||||
|
||||
import Control.Lens
|
||||
import Dodge.Creature.Statistics
|
||||
import Dodge.Data.World
|
||||
import Geometry
|
||||
|
||||
--import Data.Maybe
|
||||
--import qualified IntMapHelp as IM
|
||||
import Control.Lens
|
||||
{- | Creature attempts to moves under its own steam.
|
||||
The idea is that this may or may not work, depending on the status of the creature.
|
||||
For now, though, this cannot fail. -}
|
||||
crMvBy :: Point2 -- ^ Movement translation vector, will be made relative to creature direction
|
||||
-> Creature -> Creature
|
||||
The idea is that this may or may not work, depending on the status of the creature.
|
||||
For now, though, this cannot fail.
|
||||
-}
|
||||
crMvBy ::
|
||||
-- | Movement translation vector, will be made relative to creature direction
|
||||
Point2 ->
|
||||
Creature ->
|
||||
Creature
|
||||
crMvBy p cr = crMvAbsolute (rotateV (_crDir cr) p) cr
|
||||
|
||||
crMvAbsolute
|
||||
:: Point2 -- ^ Movement translation vector
|
||||
-> Creature
|
||||
-> Creature
|
||||
crMvAbsolute p' cr = advanceStepCounter (magV p) cr
|
||||
& crPos %~ (+.+ p)
|
||||
& crMvDir .~ argV p
|
||||
where
|
||||
crMvAbsolute ::
|
||||
-- | Movement translation vector
|
||||
Point2 ->
|
||||
Creature ->
|
||||
Creature
|
||||
crMvAbsolute p' cr =
|
||||
advanceStepCounter (magV p) cr
|
||||
& crPos %~ (+.+ p)
|
||||
& crMvDir .~ argV p
|
||||
where
|
||||
p = strengthFactor (getCrStrength cr) *.* p'
|
||||
|
||||
strengthFactor :: Int -> Float
|
||||
@@ -29,18 +34,20 @@ strengthFactor i
|
||||
| i < 1 = 0
|
||||
| otherwise = 0.1 * fromIntegral i
|
||||
|
||||
crMvForward
|
||||
:: Float -- ^ Speed
|
||||
-> Creature
|
||||
-> Creature
|
||||
crMvForward ::
|
||||
-- | Speed
|
||||
Float ->
|
||||
Creature ->
|
||||
Creature
|
||||
crMvForward speed = crMvBy (V2 speed 0)
|
||||
|
||||
advanceStepCounter
|
||||
:: Float -- ^ Speed
|
||||
-> Creature
|
||||
-> Creature
|
||||
advanceStepCounter ::
|
||||
-- | Speed
|
||||
Float ->
|
||||
Creature ->
|
||||
Creature
|
||||
advanceStepCounter speed = crStance . carriage %~ f
|
||||
where
|
||||
where
|
||||
f car = case car of
|
||||
Standing -> f (Walking 0 RightForward)
|
||||
Walking i ff -> Walking (i + ceiling speed) ff
|
||||
@@ -50,25 +57,27 @@ creatureTurn :: Float -> Creature -> Creature
|
||||
creatureTurn a = crDir +~ a
|
||||
|
||||
creatureTurnTo :: Point2 -> Creature -> Creature
|
||||
creatureTurnTo p cr
|
||||
creatureTurnTo p cr
|
||||
| vToTarg == V2 0 0 = cr -- this should deal with the angleVV error
|
||||
| otherwise = cr & crDir .~ dirToTarget
|
||||
| otherwise = cr & crDir .~ dirToTarget
|
||||
where
|
||||
vToTarg = p -.- _crPos cr
|
||||
dirToTarget = argV vToTarg
|
||||
|
||||
-- the following is perhaps not ideal because it mixes normalizeAngle with
|
||||
-- angleVV, but it seems to work
|
||||
creatureTurnTowardDir
|
||||
:: Float -- ^ Angle
|
||||
-> Float -- ^ Turn speed
|
||||
-> Creature
|
||||
-> Creature
|
||||
creatureTurnTowardDir a turnSpeed cr
|
||||
| normalizeAngle (abs (a - cdir)) <= turnSpeed
|
||||
= cr & crDir .~ dirToTarget
|
||||
| isLeftOfA (normalizeAngle dirToTarget) (normalizeAngle $ _crDir cr)
|
||||
= cr & crDir +~ turnSpeed
|
||||
creatureTurnTowardDir ::
|
||||
-- | Angle
|
||||
Float ->
|
||||
-- | Turn speed
|
||||
Float ->
|
||||
Creature ->
|
||||
Creature
|
||||
creatureTurnTowardDir a turnSpeed cr
|
||||
| normalizeAngle (abs (a - cdir)) <= turnSpeed =
|
||||
cr & crDir .~ dirToTarget
|
||||
| isLeftOfA (normalizeAngle dirToTarget) (normalizeAngle $ _crDir cr) =
|
||||
cr & crDir +~ turnSpeed
|
||||
| otherwise = cr & crDir -~ turnSpeed
|
||||
where
|
||||
cdir = _crDir cr
|
||||
@@ -76,16 +85,16 @@ creatureTurnTowardDir a turnSpeed cr
|
||||
dirToTarget = argV vToTarg
|
||||
|
||||
creatureTurnToward :: Point2 -> Float -> Creature -> Creature
|
||||
creatureTurnToward p turnSpeed cr
|
||||
creatureTurnToward p turnSpeed cr
|
||||
| vToTarg == V2 0 0 = cr -- this should deal with the angleVV error
|
||||
| errorAngleVV 3 vToTarg (unitVectorAtAngle (_crDir cr)) <= turnSpeed
|
||||
= cr & crDir .~ dirToTarget
|
||||
| errorAngleVV 3 vToTarg (unitVectorAtAngle (_crDir cr)) <= turnSpeed =
|
||||
cr & crDir .~ dirToTarget
|
||||
| isLeftOfA (normalizeAngle dirToTarget) (normalizeAngle $ _crDir cr) = cr & crDir +~ turnSpeed
|
||||
| otherwise = cr & crDir -~ turnSpeed
|
||||
| otherwise = cr & crDir -~ turnSpeed
|
||||
where
|
||||
vToTarg = p -.- _crPos cr
|
||||
dirToTarget = argV vToTarg
|
||||
|
||||
{- | Speed modifier of an item when not aiming. -}
|
||||
-- | Speed modifier of an item when not aiming.
|
||||
equipSpeed :: Item -> Float
|
||||
equipSpeed _ = 1
|
||||
|
||||
@@ -4,12 +4,11 @@ module Dodge.Creature.Impulse.UseItem (
|
||||
itemEffect,
|
||||
) where
|
||||
|
||||
--import qualified Data.IntSet as IS
|
||||
import Control.Lens
|
||||
import qualified Data.Map.Strict as M
|
||||
import Data.Maybe
|
||||
import Dodge.Cuse
|
||||
import Dodge.Data
|
||||
import Dodge.Data.World
|
||||
import Dodge.Euse
|
||||
import Dodge.HeldUse
|
||||
import Dodge.Inventory
|
||||
@@ -29,7 +28,7 @@ useItem cr' w = fromMaybe (f w) $ do
|
||||
|
||||
itemEffect :: Creature -> Item -> World -> World
|
||||
itemEffect cr it w = case it ^. itUse of
|
||||
RightUse{_rUse = eff, _useMods = usemods} ->
|
||||
HeldUse{_heldUse = eff, _heldMods = usemods} ->
|
||||
hammerTest $ tryReload cr it w $ foldr ($) (useHeld eff) (useMod usemods) it cr
|
||||
LeftUse{} -> doequipmentchange
|
||||
EquipUse{} -> doequipmentchange
|
||||
@@ -53,7 +52,7 @@ tryReload cr it w f
|
||||
| _crID cr == _yourID (_cWorld w) && itNeedsLoading it && _mouseButtons w M.!? SDL.ButtonLeft == Just False =
|
||||
crToggleReloading cr
|
||||
| otherwise =
|
||||
(runIdentity . pointerToItemLocation (_itLocation it) (return . (itUse . useHammer .~ HammerDown)))
|
||||
(runIdentity . pointerToItemLocation (_itLocation it) (return . (itUse . heldHammer .~ HammerDown)))
|
||||
. f
|
||||
|
||||
itNeedsLoading :: Item -> Bool
|
||||
@@ -104,8 +103,8 @@ toggleEquipmentAt rbops invid cr w = case rbops ^? opAllocateEquipment of
|
||||
crpoint = cWorld . creatures . ix (_crID cr)
|
||||
itmat i = _crInv cr IM.! i
|
||||
itm = itmat (crSel cr)
|
||||
onequip itm' = useE ((_eqOnEquip . _eqEq . _itUse) itm') itm'
|
||||
onremove itm' = useE ((_eqOnRemove . _eqEq . _itUse) itm') itm'
|
||||
onequip itm' = useE ((_eeOnEquip . _equipEffect . _itUse) itm') itm'
|
||||
onremove itm' = useE ((_eeOnRemove . _equipEffect . _itUse) itm') itm'
|
||||
|
||||
useLeftItem :: Int -> World -> World
|
||||
useLeftItem cid w
|
||||
@@ -114,16 +113,16 @@ useLeftItem cid w
|
||||
| otherwise = fromMaybe w $ do
|
||||
invid <- _crLeftInvSel cr
|
||||
itm <- cr ^? crInv . ix invid
|
||||
f <- cr ^? crInv . ix invid . itUse . lUse
|
||||
f <- cr ^? crInv . ix invid . itUse . leftUse
|
||||
return
|
||||
. (runIdentity . pointerToItemLocation (_itLocation itm) (return . (itUse . useHammer .~ HammerDown)))
|
||||
. (runIdentity . pointerToItemLocation (_itLocation itm) (return . (itUse . leftHammer .~ HammerDown)))
|
||||
. useL f itm cr
|
||||
$ w
|
||||
where
|
||||
cr = _creatures (_cWorld w) IM.! cid
|
||||
itmShouldBeUsed =
|
||||
isJust (cr ^? crInv . ix (crSel cr) . itUse . cUse)
|
||||
|| ( isJust (cr ^? crInv . ix (crSel cr) . itUse . eqEq . eqUse)
|
||||
|| ( isJust (cr ^? crInv . ix (crSel cr) . itUse . equipEffect . eeUse)
|
||||
&& _crLeftInvSel cr /= Just (crSel cr)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user