-
Notifications
You must be signed in to change notification settings - Fork 5
/
Util.fs
55 lines (43 loc) · 1.53 KB
/
Util.fs
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
module Util
open GameCore.GameModel
open Constants
open Model
let distanceBetween (x1, y1) (x2, y2) =
sqrt <| (float x2 - float x1)**2. + (float y2 - float y1)**2.
let getTile x y map =
if x < 0 || y < 0 || x >= dungeonSize || y >= dungeonSize then None
else Some <| List.item (x * dungeonSize + y) map
let getTileKind x y map =
match getTile x y map with
| Some (Tile (_, _, kind, _)) -> Some kind
| _ -> None
let isOpen map x y =
match getTile x y map with
| None -> false
| Some (Tile (_, _, kind, _)) ->
match kind with
| Room _ | Door _ | Corridor _ -> true
| _ -> false
let isVisible (x, y, width, height) =
x + width/2 > 0
&& x - width/2 < screenWidth
&& y > 0
&& y - height < screenHeight
let renderRect (wx, wy) (width, height) =
if showGrid then
wx - width / 2 + 1, wy - height + 1, width - 2, height - 2
else
wx - width / 2, wy - height, width, height
let worldPos (tx, ty) = tx * tilewidth, ty * tileheight
let currentWorldPos runState entity =
let wx, wy = entity.position |> worldPos
match entity.state with
| Walking (startTime, path) ->
let timeBetweenTiles = entity.timeBetweenTiles
let moveTime = (runState.elapsed - startTime) % timeBetweenTiles
let nextPos = List.head path
let distance = moveTime / timeBetweenTiles
let nx, ny = nextPos |> worldPos
let dx, dy = nx - wx, ny - wy
wx + int (float dx * distance), wy + int (float dy * distance)
| _ -> wx, wy