Skip to content

Commit

Permalink
Commenting, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mahsu committed Dec 6, 2015
1 parent 49af92c commit 446feef
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 61 deletions.
7 changes: 6 additions & 1 deletion actors.mli
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
type dir_1d = | Left | Right
type dir_1d = | Left | Right
type dir_2d = | North | South | East | West

(* Generic xy record for easy position access *)
type xy = {
mutable x: float;
mutable y: float;
}

(* Controls correspond to keyboard input *)
type controls =
| CLeft
| CRight
| CUp
| CDown

(* Player ability type *)
type pl_typ =
| BigM
| SmallM
Expand All @@ -38,12 +41,14 @@ type block_typ =
| Panel
| Ground

(* Player action type *)
type player_typ =
| Standing
| Jumping
| Running
| Crouching

(* Particle Type *)
type part_typ =
| GoombaSquish
| BrickChunkL
Expand Down
49 changes: 21 additions & 28 deletions director.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,6 @@ let collid_objs = ref [] (* List of next iteration collidable objects *)
let particles = ref [] (* List of next iteration particles *)
let last_time = ref 0. (* Used for calculating fps *)

(*game_over displays a black screen when you finish a game.*)
let game_over state =
state.ctx##rect (0.,0.,512.,512.);
state.ctx##fillStyle <- (Js.string "black");
state.ctx##fill ();
state.ctx##fillStyle <- (Js.string "white");
state.ctx##font <- (Js.string "20px 'Press Start 2P'");
state.ctx##fillText (Js.string ("You win!"), 180., 128.);
failwith "Game over."

(*gave_loss displays a black screen stating a loss to finish that level play.*)
let game_loss state =
state.ctx##rect (0.,0.,512.,512.);
state.ctx##fillStyle <- (Js.string "black");
state.ctx##fill ();
state.ctx##fillStyle <- (Js.string "white");
state.ctx##font <- (Js.string "20px 'Press Start 2P'");
state.ctx##fillText (Js.string ("GAME OVER. You lose!"), 60., 128.);
failwith "Game over."

(* Calculates fps as the difference between [t0] and [t1] *)
let calc_fps t0 t1 =
Expand Down Expand Up @@ -144,7 +125,9 @@ let col_enemy_enemy t1 s1 o1 t2 s2 o2 dir =
end
end

let obj_at_pos dir (pos: xy) (collids: Object.collidable list) : Object.collidable list =
(* Gets the object at a given position *)
let obj_at_pos dir (pos: xy) (collids: Object.collidable list)
: Object.collidable list =
match dir with
| Left -> List.filter (fun (col: Object.collidable) ->
(get_obj col).pos.y = pos.y && (get_obj col).pos.x = pos.x -. 16.)
Expand All @@ -153,12 +136,14 @@ let obj_at_pos dir (pos: xy) (collids: Object.collidable list) : Object.collidab
(get_obj col).pos.y = pos.y && (get_obj col).pos.x = pos.x +. 16.)
collids

(* Returns whether the object at a given position is a block *)
let is_block dir pos collids =
match obj_at_pos dir pos collids with
| [] -> false
| [Block (_,_,_)] -> true
| _ -> false

(* Returns whether the given object is a red koopa *)
let is_rkoopa collid =
match collid with
| Enemy(RKoopa,_,_) -> true
Expand Down Expand Up @@ -229,14 +214,15 @@ let process_collision (dir : Actors.dir_2d) (c1 : Object.collidable)
let spawned_item = spawn_above o1.dir o2 typ context in
collide_block dir o1;
(Some spawned_item, Some updated_block)
| Brick -> if t1 = BigM then (collide_block dir o1; dec_health o2; (None, None))
| Brick -> if t1 = BigM then begin
collide_block dir o1; dec_health o2; (None, None) end
else (collide_block dir o1; (None,None))
| Panel -> game_over state
| Panel -> Draw.game_win state.ctx; (None,None)
| _ -> collide_block dir o1; (None,None)
end
| (Player(_,s1,o1), Block(t,s2,o2), _) ->
begin match t with
| Panel -> game_over state
| Panel -> Draw.game_win state.ctx; (None,None)
| _ ->
begin match dir with
| South -> state.multiplier <- 1 ; collide_block dir o1; (None, None)
Expand All @@ -245,6 +231,7 @@ let process_collision (dir : Actors.dir_2d) (c1 : Object.collidable)
end
| (_, _, _) -> (None,None)

