Code covered by the BSD License  

Highlights from
Stellaria (Tech Demo) - The best MATLAB shooting game ever

image thumbnail

Stellaria (Tech Demo) - The best MATLAB shooting game ever

by

 

14 May 2011 (Updated )

If you think you have played the best MATLAB game before. Think again after you try this one.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

stl_addToGlobalPlayerBullets.m
function [addedBulletIndices, nBullets] = stl_addToGlobalPlayerBullets(bulletsToAdd)
%% STL_ADDTOGLOBALPLAYERBULLETS - Add several bullets to the global bullet
% queue (ѷӵ\ȫѾ)
%
% * Please note that the word "bullet" represents all friendly projectiles,
% * including bullets and missiles that interact with the hostile crafts
% 
%% USAGE ÷:
% ------
% [addedBulletIndices, nBullets] = stl_addToGlobalPlayerBullets(bulletsToAdd)
% 
%% Input 
% ------
% bulletsToAdd: Contains the bullet structures to add, an 1 X nBullets struct 
%               that contains fields as can be found in stl_initVariables
%              ҪӵѷӵݣΪ1 x nBullets СĽṹ顣
%               μstl_initVariables
%
%% Output :
% -------
% addedBulletIndices: The indices of the bullets added, must be an 1 x nBullets (output)
%                    cell array
%                       ȫѷӵУЩոձѷӵ±. Ϊ
%                       1 x nBullets (ֵ)Ԫ
% nBullets: The actual number of bullets added, must be equal or lower than
%           the length of bulletsToAdd 
%           ʵʼѷӵĸضСڻbulletsToAddij
% 
%% Dependencies ϵ:
% -------
% -- Global Variables Created:
%       none
% -- Global Variables Used:
% global queueGlobalFriendlyFire
% global availGlobalFriendlyFire
% global numAvailGlobalFriendlyFire
% -- Global Variables Modifed:
% global queueGlobalFriendlyFire
% global availGlobalFriendlyFire
% global numAvailGlobalFriendlyFire
% -- Functions Used:
%       stl_addToGlobalImage
%
%% Created by Mingjing Zhang, Stellari Studio, 2011.

%% Update History
%
%  Date                     Updater                 Modifications Made
% --------------            -----------------       --------------------
%  Mar 25, 2011             M. Zhang                wrote it
% --------------            -----------------       --------------------

%% --------------------Code Starts Here -------------------------

%% Declare the Global Variables ȫֱ
global queueGlobalFriendlyFire
global availGlobalFriendlyFire
global numAvailGlobalFriendlyFire
global usedGlobalFriendlyFire

global PLAYER_CONST
global PLAYER_BULLET_ANIM

%% Exception Processing ⴦
% If the number of available bullets is smaller than the number of bullets to
% add, then add only the number of available bullets.
% ȫֶУʹõFriendlyFireĿСҪӵӵĿôֻȫֶ
% Ϊֹӵӡ
nBullets = min(numel(bulletsToAdd), numAvailGlobalFriendlyFire);

% If no bullets is added at all, then return immediately with empty return
% values.
% ûӵӣôؿվ
% If not, create the return value with the given size.
% ôԤؾ
if nBullets <= 0
    addedBulletIndices = [];
    return;
else
    addedBulletIndices = zeros(1, nBullets);
end

%% Main Code
% Assign image handles to the newly added enemies
% ͼζ
 [addedImageIndices addedImageHandle nImages] = stl_addToGlobalImageHandles(nBullets);

% For debugging
if nImages < nBullets
    disp('stl_addToGlobalPlayerBullets(): image queue overflow');
end

% The number of bullets added must not exceed the number of available
% images
% ӵĿɴڿõͼζĿ
nBullets = nImages;

% Put nBullets available handles to a temporary array.
% * Not really necessary here, but may be useful if the mechanism of
% * queueing were to be altered in a future version. 
% ҳǰnBulletsõHandlesҽʱС
% * ʵһDZġĴиı˶еʵַʽ
% * ôдܾDZҪˡ
tempGlobalFriendlyFire = availGlobalFriendlyFire(1:nBullets);

% ͼζȫ򲻿µӵ
if nBullets <= 0
    addedBulletIndices = [];
    return;
end

% Fill the bullet queue.
% ӵ
% bulletsToAdd(1:nBullets)
% queueGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets))
% tempGlobalFriendlyFire(1:nBullets)
% numAvailGlobalFriendlyFire
queueGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets)) = bulletsToAdd(1:nBullets);

% Save the image handles procured
% ȡimage
[queueGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets)).ImageHandle] = ...
    addedImageHandle{:};
[queueGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets)).ImageHandleIndex] = ...
    addedImageIndices{:};

for i = tempGlobalFriendlyFire(1:nBullets)
    queueGlobalFriendlyFire(i).CurAttBox(:, 5) = i;
    queueGlobalFriendlyFire(i).CurDefBox(:, 5) = i;
end

% Update the number of available bullet handles
% ¿ӵĿ
numAvailGlobalFriendlyFire = numAvailGlobalFriendlyFire - nBullets;

% delete the used values, and pad the unused elements with zero.
% ɾռõЩʣԪ㲹롣
availGlobalFriendlyFire(1:numAvailGlobalFriendlyFire) = ...
    availGlobalFriendlyFire((1:numAvailGlobalFriendlyFire)+nBullets);
availGlobalFriendlyFire(numAvailGlobalFriendlyFire+1:end) = 0;
usedGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets)) = true;
% Get the indices and number of added enemies
% ·ֵϢ
addedBulletIndices = num2cell(tempGlobalFriendlyFire(1:nBullets));
% addedEnemies = num2cell(queueGlobalEnemies(tempGlobalEnemies));
    
set([queueGlobalFriendlyFire(tempGlobalFriendlyFire(1:nBullets)).ImageHandle], 'Visible', 'on');

%% --------------- Code Ends Here --------------------------------
return;

Contact us