-
Notifications
You must be signed in to change notification settings - Fork 2
/
world.ml
165 lines (144 loc) · 3.52 KB
/
world.ml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
open Core.Std
open Types
open Sprite
open Board
type t = {
board : Board.t;
preview : Board.t;
mutable dirty : bool;
mutable current : Sprite.t;
mutable state : state;
mutable score : int;
mutable lines : int;
mutable level : int;
mutable future : piece;
mutable delay : float;
(* starting position for new pieces *)
start_row : int;
start_col : int;
conf : configuration
}
let update_preview w =
Board.clear w.preview;
Board.place_piece w.preview { Sprite.
piece = w.future;
rotation = R0;
row = Piece.preview_offset w.future;
col = 1
}
let level_delay c level =
(0.9 ** (Float.of_int level)) *. c.fall_delay
let make conf level =
let mid = conf.n / 2 in
let w = {
board = Board.make conf.m conf.n;
preview = Board.make conf.ph conf.pw;
dirty = true; (* render the initial board *)
state = Falling conf.fall_delay;
score = 0;
lines = 0;
level = level;
future = Piece.random ();
current = Sprite.make_random 1 mid;
delay = level_delay conf level;
start_row = 1;
start_col = mid;
conf = conf
}
in
update_preview w;
w
let reset w level =
Board.clear w.board;
w.state <- Falling w.conf.fall_delay;
w.dirty <- true;
w.score <- 0;
w.lines <- 0;
w.level <- 0;
w.delay <- w.conf.fall_delay;
w.future <- Piece.random ();
w.current <- Sprite.make_random w.start_row w.start_col;
update_preview w
(* Place piece on board *)
let place_current_piece w =
Board.place_piece w.board w.current
let remove_current_piece w =
Board.remove_piece w.board w.current
let fix_current_piece w =
let s = w.current in
let c = Color.tweak (Piece.color s.piece) in
Board.draw_piece w.board s (Some c)
let can_place w s =
Board.can_place_piece w.board s
(* State transitions *)
(* Pause / Unpause *)
let pause w =
match w.state with
| Paused _ -> ()
| s -> begin
w.state <- Paused s;
w.dirty <- true
end
let unpause w =
match w.state with
| Paused s -> begin
w.state <- s;
w.dirty <- true
end
| _ -> ()
(* Next piece *)
let next_piece w =
w.current <- Sprite.make w.future w.start_row w.start_col;
w.future <- Piece.random ();
update_preview w;
w.dirty <- true;
if can_place w w.current then
w.state <- Falling(w.delay)
else
w.state <- Dead
let clear_lines w n =
let compact_delay = 0.250 (* s *) in
w.state <- Compacting(compact_delay);
w.lines <- n + w.lines;
w.score <- n * n + w.score;
w.dirty <- true;
if w.lines > 10 + w.level * 10 then
begin
w.level <- w.level + 1;
w.delay <- level_delay w.conf w.level;
end
let finalize_current_piece w =
fix_current_piece w;
w.score <- 1 + w.score;
w.dirty <- true;
match List.length (Board.filled_rows w.board) with
| 0 -> next_piece w
| n -> clear_lines w n
let finish_compacting w =
Board.compact w.board;
next_piece w
(* Move piece on board *)
let move_current_piece w ~drop ~action =
let d = if drop then 1 else 0 in
let s' = Sprite.move_down w.current d in
let f = match action with
| Some Left -> Sprite.move_left
| Some Right -> Sprite.move_right
| Some Rotate -> Sprite.rotate_cw
| Some Rotate' -> Sprite.rotate_ccw
| _ -> Sprite.dont_move
in
let s'' = f s' in
let s = if can_place w s'' then s'' else s' in
if can_place w s then
( w.current <- s; true )
else
false
let drop_current_piece w =
let action = Some Drop in
remove_current_piece w;
while (move_current_piece w ~drop:true ~action) do
()
done;
place_current_piece w;
finalize_current_piece w