-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.ml
139 lines (118 loc) · 2.92 KB
/
sprite.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
(**[mario_type] is the different types of mario*)
type mario_type =
| SmolMario
| SuperMario
| FireMario
(**[pwr_up] is the different types of powerup*)
type pwr_up =
| Mushroom
| FireFlower
(**[sprite] is the different types of sprites there are*)
type sprite =
| Mario of mario_type
| Goomba
| Object
| Koopa
| KoopaShell
| Fireball
| Coin
| BulletBill
| Powerup of pwr_up
type point = {
x : float;
y : float;
}
(**[hitbox] is a hitbox for a sprite*)
type hitbox = {
lower_left : point;
width : int;
height : int;
}
(** AF: this represents a sprite, such that most fields are protected*)
type t = {
mutable kind : sprite;
mutable pos : point;
mutable vel : point;
size : int * int;
(*width * height*)
mutable alive : bool;
mutable coins : int;
mutable inv_time : int;
mutable fire_time : int;
}
let position boi = boi.pos
let velocity boi = boi.vel
let size boi = boi.size
let coins boi = boi.coins
let is_alive boi = boi.alive
let can_fire boi =
match boi.kind with
| Mario FireMario -> boi.fire_time = 0
| _ -> false
let kill boi =
match boi.kind with
| Mario mt ->
if boi.inv_time > 0 then ()
else (
boi.inv_time <- 60;
match mt with
| SmolMario -> boi.alive <- false
| SuperMario -> boi.kind <- Mario SmolMario
| FireMario -> boi.kind <- Mario SuperMario )
| Koopa ->
boi.kind <- KoopaShell;
boi.vel <- { x = 0.; y = 0. }
| _ -> boi.alive <- false
let got_coin boi c =
match boi.kind with
| Mario _ ->
boi.coins <- boi.coins + 1;
kill c
(*these bois can't get coins*)
| Goomba | Object | Koopa | KoopaShell | Fireball | Coin | BulletBill
| Powerup _ ->
()
let power_up boi power =
match (boi.kind, power.kind) with
| Mario mt, Powerup Mushroom ->
if mt = FireMario then (
got_coin boi
{
kind = Coin;
pos = { x = 0.; y = 0. };
vel = { x = 0.; y = 0. };
size = (1, 1);
alive = true;
coins = 0;
inv_time = 0;
fire_time = 0;
};
kill power )
else (
boi.kind <- Mario SuperMario;
kill power )
| Mario _, Powerup FireFlower ->
boi.kind <- Mario FireMario;
kill power
| _, _ -> ()
let collision boi =
{ lower_left = boi.pos; width = fst boi.size; height = snd boi.size }
let spritetype boi = boi.kind
let update_position boi pos = boi.pos <- pos
let update_velocity boi vel = boi.vel <- vel
let fire boi =
match boi.kind with Mario FireMario -> boi.fire_time <- 60 | _ -> ()
let tick_timers boi =
if boi.inv_time > 0 then boi.inv_time <- boi.inv_time - 1 else ();
if boi.fire_time > 0 then boi.fire_time <- boi.fire_time - 1 else ()
let make nkind npos w h =
{
kind = nkind;
pos = npos;
vel = { x = 0.; y = 0. };
size = (w, h);
alive = true;
coins = 0;
inv_time = 0;
fire_time = 0;
}