Commit a1a3dbad authored by Joris Guyonvarch's avatar Joris Guyonvarch

Adding a target to catch

parent b3975b15
...@@ -17,10 +17,9 @@ ...@@ -17,10 +17,9 @@
<div id="game"></div> <div id="game"></div>
<script type="text/javascript"> <script type="text/javascript">
var myPorts = {};
var myContainer = document.getElementById('game');
var myModule = Elm.Main; var myModule = Elm.Main;
Elm.embed(myModule, myContainer, myPorts); var myContainer = document.getElementById('game');
Elm.embed(myModule, myContainer);
</script> </script>
<a href="https://github.com/guyonvarch/avoid"> <a href="https://github.com/guyonvarch/avoid">
......
...@@ -5,22 +5,24 @@ import Player (..) ...@@ -5,22 +5,24 @@ import Player (..)
import Game (Game) import Game (Game)
import Enemy (..) import Enemy (..)
import Board (boardSize) import Board (boardSize)
import Target (..)
display : Game -> Element display : Game -> Element
display {time, player, enemyState, bestTime} = display {time, player, target, enemyState, bestScore} =
let enemyForms = map (enemyForm time) enemyState.enemies let enemyForms = map (enemyForm time) enemyState.enemies
forms = boardForms forms = boardForms
++ playerForms player ++ playerForms player
++ targetForms target.pos
++ enemyForms ++ enemyForms
++ bestTimeForms bestTime ++ scoreForms target.score
++ timeForms time ++ bestScoreForms bestScore
in collage (truncate boardSize.x) (truncate boardSize.y) forms in collage (truncate boardSize.x) (truncate boardSize.y) forms
boardForms : [Form] boardForms : [Form]
boardForms = [filled boardColor (rect boardSize.x boardSize.y)] boardForms = [filled boardColor (rect boardSize.x boardSize.y)]
boardColor : Color boardColor : Color
boardColor = rgb 34 122 34 boardColor = rgb 17 17 17
playerForms : Player -> [Form] playerForms : Player -> [Form]
playerForms player = [circleForm player.pos playerSize playerColor] playerForms player = [circleForm player.pos playerSize playerColor]
...@@ -28,6 +30,12 @@ playerForms player = [circleForm player.pos playerSize playerColor] ...@@ -28,6 +30,12 @@ playerForms player = [circleForm player.pos playerSize playerColor]
playerColor : Color playerColor : Color
playerColor = rgb 224 224 224 playerColor = rgb 224 224 224
targetForms : Vec2 -> [Form]
targetForms pos = [circleForm pos targetSize targetColor]
targetColor : Color
targetColor = rgb 34 85 34
enemyForm : Float -> Enemy -> Form enemyForm : Float -> Enemy -> Form
enemyForm time enemy = enemyForm time enemy =
let pos = enemyMove enemy time let pos = enemyMove enemy time
...@@ -39,17 +47,25 @@ enemyColor = rgb 170 0 0 ...@@ -39,17 +47,25 @@ enemyColor = rgb 170 0 0
circleForm : Vec2 -> Float -> Color -> Form circleForm : Vec2 -> Float -> Color -> Form
circleForm pos size color = circleForm pos size color =
let outline = circle size let outline = circle size
|> filled black |> filled outlineColor
inside = circle (size - 2) inside = circle (size - 1)
|> filled color |> filled color
in group [outline, inside] in group [outline, inside]
|> move (pos.x, pos.y) |> move (pos.x, pos.y)
bestTimeForms : Float -> [Form] outlineColor : Color
bestTimeForms bestTime = outlineColor = rgb 34 34 34
if(bestTime > 0) then
let seconds = truncate (bestTime / 1000) scoreForms : Int -> [Form]
text = "Record: " ++ (show seconds) scoreForms score =
let text = (show score)
scorePos = { x = 0.0, y = boardSize.y / 2 - 30 }
in [textForm text scorePos centered]
bestScoreForms : Int -> [Form]
bestScoreForms bestScore =
if(bestScore > 0) then
let text = "Record: " ++ (show bestScore)
pos = pos =
{ x = boardSize.x / 2 - 65 { x = boardSize.x / 2 - 65
, y = -boardSize.y / 2 + 30 , y = -boardSize.y / 2 + 30
...@@ -57,13 +73,6 @@ bestTimeForms bestTime = ...@@ -57,13 +73,6 @@ bestTimeForms bestTime =
in [textForm text pos rightAligned] in [textForm text pos rightAligned]
else [] else []
timeForms : Float -> [Form]
timeForms time =
let seconds = truncate (time / 1000)
text = (show seconds)
pos = { x = 0.0, y = boardSize.y / 2 - 30 }
in [textForm text pos centered]
textForm : String -> Vec2 -> (Text -> Element) -> Form textForm : String -> Vec2 -> (Text -> Element) -> Form
textForm content pos alignment = textForm content pos alignment =
let textElement = toText content let textElement = toText content
......
...@@ -15,7 +15,7 @@ enemyMove enemy time = ...@@ -15,7 +15,7 @@ enemyMove enemy time =
enemy.move enemy.initTime enemy.initPos enemy.initDest time enemy.move enemy.initTime enemy.initPos enemy.initDest time
enemySize : Float enemySize : Float
enemySize = 8 enemySize = 5
enemySpeed : Float -> Float enemySpeed : Float -> Float
enemySpeed dt = dt / 25 enemySpeed dt = dt / 25
......
...@@ -18,12 +18,12 @@ initEnemyState = ...@@ -18,12 +18,12 @@ initEnemyState =
, lastSpawn = -spawn , lastSpawn = -spawn
} }
playerEnemyCollision : Float -> Player -> Enemy -> Bool
playerEnemyCollision time player enemy =
let enemyPos = enemyMove enemy time
in (distance enemyPos player.pos) < enemySize + playerSize
playerEnemiesCollision : Float -> Player -> [Enemy] -> Bool playerEnemiesCollision : Float -> Player -> [Enemy] -> Bool
playerEnemiesCollision time player enemies = playerEnemiesCollision time player enemies =
let collision = playerEnemyCollision time player let collision = playerEnemyCollision time player
in length (filter collision enemies) > 0 in length (filter collision enemies) > 0
playerEnemyCollision : Float -> Player -> Enemy -> Bool
playerEnemyCollision time player enemy =
let enemyPos = enemyMove enemy time
in (distance enemyPos player.pos) < enemySize + playerSize
...@@ -3,23 +3,27 @@ module Game where ...@@ -3,23 +3,27 @@ module Game where
import Player (..) import Player (..)
import Enemy (..) import Enemy (..)
import EnemyState (..) import EnemyState (..)
import Target(..)
import Vec2 (Vec2) import Vec2 (Vec2)
type Game = type Game =
{ time : Float { time : Float
, player : Player , player : Player
, target : Target
, enemyState : EnemyState , enemyState : EnemyState
, bestTime : Float , bestScore : Int
} }
initialGame : Vec2 -> Float -> Game initialGame : Vec2 -> Int -> Game
initialGame playerPos bestTime = initialGame playerPos bestScore =
let initPlayer = let initPlayer =
{ pos = playerPos { pos = playerPos
, speed = { x = 0, y = 0 } , speed = { x = 0, y = 0 }
} }
in { time = 0 in
, player = initPlayer { time = 0
, enemyState = initEnemyState , player = initPlayer
, bestTime = bestTime , target = initTarget
} , enemyState = initEnemyState
, bestScore = bestScore
}
...@@ -16,7 +16,7 @@ getInput : Signal Input ...@@ -16,7 +16,7 @@ getInput : Signal Input
getInput = getInput =
let dtSignal = delta let dtSignal = delta
dirSignal = lift recordIntToVec2 Keyboard.arrows dirSignal = lift recordIntToVec2 Keyboard.arrows
randomFloatsSignal = Random.floatList (lift (\_ -> 3) dtSignal) randomFloatsSignal = Random.floatList (lift (\_ -> 5) dtSignal)
randomValuesSignal = lift floatsToRandomValues randomFloatsSignal randomValuesSignal = lift floatsToRandomValues randomFloatsSignal
in sampleOn dtSignal <| Input <~ dirSignal in sampleOn dtSignal <| Input <~ dirSignal
~ dtSignal ~ dtSignal
...@@ -32,8 +32,10 @@ recordIntToVec2 {x, y} = ...@@ -32,8 +32,10 @@ recordIntToVec2 {x, y} =
} }
floatsToRandomValues : [Float] -> RandomValues floatsToRandomValues : [Float] -> RandomValues
floatsToRandomValues [enemyAngle, enemyX, enemyY] = floatsToRandomValues [enemyAngle, enemyX, enemyY, targetX, targetY] =
{ enemyAngle = enemyAngle { enemyAngle = enemyAngle
, enemyX = enemyX , enemyX = enemyX
, enemyY = enemyY , enemyY = enemyY
, targetX = targetX
, targetY = targetY
} }
...@@ -7,4 +7,4 @@ import Input (getInput) ...@@ -7,4 +7,4 @@ import Input (getInput)
import Vec2 (originVec) import Vec2 (originVec)
main : Signal Element main : Signal Element
main = lift display (foldp step (initialGame originVec 0) getInput) main = lift display (foldp step (initialGame { x = -50, y = 0 } 0) getInput)
...@@ -4,4 +4,6 @@ type RandomValues = ...@@ -4,4 +4,6 @@ type RandomValues =
{ enemyAngle : Float { enemyAngle : Float
, enemyX : Float , enemyX : Float
, enemyY : Float , enemyY : Float
, targetX : Float
, targetY : Float
} }
...@@ -11,20 +11,23 @@ import Physics (getNewPosAndSpeed, getMove) ...@@ -11,20 +11,23 @@ import Physics (getNewPosAndSpeed, getMove)
import Board (boardSize, boardDiagonal) import Board (boardSize, boardDiagonal)
import Geometry (..) import Geometry (..)
import RandomValues (RandomValues) import RandomValues (RandomValues)
import Target(..)
step : Input -> Game -> Game step : Input -> Game -> Game
step {dir, delta, randomValues} {time, player, enemyState, bestTime} = step {dir, delta, randomValues} {time, player, target, enemyState, bestScore} =
if(playerEnemiesCollision time player enemyState.enemies) then if(playerEnemiesCollision time player enemyState.enemies) then
let newBestTime = if(time > bestTime) then time else bestTime let newBestScore = if(target.score > bestScore) then target.score else bestScore
in initialGame player.pos newBestTime in initialGame player.pos newBestScore
else else
let newTime = time + delta let newTime = time + delta
newPlayer = playerStep delta dir player newPlayer = playerStep delta dir player
newTarget = targetStep player randomValues target
newEnemyState = enemyStep time randomValues enemyState newEnemyState = enemyStep time randomValues enemyState
in { time = newTime in { time = newTime
, player = newPlayer , player = newPlayer
, target = newTarget
, enemyState = newEnemyState , enemyState = newEnemyState
, bestTime = bestTime , bestScore = bestScore
} }
playerStep : Float -> Vec2 -> Player -> Player playerStep : Float -> Vec2 -> Player -> Player
...@@ -34,6 +37,15 @@ playerStep dt dir player = ...@@ -34,6 +37,15 @@ playerStep dt dir player =
, speed = speed , speed = speed
} }
targetStep : Player -> RandomValues -> Target -> Target
targetStep player randomValues target =
if(targetCollision player target) then
{ score = target.score + 1
, pos = randomBoardPosition (randomValues.targetX, randomValues.targetY) (0.8, 0.8)
}
else
target
enemyStep : Float -> RandomValues -> EnemyState -> EnemyState enemyStep : Float -> RandomValues -> EnemyState -> EnemyState
enemyStep time randomValues {enemies, spawn, lastSpawn} = enemyStep time randomValues {enemies, spawn, lastSpawn} =
let isPresent enemy = (distance (enemyMove enemy time) originVec) < enemyAwayDist let isPresent enemy = (distance (enemyMove enemy time) originVec) < enemyAwayDist
...@@ -65,8 +77,12 @@ enemyInitPos randomValues = ...@@ -65,8 +77,12 @@ enemyInitPos randomValues =
enemyDestination : RandomValues -> Vec2 enemyDestination : RandomValues -> Vec2
enemyDestination randomValues = enemyDestination randomValues =
let destWidth = boardSize.x randomBoardPosition (randomValues.enemyX, randomValues.enemyY) (1, 1)
destHeight = boardSize.y
in { x = destWidth * randomValues.enemyX - destWidth / 2 randomBoardPosition : (Float, Float) -> (Float, Float) -> Vec2
, y = destHeight * randomValues.enemyY - destHeight / 2 randomBoardPosition (randomX, randomY) (percentX, percentY) =
let width = boardSize.x * percentX
height = boardSize.y * percentY
in { x = width * randomX - width / 2
, y = height * randomY - height / 2
} }
module Target where
import Board (boardSize)
import Geometry (distance)
import Vec2 (Vec2)
import Player (..)
type Target =
{ score : Int
, pos : Vec2
}
targetSize : Float
targetSize = 20
targetCollision : Player -> Target -> Bool
targetCollision player target =
(distance player.pos target.pos) < playerSize + targetSize
initTarget : Target
initTarget =
{ score = 0
, pos = { x = 50, y = 0 }
}
...@@ -8,7 +8,7 @@ h1 { ...@@ -8,7 +8,7 @@ h1 {
font-weight: bold; font-weight: bold;
background-color: #225522; background-color: #225522;
min-width: 500px; min-width: 500px;
color: #e0e0e0; color: white;
margin: 0; margin: 0;
padding-top: 20px; padding-top: 20px;
padding-bottom: 20px; padding-bottom: 20px;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment