From daedd5d22a0f5bb31c98f17bbe76b67c0ad9f58b Mon Sep 17 00:00:00 2001 From: Nicolas Glondu Date: Sun, 6 Nov 2011 04:47:13 +0100 Subject: [PATCH] pacman can now eat ghosts in bonus mode --- src/default.opa | 14 ++++++------- src/food.opa | 10 +++++++++- src/ghost.opa | 18 +++++++++-------- src/info.opa | 2 +- src/opacman.opa | 52 +++++++++++++++++++++++++++++++++++-------------- src/pacman.opa | 8 ++++---- src/types.opa | 5 +++-- 7 files changed, 71 insertions(+), 38 deletions(-) diff --git a/src/default.opa b/src/default.opa index 6326119..3afece6 100644 --- a/src/default.opa +++ b/src/default.opa @@ -94,20 +94,20 @@ Default = {{ mouth_steps = 10 } : Pacman.t - @private make_ghost(ai, prison, dir, color, eye_color) = + @private make_ghost(ai, prison, color, eye_color) = ~{x y} = Set.random_get(ghost_prison) |> Option.get { ~ai ~color ~eye_color - base = Base.make(x, y, dir, 11) + base = Base.make(x, y, {up}, 11) prison = some(prison) eye_step = 0 eye_steps = 32 } : Ghost.t ghosts = [ - make_ghost({dumb}, 60, {up}, Color.orange, Color.crimson), - make_ghost({guard}, 200, {up}, Color.darkred, Color.gold), - make_ghost({dumb}, 400, {up}, Color.purple, Color.silver), - make_ghost({guard}, 600, {up}, Color.green, Color.navy), - ] : list(Ghost.t) + ("g1", make_ghost({dumb}, 60, Color.orange, Color.crimson)), + ("g2", make_ghost({guard}, 200, Color.darkred, Color.gold)), + ("g3", make_ghost({dumb}, 400, Color.purple, Color.silver)), + ("g4", make_ghost({guard}, 600, Color.green, Color.navy)), + ] : list((string,Ghost.t)) }} diff --git a/src/food.opa b/src/food.opa index 7e3ec81..0293c17 100644 --- a/src/food.opa +++ b/src/food.opa @@ -9,7 +9,15 @@ else (food, 0) match f with | {normal} -> (food, food_points+d, cur_steroids) - | {steroids} -> (food, steroid_points+d, some(steroid_len)) + | {steroids} -> + new_steroids = match cur_steroids with + | {none} -> + {cycles = steroid_len + combo = 0} + | {some=s} -> + {cycles = s.cycles+steroid_len + combo = s.combo} + (food, steroid_points+d, some(new_steroids)) draw(g, ctx:Canvas.context) = food = g.food diff --git a/src/ghost.opa b/src/ghost.opa index 8329fc8..c100d4f 100644 --- a/src/ghost.opa +++ b/src/ghost.opa @@ -110,22 +110,24 @@ prison = none} else {ghost with prison = some(t-1)} - move(g) = + move(g:Game.status) = ghosts = List.map( - ghost -> - (match ghost.ai with - | {dumb} -> move_one_dumb(ghost) - | {guard} -> move_one_guard(ghost, g.pacman.base) - ) |> move_prison, + (gid, ghost) -> + ghost = + (match ghost.ai with + | {dumb} -> move_one_dumb(ghost) + | {guard} -> move_one_guard(ghost, g.pacman.base) + ) |> move_prison + (gid, ghost), g.ghosts) {g with ~ghosts} draw(g, ctx:Canvas.context) = ghosts = match g.on_steroids with - | {none} -> g.ghosts + | {none} -> List.map(_.f2, g.ghosts) | {some=_} -> List.map( - g -> {g with color=scared_color eye_color=scared_eye_color}, + g -> {g.f2 with color=scared_color eye_color=scared_eye_color}, g.ghosts) List.iter(draw_one(ctx, _), ghosts) diff --git a/src/info.opa b/src/info.opa index 424155b..d4af370 100644 --- a/src/info.opa +++ b/src/info.opa @@ -33,7 +33,7 @@ do match g.on_steroids with | {none} -> void - | {some=d} -> cft("Bonus: {1+d/fps}s", 30) + | {some=s} -> cft("Bonus: {1+s.cycles/fps}s", 30) do Canvas.restore(ctx) diff --git a/src/opacman.opa b/src/opacman.opa index 8612280..51b6c64 100644 --- a/src/opacman.opa +++ b/src/opacman.opa @@ -54,28 +54,50 @@ default_game = { do Canvas.restore(ctx) void -check_collision(g) = +check_collision(g:Game.status):Game.status = pc = Base.center(g.pacman.base) has_collision = List.fold( - ghost, res -> - if res then res + (gid, ghost), res -> + if Option.is_some(res) then res else gc = Base.center(ghost.base) x = gc.f1 - pc.f1 y = gc.f2 - pc.f2 d = Math.sqrt_i(x*x+y*y) - d < base_size, - g.ghosts, false) - if has_collision && g.lives == 1 then - {g with - state = {game_over} - lives = 0} - else if has_collision then - {default_game with - food = g.food - score = g.score - lives = g.lives-1} - else g + if d < base_size then some(gid) + else res, + g.ghosts, none) + if Option.is_none(has_collision) then g + else + match g.on_steroids with + | {some=s} -> + cid = Option.get(has_collision) + on_steroids = some({s with combo=s.combo+1}) + ghosts = List.map( + (gid, ghost) -> + if gid != cid then (gid, ghost) + else + ~{x y} = + Set.random_get(Default.ghost_prison) + |> Option.get + base = Base.make(x, y, {up}, 11) + prison = some(300) + (gid, {ghost with ~base ~prison}), + g.ghosts) + score = g.score + (match s.combo with + | 0 -> 100 | 1 -> 200 | 3 -> 500 | _ -> 1000) + lives = g.lives + (score/life_points - g.score/life_points) + {g with ~ghosts ~score ~lives ~on_steroids} + | {none} -> + if g.lives == 1 then + {g with + state = {game_over} + lives = 0} + else + {default_game with + food = g.food + score = g.score + lives = g.lives-1} @client clean_frame(ctx:Canvas.context) = Canvas.clear_rect( diff --git a/src/pacman.opa b/src/pacman.opa index 2394735..09796a7 100644 --- a/src/pacman.opa +++ b/src/pacman.opa @@ -44,7 +44,7 @@ draw_clones({~x y=_} -> {~x y=-1}, p, ctx) else draw_one(p, ctx) - move(g) = + move(g:Game.status) = p = g.pacman ignore_incr = p.base.cur_step < 0 cur_step = p.base.cur_step + 1 @@ -78,9 +78,9 @@ lives = g.lives + (score/life_points - g.score/life_points) on_steroids = match steroids with | {none} -> none - | {some=t} -> - if t < 1 then none - else some(t-1) + | {some=s} -> + if s.cycles < 1 then none + else some({s with cycles=s.cycles-1}) mouth = p.mouth_step + p.mouth_incr dmouth = if (mouth == p.mouth_steps-1 || mouth == 0) then -p.mouth_incr diff --git a/src/types.opa b/src/types.opa index b1b2fa4..310e68e 100644 --- a/src/types.opa +++ b/src/types.opa @@ -56,9 +56,10 @@ type Game.state = type Game.status = { state : Game.state pacman : Pacman.t - ghosts : list(Ghost.t) + ghosts : list((string, Ghost.t)) food : map(Base.pos, Food.t) score : int lives : int - on_steroids : option(int) /* Number of cycles on steroids */ + on_steroids : option({cycles:int combo:int}) + /* Number of cycles on steroids and ghost combo */ }