Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Make it easier to follow movement by avoiding abrupt movement. Model camera following player. + Increase interactive area to input command to move.
- Loading branch information
Showing
5 changed files
with
121 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
module Common exposing (..) | |||
|
|||
withListTransformApplied : List (a -> a) -> a -> a | |||
withListTransformApplied listTransform original = | |||
case listTransform |> List.head of | |||
Just transform -> withListTransformApplied (listTransform |> List.tail |> Maybe.withDefault []) (transform original) | |||
Nothing -> original |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,90 @@ | |||
module Console exposing (State, init, updateForChangedGame, updateForTimeProgress, applyCameraTransformToSvg) | |||
|
|||
import GameWorld | |||
import Visuals | |||
import Common exposing (..) | |||
import Point2d exposing (Point2d) | |||
import Vector2d exposing (Vector2d) | |||
import Dict | |||
import Tuple2 | |||
import Svg | |||
import Svg.Attributes as SA | |||
|
|||
|
|||
type alias State = | |||
{ knownGame : GameWorld.State | |||
, cameraVelocity : Vector2d | |||
, cameraOffset : Vector2d | |||
} | |||
|
|||
|
|||
init : GameWorld.State -> State | |||
init game = | |||
{ knownGame = game | |||
, cameraOffset = game |> defaultCameraOffsetFromGame |> Maybe.withDefault Vector2d.zero | |||
, cameraVelocity = Vector2d.zero | |||
} | |||
|
|||
applyCameraTransformToSvg : State -> Svg.Svg event -> Svg.Svg event | |||
applyCameraTransformToSvg state = | |||
List.singleton >> | |||
(Svg.g [ SA.transform (Visuals.svgTransformTranslate (state.cameraOffset |> Vector2d.components |> Tuple2.mapBoth negate))]) | |||
|
|||
defaultCameraOffsetFromGame : GameWorld.State -> Maybe Vector2d | |||
defaultCameraOffsetFromGame game = | |||
let | |||
playerVisualLocation = | |||
case game.playerLocation of | |||
GameWorld.OnNode nodeId -> game.nodes |> Dict.get nodeId |> Maybe.map .visualLocation | |||
in | |||
playerVisualLocation | |||
|> Maybe.map (Point2d.coordinates >> Vector2d.fromComponents) | |||
|
|||
updateForChangedGame : GameWorld.State -> State -> State | |||
updateForChangedGame gameState stateBefore = { stateBefore | knownGame = gameState } | |||
|
|||
updateForTimeProgress : Int -> State -> State | |||
updateForTimeProgress = updateAnimateCamera | |||
|
|||
updateAnimateCamera : Int -> State -> State | |||
updateAnimateCamera progressAmountMilli consoleBefore = | |||
let | |||
stepSizes = | |||
(animateCameraStepSizeMax |> List.repeat (progressAmountMilli // animateCameraStepSizeMax)) ++ | |||
[ progressAmountMilli % animateCameraStepSizeMax ] | |||
in | |||
consoleBefore | |||
|> withListTransformApplied (stepSizes |> List.map updateAnimateCameraSingleStep) | |||
|
|||
animateCameraStepSizeMax : Int | |||
animateCameraStepSizeMax = 5 | |||
|
|||
updateAnimateCameraSingleStep : Int -> State -> State | |||
updateAnimateCameraSingleStep progressAmountMilli consoleBefore = | |||
if progressAmountMilli < 1 | |||
then consoleBefore | |||
else | |||
let | |||
cameraDestinationOffset = (init consoleBefore.knownGame).cameraOffset | |||
|
|||
progressSeconds = (progressAmountMilli |> toFloat) * 1e-3 | |||
|
|||
cameraVelocityAcceleratedToDestination = | |||
{ offset = consoleBefore.cameraOffset, velocity = consoleBefore.cameraVelocity } | |||
|> accelerateCameraTowardsOffset (progressSeconds * 3) cameraDestinationOffset | |||
|> .velocity | |||
|
|||
dampFactor = 0.9 ^ (progressSeconds * 30) | |||
|
|||
cameraVelocity = cameraVelocityAcceleratedToDestination |> Vector2d.scaleBy dampFactor | |||
|
|||
cameraOffset = cameraVelocity |> Vector2d.scaleBy progressSeconds |> Vector2d.sum consoleBefore.cameraOffset | |||
in | |||
{ consoleBefore | cameraOffset = cameraOffset, cameraVelocity = cameraVelocity } | |||
|
|||
accelerateCameraTowardsOffset : Float -> Vector2d -> { offset: Vector2d, velocity : Vector2d } -> { velocity: Vector2d } | |||
accelerateCameraTowardsOffset accelerationFactor cameraDestinationOffset cameraBefore = | |||
let | |||
offsetAcceleration = Vector2d.difference cameraDestinationOffset cameraBefore.offset |> Vector2d.scaleBy accelerationFactor | |||
in | |||
{ velocity = cameraBefore.velocity |> Vector2d.sum offsetAcceleration } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters