📄 staker.m
字号:
make_panel(hMain,[1 1 390 45]);
make_button(hMain,[11 11 50 25],'Callback',@callback_main,...
'String','Help','Tag','HELP');
make_button(hMain,[71 11 95 25],'Callback',@callback_main,...
'String','Preferences','Tag','PREFERENCES');
make_button(hMain,[271 11 50 25],'Callback',@callback_main,...
'String','Quit','Tag','QUIT');
hStart = make_button(hMain,[331 11 50 25],'Callback',@callback_main,...
'Enable','off','String','Start','Tag','START');
%----------------------------------------------------------------------
function update_main
%
% Update function for main menu.
%
%----------------------------------------------------------------------
set(hSuspended,'String',[{'none'}; game2str],'Value',currentGame+1);
if (currentGame == 0),
set(hLocation,'Enable','on','Value',locationIndex);
updateProfileFcns{1}(profiles{1});
updateProfileFcns{2}(profiles{2});
if ((locationIndex == 1) || any(cellfun('isempty',profiles))),
set(hStart,'Enable','off');
else
set(hStart,'Enable','on');
end
else
gameData = status.suspendedGames(currentGame);
set(hLocation,'Enable','off','Value',gameData.locationIndex+2);
updateProfileFcns{1}(gameData.players(1));
updateProfileFcns{2}(gameData.players(2));
set(hStart,'Enable','on');
end
end
%----------------------------------------------------------------------
function callback_main(source,event)
%
% Callback function for main menu uicontrols.
%
%----------------------------------------------------------------------
switch get(source,'Tag'),
case 'HELP',
display_help(hMain);
case 'LOCATION',
locationIndex = get(source,'Value');
if ((locationIndex == 1) || any(cellfun('isempty',profiles))),
set(hStart,'Enable','off');
else
set(hStart,'Enable','on');
end
drawnow;
case 'PREFERENCES',
edit_preferences;
case {'QUIT','STAKER_MAIN'},
delete(hMain);
delete(hGame);
drawnow;
case 'START',
set(hMain,'Pointer','watch');
drawnow;
if (currentGame == 0),
players = [profiles{:}];
nPlayers = length(players);
players = players(randperm(nPlayers));
currentPlayer = 1;
players(1).isCurrent = true;
nMoves = 0;
generate_terrain;
add_game_data;
currentGame = length(status.suspendedGames);
save(STATUS_FILE,'-struct','status','-mat');
for i = 1:nPlayers,
delete(players(i).file);
end
else
gameData = status.suspendedGames(currentGame);
players = gameData.players;
nPlayers = length(players);
currentPlayer = gameData.currentPlayer;
nMoves = gameData.nMoves;
locationIndex = gameData.locationIndex;
locationValue = gameData.locationValue;
mapGenerationState = gameData.mapGenerationState;
mapZ = gameData.mapZ;
mapC = gameData.mapC;
horizonZ = gameData.horizonZ;
edgeColor = gameData.edgeColor;
isWater = gameData.isWater;
waterLevel = gameData.waterLevel;
windVector = gameData.windVector;
timeOfDay = gameData.timeOfDay;
generate_terrain;
status.suspendedGames(currentGame).lastPlayed = clock;
end
if ishandle(hGame),
updateGameFcn();
else
initialize_game;
end
set(hMain,'Pointer','arrow','Visible','off');
set(hGame,'Visible','on');
drawnow;
case 'SUSPENDED',
currentGame = get(source,'Value')-1;
if (currentGame == 0),
set(hLocation,'Enable','on','Value',locationIndex);
updateProfileFcns{1}(profiles{1});
updateProfileFcns{2}(profiles{2});
if ((locationIndex == 1) || any(cellfun('isempty',profiles))),
set(hStart,'Enable','off');
else
set(hStart,'Enable','on');
end
else
gameData = status.suspendedGames(currentGame);
set(hLocation,'Enable','off','Value',gameData.locationIndex+2);
updateProfileFcns{1}(gameData.players(1));
updateProfileFcns{2}(gameData.players(2));
set(hStart,'Enable','on');
end
drawnow;
end
end
%----------------------------------------------------------------------
function initialize_profile(panelIndex)
%
% Initializes a profile panel in the main menu figure window.
%
%----------------------------------------------------------------------
% Create profile panel:
updateProfileFcns{panelIndex} = @update_profile;
hProfile = make_panel(hMain,[11+190*(panelIndex-1) 56 180 240],...
'BackgroundColor',accentColor);
position = [5 215 170 20];
hFile = make_text(hProfile,position,'BackgroundColor',accentColor,...
'FontWeight','normal',...
'HorizontalAlignment','center',...
'String','no profile');
extent = get(hFile,'Extent');
set(hFile,'Position',...
[position(1:2)+(position(3:4)-extent(3:4))./2 extent(3:4)]);
make_panel(hProfile,[5 228 170 1],'BorderType','line',...
'HighlightColor',textColor);
% Create uicontrols for empty panel:
hCreate = make_button(hProfile,[5 190 120 25],...
'Callback',@callback_profile,...
'String','Create Profile','Tag','CREATE');
hSelect = make_button(hProfile,[5 160 120 25],...
'Callback',@callback_profile,...
'String','Select Profile','Tag','SELECT');
hComputer = make_button(hProfile,[5 130 80 25],...
'Callback',@callback_profile,...
'Enable','off','String','Computer',...
'Tag','COMPUTER');
% Create uicontrols for panel containing a profile:
hAxes = make_axes(hProfile,[5 205 30 10]);
hPointer = plot_patch(hAxes,[0 0.5 1],[1 0 1],[1 0 0],...
'Visible','off');
hPlayer = make_text(hProfile,[5 185 170 20],...
'BackgroundColor',accentColor,...
'FontSize',2*fontSize,'FontWeight','normal',...
'Visible','off');
hAxes = make_axes(hProfile,[5 165 170 15]);
xData = [0 1 2 3; 1 2 3 4; 1 2 3 4; 0 1 2 3]./4;
hCapacity = [plot_patch(hAxes,[0 1 1 0],[0 0 1 1],[0 1 0],...
'Visible','off') ...
plot_patch(hAxes,xData,[zeros(2,4); ones(2,4)],...
'none','EdgeColor',[0 0 0],'Visible','off')];
hData = make_text(hProfile,[5 105 170 50],...
'BackgroundColor',accentColor,...
'FontWeight','normal','Visible','off');
hPanel = make_panel(hProfile,[19 34 142 67],...
'BackgroundColor',backColor,...
'BorderType','beveledin','Visible','off');
hArsenal = make_text(hProfile,[20 35 130 65],...
'BackgroundColor',backColor,...
'FontWeight','normal','Visible','off');
hSlider = make_slider(hProfile,[150 35 10 65],...
'Callback',@callback_profile,'Enable','off',...
'Tag','SLIDER','Value',1,'Visible','off');
hShop = make_button(hProfile,[30 5 50 25],...
'Callback',@callback_profile,'String','Shop',...
'Tag','SHOP','Visible','off');
hRemove = make_button(hProfile,[90 5 60 25],...
'Callback',@callback_profile,...
'String','Remove','Tag','REMOVE',...
'Visible','off');
%--------------------------------------------------------------------
function update_profile(newProfile)
%
% Update function for profile panel.
%
%--------------------------------------------------------------------
if isempty(newProfile),
% Clear profile panel:
set(hFile,'String','no profile');
position = get(hFile,'Position');
extent = get(hFile,'Extent');
set(hFile,'Position',...
[position(1:2)+(position(3:4)-extent(3:4))./2 extent(3:4)]);
set([hPointer hPlayer hCapacity hData hPanel hArsenal hSlider ...
hShop hRemove],'Visible','off');
set([hCreate hSelect hComputer],'Visible','on');
else
% Update profile panel:
[filePath,fileName,fileExt] = fileparts(newProfile.file);
set(hFile,'String',[fileName,fileExt]);
position = get(hFile,'Position');
extent = get(hFile,'Extent');
set(hFile,'Position',...
[position(1:2)+(position(3:4)-extent(3:4))./2 extent(3:4)]);
if (newProfile.isCurrent),
set(hPointer,'Visible','on');
else
set(hPointer,'Visible','off');
end
set(hPlayer,'String',newProfile.name);
capacity = newProfile.capacity;
set(hCapacity(1),...
'FaceColor',min([2-(capacity/50) (capacity/50) 0],1),...
'XData',[0 0.01 0.01 0].*capacity);
earnings = num2money(newProfile.earnings);
stats = sprintf('%i%c',[newProfile.record; 45 45 32]);
set(hData,'String',{['Earnings: ',earnings]; ...
['Record: ',stats]; ...
['Staker class: ',newProfile.class]; ...
'Arsenal:'});
arsenal = num2arsenal(newProfile.arsenal);
set(hArsenal,'String',arsenal,'UserData',arsenal);
if (length(arsenal) > 1),
nBombs = length(arsenal)-1;
set(hSlider,'Enable','on','Max',nBombs,...
'SliderStep',[1 3]./nBombs,'Value',nBombs);
else
set(hSlider,'Enable','off','Max',1,'SliderStep',[1 3],...
'Value',1);
end
set([hCreate hSelect hComputer],'Visible','off');
set([hPlayer hCapacity hData hPanel hArsenal hSlider],...
'Visible','on');
if (currentGame == 0),
set([hShop hRemove],'Visible','on');
else
set([hShop hRemove],'Visible','off');
end
end
end
%--------------------------------------------------------------------
function callback_profile(source,event)
%
% Callback function for profile panel uicontrols.
%
%--------------------------------------------------------------------
% Make new profile:
switch get(source,'Tag'),
case 'CREATE',
newProfile = create_profile;
case 'REMOVE',
newProfile = [];
case 'SELECT',
filePath = fullfile(STAKER_PATH,filesep);
[fileName,filePath,fileIndex] = uigetfile('*.prof',...
'Select profile',...
filePath);
if (isequal(fileName,0) || isequal(filePath,0)),
return;
end
if (fileIndex > 1),
bomb_error(hMain,'wrongExtension','Load','.prof');
return;
end
filePath = fullfile(filePath,fileName);
try
newProfile = load(filePath,'-mat');
catch
bomb_error(hMain,'corruptedFile',fileName);
return;
end
if (~isequal(fieldnames(newProfile),PROFILE_FIELDS)),
bomb_error(hMain,'badFileContents',fileName,'.prof');
return;
end
if any(~cellfun('isempty',profiles)),
loaded = [profiles{:}];
if ismember(newProfile.ID,[loaded.ID]),
bomb_error(hMain,'duplicateProfile',fileName);
return;
end
end
if (~isempty(status.suspendedGames)),
loaded = [status.suspendedGames.players];
if ismember(newProfile.ID,[loaded.ID]),
bomb_error(hMain,'duplicateProfile',fileName);
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -