


## brick.pkg
# Compiled by:
# src/lib/x-kit/tut/badbricks-game/badbricks-game-app.libstipulate
include threadkit; # threadkit is from src/lib/src/lib/thread-kit/src/core-thread-kit/threadkit.pkg #
package xg = xgeometry; # xgeometry is from src/lib/std/2d/xgeometry.pkg# package wg = widget; # widget is from src/lib/x-kit/widget/basic/widget.pkg package bu = brick_util; # brick_util is from src/lib/x-kit/tut/badbricks-game/brick-util.pkg package bv = brickview; # brickview is from src/lib/x-kit/tut/badbricks-game/brickview.pkgherein
package brick
: Brick # brick_util is from src/lib/x-kit/tut/badbricks-game/brick-util.pkg {
Brick
=
BRICK
{ brickview: bv::Brickview,
position: bu::Position,
#
good: Ref( Bool ),
shown: Ref( Bool ),
#
state: Ref( bu::State )
};
fun set_good (BRICK { good, ... } ) = good := TRUE;
fun set_state (BRICK { state, ... }, s) = state := s;
fun set_shown (BRICK { shown, ... } ) = shown := TRUE;
fun view_of (BRICK { brickview, ... } ) = brickview;
fun state_of (BRICK { state, ... } ) = *state;
fun is_shown (BRICK { shown, ... } ) = *shown;
fun is_good (BRICK { good, ... } ) = *good;
fun position_of (BRICK { position, ... } ) = position;
fun as_widget (BRICK { brickview, ... } ) = bv::as_widget brickview;
fun is_bad b
=
case (state_of b)
#
bu::NO_BRICK_STATE => TRUE;
bu::BAD_STATE _ => TRUE;
_ => FALSE;
esac;
fun enumerate_neighbors no (brick, range, brick_at)
=
{ p = position_of brick;
no (brick_at (bu::west_of p));
no (brick_at (bu::northwest_of p));
no (brick_at (bu::northeast_of p));
no (brick_at (bu::east_of p));
no (brick_at (bu::southeast_of p));
no (brick_at (bu::southwest_of p));
if (range == bu::LONG)
#
no (brick_at (bu::west_of (bu::west_of p)));
no (brick_at (bu::west_of (bu::northwest_of p)));
no (brick_at (bu::northwest_of (bu::northwest_of p)));
no (brick_at (bu::northwest_of (bu::northeast_of p)));
no (brick_at (bu::northeast_of (bu::northeast_of p)));
no (brick_at (bu::northeast_of (bu::east_of p)));
no (brick_at (bu::east_of (bu::east_of p)));
no (brick_at (bu::east_of (bu::southeast_of p)));
no (brick_at (bu::southeast_of (bu::southeast_of p)));
no (brick_at (bu::southeast_of (bu::southwest_of p)));
no (brick_at (bu::southwest_of (bu::southwest_of p)));
no (brick_at (bu::southwest_of (bu::west_of p)));
fi;
};
fun neighbor_count prior (brick, range, brick_at)
=
{ count = REF 0;
fun inc v
=
count := *count + v;
enumerate_neighbors
(inc o prior)
(brick, range, brick_at);
*count;
};
neighbor_good_count
=
neighbor_count (fn brick = is_good brick ?? 1 :: 0);
neighbor_bad_count
=
neighbor_count (fn brick = is_bad brick ?? 1 :: 0);
neighbor_ok_count
=
neighbor_count
(fn brick = case (state_of brick)
#
bu::OK_STATE => 1;
_ => 0;
esac
);
fun end_show (brick, brick_at)
=
if (not (is_shown brick or is_good brick))
count = neighbor_good_count (brick, bu::SHORT, brick_at);
set_state (brick, bu::BAD_STATE count);
bv::end_view (view_of brick) (int::to_string count);
set_shown brick;
# original decrements bad brick count
fi;
fun show (brick, brick_at)
=
{ count = neighbor_good_count (brick, bu::SHORT, brick_at);
set_state (brick, bu::BAD_STATE count);
set_shown brick;
bv::show_view (view_of brick) (int::to_string count);
};
fun show_and_flood (brick, brick_at)
=
show_af (brick, 0)
where
fun show_af (brick, count)
=
if (is_shown brick)
#
count;
else
p = position_of brick;
count' = if (is_good brick)
count;
else
show (brick, brick_at);
count+1;
fi;
case (state_of brick)
#
bu::BAD_STATE 0
=>
show_af (brick_at (bu::southwest_of p),
show_af (brick_at (bu::southeast_of p),
show_af (brick_at (bu::east_of p),
show_af (brick_at (bu::northeast_of p),
show_af (brick_at (bu::northwest_of p),
show_af (brick_at (bu::west_of p), count'))))));
_ => count';
esac;
fi;
end;
fun highlight_on (BRICK { brickview, ... } ) = bv::highlight_on brickview;
fun highlight_off (BRICK { brickview, ... } ) = bv::highlight_off brickview;
fun toggle_marking brick
=
case (state_of brick)
#
bu::UNKNOWN_STATE
=>
{ bv::mark_view (view_of brick);
set_state (brick, bu::OK_STATE);
};
bu::OK_STATE
=>
{ bv::norm_view (view_of brick);
set_state (brick, bu::UNKNOWN_STATE);
};
_ => ();
esac;
fun set_text (BRICK { brickview, ... }, text)
=
bv::set_text brickview text;
fun make_brick root_window (arg as (point, _, _))
=
BRICK
{
brickview => bv::make_brickview root_window arg,
position => point,
#
good => REF FALSE,
shown => REF FALSE,
state => REF bu::UNKNOWN_STATE
};
fun make_no_brick root_window palette
=
BRICK
{
brickview => bv::make_brickview root_window (xg::point::zero, make_mailslot(), palette),
position => xg::point::zero,
good => REF FALSE,
shown => REF TRUE,
state => REF bu::NO_BRICK_STATE
};
fun reset (BRICK { brickview, state, good, shown, ... } )
=
{ state := bu::UNKNOWN_STATE;
shown := FALSE;
good := FALSE;
bv::norm_view brickview;
};
}; # package brick
end;


