-
Notifications
You must be signed in to change notification settings - Fork 110
/
particle.ml
91 lines (81 loc) · 2.43 KB
/
particle.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
open Actors
open Sprite
type part_params = {
sprite: Sprite.sprite;
rot: float;
lifetime: int;
}
type particle = {
params: part_params;
part_type: Actors.part_typ;
pos: Actors.xy;
vel: Actors.xy;
acc: Actors.xy;
mutable kill: bool;
mutable life: int;
}
(* Converts an x,y [pair] to an Actors.xy record *)
let pair_to_xy pair = {
x = fst pair;
y = snd pair;
}
(* Function wrapper to assist in generating the template paramss for a
* particle. *)
let make_params sprite rot lifetime =
{
sprite;
rot;
lifetime;
}
(* Generate the template for a specific particle type *)
let make_type typ ctx =
match typ with
| GoombaSquish as t -> make_params (Sprite.make_particle t ctx) 0. 30
| BrickChunkL as t -> make_params (Sprite.make_particle t ctx) 0. 300
| BrickChunkR as t -> make_params (Sprite.make_particle t ctx) 0. 300
| Score100 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score200 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score400 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score800 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score1000 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score2000 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score4000 as t -> make_params (Sprite.make_particle t ctx) 0. 30
| Score8000 as t -> make_params (Sprite.make_particle t ctx) 0. 30
let make ?vel:(vel=(0.,0.)) ?acc:(acc=(0.,0.)) part_type pos ctx =
let params = make_type part_type ctx in
let pos = pair_to_xy pos and vel = pair_to_xy vel
and acc = pair_to_xy acc in
{
params;
part_type;
pos;
vel;
acc;
kill = false;
life = params.lifetime;
}
let make_score score pos ctx =
let t = match score with
| 100 -> Score100
| 200 -> Score200
| 400 -> Score400
| 800 -> Score800
| 1000 -> Score1000
| 2000 -> Score2000
| 4000 -> Score4000
| 8000 -> Score8000
| _ -> Score100
in make ~vel:(0.5,-0.7) t pos ctx
(* Mutably update the velocity of a particle *)
let update_vel part =
part.vel.x <- (part.vel.x +. part.acc.x);
part.vel.y <- (part.vel.y +. part.acc.y)
(* Mutably update the position of a particle *)
let update_pos part =
part.pos.x <- (part.vel.x +. part.pos.x);
part.pos.y <- (part.vel.y +. part.pos.y)
let process part =
part.life <- part.life - 1;
if part.life = 0 then (part.kill <- true);
update_vel part;
update_pos part