(* Run the broad phase object filtering *)
let broad_phase collid all_collids state =
let obj = get_obj collid in
List.filter (fun c ->
Expand Down Expand Up @@ -332,6 +319,8 @@ let update_collidable state (collid:Object.collidable) all_collids =
evolved
end else []

(* Converts a keypress to a list of control keys, allowing more than one key
* to be processed each frame. *)
let translate_keys () =
let k = pressed_keys in
let ctrls = [(k.left,CLeft);(k.right,CRight);(k.up,CUp);(k.down,CDown)] in
Expand Down Expand Up @@ -363,9 +352,10 @@ let run_update_collid state collid all_collids =
particles := !particles @ new_parts;
collid

(* Primary update function to update and persist a particle *)
let run_update_particle state part =
Particle.process part;
let x = part.pos.x -. state.vpt.pos.x and y = part.pos.y -. state.vpt.pos.y in
let x=part.pos.x -. state.vpt.pos.x and y=part.pos.y -. state.vpt.pos.y in
Draw.render part.params.sprite (x,y);
if not part.kill then particles := part :: !particles

Expand All @@ -389,7 +379,7 @@ let update_loop canvas (player,objs) map_dim =
} in
state.ctx##scale(scale,scale);
let rec update_helper time state player objs parts =
if state.game_over = true then game_over state else begin
if state.game_over = true then Draw.game_win state.ctx else begin
collid_objs := [];
particles := [];

Expand All @@ -405,9 +395,10 @@ let update_loop canvas (player,objs) map_dim =

let player = run_update_collid state player objs in

if (get_obj player).kill = true then game_loss state else begin
let state =
{state with vpt = Viewport.update state.vpt (get_obj player).pos} in
if (get_obj player).kill = true
then Draw.game_loss state.ctx else begin
let state = {
state with vpt = Viewport.update state.vpt (get_obj player).pos} in
List.iter (fun obj -> ignore (run_update_collid state obj objs)) objs;
List.iter (fun part -> run_update_particle state part) parts;
Draw.fps canvas fps;
Expand All @@ -419,6 +410,7 @@ let update_loop canvas (player,objs) map_dim =
end
in update_helper 0. state player objs []

(* Keydown event handler translates a key press *)
let keydown evt =
let () = match evt##keyCode with
| 38 | 32 | 87 -> pressed_keys.up <- true
Expand All @@ -429,6 +421,7 @@ let keydown evt =
| _ -> ()
in Js._true

(* Keyup event handler translates a key release *)
let keyup evt =
let () = match evt##keyCode with
| 38 | 32 | 87 -> pressed_keys.up <- false
Expand Down
2 changes: 2 additions & 0 deletions director.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ val update_loop : Dom_html.canvasElement Js.t
-> float*float
-> unit

(* Keydown event handler function *)
val keydown : #Dom_html.keyboardEvent Js.t -> bool Js.t

(* Keyup event handler function *)
val keyup : #Dom_html.keyboardEvent Js.t -> bool Js.t
20 changes: 20 additions & 0 deletions draw.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ let fps canvas fps_val =
let context = canvas##getContext (Dom_html._2d_) in
ignore context##fillText (Js.string fps_str, 10.,18.)

(*game_win displays a black screen when you finish a game.*)
let game_win ctx =
ctx##rect (0.,0.,512.,512.);
ctx##fillStyle <- (Js.string "black");
ctx##fill ();
ctx##fillStyle <- (Js.string "white");
ctx##font <- (Js.string "20px 'Press Start 2P'");
ctx##fillText (Js.string ("You win!"), 180., 128.);
failwith "Game over."

(*gave_loss displays a black screen stating a loss to finish that level play.*)
let game_loss ctx =
ctx##rect (0.,0.,512.,512.);
ctx##fillStyle <- (Js.string "black");
ctx##fill ();
ctx##fillStyle <- (Js.string "white");
ctx##font <- (Js.string "20px 'Press Start 2P'");
ctx##fillText (Js.string ("GAME OVER. You lose!"), 60., 128.);
failwith "Game over."

