function varargout = pokemon(varargin)
% POKEMON MATLAB code for pokemon.fig
%      POKEMON, by itself, creates a new POKEMON or raises the existing
%      singleton*.
%
%      H = POKEMON returns the handle to a new POKEMON or the handle to
%      the existing singleton*.
%
%      POKEMON('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in POKEMON.M with the given input arguments.
%
%      POKEMON('Property','Value',...) creates a new POKEMON or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pokemon_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pokemon_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help pokemon

% Last Modified by GUIDE v2.5 27-Nov-2016 12:31:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pokemon_OpeningFcn, ...
                   'gui_OutputFcn',  @pokemon_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before pokemon is made visible.
function pokemon_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to pokemon (see VARARGIN)

% Choose default command line output for pokemon
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes pokemon wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%
global img_eevee;
global img_ball;
global h_eevee;
global h_alpha_eevee;
global h_alpha_ball;
global h_axes;
global h_background;
global x_max;
global y_max;
global x_pokemon;
global y_pokemon;
global dx_pokemon;
global dy_pokemon;
global s0;
global theta0;
% define grid dimensions
x_max = 10.0;
y_max = 10.0;
% define pokemon size
dx_pokemon = 0.2*x_max;
dy_pokemon = 0.2*y_max;
% define initial pokemon position
x_pokemon = x_max-dx_pokemon;
y_pokemon = y_max-dy_pokemon;
% read in background image
img_background = imread('background.jpg');
% read in pokemon image
% NOTE: obtain handles to map and alpha chanel 
%       (for setting transparent background later)
% read in pokemon image
[img_eevee, h_map_eevee, h_alpha_eevee] = imread('Eevee.png');
% read in ball image
[img_ball, h_map_ball, h_alpha_ball] = imread('Pokeball.png');
% find handle of axes object
h_axes = findobj('Tag','axes1');
% set axes suitable for game
axes(h_axes);
axis([0 x_max 0 y_max]);
hold on;
% set limts of image to exactly fit axes
h_background = imshow(img_background,'Parent',h_axes,'Xdata',[0 x_max],'Ydata',[0 y_max]);
% draw pokemon
h_eevee = imshow(img_eevee,'Parent',h_axes,'Xdata',[x_pokemon x_pokemon+dx_pokemon],'Ydata',[y_pokemon y_pokemon+dy_pokemon]);
set(h_eevee, 'AlphaData', h_alpha_eevee);
% read in default model parameters and set labels
h = findobj('Tag','slider1');
s0 = get(h,'Value');
h = findobj('Tag','text1');
set(h,'String',[num2str(s0)]);
h = findobj('Tag','slider2');
theta0 = get(h,'Value');
h = findobj('Tag','text2');
set(h,'String',[num2str(theta0)]);


% --- Outputs from this function are returned to the command line.
function varargout = pokemon_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global img_ball;
global h_ball;
global h_alpha_ball;
global h_eevee;
global h_axes;
global h_text;
global x_max;
global y_max;
global x_pokemon;
global y_pokemon;
global dx_pokemon;
global dy_pokemon;
global s0;
global theta0;
% model constants
g = 9.81;
% define ball size
dx_ball = 0.05*x_max;
dy_ball = 0.05*y_max;
% model parameters
%theta0 = 80.0;
%s0 = 15.0;
h0 = 2.0;
% model parameters -- time-stepping
dt = 0.05;
t_max = 10.0;
% calculate initial velocity components
u = s0*cos(pi*theta0/180.0);
v = s0*sin(pi*theta0/180.0);
% set initial conditions
p(1) = 0.0;
p(2) = h0;
% model
for t=dt:dt:t_max,
    dp = dt*u;
    p(1) = p(1) + dp;
    dp = dt*v;
    p(2) = p(2) + dp;
    %disp(['(', num2str(p(1)), ',', num2str(p(2)), ') @ time ', num2str(t)]);
    %axes('pos',[p(1)/10,p(2)/10,0.05,0.05])
    %h = imshow(img_ball);
    h_ball = imshow(img_ball,'Parent',h_axes,'Xdata',[p(1) p(1)+dx_ball],'Ydata',[y_max-p(2) y_max-p(2)+dy_ball]);
    set(h_ball, 'AlphaData', h_alpha_ball);
    pause(0.005);
    if ( (p(1)+dx_ball > x_pokemon) && (p(1) < x_pokemon+dx_pokemon) && (y_max-p(2)+dy_ball > y_pokemon) && (y_max-p(2) < y_pokemon+dy_pokemon) ),
        delete(h_ball);
        delete(h_eevee);
        h_text = text(h_axes,x_pokemon-dx_pokemon/2,y_pokemon+dy_pokemon/2,'Gottya!','Color','red','FontSize',18);
        % disable 'Throw' button
        h = findobj('Tag','pushbutton1');
        set(h,'Enable','off');
        % exit loop
        break;
    end
    delete(h_ball);
    %scatter(p(1),p(2));
    if (p(2) < 0.0)
        break;
    end
    % update vertical velocity (horizontal velocity unchanged)
    dv = -dt*g;
    v = v + dv;
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global h_axes;
global h_ball;
global h_eevee;
global h_alpha_eevee;
global h_text;
global img_eevee;
global x_max;
global y_max;
global x_pokemon;
global y_pokemon;
global dx_pokemon;
global dy_pokemon;
global s0;
global theta0;
delete(h_ball);
delete(h_eevee);
delete(h_text);
% change pokemon size
loc_rand = rand;
dx_pokemon = 0.04*(1+4*loc_rand)*x_max;
dy_pokemon = 0.04*(1+4*loc_rand)*y_max;
% change pokemon position
x_pokemon = 0.5*(1+rand)*x_max-dx_pokemon;
y_pokemon = (5-1)*dy_pokemon;
% draw pokemon
h_eevee = imshow(img_eevee,'Parent',h_axes,'Xdata',[x_pokemon x_pokemon+dx_pokemon],'Ydata',[y_pokemon y_pokemon+dy_pokemon]);
set(h_eevee, 'AlphaData', h_alpha_eevee);
% reset model parameters and set labels
h = findobj('Tag','slider1');
set(h,'Value',0.0);
s0 = get(h,'Value');
h = findobj('Tag','text1');
set(h,'String',[num2str(s0)]);
h = findobj('Tag','slider2');
set(h,'Value',0.0);
theta0 = get(h,'Value');
h = findobj('Tag','text2');
set(h,'String',[num2str(theta0)]);
% enable 'Throw' button
h = findobj('Tag','pushbutton1');
set(h,'Enable','on');

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global s0;
h = findobj('Tag','slider1');
s0 = get(h,'Value');
h = findobj('Tag','text1');
set(h,'String',[num2str(s0)]);


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global theta0;
h = findobj('Tag','slider2');
theta0 = get(h,'Value');
h = findobj('Tag','text2');
set(h,'String',[num2str(theta0)]);


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
