forked from mkm3110/CornellSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrect.ml
More file actions
51 lines (43 loc) · 1.51 KB
/
Copy pathrect.ml
File metadata and controls
51 lines (43 loc) · 1.51 KB
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
(* ;; 288 224 *)
type t = {
mutable left : int;
mutable top : int;
mutable right : int;
mutable bottom : int;
}
let char_size = 16
(* true if the position is vertically at the same level at the rectable *)
let in_vertical (pos : Position.t) rect =
pos.y < rect.top && pos.y > rect.bottom + -char_size
(* true if the position is horizontally at the same level at the rectable *)
let in_horizontal (pos : Position.t) rect =
pos.x > rect.left - char_size && pos.x < rect.right
let in_rect (pos : Position.t) rect =
in_vertical pos rect && in_horizontal pos rect
(* return true if the object of the position will enter rectagle if moving in
the direction *)
let will_enter_rect pos rect dir offset =
match dir with
| 'a' ->
if in_vertical pos rect && pos.x > rect.left then
pos.x < rect.right + offset
else false
| 'd' ->
if in_vertical pos rect && pos.x < rect.right then
pos.x > rect.left - (char_size + offset)
else false
| 'w' ->
if in_horizontal pos rect && pos.y < rect.top then
pos.y > rect.bottom - offset
else false
| 's' ->
if in_horizontal pos rect && pos.y > rect.bottom then
pos.y < rect.top + offset
else false
| _ -> false
let make_rect top left right bottom = { left; right; top; bottom }
let make_rect_wh (pos : Position.t) w h =
{ left = pos.x; right = pos.x + w; top = pos.y + h; bottom = pos.y }
let draw rect =
Graphics.fill_rect rect.left rect.bottom (rect.right - rect.left)
(rect.top - rect.bottom)