Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished homework and optional challenge #12

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Main exposing (..)

import Asteroids
import Mario
import TimeTravel exposing (addTimeTravel)

import Playground

Expand All @@ -14,4 +15,5 @@ gameApplication game =
-- The main entry point for the app

main = Mario.game
|> addTimeTravel
|> gameApplication
41 changes: 37 additions & 4 deletions src/Mario.elm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ view computer mario =
[ rectangle (rgb 174 238 238) w h -- sky
, rectangle (rgb 74 163 41) w 100 -- ground
|> moveY b
, rectangle (rgb 245 97 11) 100 100 -- lava
|> moveY b
|> moveX 200
, mario.trace
|> pathToPolygonVertices 1.5
|> polygon black
Expand Down Expand Up @@ -98,9 +101,21 @@ update computer mario =
newY = max 0 (mario.y + dt * vy)
in
{ mario
| x = newX
, y = newY
, vx = vx
| x =
if touchingLava newX newY then
0
else
newX
, y =
if touchingLava newX newY then
0
else
newY
, vx =
if touchingLava newX newY then
0
else
vx
, vy = (newY - mario.y) / dt
, dir =
if (toX computer.keyboard) < 0 then
Expand All @@ -109,7 +124,11 @@ update computer mario =
Right
else
mario.dir -- face direction of last movement when standing still
, trace = addPointUnlessDuplicate (newX, newY) mario.trace
, trace =
if touchingLava newX newY then
[]
else
addPointUnlessDuplicate (newX, newY) mario.trace
}

addPointUnlessDuplicate point path =
Expand All @@ -118,6 +137,20 @@ addPointUnlessDuplicate point path =
else
point :: path


touchingLava x y =
if x < 250 then
if x > 150 then
if y == 0 then
True
else
False
else
False
else
False


-- HELPERS

-- Elm’s playground package doesn't have any way to stroke a path.
Expand Down
88 changes: 88 additions & 0 deletions src/TimeTravel.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,91 @@ module TimeTravel exposing (addTimeTravel)
import Playground exposing (..)
import Set

controlBarHeight = 64
maxVisibleHistory = 2000

-- Converts an index in the history list to an x coordinate on the screen
historyIndexToX computer index =
(toFloat index) / maxVisibleHistory * computer.screen.width

addTimeTravel rawGame =
{ initialState = initialStateWithTimeTravel rawGame
, updateState = updateWithTimeTravel rawGame
, view = viewWithTimeTravel rawGame
}

initialStateWithTimeTravel rawGame =
{rawModel = rawGame.initialState
, paused = False
, history = []
, historyPlaybackPosition = 0}

viewWithTimeTravel rawGame computer model =
let
-- Creates a rectangle at the top of the screen, stretching from the
-- left edge up to a specific position within the history timeline
historyBar color opacity index =
let
width = historyIndexToX computer index
in
rectangle color width controlBarHeight
|> move (computer.screen.left + width / 2)
(computer.screen.top - controlBarHeight / 2)
|> fade opacity
helpMessage =
if model.paused then
"Press R to resume"
else
"Press T to time travel"
in
(rawGame.view computer model.rawModel) ++
[ historyBar black 0.3 maxVisibleHistory
, historyBar red 0.6 (List.length model.history) -- idk why this won't work?
, historyBar purple 0.6 model.historyPlaybackPosition
, words white helpMessage
|> move 0 (computer.screen.top - controlBarHeight / 2)
]


updateWithTimeTravel rawGame computer model =
if (keyPressed "T" computer) && (computer.mouse.down)then
let
newPlaybackPosition = min (mousePosToHistoryIndex computer) (List.length model.history)
replayHistory pastInputs = List.foldl rawGame.updateState rawGame.initialState pastInputs
in
{ model
| historyPlaybackPosition = newPlaybackPosition
, rawModel = replayHistory (List.take newPlaybackPosition model.history)
}
else if keyPressed "T" computer then
{model | paused = True}
else if keyPressed "R" computer then
{model
| paused = False
, history = List.take model.historyPlaybackPosition model.history
}
else if keyPressed "C" computer then
{model
| history = []
, rawModel = List.foldl rawGame.updateState rawGame.initialState model.history
}
else if model.paused then
model
else
{ model | rawModel = rawGame.updateState computer model.rawModel
, history = model.history ++ [computer]
, historyPlaybackPosition = List.length model.history + 1}


keyPressed keyName computer =
[ String.toLower keyName
, String.toUpper keyName
]
|> List.any (\key -> Set.member key computer.keyboard.keys)


-- Converts the mouse's current position to an index within the history list
mousePosToHistoryIndex computer =
(computer.mouse.x - computer.screen.left)
/ computer.screen.width * maxVisibleHistory
|> round