PreviousUpNext

15.4.1364  src/lib/x-kit/tut/badbricks-game/brick.pkg

## brick.pkg

# Compiled by:
#     src/lib/x-kit/tut/badbricks-game/badbricks-game-app.lib

stipulate
    include package   threadkit;                        # threadkit                             is from   src/lib/src/lib/thread-kit/src/core-thread-kit/threadkit.pkg
    #
    package mps =  microthread_preemptive_scheduler;    # microthread_preemptive_scheduler      is from   src/lib/src/lib/thread-kit/src/core-thread-kit/microthread-preemptive-scheduler.pkg

    package g2d=  geometry2d;                           # geometry2d                            is from   src/lib/std/2d/geometry2d.pkg
#    package wg =  widget;                              # widget                                is from   src/lib/x-kit/widget/old/basic/widget.pkg
    package bj =  brick_junk;                           # brick_junk                            is from   src/lib/x-kit/tut/badbricks-game/brick-junk.pkg
    package bv =  brickview;                            # brickview                             is from   src/lib/x-kit/tut/badbricks-game/brickview.pkg
herein

    package  brick
    :        Brick                                      # Brick                                 is from   src/lib/x-kit/tut/badbricks-game/brick.api
    {
        Brick = BRICK
                  { brickview:   bv::Brickview,
                    position:    bj::Position,
                    #
                    good:        Ref( Bool ),
                    shown:       Ref( Bool ),
                    #
                    state:       Ref( bj::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)
                #
                bj::NO_BRICK_STATE =>  TRUE;
                bj::BAD_STATE _    =>  TRUE;
                _                  =>  FALSE;
            esac;

        fun enumerate_neighbors no (brick, range, brick_at)
            =
            {   p =  position_of  brick;
                #
                no (brick_at (bj::west_of      p));
                no (brick_at (bj::northwest_of p));
                no (brick_at (bj::northeast_of p));
                no (brick_at (bj::east_of      p));
                no (brick_at (bj::southeast_of p));
                no (brick_at (bj::southwest_of p));

                if (range == bj::LONG)
                    #
                    no (brick_at (bj::west_of      (bj::west_of      p)));
                    no (brick_at (bj::west_of      (bj::northwest_of p)));
                    no (brick_at (bj::northwest_of (bj::northwest_of p)));
                    no (brick_at (bj::northwest_of (bj::northeast_of p)));
                    no (brick_at (bj::northeast_of (bj::northeast_of p)));
                    no (brick_at (bj::northeast_of (bj::east_of      p)));
                    no (brick_at (bj::east_of      (bj::east_of      p)));
                    no (brick_at (bj::east_of      (bj::southeast_of p)));
                    no (brick_at (bj::southeast_of (bj::southeast_of p)));
                    no (brick_at (bj::southeast_of (bj::southwest_of p)));
                    no (brick_at (bj::southwest_of (bj::southwest_of p)));
                    no (brick_at (bj::southwest_of (bj::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 (\\ brick =  is_good brick ?? 1 :: 0);

        neighbor_bad_count
            =
            neighbor_count (\\ brick =  is_bad brick ?? 1 :: 0);

        neighbor_ok_count
            = 
            neighbor_count
                (\\ brick = case (state_of brick)
                                #       
                                bj::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, bj::SHORT, brick_at);

                set_state (brick, bj::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, bj::SHORT, brick_at);
                #
                set_state (brick, bj::BAD_STATE count);
                set_shown brick;
                bv::show_view (view_of brick) (int::to_string count);
            };


        fun show_and_flood (brick, brick_at)
            =
            show_and_flood' (brick, 0)
            where
                fun show_and_flood' (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)
                            #
                            bj::BAD_STATE  0
                                =>
                                show_and_flood' (brick_at (bj::southwest_of p),
                                show_and_flood' (brick_at (bj::southeast_of p),
                                show_and_flood' (brick_at (bj::east_of      p),
                                show_and_flood' (brick_at (bj::northeast_of p),
                                show_and_flood' (brick_at (bj::northwest_of p),
                                show_and_flood' (brick_at (bj::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)
                #
                bj::UNKNOWN_STATE
                    =>
                    {   bv::mark_view (view_of brick);
                        set_state (brick, bj::OK_STATE);
                    };

                bj::OK_STATE
                    =>
                    {   bv::norm_view (view_of brick);
                        set_state (brick, bj::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 bj::UNKNOWN_STATE
              };


        fun make_no_brick  root_window  palette
            =
            BRICK
              { 
                brickview  =>  bv::make_brickview  root_window  (g2d::point::zero, make_mailslot(), palette),
                position   =>  g2d::point::zero,
                good       =>  REF FALSE,
                shown      =>  REF TRUE,
                state      =>  REF bj::NO_BRICK_STATE
              };


        fun reset (BRICK { brickview, state, good, shown, ... } )
            =
            {   state :=  bj::UNKNOWN_STATE;

                shown :=  FALSE;
                good  :=  FALSE;

                bv::norm_view  brickview;
            };

    };                                                  # package brick

end;


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext