Commit d37a301e authored by Joris Guyonvarch's avatar Joris Guyonvarch

Adding a power to change the player color that reverse points to catch and points to avoid

parent 4521cdf1
Avoid
=====
cAtchVoid
=========
Avoid red points and catch the most green points as possible.
Catch the points of your color, avoid the other points.
You can play at [http://guyonvarch.github.io/avoid](http://guyonvarch.github.io/avoid).
Use the arrow keys to move and 'e' to change your color.
You can play at [http://guyonvarch.github.io/catchvoid](http://guyonvarch.github.io/catchvoid).
......@@ -3,7 +3,7 @@
<html>
<head>
<title>Avoid</title>
<title>cAtchVoid</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="resources/elm-runtime.js"></script>
......@@ -12,17 +12,25 @@
<body>
<h1>Avoid</h1>
<h1>cAtchVoid</h1>
<div id="game"></div>
<p>
Catch the points of your color, avoid the other points.
</p>
<p>
Use the arrow keys to move and 'e' to change your color.
</p>
<script type="text/javascript">
var myModule = Elm.Main;
var myContainer = document.getElementById('game');
Elm.embed(myModule, myContainer);
</script>
<a href="https://github.com/guyonvarch/avoid">
<a href="https://github.com/guyonvarch/catchvoid">
<img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67"
alt="Fork me on GitHub"
......
......@@ -2,20 +2,22 @@ module Cloud where
import Point (..)
import Player (..)
import Config (..)
import Geometry (distance)
type Cloud =
{ greenPoints : [Point]
, redPoints : [Point]
{ points : Config -> [Point]
, spawn : Float
, lastSpawn : Float
}
initCloud : Cloud
initCloud =
let spawn = 200
in { greenPoints = []
, redPoints = []
let spawn = 100
in { points config =
case config of
White -> []
Black -> []
, spawn = spawn
, lastSpawn = -spawn
}
......
......@@ -8,26 +8,33 @@ import Point (..)
import RandomValues (..)
import Physics (getMove)
import Cloud (..)
import Config (..)
cloudStep : Float -> RandomValues -> Player -> Cloud -> (Cloud, Int)
cloudStep time {greenPoint, redPoint} player {greenPoints, redPoints, spawn, lastSpawn} =
let insideGreenPoints = presentPoints time greenPoints
insideNotCaughtGreenPoints = filter (not . (playerPointCollision time player)) insideGreenPoints
addScore = (length insideGreenPoints) - (length insideNotCaughtGreenPoints)
presentRedPoints = presentPoints time redPoints
cloudStep time randomValues player {points, spawn, lastSpawn} =
let pointsToCatch = presentPoints time (points player.config)
presentAndNotCaughtPoints = filter (not . (playerPointCollision time player)) pointsToCatch
addScore = (length pointsToCatch) - (length presentAndNotCaughtPoints)
presentOtherPoints = presentPoints time (points (otherConfig player.config))
newCloud =
if time > lastSpawn + spawn then
let newGreenPoint = newPoint time greenPoint
newRedPoint = newPoint time redPoint
let newPoint1 = newPoint time randomValues.point1
newPoint2 = newPoint time randomValues.point2
in
{ greenPoints = newGreenPoint :: insideNotCaughtGreenPoints
, redPoints = newRedPoint :: presentRedPoints
{ points config =
if(config == player.config) then
newPoint1 :: presentAndNotCaughtPoints
else
newPoint2 :: presentOtherPoints
, spawn = spawn - sqrt(spawn) / 50
, lastSpawn = time
}
else
{ greenPoints = insideNotCaughtGreenPoints
, redPoints = presentRedPoints
{ points config =
if(config == player.config) then
presentAndNotCaughtPoints
else
presentOtherPoints
, spawn = spawn
, lastSpawn = lastSpawn
}
......
module Config where
data Config = White | Black
otherConfig : Config -> Config
otherConfig config =
case config of
White -> Black
Black -> White
......@@ -5,15 +5,16 @@ import Player (..)
import Game (Game)
import Point (..)
import Board (boardSize)
import Config (..)
display : Game -> Element
display {time, score, player, cloud, bestScore} =
let greenPointForms = map (pointForm time greenPointColor) cloud.greenPoints
redPointForms = map (pointForm time redPointColor) cloud.redPoints
let whitePointForms = map (pointForm time (configColor White)) (cloud.points White)
blackPointForms = map (pointForm time (configColor Black)) (cloud.points Black)
forms = boardForms
++ playerForms player
++ greenPointForms
++ redPointForms
++ whitePointForms
++ blackPointForms
++ scoreForms score
++ bestScoreForms bestScore
in collage (truncate boardSize.x) (truncate boardSize.y) forms
......@@ -22,10 +23,12 @@ boardForms : [Form]
boardForms = [filled boardColor (rect boardSize.x boardSize.y)]
boardColor : Color
boardColor = rgb 17 17 17
boardColor = rgb 103 123 244
playerForms : Player -> [Form]
playerForms player = [circleForm player.pos playerSize playerColor]
playerForms player =
let playerColor = configColor player.config
in [circleForm player.pos playerSize playerColor]
playerColor : Color
playerColor = rgb 224 224 224
......@@ -35,11 +38,11 @@ pointForm time color point =
let pos = pointMove point time
in circleForm pos pointSize color
greenPointColor : Color
greenPointColor = rgb 34 85 34
redPointColor : Color
redPointColor = rgb 170 0 0
configColor : Config -> Color
configColor config =
case config of
White -> rgb 240 240 240
Black -> rgb 14 17 33
circleForm : Vec2 -> Float -> Color -> Form
circleForm pos size color =
......@@ -64,15 +67,16 @@ bestScoreForms bestScore =
if(bestScore > 0) then
let text = "Record: " ++ (show bestScore)
pos =
{ x = boardSize.x / 2 - 65
{ x = -boardSize.x / 2 + 100
, y = -boardSize.y / 2 + 30
}
in [textForm text pos rightAligned]
in [textForm text pos leftAligned]
else []
textForm : String -> Vec2 -> (Text -> Element) -> Form
textForm content pos alignment =
let textElement = toText content
|> Text.height 30
|> typeface ["calibri", "arial"]
|> Text.color textColor
|> bold
......@@ -82,4 +86,4 @@ textForm content pos alignment =
|> move (pos.x, pos.y)
textColor : Color
textColor = rgb 224 224 224
textColor = rgb 14 17 33
......@@ -3,9 +3,12 @@ module Game where
import Player (..)
import Cloud (..)
import Vec2 (Vec2)
import Config (..)
import Keyboard (KeyCode)
type Game =
{ time : Float
, keysDown : [KeyCode]
, score : Int
, player : Player
, cloud : Cloud
......@@ -17,9 +20,11 @@ initialGame playerPos bestScore =
let initPlayer =
{ pos = playerPos
, speed = { x = 0, y = 0 }
, config = White
}
in
{ time = 0
, keysDown = []
, score = 0
, player = initPlayer
, cloud = initCloud
......
module Input where
import Keyboard
import Random
import Char (toCode)
import RandomValues (RandomValues)
import Keyboard (KeyCode, keysDown, arrows)
import Random
import Vec2 (Vec2)
type Input =
{ dir : Vec2
, inputKeysDown : [KeyCode]
, delta : Time
, randomValues : RandomValues
}
......@@ -15,10 +17,11 @@ type Input =
getInput : Signal Input
getInput =
let dtSignal = delta
dirSignal = lift recordIntToVec2 Keyboard.arrows
dirSignal = lift recordIntToVec2 arrows
randomFloatsSignal = Random.floatList (lift (\_ -> 6) dtSignal)
randomValuesSignal = lift floatsToRandomValues randomFloatsSignal
in sampleOn dtSignal <| Input <~ dirSignal
~ keysDown
~ dtSignal
~ randomValuesSignal
......@@ -33,16 +36,16 @@ recordIntToVec2 {x, y} =
floatsToRandomValues : [Float] -> RandomValues
floatsToRandomValues [angle1, x1, y1, angle2, x2, y2] =
let greenPoint =
let point1 =
{ angle = angle1
, x = x1
, y = y1
}
redPoint =
point2 =
{ angle = angle2
, x = x2
, y = y2
}
in { greenPoint = greenPoint
, redPoint = redPoint
in { point1 = point1
, point2 = point2
}
module Player where
import Vec2 (..)
import Config (Config)
type Player =
{ pos : Vec2
, speed : Vec2
, config : Config
}
playerSize : Float
playerSize = 10
playerSpeed : Float -> Float
playerSpeed dt = dt / 500
playerSpeed dt = dt / 400
......@@ -18,7 +18,7 @@ pointSize : Float
pointSize = 5
pointSpeed : Float -> Float
pointSpeed dt = dt / 25
pointSpeed dt = dt / 20
pointSpawnDist : Float
pointSpawnDist = boardDiagonal * 3 / 5
......
module RandomValues where
type RandomValues =
{ greenPoint : PointRandomValues
, redPoint : PointRandomValues
{ point1 : PointRandomValues
, point2 : PointRandomValues
}
type PointRandomValues =
......
......@@ -11,26 +11,38 @@ import Input (Input)
import Physics (getNewPosAndSpeed)
import RandomValues (..)
import CloudStep (cloudStep)
import Config (otherConfig)
import Keyboard (KeyCode)
import Char (fromCode, toCode)
step : Input -> Game -> Game
step {dir, delta, randomValues} {time, score, player, cloud, bestScore} =
if(playerPointsCollision time player cloud.redPoints) then
let newBestScore = if(score > bestScore) then score else bestScore
in initialGame player.pos newBestScore
else
let newTime = time + delta
newPlayer = playerStep delta dir player
(newCloud, addScore) = cloudStep time randomValues player cloud
in { time = newTime
, score = score + addScore
, player = newPlayer
, cloud = newCloud
, bestScore = bestScore
}
step {dir, inputKeysDown, delta, randomValues} {time, keysDown, score, player, cloud, bestScore} =
let hostilePoints = cloud.points (otherConfig player.config)
in if(playerPointsCollision time player hostilePoints) then
let newBestScore = if(score > bestScore) then score else bestScore
in initialGame player.pos newBestScore
else
let newTime = time + delta
newPlayer = playerStep delta dir (newKeyCode keysDown inputKeysDown) player
(newCloud, addScore) = cloudStep time randomValues newPlayer cloud
in { time = newTime
, keysDown = inputKeysDown
, score = score + addScore
, player = newPlayer
, cloud = newCloud
, bestScore = bestScore
}
playerStep : Float -> Vec2 -> Player -> Player
playerStep dt dir player =
playerStep : Float -> Vec2 -> (KeyCode -> Bool) -> Player -> Player
playerStep dt dir newKey player =
let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
newConfig = if (newKey (toCode 'e')) then otherConfig player.config else player.config
in { pos = inBoard playerSize pos
, speed = speed
, config = newConfig
}
newKeyCode : [KeyCode] -> [KeyCode] -> KeyCode -> Bool
newKeyCode lastKeyCodes newKeyCodes keyCode =
let contains = (\l -> l > 0) . length . filter (\kc -> kc == keyCode)
in not (contains lastKeyCodes) && (contains newKeyCodes)
body {
margin: 0;
background-color: #111111;
background-color: #05060c;
font-family: calibri;
}
h1 {
font-weight: bold;
background-color: #225522;
background-color: #1b203f;
min-width: 500px;
color: white;
margin: 0;
padding-top: 20px;
padding-bottom: 20px;
font-size: 36px;
padding-top: 30px;
padding-bottom: 30px;
text-align: center;
}
#game {
margin-left: auto;
margin-right: auto;
margin-bottom: 40px;
border-top: 10px dashed #222222;
border-bottom: 10px dashed #222222;
width: 500px;
height: 500px;
}
p {
text-align: center;
color: #eeeeee;
font-style: italic;
font-size: 17px;
}
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