%% DEFINES
% constants
width     = 256;
height    = 128;
start     = [32, 64];
speed     = 10;
play_time = 30;

% playable area
area                     = false(height, width);
area(start(2), start(1)) = true;

% walls
walls                                     = true(height, width);
walls(64:64+32, 16:width-16)              = false; % long strip
walls(64-8:64+32+8, 16:16+48)             = false; % starting area
walls(64-8:64+32+8, width-16-48:width-16) = false; % ending area
walls(64-16:64, width/2-8:width/2+8)      = false; % divit

% obstacle
obstacle                                  = false(height, width);
obstacle(64:64+32, 16+48+1:width-16-48-1) = true;

% movement kernel
[x,y]    = meshgrid(1:speed*2, 1:speed*2);
movement = (x - speed).^2 + (y - speed).^2 <= speed^2;


%% PLAY
% loop until finished
for i=1:play_time
    
    % move
    area = imdilate(area, movement, 'same');
    
    % mask walls
    area = area & ~walls;
    
    % mask obstacle
    obs = false(height, width);
    if mod(i, 7) == 0
        area = area & ~obstacle;
        obs = obstacle;
    end
    
    % setup output image
    output         = zeros(height, width, 'uint8');
    output(~walls) = 170;
    output(area)   = 255;
    output(obs)    = 30;
    
    % write to gif
    if i == 1
        imwrite(output,'Bound.gif','gif','Loopcount',inf,'DelayTime',0.1);
    else
        imwrite(output,'Bound.gif','gif','Writemode','append','DelayTime',0.1);
    end
    
    % finished
    if sum(sum(area(64-8:64+32+8, width-16-48:width-16))) == ...
            numel(area(64-8:64+32+8, width-16-48:width-16))
        break
    end
    
end

