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
, _cameraRot :: Float
, _cameraZoom :: Float
, _itemZoom :: Float
, _defaultZoom :: Float
, _cameraViewFrom :: Point2
, _viewDistance :: Float
, _creatures :: IM.IntMap Creature
+2
View File
@@ -24,6 +24,8 @@ defaultWorld = World
, _cameraCenter = V2 0 0
, _cameraRot = 0
, _cameraZoom = 1
, _itemZoom = 1
, _defaultZoom = 1
, _cameraViewFrom = V2 0 0
, _viewDistance = 1000
, _modifications = IM.empty
+2 -1
View File
@@ -151,7 +151,8 @@ rifle = bangCane
& itConsumption . ammoBaseMax .~ 1
& itUse . useAim . aimWeight .~ 6
& 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 = rifle
& itModules . at ModRifleMag ?~ DefaultModule
+34 -17
View File
@@ -8,6 +8,7 @@ module Dodge.Update.Camera
import Dodge.Data
import Dodge.Base
import Dodge.Zone
import Dodge.Creature.Test
import Geometry
--import Geometry.ConvexPoly
import Dodge.GameRoom
@@ -26,8 +27,8 @@ import qualified SDL
update where your avatar's view is from. -}
updateCamera :: Configuration -> World -> World
updateCamera cfig = setViewDistance cfig
. moveCamera cfig
. autoZoomCamera cfig . updateScopeZoom
. moveZoomCamera cfig
. updateScopeZoom
. rotateCamera cfig
moveZoomCamera :: Configuration -> World -> World
@@ -35,20 +36,31 @@ moveZoomCamera cfig w = w
& cameraCenter .~ newcen
& cameraViewFrom .~ newvf
& cameraZoom .~ newzoom
& defaultZoom .~ newDefaultZoom
& itemZoom .~ newItemZoom
where
newvf = _crPos (you w) +.+ fromMaybe (V2 0 0) (yourItem w ^? _Just . itScope . scopePos)
newcen = newvf
newzoom = changeZoom (_cameraZoom w)
wallZoom = farWallDist newvf cfig w
idealZoom
| SDL.ButtonRight `S.member` _mouseButtons w
= theScopeZoom * maybe zoomNoItem zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom
| otherwise = zoomNoItem wallZoom
-- = maybe zoomNoItem zoomFromItem (yourItem w ^? itZoom) wallZoom
changeZoom curZoom
newcen = newvf +.+ offset
offset = rotateV (_cameraRot w) $ ((newzoom - newDefaultZoom)/(newDefaultZoom*newzoom)) *.* _mousePos w
--newzoom = changeZoom (_cameraZoom w) idealZoom'
newzoom = newDefaultZoom * newItemZoom
idealDefaultZoom = clipZoom wallZoom
newDefaultZoom = changeZoom (_defaultZoom w) idealDefaultZoom
idealItemZoom
| crIsAiming (you w)
= 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 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed
| 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
zoomInSpeed = 25
zoomOutSpeed = 15
@@ -62,7 +74,7 @@ moveCamera cfig w = w
where
camPos = _cameraViewFrom w
wallZoom = farWallDist camPos cfig w
basezoom = zoomNoItem wallZoom
basezoom = clipZoom wallZoom
aimRangeFactor
| _cameraZoom w == 0 = 0
| 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 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
:: ItZoom
@@ -176,10 +193,10 @@ zoomFromItem
-> Float
zoomFromItem ItZoom {_itZoomMax = zMax, _itZoomMin = zMin, _itZoomFac = zFac}
= min zMax . max zMin . (zFac *)
zoomNoItem
clipZoom
:: Float -- ^ Furthest viewable distance
-> 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. -}
autoZoomCamera :: Configuration -> World -> World
autoZoomCamera cfig w = w & cameraZoom %~ changeZoom
@@ -188,9 +205,9 @@ autoZoomCamera cfig w = w & cameraZoom %~ changeZoom
wallZoom = farWallDist camPos cfig w
idealZoom
| SDL.ButtonRight `S.member` _mouseButtons w
= theScopeZoom * maybe zoomNoItem zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom
| otherwise = zoomNoItem wallZoom
-- = maybe zoomNoItem zoomFromItem (yourItem w ^? itZoom) wallZoom
= theScopeZoom * maybe clipZoom zoomFromItem (yourItem w ^? _Just . itUse . useAim . aimZoom) wallZoom
| otherwise = clipZoom wallZoom
-- = maybe clipZoom zoomFromItem (yourItem w ^? itZoom) wallZoom
changeZoom curZoom
| curZoom > idealZoom + 0.01 = ((zoomOutSpeed-1)*curZoom + idealZoom) / zoomOutSpeed
| curZoom < idealZoom - 0.01 = ((zoomInSpeed -1)*curZoom + idealZoom) / zoomInSpeed