From e15b5de4a402e2ee84a044482f72cb1aec2a9d7c Mon Sep 17 00:00:00 2001 From: Nicolas Glondu Date: Wed, 19 Oct 2011 04:41:40 +0200 Subject: [PATCH] Pacman can now stop facing each direction --- src/base.opa | 25 +++++++++++++++++++------ src/opacman.opa | 2 +- src/pacman.opa | 6 +++--- src/types.opa | 5 ++++- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/base.opa b/src/base.opa index 987fd78..019bc68 100644 --- a/src/base.opa +++ b/src/base.opa @@ -4,11 +4,10 @@ facing_angle(dir:Base.direction) = match dir with - | {up} -> -Math.PI/2. - | {down} -> Math.PI/2. - | {left} -> Math.PI - | {right} -> 0. - | {still} -> 0. + | {up} | {still_up} -> -Math.PI/2. + | {down} | {still_down} -> Math.PI/2. + | {left} | {still_left} -> Math.PI + | {right} | {still_right} -> 0. deltas(dir:Base.direction) = match dir with @@ -16,7 +15,7 @@ | {down} -> (0, 1) | {left} -> (-1, 0) | {right} -> (1, 0) - | {still} -> (0, 0) + | _ -> (0, 0) back(dir:Base.direction):Base.direction = match dir with @@ -26,6 +25,20 @@ | {right} -> {left} | x -> x + is_still(dir:Base.direction) = + match dir with + | {still_up} | {still_down} + | {still_left} | {still_right} -> true + | _ -> false + + get_still(dir:Base.direction) = + match dir with + | {up} -> {still_up} + | {down} -> {still_down} + | {left} -> {still_left} + | {right} -> {still_right} + | x -> x + }} @both make(x, y, dir, max_steps) = { diff --git a/src/opacman.opa b/src/opacman.opa index 128a953..5084d4c 100644 --- a/src/opacman.opa +++ b/src/opacman.opa @@ -100,7 +100,7 @@ default_game = { | (_, {some=100}) -> {p with next_dir={right}} // space (pause) - | (_, {some=32}) -> {p with next_dir={still}} + | (_, {some=32}) -> {p with next_dir=Base.Dir.get_still(p.base.dir)} | _ -> p game.set({g with pacman=p}) diff --git a/src/pacman.opa b/src/pacman.opa index 281ffca..ae932fb 100644 --- a/src/pacman.opa +++ b/src/pacman.opa @@ -38,7 +38,7 @@ ignore_incr = p.base.cur_step < 0 cur_step = p.base.cur_step + 1 cur_step = if cur_step >= p.base.max_steps then 0 - else if p.base.dir == {still} then 0 + else if Base.Dir.is_still(p.base.dir) then 0 else cur_step test_wall(on_ok, on_err, x, y) = if Wall.at(x,y) then on_err @@ -49,10 +49,10 @@ do print_infos(g) (dx, dy) = Base.Dir.deltas(p.base.dir) (ddx, ddy) = Base.Dir.deltas(p.next_dir) - dir = test_wall(p.next_dir, {still}, + dir = test_wall(p.next_dir, Base.Dir.get_still(p.next_dir), p.base.pos.x+dx+ddx, p.base.pos.y+dy+ddy) (dx, dy, dir) = - test_wall((dx,dy,dir), (0,0,{still}), + test_wall((dx,dy,dir), (0,0,Base.Dir.get_still(dir)), p.base.pos.x+dx, p.base.pos.y+dy) if ignore_incr then (dir, 0, 0) else (dir, dx, dy) diff --git a/src/types.opa b/src/types.opa index 29b59ba..02f6247 100644 --- a/src/types.opa +++ b/src/types.opa @@ -1,4 +1,7 @@ -type Base.direction = {still} / {up} / {down} / {left} / {right} +type Base.direction = + {up} / {down} / {left} / {right} + / {still_up} / {still_down} + / {still_left} / {still_right} type Base.pos = { x : int