Files
uni/third/semester1/CT3536: Games Programming/notes/_minted-CT3536-Notes/A7BE8131C3FB47CF92119BD186303A64A97D9E8903456DD8DEC827D96A24153A.pygtex
2023-12-07 02:05:57 +00:00

25 lines
3.5 KiB
Plaintext

\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// Here, we find the 3D position that's just above the enemy's head, and convert this to a vieweort position when viewed through the player's camera.}
\PYG{c+c1}{// Finally, this viewport position is converted to a pixel position as required by the GUI sprite.}
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{MonsterManager}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Update}\PYG{+w}{ }\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{gameState}\PYG{o}{==}\PYG{n}{GameStates}\PYG{p}{.}\PYG{n}{Playing}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{numMonstersAlive}\PYG{o}{==}\PYG{l+m}{1}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// put the final enemy indicator (GUI object) above the final monster}
\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{go}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{GUIManager}\PYG{p}{.}\PYG{n}{instance}\PYG{p}{.}\PYG{n}{finalEnemyIndicator}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{Monster}\PYG{+w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