function movie = animateFlood(terrain, waterLevels, animationDelay, filename)
    % Creates an animation of varying water levels.
    %
    % terrain is a fixed two-dimensional array portraying ground height
    % waterLevel is a three-dimensional array of water levels per time step
    %     (such as the result of simluateFlood function)
    % animationDelay is time between frames
    % filename, if given, will be used for writing result to AVI file
    for k = 1:size(waterLevels,3)
        plotTerrain(terrain, waterLevels(:,:,k));
        pause(animationDelay);
        movie(k) = getframe;
    end
   
    if nargin == 4
        movie2avi(movie, filename, 'fps', 10); % 10 frames per second
    end
