1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
module Step where
import Vec2 (..)
import Game (..)
import Player (..)
import EnemyState (..)
import Player (playerSpeed)
import Enemy (enemySpeed, enemyMove, enemyAwayDist)
import Input (Input)
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, target, enemyState, bestScore} =
if(playerEnemiesCollision time player enemyState.enemies) then
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
, bestScore = bestScore
}
playerStep : Float -> Vec2 -> Player -> Player
playerStep dt dir player =
let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
in { pos = inBoard playerSize pos
, 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
presentEnemies = filter isPresent enemies
in if time > lastSpawn + spawn then
let newEnemy =
{ initTime = time
, initPos = enemyInitPos randomValues
, initDest = enemyDestination randomValues
, move initTime initPos initDest time =
let delta = time - initTime
move = getMove (enemySpeed delta) (initDest `sub` initPos)
in initPos `add` move
}
in { enemies = newEnemy :: presentEnemies
, spawn = spawn - sqrt(spawn) / 50
, lastSpawn = time
}
else { enemies = presentEnemies
, spawn = spawn
, lastSpawn = lastSpawn
}
enemyInitPos : RandomValues -> Vec2
enemyInitPos randomValues =
let angle = randomValues.enemyAngle * (degrees 360)
dist = boardDiagonal * 3 / 5
in polarToCartesian angle dist
enemyDestination : RandomValues -> Vec2
enemyDestination randomValues =
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
}
|