deduplicate direction function

This commit is contained in:
nek0 2018-07-06 17:17:57 +02:00
parent 0f3c33b243
commit 19476e3fbb
3 changed files with 39 additions and 56 deletions

View file

@ -20,29 +20,11 @@ import Types
import MainGame.WorldMap (checkBoundsCollision2, drawTile) import MainGame.WorldMap (checkBoundsCollision2, drawTile)
import Util (direction)
updateMind :: Double -> Affection UserData () updateMind :: Double -> Affection UserData ()
updateMind dt = do updateMind dt = do
ud <- getAffection ud <- getAffection
let direction :: V2 Double -> Direction -> Direction
direction vel'@(V2 vr _) rot' = if sqrt (vel' `dot` vel') > 0
then
let xuu =
acos ((vel' `dot` V2 0 1) /
sqrt (vel' `dot` vel')) / pi * 180
xu = if vr < 0 then 360 - xuu else xuu
d
| xu < 22.5 = NE
| xu > 22.5 && xu < 67.5 = E
| xu > 67.5 && xu < 112.5 = SE
| xu > 112.5 && xu < 157.5 = S
| xu > 157.5 && xu < 202.5 = SW
| xu > 202.5 && xu < 247.5 = W
| xu > 247.5 && xu < 292.5 = NW
| xu > 292.5 && xu < 337.5 = N
| xu > 337.5 = NE
| otherwise = NE
in d
else rot'
(nws, _) <- liftIO $ yieldSystemT (worldState ud) $ do (nws, _) <- liftIO $ yieldSystemT (worldState ud) $ do
emap allEnts $ do emap allEnts $ do
with anim with anim
@ -97,13 +79,13 @@ updateMind dt = do
| sqrt (colldpos `dot` colldpos) > 0 -> | sqrt (colldpos `dot` colldpos) > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "standing" , aiName = "standing"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -112,7 +94,7 @@ updateMind dt = do
| sqrt (colldpos `dot` colldpos) > 0 -> | sqrt (colldpos `dot` colldpos) > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "walking" , aiName = "walking"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -120,7 +102,7 @@ updateMind dt = do
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
x -> error ("unknown animation name" ++ x) x -> error ("unknown animation name" ++ x)
@ -154,7 +136,7 @@ updateMind dt = do
) )
ent = unchanged ent = unchanged
{ mmpos = Set $ pos' + colldpos { mmpos = Set $ pos' + colldpos
, rot = Set (direction vel' rot') , rot = Set (fromMaybe rot' $ direction vel')
, anim = Set nstat , anim = Set nstat
} }
return ent return ent

View file

@ -338,26 +338,6 @@ updateMap dt = do
, stateData = mendat , stateData = mendat
} }
else do else do
let direction :: V2 Double -> Direction -> Direction
direction vel'@(V2 vr _) rot' = if sqrt (vel' `dot` vel') > 0
then
let xuu =
acos ((vel' `dot` V2 0 1) /
sqrt (vel' `dot` vel')) / pi * 180
xu = if vr < 0 then 360 - xuu else xuu
d
| xu < 22.5 = NE
| xu > 22.5 && xu < 67.5 = E
| xu > 67.5 && xu < 112.5 = SE
| xu > 112.5 && xu < 157.5 = S
| xu > 157.5 && xu < 202.5 = SW
| xu > 202.5 && xu < 247.5 = W
| xu > 247.5 && xu < 292.5 = NW
| xu > 292.5 && xu < 337.5 = N
| xu > 337.5 = NE
| otherwise = NE
in d
else rot'
(nws, _) <- liftIO $ yieldSystemT (worldState ud) $ do (nws, _) <- liftIO $ yieldSystemT (worldState ud) $ do
emap allEnts $ do emap allEnts $ do
with anim with anim
@ -413,13 +393,13 @@ updateMap dt = do
| sqrt (vel' `dot` vel') > 0 -> | sqrt (vel' `dot` vel') > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "standing" , aiName = "standing"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -428,7 +408,7 @@ updateMap dt = do
| sqrt (vel' `dot` vel') > 0 -> | sqrt (vel' `dot` vel') > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "walking" , aiName = "walking"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -436,13 +416,13 @@ updateMap dt = do
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
x -> error ("unknown animation name" ++ x) x -> error ("unknown animation name" ++ x)
ent = unchanged ent = unchanged
{ pos = Set npos { pos = Set npos
, rot = Set $ direction vel' rot' , rot = Set $ fromMaybe rot' (direction vel')
, anim = Set nstat , anim = Set nstat
} }
return ent return ent
@ -464,13 +444,13 @@ updateMap dt = do
| sqrt (colldpos `dot` colldpos) > 0 -> | sqrt (colldpos `dot` colldpos) > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "standing" , aiName = "standing"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -479,7 +459,7 @@ updateMap dt = do
| sqrt (colldpos `dot` colldpos) > 0 -> | sqrt (colldpos `dot` colldpos) > 0 ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
, aiName = "walking" , aiName = "walking"
} }
, asCurrentFrame = 0 , asCurrentFrame = 0
@ -487,7 +467,7 @@ updateMap dt = do
| otherwise -> | otherwise ->
stat stat
{ asId = aId { asId = aId
{ aiDirection = direction vel' rot' { aiDirection = fromMaybe rot' (direction vel')
} }
} }
x -> error ("unknown animation name" ++ x) x -> error ("unknown animation name" ++ x)
@ -521,7 +501,7 @@ updateMap dt = do
) )
ent = unchanged ent = unchanged
{ pos = Set $ pos' + colldpos { pos = Set $ pos' + colldpos
, rot = Set (direction vel' rot') , rot = Set (fromMaybe rot' $ direction vel')
, anim = Set nstat , anim = Set nstat
} }
return ent return ent

View file

@ -15,7 +15,7 @@ import qualified Graphics.Rendering.OpenGL as GL hiding (get)
import System.Exit (exitFailure) import System.Exit (exitFailure)
import Linear import Linear hiding (E(..))
import NanoVG hiding (V2(..)) import NanoVG hiding (V2(..))
import NanoVG.Internal.Image (ImageFlags(..)) import NanoVG.Internal.Image (ImageFlags(..))
@ -225,3 +225,24 @@ loadAnimationSprites fp nvg idconfs = do
, Animation dur imgs pb , Animation dur imgs pb
) )
) idconfs ) idconfs
direction :: V2 Double -> Maybe Direction
direction vel'@(V2 vr _) = if sqrt (vel' `dot` vel') > 0
then
let xuu =
acos ((vel' `dot` V2 0 1) /
sqrt (vel' `dot` vel')) / pi * 180
xu = if vr < 0 then 360 - xuu else xuu
d
| xu < 22.5 = NE
| xu > 22.5 && xu < 67.5 = E
| xu > 67.5 && xu < 112.5 = SE
| xu > 112.5 && xu < 157.5 = S
| xu > 157.5 && xu < 202.5 = SW
| xu > 202.5 && xu < 247.5 = W
| xu > 247.5 && xu < 292.5 = NW
| xu > 292.5 && xu < 337.5 = N
| xu > 337.5 = NE
| otherwise = NE
in Just d
else Nothing