%script proj2
%2d projectile script
close all;
disp(' ')
disp('Click the Mouse on the Picture to Continue')
disp('Press a Keyboard Key to Fire!')
vel = input('Velocity of Plane? [0 to 1]: ');

n = ceil(10/vel + 1);
time = linspace(0,1/vel,n)';

x_1 = (vel * time);
y_1 = ones(n,1);

%M = avifile('2dhit2');

for m=1:n

   option = waitforbuttonpress;

   % no go
   if option == 0
      hold on
      axis([-.1 1.1 -.1 1.1])
      plot(x_1(m),y_1(m),'.');
%      w = getframe;
%      M = addframe(M,w);
      hold off
      pause(.1)

   % go
   else
 
      tinit = (m-1)/10;
      [theta,collision] = partial(tinit,vel);

      if (theta < atan(2) | theta > pi - atan(2))
%         M = close(M); 
	     disp('Trajectory not Possible')
         disp(' ')
         close all
         break;
         
      end
      tdif = collision-tinit;
      tohit = round(10*tdif);
      launch_time = (linspace(0,tdif,tohit+1))';
      x_2 = 1/2 + 2*(launch_time)*cos(theta);
      y_2 = 2*(launch_time)*sin(theta);


      for count=1:tohit
      	hold on
        axis([-.1 1.1 -.1 1.1])
        plot(x_1(m+count-1),y_1(m+count-1),'.',x_2(count),y_2(count),'x')
%        w = getframe;
%        M = addframe(M,w);

        pause(.1)
      end
      plot(x_2(count+1),y_2(count+1),'r^')
%      w = getframe;
%      M = addframe(M,w);
%      M = close(M);
      disp('BOOM!!!')
      disp(' ')
      hold off
      break;
   end

end