let draw_background_color canvas = failwith "todo"
let debug f = Printf.ksprintf (fun s -> Firebug.console##log (jstr s)) f
let alert f = Printf.ksprintf (fun s -> Dom_html.window##alert(Js.string s); failwith "poo") f
Expand Down
10 changes: 10 additions & 0 deletions draw.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ val render : Sprite.sprite -> float * float -> unit
(* Clears the canvas *)
val clear_canvas : Dom_html.canvasElement Js.t -> unit

(* Draw the given sprite as a background *)
val draw_bgd : Sprite.sprite -> float -> unit

(* Draws the axis aligned bounding box of the sprite at the position *)
val render_bbox : Sprite.sprite -> float * float -> unit

(* Draws the fps on the canvas *)
val fps : Dom_html.canvasElement Js.t -> float -> unit

(* Draw the heads up display *)
val hud : Dom_html.canvasElement Js.t -> int -> int -> unit

(* Draw the game win screen *)
val game_win : Dom_html.canvasRenderingContext2D Js.t -> unit

(* Draw the game loss screen *)
val game_loss : Dom_html.canvasRenderingContext2D Js.t -> unit
9 changes: 8 additions & 1 deletion particle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ type particle = {
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
Expand All @@ -45,7 +49,8 @@ let make_type typ ctx =

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
let pos = pair_to_xy pos and vel = pair_to_xy vel
and acc = pair_to_xy acc in
{
params;
part_type;
Expand All @@ -69,10 +74,12 @@ let make_score score pos ctx =
| _ -> 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)
Expand Down
22 changes: 15 additions & 7 deletions particle.mli
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
open Actors
open Sprite

(* Template params associated with a particle *)
type part_params = {
sprite: Sprite.sprite;
rot: float;
lifetime: int;
sprite: Sprite.sprite; (* Backing sprite *)
rot: float; (* Rotation *)
lifetime: int; (* Life span *)
}

type particle = {
Expand All @@ -13,11 +14,18 @@ type particle = {
pos: Actors.xy;
vel: Actors.xy;
acc: Actors.xy;
mutable kill: bool;
mutable life: int;
mutable kill: bool; (* Kill the particle in the next frame *)
mutable life: int; (* Remaining lifespan of particle *)
}

val make : ?vel:float*float -> ?acc:float*float -> Actors.part_typ -> float*float -> Dom_html.canvasRenderingContext2D Js.t -> particle
(* Makes a new particle of the given particle type with at a position. *)
val make : ?vel:float*float -> ?acc:float*float -> Actors.part_typ
-> float*float -> Dom_html.canvasRenderingContext2D Js.t -> particle

val make_score : int -> float*float -> Dom_html.canvasRenderingContext2D Js.t -> particle
(* Make a score particle. The first int indicates the score to spawn *)
val make_score : int -> float*float -> Dom_html.canvasRenderingContext2D Js.t
-> particle

(* Process a particle, updating its velocity and position. Also marks it as
* killable if it exceeds its lifespan *)
val process : particle -> unit
23 changes: 9 additions & 14 deletions sprite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ open Actors

type xy = float * float

type animation_typ = | Reflect | Frame

type sprite_params =
{
Expand All @@ -13,7 +12,6 @@ type sprite_params =
src_offset: xy;
bbox_offset: xy;
bbox_size: xy;
anim: animation_typ;
loop: bool;
}

Expand All @@ -27,8 +25,8 @@ type sprite =
}

(*setup_sprite is used to initialize a sprite.*)
let setup_sprite ?anim:(anim=Frame) ?loop:(loop=true)
?bb_off:(bbox_offset=(0.,0.)) ?bb_sz:(bbox_size=(0.,0.))
let setup_sprite ?loop:(loop=true) ?bb_off:(bbox_offset=(0.,0.))
?bb_sz:(bbox_size=(0.,0.))
img_src max_frames max_ticks frame_size src_offset =
let bbox_size = if bbox_size = (0.,0.) then frame_size else bbox_size in
let img_src = "./sprites/" ^ img_src in
Expand All @@ -40,7 +38,6 @@ let setup_sprite ?anim:(anim=Frame) ?loop:(loop=true)
src_offset;
bbox_offset;
bbox_size;
anim;
loop;
}

Expand Down Expand Up @@ -130,6 +127,7 @@ let make_type typ (dir : Actors.dir_1d) =
| SItem t -> make_item t
| SBlock t -> make_block t

(* Makes a sprite from provided [params]. *)
let make_from_params params context =
let img = (Dom_html.createImg Dom_html.document) in
img##src <- (Js.string params.img_src) ;
Expand All @@ -146,10 +144,12 @@ let make spawn dir context =
let params = make_type spawn dir in
make_from_params params context

(* Make a background *)
let make_bgd context =
let params = setup_sprite "bgd-1.png" 1 0 (512.,256.) (0.,0.) in
make_from_params params context

(* Make a particle from the given particle type *)
let make_particle ptyp context =
let params = make_particle ptyp in
make_from_params params context
Expand All @@ -162,17 +162,12 @@ let transform_enemy enemy_typ spr dir =
spr.params <- params;
spr.img <- img

let reflect_sprite spr = failwith "todo"

(*update_animation is the main method to cycle through sprite animations*)
let update_animation (spr: sprite) =
(* Only advance frame when ticked *)
let curr_ticks = !(spr.ticks) in
if curr_ticks >= spr.params.max_ticks then (
if curr_ticks >= spr.params.max_ticks then begin
spr.ticks := 0;
match spr.params.anim with
| Frame ->
if spr.params.loop then
spr.frame := (!(spr.frame) + 1) mod spr.params.max_frames
| Reflect -> reflect_sprite spr
) else spr.ticks := curr_ticks + 1
if spr.params.loop then
spr.frame := (!(spr.frame) + 1) mod spr.params.max_frames
end else spr.ticks := curr_ticks + 1
Loading

0 comments on commit 446feef

Please sign in to comment.