Commit a1a3dbad authored by Joris Guyonvarch's avatar Joris Guyonvarch

Adding a target to catch

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