Smooth camera movement when aiming, breaks sniper scope

This commit is contained in:
2022-06-04 22:06:19 +01:00
parent b0e880c96f
commit 991aa17b33
4 changed files with 40 additions and 18 deletions
+2
View File
@@ -79,6 +79,8 @@ data World = World
, _cameraCenter :: Point2 , _cameraCenter :: Point2
, _cameraRot :: Float , _cameraRot :: Float
, _cameraZoom :: Float , _cameraZoom :: Float
, _itemZoom :: Float
, _defaultZoom :: Float
, _cameraViewFrom :: Point2 , _cameraViewFrom :: Point2
, _viewDistance :: Float , _viewDistance :: Float
, _creatures :: IM.IntMap Creature , _creatures :: IM.IntMap Creature
+2
View File
@@ -24,6 +24,8 @@ defaultWorld = World
, _cameraCenter = V2 0 0 , _cameraCenter = V2 0 0
, _cameraRot = 0 , _cameraRot = 0
, _cameraZoom = 1 , _cameraZoom = 1
, _itemZoom = 1
, _defaultZoom = 1
, _cameraViewFrom = V2 0 0 , _cameraViewFrom = V2 0 0
, _viewDistance = 1000 , _viewDistance = 1000
, _modifications = IM.empty , _modifications = IM.empty
+2 -1
View File
@@ -151,7 +151,8 @@ rifle = bangCane
& itConsumption . ammoBaseMax .~ 1 & itConsumption . ammoBaseMax .~ 1
& itUse . useAim . aimWeight .~ 6 & itUse . useAim . aimWeight .~ 6
& itUse . useAim . aimRange .~ 1 & itUse . useAim . aimRange .~ 1
& itUse . useAim . aimZoom .~ defaultItZoom {_itZoomFac = 1.5} -- & itUse . useAim . aimZoom .~ defaultItZoom {_itZoomFac = 1.5}
& itUse . useAim . aimZoom .~ defaultItZoom {_itZoomFac = 2}
repeater :: Item repeater :: Item
repeater = rifle repeater = rifle
& itModules . at ModRifleMag ?~ DefaultModule & itModules . at ModRifleMag ?~ DefaultModule
+34 -17
View File
@@ -8,6 +8,7 @@ module Dodge.Update.Camera
import Dodge.Data import Dodge.Data
import Dodge.Base import Dodge.Base
import Dodge.Zone import Dodge.Zone
import Dodge.Creature.Test
import Geometry import Geometry
--import Geometry.ConvexPoly --import Geometry.ConvexPoly
import Dodge.GameRoom import Dodge.GameRoom
@@ -26,8 +27,8 @@ import qualified SDL
update where your avatar's view is from. -} update where your avatar's view is from. -}
updateCamera :: Configuration -> World -> World updateCamera :: Configuration -> World -> World
updateCamera cfig = setViewDistance cfig updateCamera cfig = setViewDistance cfig
. moveCamera cfig . moveZoomCamera cfig
. autoZoomCamera cfig . updateScopeZoom . updateScopeZoom
. rotateCamera cfig . rotateCamera cfig
moveZoomCamera :: Configuration -> World -> World moveZoomCamera :: Configuration -> World -> World
@@ -35,20 +36,31 @@ moveZoomCamera cfig w = w
& cameraCenter .~ newcen & cameraCenter .~ newcen
& cameraViewFrom .~ newvf & cameraViewFrom .~ newvf
& cameraZoom .~ newzoom & cameraZoom .~ newzoom
& defaultZoom .~ newDefaultZoom
& itemZoom .~ newItemZoom
where where
newvf = _crPos (you w) +.+ fromMaybe (V2 0 0) (yourItem w ^? _Just . itScope . scopePos) newvf = _crPos (you w) +.+ fromMaybe (V2 0 0) (yourItem w ^? _Just . itScope . scopePos)
newcen = newvf newcen = newvf +.+ offset
newzoom = changeZoom (_cameraZoom w) offset = rotateV (_cameraRot w) $ ((newzoom - newDefaultZoom)/(newDefaultZoom*newzoom)) *.* _mousePos w
wallZoom = farWallDist newvf cfig w --newzoom = changeZoom (_cameraZoom w) idealZoom'
idealZoom newzoom = newDefaultZoom * newItemZoom
| SDL.ButtonRight `S.member` _mouseButtons w idealDefaultZoom = clipZoom wallZoom
= theScopeZoom * maybe zoomNoItem zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom newDefaultZoom = changeZoom (_defaultZoom w) idealDefaultZoom
| otherwise = zoomNoItem wallZoom idealItemZoom
-- = maybe zoomNoItem zoomFromItem (yourItem w ^? itZoom) wallZoom | crIsAiming (you w)
changeZoom curZoom = fromMaybe 1 $ fmap zoomFromItem' (yourItem w ^? _Just . itUse . useAim . aimZoom)
| otherwise = 1
newItemZoom = changeZoom (_itemZoom w) idealItemZoom
changeZoom curZoom idealZoom
| curZoom > idealZoom + 0.01 = ((zoomOutSpeed-1)*curZoom + idealZoom) / zoomOutSpeed | curZoom > idealZoom + 0.01 = ((zoomOutSpeed-1)*curZoom + idealZoom) / zoomOutSpeed
| curZoom < idealZoom - 0.01 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed | curZoom < idealZoom - 0.01 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed
| otherwise = idealZoom | otherwise = idealZoom
wallZoom = farWallDist newvf cfig w
idealZoom'
| SDL.ButtonRight `S.member` _mouseButtons w
= theScopeZoom * maybe clipZoom zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) newDefaultZoom
| otherwise = newDefaultZoom
-- = maybe clipZoom zoomFromItem (yourItem w ^? itZoom) wallZoom
-- these speeds are inverted, larger means slower -- these speeds are inverted, larger means slower
zoomInSpeed = 25 zoomInSpeed = 25
zoomOutSpeed = 15 zoomOutSpeed = 15
@@ -62,7 +74,7 @@ moveCamera cfig w = w
where where
camPos = _cameraViewFrom w camPos = _cameraViewFrom w
wallZoom = farWallDist camPos cfig w wallZoom = farWallDist camPos cfig w
basezoom = zoomNoItem wallZoom basezoom = clipZoom wallZoom
aimRangeFactor aimRangeFactor
| _cameraZoom w == 0 = 0 | _cameraZoom w == 0 = 0
| otherwise = fromMaybe 0 (yourItem w ^? _Just . itUse . useAim . aimRange) / _cameraZoom w | otherwise = fromMaybe 0 (yourItem w ^? _Just . itUse . useAim . aimRange) / _cameraZoom w
@@ -169,6 +181,11 @@ notAtTerminal w = isNothing $ w ^? hud . hudElement . subInventory . termParams
--zoomCamBy :: Float -> World -> World --zoomCamBy :: Float -> World -> World
--zoomCamBy x w = w {_cameraZoom = max (_cameraZoom w + x) 0.01} --zoomCamBy x w = w {_cameraZoom = max (_cameraZoom w + x) 0.01}
zoomFromItem'
:: ItZoom
-> Float
zoomFromItem' ItZoom {_itZoomMax = zMax, _itZoomMin = zMin, _itZoomFac = zFac}
= min zMax $ max zMin zFac
zoomFromItem zoomFromItem
:: ItZoom :: ItZoom
@@ -176,10 +193,10 @@ zoomFromItem
-> Float -> Float
zoomFromItem ItZoom {_itZoomMax = zMax, _itZoomMin = zMin, _itZoomFac = zFac} zoomFromItem ItZoom {_itZoomMax = zMax, _itZoomMin = zMin, _itZoomFac = zFac}
= min zMax . max zMin . (zFac *) = min zMax . max zMin . (zFac *)
zoomNoItem clipZoom
:: Float -- ^ Furthest viewable distance :: Float -- ^ Furthest viewable distance
-> Float -> Float
zoomNoItem = min 20 . max 0.2 clipZoom = min 20 . max 0.2
{- Automatically sets the zoom of the camera according to the surrounding walls. -} {- Automatically sets the zoom of the camera according to the surrounding walls. -}
autoZoomCamera :: Configuration -> World -> World autoZoomCamera :: Configuration -> World -> World
autoZoomCamera cfig w = w & cameraZoom %~ changeZoom autoZoomCamera cfig w = w & cameraZoom %~ changeZoom
@@ -188,9 +205,9 @@ autoZoomCamera cfig w = w & cameraZoom %~ changeZoom
wallZoom = farWallDist camPos cfig w wallZoom = farWallDist camPos cfig w
idealZoom idealZoom
| SDL.ButtonRight `S.member` _mouseButtons w | SDL.ButtonRight `S.member` _mouseButtons w
= theScopeZoom * maybe zoomNoItem zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom = theScopeZoom * maybe clipZoom zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom
| otherwise = zoomNoItem wallZoom | otherwise = clipZoom wallZoom
-- = maybe zoomNoItem zoomFromItem (yourItem w ^? itZoom) wallZoom -- = maybe clipZoom zoomFromItem (yourItem w ^? itZoom) wallZoom
changeZoom curZoom changeZoom curZoom
| curZoom > idealZoom + 0.01 = ((zoomOutSpeed-1)*curZoom + idealZoom) / zoomOutSpeed | curZoom > idealZoom + 0.01 = ((zoomOutSpeed-1)*curZoom + idealZoom) / zoomOutSpeed
| curZoom < idealZoom - 0.01 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed | curZoom < idealZoom - 0.01 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed