📄 staker.m
字号:
end
end
newProfile.file = filePath;
case 'SHOP',
newProfile = shop_armory(profiles{panelIndex});
case 'SLIDER',
arsenal = get(hArsenal,'UserData');
nBombs = length(arsenal);
arsenalIndex = nBombs-round(get(source,'Value'));
set(hArsenal,'String',arsenal(arsenalIndex:nBombs));
drawnow;
return;
end
% Update profile panel:
profiles{panelIndex} = newProfile;
update_profile(newProfile);
if ((locationIndex == 1) || any(cellfun('isempty',profiles))),
set(hStart,'Enable','off');
else
set(hStart,'Enable','on');
end
drawnow;
end
%--------------------------------------------------------------------
function newProfile = create_profile
%
% Opens a modal window for creating a new profile.
%
%--------------------------------------------------------------------
% Initialize profile information:
newProfile = default_player_data;
% Create modal figure window:
position = get(hMain,'Position');
position = [position(1:2)+(position(3:4)-[200 95])./2 200 95];
hModal = make_figure(position,...
'CloseRequestFcn',@callback_create,...
'Name','Create Profile','Tag','FIGURE',...
'WindowStyle','modal');
% Create uicontrol objects:
make_panel(hModal,[1 1 200 95]);
make_text(hModal,[11 66 180 20],'String','Player name:');
hName = make_edit(hModal,[21 46 170 20],...
'HorizontalAlignment','left','String','');
make_button(hModal,[71 11 50 25],'Callback',@callback_create,...
'String','Save','Tag','SAVE');
make_button(hModal,[131 11 60 25],'Callback',@callback_create,...
'String','Cancel','Tag','CANCEL');
% Wait for window to be closed:
set(hModal,'Visible','on');
drawnow;
waitfor(hModal);
%------------------------------------------------------------------
function callback_create(source,event)
%
% Callback function for profile creation uicontrols.
%
%------------------------------------------------------------------
switch get(source,'Tag'),
case {'CANCEL','FIGURE'},
newProfile = [];
case 'SAVE',
name = get(hName,'String');
if isempty(name),
bomb_error(hMain,'emptyString','Player name');
return;
elseif (length(name) > MAX_CHARS),
bomb_error(hMain,'oversizedString','Player name');
return;
end
filePath = fullfile(STAKER_PATH,[name,'.prof']);
[fileName,filePath,fileIndex] = uiputfile('*.prof',...
'Save profile',...
filePath);
if (isequal(fileName,0) || isequal(filePath,0)),
return;
end
if (fileIndex > 1),
bomb_error(hMain,'wrongExtension','Save','.prof');
return;
end
newProfile.file = fullfile(filePath,fileName);
newProfile.name = name;
save(newProfile.file,'-struct','newProfile','-mat');
end
delete(hModal);
drawnow;
end
end
%--------------------------------------------------------------------
function customer = shop_armory(customer)
%
% Opens a modal window for shopping at the armory.
%
%--------------------------------------------------------------------
% Initialize variables:
shopLogo = flipdim(imread(fullfile(TEXTURE_PATH,...
'shoplogo.bmp')),1);
bombIndex = ~(customer.unlocked);
inventory = {BOMB_DATA.name};
inventory(bombIndex) = {'???'};
inventory = reshape(sprintf('%11.10s',inventory{:}),11,N_BOMBS).';
bombCosts = cellfun(@num2money,{BOMB_DATA.cost}.',...
'UniformOutput',false);
bombCosts(bombIndex) = {'$???'};
inventory = strcat(inventory,{' = '},bombCosts);
isBuying = true;
price = 0;
% Create modal figure window:
position = get(hMain,'Position');
position = [position(1:2)+(position(3:4)-[530 350])./2 530 350];
hModal = make_figure(position,'CloseRequestFcn',@callback_shop,...
'Name','Armory','Tag','STAKER_SHOP',...
'WindowStyle','modal');
% Create logo panel:
make_panel(hModal,[1 251 190 100]);
hAxes = make_axes(hModal,[6 255 180 90],'XLim',[0 180],...
'YLim',[0 90]);
plot_surface(hAxes,[0 180; 0 180],[0 0; 90 90],zeros(2),shopLogo);
% Create customer panel:
make_panel(hModal,[1 46 190 205]);
make_text(hModal,[11 226 170 15],'String','Customer:');
make_text(hModal,[11 206 170 20],'FontSize',2*fontSize,...
'FontWeight','normal','String',customer.name);
make_text(hModal,[16 176 160 23],'FontWeight','normal',...
'String',{['Staker class: ',customer.class]; ...
'Current arsenal:'});
hCurrent = make_list(hModal,[16 101 160 70],...
'Callback',@callback_shop,...
'String',num2arsenal(customer.arsenal),...
'Tag','CURRENT');
make_text(hModal,[11 76 170 15],'String','Available funds:');
make_panel(hModal,[61 56 120 20],'BackgroundColor',backColor,...
'BorderType','beveledin');
hFunds = make_text(hModal,[62 57 113 14],...
'BackgroundColor',backColor,...
'FontWeight','normal',...
'HorizontalAlignment','right',...
'String',num2money(customer.earnings),...
'Value',customer.earnings);
if (customer.earnings <= 0),
set(hFunds,'ForegroundColor',[0.7 0 0]);
end
% Create description panel:
make_panel(hModal,[191 181 340 170]);
make_text(hModal,[201 325 200 15],'String','Weapon description:');
make_panel(hModal,[200 190 322 135],'BackgroundColor',backColor,...
'BorderType','beveledin');
hDescription = make_text(hModal,[201 191 320 133],...
'BackgroundColor',backColor,...
'FontWeight','normal',...
'String','(select munition below)');
% Create weapon inventory panel:
make_panel(hModal,[311 46 220 135]);
make_text(hModal,[321 156 200 15],'String','Available weapons:');
hAvailable = make_list(hModal,[321 56 200 100],...
'Callback',@callback_shop,...
'String',inventory,'Tag','AVAILABLE');
% Create total panel:
make_panel(hModal,[191 46 120 135]);
make_text(hModal,[201 156 100 15],'String','Price:');
hPrice = make_text(hModal,[201 141 100 15],...
'HorizontalAlignment','right','String','$0');
make_text(hModal,[231 121 20 15],'FontSize',2*fontSize,...
'FontWeight','normal','String',char(215));
hMultiplier = make_edit(hModal,[251 111 30 28],...
'Callback',@callback_shop,...
'Enable','off','Min',MAX_BOMBS,...
'String','1','Tag','MULTIPLIER');
hUp = make_button(hModal,[281 125 20 14],...
'Callback',@callback_shop,'Enable','off',...
'FontWeight','bold','String','+','Tag','UP');
hDown = make_button(hModal,[281 111 20 14],...
'Callback',@callback_shop,'Enable','off',...
'FontWeight','bold','String','-','Tag','DOWN');
make_panel(hModal,[201 106 100 1],'BorderType','line',...
'HighlightColor',textColor);
hTotal = make_text(hModal,[201 86 100 15],...
'HorizontalAlignment','right','String','$0');
hBuyOrSell = make_button(hModal,[226 56 50 25],...
'Callback',@callback_shop,...
'Enable','off','String','Buy',...
'Tag','BUYORSELL');
% Create button panel:
make_panel(hModal,[1 1 530 45]);
make_button(hModal,[11 11 50 25],'Callback',@callback_shop,...
'String','Help','Tag','HELP');
make_button(hModal,[470 11 50 25],'Callback',@callback_shop,...
'String','Exit','Tag','EXIT');
% Wait for window to be closed:
set(hModal,'Visible','on');
drawnow;
waitfor(hModal);
%------------------------------------------------------------------
function callback_shop(source,event)
%
% Callback function for armory uicontrols.
%
%------------------------------------------------------------------
switch get(source,'Tag'),
case 'AVAILABLE',
isBuying = true;
value = get(hAvailable,'Value');
bombList = get(hAvailable,'String');
bombName = strtrim(strtok(bombList{value},'='));
if strcmp(bombName,'???'),
set(hDescription,'String','???: (unavailable)');
set([hPrice hTotal],'String','$0');
set(hMultiplier,'Enable','off','String','1');
set([hUp hDown],'Enable','off');
set(hBuyOrSell,'Enable','off','String','Buy');
else
bombIndex = find(strcmp({BOMB_DATA.name},bombName));
bombText = {[bombName,':'],...
[' ',BOMB_DATA(bombIndex).description]};
set(hDescription,'String',textwrap(hDescription,bombText));
price = BOMB_DATA(bombIndex).cost;
set([hPrice hTotal],'String',num2money(price));
if (value == 1),
maxValue = 0;
else
maxValue = min(MAX_BOMBS-customer.arsenal(bombIndex),...
floor(get(hFunds,'Value')/price));
end
if (maxValue <= 0),
set(hMultiplier,'Enable','off','String','1');
set([hUp hDown],'Enable','off');
set(hBuyOrSell,'Enable','off','String','Buy');
else
set(hMultiplier,'Max',maxValue,'String','1','Value',1);
if (maxValue > 1),
set([hMultiplier hUp],'Enable','on');
else
set([hMultiplier hUp],'Enable','off');
end
set(hDown,'Enable','off');
set(hBuyOrSell,'Enable','on','String','Buy');
end
end
drawnow;
case 'BUYORSELL',
value = get(hMultiplier,'Value');
if isBuying,
customer.arsenal(bombIndex) = ...
customer.arsenal(bombIndex)+value;
funds = customer.earnings-value*price;
if (funds <= 0),
set(hFunds,'ForegroundColor',[0.7 0 0]);
end
else
customer.arsenal(bombIndex) = ...
customer.arsenal(bombIndex)-value;
funds = customer.earnings+value*price;
if (funds > 0),
set(hFunds,'ForegroundColor',textColor);
end
end
customer.earnings = funds;
set(hFunds,'String',num2money(funds),'Value',funds);
maxValue = get(hMultiplier,'Max')-value;
if (maxValue > 0),
value = min(value,maxValue);
set(hMultiplier,'Max',maxValue,'String',num2str(value),...
'Value',value);
if (value == maxValue),
set(hUp,'Enable','off');
end
if (value == 1),
set(hDown,'Enable','off');
end
set(hTotal,'String',num2money(value*price));
else
if isBuying,
set(hTotal,'String',num2money(price));
else
if (sum(customer.arsenal > 0) < get(hCurrent,'Value')),
set(hCurrent,'Value',1);
end
set(hDescription,'String','(select munition below)');
set([hPrice hTotal],'String','$0');
end
set(hMultiplier,'Enable','off','String','1');
set([hUp hDown],'Enable','off');
set(hBuyOrSell,'Enable','off');
end
set(hCurrent,'String',num2arsenal(customer.arsenal));
drawnow;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -