-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.ml
410 lines (368 loc) · 11 KB
/
editor.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
open Sys
open Graphics
open Util
open Math2d
open Collide
open Mlgrope
open Backend
open Frontend
open Level
open Player
exception Play of game_state
type entity_property =
| Position of vec
| Radius
| Size
| Length
| Angle
| Strength
| Vertex of vec (* TODO *)
type editor = {
size : vec;
state: game_state;
selected : entity option;
selected_property : entity_property;
}
let panel_width = 200.
let panel_color = Graphics.rgb 127 127 127
let grid_size = 10.
let grid_color = Graphics.rgb 230 230 230
let handle_size = 10.
let handle_color = Graphics.black
let strength_per_division = 50.
let stick_to_grid pt =
Math2d.map (fun k -> grid_size *. round_float (k /. grid_size)) pt
let update_position entity position =
match entity with
| Ball(b) -> Ball{b with position}
| Bubble(b) -> Bubble{b with position}
| Magnet(m) -> Magnet{m with position}
| Rope(b) -> Rope{b with position}
| Elastic(b) -> Elastic{b with position}
| Goal(b) -> Goal{position}
| Star(b) -> Star{position}
| Block(b) ->
let center = average b.vertices in
let delta = position -: center in
let vertices = List.map (fun v -> v +: delta) b.vertices in
Block{b with vertices}
| Fan(f) -> Fan{f with position}
| Spike(s) -> Spike{s with position}
let draw_grid size =
let grid_size = int_of_float grid_size in
let (w, h) = ints_of_vec size in
Graphics.set_color grid_color;
for i = 0 to w/grid_size do
Graphics.moveto (i*grid_size) 0;
Graphics.lineto (i*grid_size) h
done;
for i = 0 to h/grid_size do
Graphics.moveto 0 (i*grid_size);
Graphics.lineto w (i*grid_size)
done
let panel_block_vertices pos =
let block_size = 10. in
[
pos +: {x = block_size; y = block_size};
pos +: {x = -. block_size; y = block_size};
pos +: {x = -. block_size; y = -. block_size};
pos +: {x = block_size; y = -. block_size};
]
let panel_entities size =
let position = {x = size.x +. (panel_width /. 2.); y = 0.} in
let radius = 20. in
let l = [
Ball{position; speed = vec0; links = []; previous_links = []};
Bubble{position; radius};
Magnet{position; radius; strength = 1.};
Rope{position; radius; length = radius};
Elastic{position; radius; length = radius; strength = 1.};
Goal{position};
Star{position};
Block{color = 0xFF69B4; vertices = panel_block_vertices position};
Fan{position; size = {x = 30.; y = 20.}; angle = 0.; strength = 1.};
Spike{position; angle = 0.5 *. pi};
] in
let n = List.length l in
let h = round_float (size.y /. (float_of_int (n+1))) in
List.mapi (fun i e ->
update_position e {position with y = (float_of_int (i+1)) *. h}
) l
let draw_panel size =
let (w, h) = ints_of_vec size in
Graphics.set_color panel_color;
Graphics.fill_rect w 0 (w + int_of_float panel_width) h;
let l = panel_entities size in
List.iter Frontend.draw_entity l
let radius_handle_position e =
match e with
| Bubble{position; radius} | Magnet{position; radius} | Rope{position; radius}
| Elastic{position; radius} ->
position +: {x = radius; y = 0.} (* TODO: draw handle in a corner *)
| _ -> raise Not_found
let size_handle_position e =
match e with
| Fan{position; size; angle} ->
position +: size.x *: (vec_of_angle angle) +: 0.5 *. size.y *: (vec_of_angle (angle -. 0.5 *. pi))
| _ -> raise Not_found
let length_handle_position e =
match e with
| Rope{position; length} | Elastic{position; length} ->
position +: {x = length; y = 0.} (* TODO: draw handle in a corner *)
| _ -> raise Not_found
let angle_handle_position e =
match e with
| Fan{position; size; angle} ->
let r = 0.5 *. size.y in
let angle = angle -. 0.5 *. pi in
position +: r *: vec_of_angle angle
| Spike{position; angle} ->
position +: spike_edge_size *: vec_of_angle angle
| _ -> raise Not_found
let strength_handle_position e =
match e with
| Magnet{position; strength} | Elastic{position; strength} ->
position +: {x = strength *. strength_per_division; y = 0.}
| Fan{position; angle; strength} ->
position +: strength *. strength_per_division *: vec_of_angle angle
| _ -> raise Not_found
let vertex_handle_position v e =
match e with
| Block(_) -> v
| _ -> raise Not_found
let handle_position prop e =
match prop with
| Radius -> radius_handle_position e
| Size -> size_handle_position e
| Length -> length_handle_position e
| Angle -> angle_handle_position e
| Strength -> strength_handle_position e
| Vertex(v) -> vertex_handle_position v e
| _ -> raise Not_found
let draw_handle prop e =
let (x, y) = ints_of_vec (handle_position prop e) in
let hs = int_of_float handle_size in
Graphics.set_color handle_color;
match prop with
| Angle ->
Graphics.fill_circle x y (hs/2)
| Strength ->
let l = Array.of_list [
(x, y + hs/2);
(x - hs/2, y);
(x, y - hs/2);
(x + hs/2, y);
] in
Graphics.fill_poly l
| _ ->
Graphics.fill_rect (x - hs/2) (y - hs/2) hs hs
let handles_of_entity e =
match e with
| Ball(_) -> []
| Bubble(_) -> [Radius]
| Magnet(_) -> [Radius; Strength]
| Rope(_) -> [Radius; Length]
| Elastic(_) -> [Radius; Length; Strength]
| Block{vertices} ->
List.fold_left (fun props v ->
(Vertex v)::props
) [] vertices
| Spike(_) -> [Angle]
| Fan(_) -> [Size; Angle; Strength]
| Goal(_) | Star(_) -> []
let draw_handles e =
List.iter (fun prop -> draw_handle prop e) (handles_of_entity e)
let draw_entity e =
Frontend.draw_entity e;
draw_handles e
let step ed =
draw_grid ed.size;
draw_panel ed.size;
List.iter draw_entity ed.state;
ed
let intersect_entity pt entity =
match entity with
| Ball{position} ->
Collide.circle_point position Mlgrope.ball_radius pt
| Goal{position} ->
let (a, b) = ends_of_box position goal_size in
Collide.box_point a b pt
| Star{position} ->
let (a, b) = ends_of_box position star_size in
Collide.box_point a b pt
| Bubble{position; radius} | Rope{position; radius} | Elastic{position; radius}
| Magnet{position; radius} ->
Collide.circle_point position radius pt
| Block{vertices} -> Collide.polygon_point vertices pt
| Fan{position; size; angle} ->
let pt = pt -: position in
let pt = rotate angle pt in
let a = vec0 -: {x = 0.; y = size.y /. 2.} in
let b = a +: size in
Collide.box_point a b pt
| Spike{position} ->
Collide.circle_point position (spike_edge_size /. 2.) pt
let intersect_handles pt e =
List.find (fun prop ->
Collide.circle_point (handle_position prop e) (handle_size /. 2.) pt
) (handles_of_entity e)
let rec intersect_entities pt state =
match state with
| e::state -> (
try
let prop = intersect_handles pt e in
Some(e, prop)
with Not_found ->
if intersect_entity pt e then
Some(e, Position (pt -: position_of_entity e))
else
intersect_entities pt state
)
| [] -> None
let swap_entity entity updated =
fun e -> if e == entity then updated else e
let update_radius entity position =
let radius = distance (position_of_entity entity) position in
match entity with
| Bubble(b) -> Bubble{b with radius}
| Magnet(m) -> Magnet{m with radius}
| Rope(r) -> Rope{r with radius}
| Elastic(e) -> Elastic{e with radius}
| _ -> entity
let update_length entity position =
let length = distance (position_of_entity entity) position in
match entity with
| Rope(r) -> Rope{r with length}
| Elastic(e) -> Elastic{e with length}
| _ -> entity
let update_size entity position =
(* let delta = position -: (position_of_entity entity) in
let size = {x = abs_float delta.x; y = abs_float (2. *. delta.y)} in *)
match entity with
| Fan(f) ->
let delta = position -: f.position in
let delta = rotate f.angle delta in
let size = {x = abs_float delta.x; y = abs_float (2. *. delta.y)} in
Fan{f with size}
| _ -> entity
let update_angle entity position =
let delta = position -: (position_of_entity entity) in
let angle = angle_of_vec delta in
match entity with
| Fan(f) -> Fan{f with angle = normalize_angle (angle +. 0.5 *. pi)}
| Spike(s) ->
let angle = 0.5 *. pi *. (round_float (angle /. (0.5 *. pi))) in
Spike{s with angle}
| _ -> entity
let update_strength entity position =
let ep = position_of_entity entity in
let d = distance ep position in
let strength = copysign (d /. strength_per_division) (position.x -. ep.x) in
match entity with
| Magnet(m) -> Magnet{m with strength}
| Elastic(e) -> Elastic{e with strength}
| Fan(f) -> Fan{f with strength}
| _ -> entity
let update_vertex entity vertex position =
match entity with
| Block(b) ->
let vertices = List.map (fun v ->
if v == vertex then position else v
) b.vertices in
Block{b with vertices}
| _ -> entity
let update ed entity prop position =
let ed = {ed with selected_property = prop} in
let (updated, selected_property) = match prop with
| Position(delta) -> (update_position entity (position -: stick_to_grid delta), prop)
| Radius -> (update_radius entity position, prop)
| Length -> (update_length entity position, prop)
| Size -> (update_size entity position, prop)
| Angle -> (update_angle entity position, prop)
| Strength -> (update_strength entity position, prop)
| Vertex(v) -> (update_vertex entity v position, Vertex(position))
in
let state = List.map (swap_entity entity updated) ed.state in
{ed with state; selected = Some(updated); selected_property}
let remove ed entity =
let state = List.filter (fun e -> e != entity) ed.state in
{ed with state; selected = None}
let handle_event path ed s s' =
let pos = Frontend.mouse_of_status s' in
let outside = pos.x > ed.size.x in
let update ed e prop pos =
let pos = if outside then pos else stick_to_grid pos in
update ed e prop pos
in
match (s, s') with
| ({button = false}, {button = true}) -> (
match intersect_entities pos ed.state with
| Some(e, prop) -> update ed e prop pos
| None -> (
let l = panel_entities ed.size in
try
let e = List.find (intersect_entity pos) l in
let ed = {ed with state = e::ed.state} in
update ed e (Position (pos -: position_of_entity e)) pos
with Not_found -> ed
)
)
| ({button = true}, {button}) -> (
if not button && outside then
match ed.selected with
| Some(e) -> remove ed e
| None -> ed
else
let ed = match ed.selected with
| Some(e) -> update ed e ed.selected_property pos
| None -> ed
in
if button then ed else {ed with selected = None}
)
| (_, {keypressed = true; key = 'q'}) ->
raise Exit
| (_, {keypressed = true; key = 'w'}) ->
let ch = open_out path in
Level.output ch ed.state;
close_out ch;
Printf.printf "Saved %s\n%!" path;
ed
| (_, {keypressed = true; key = 'p'}) ->
raise (Play ed.state)
| _ -> ed
let run size path =
let state =
if Sys.file_exists path then
let ch = open_in path in
let state = Level.input ch in
close_in ch;
Printf.printf "Loaded %s\n%!" path;
state
else
[
Ball{
position = 0.5 *: size;
speed = vec0;
links = [];
previous_links = [];
};
]
in
let rec run state =
let ed = {
size;
state;
selected = None;
selected_property = Position vec0;
}
in
Printf.printf "Press w to save, p to play, q to quit\n%!";
try
Frontend.run step (handle_event path) (size +: {x = panel_width; y = 0.}) ed
with Play(state) -> (
try
Player.run size state
with _ -> run state
)
in
run state