Add CT3536 Games Programming

This commit is contained in:
2023-12-07 02:05:57 +00:00
parent eb94d02f16
commit 71e1a59261
14438 changed files with 919425 additions and 0 deletions

View File

@ -0,0 +1,92 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
// inspector settings
public Rigidbody rigidBody;
public GameObject miniAsteroid;
public GameObject smallAsteroid; // spawn small asteroids in the place of the large asteroid
// Use this for initialization
void Start () {
// randomise size+mass
transform.localScale = new Vector3(Random.Range(0.06f,0.09f), Random.Range(0.06f,0.09f), Random.Range
(0.06f,0.09f));
rigidBody.mass = transform.localScale.x * transform.localScale.y * transform.localScale.z;
// randomise velocity
rigidBody.velocity = new Vector3 (Random.Range (-20f, 20f), 0f, Random.Range (-20f, 20f));
rigidBody.angularVelocity = new Vector3 (Random.Range (-20f, 20f), Random.Range (-
20f, 20f), Random.Range (-20f, 20f));
// start periodically checking for being off-screen
InvokeRepeating ("CheckScreenEdges", 0.2f, 0.2f);
}
private void CheckScreenEdges() {
Vector3 pos = transform.position;
Vector3 vel = rigidBody.velocity;
float xTeleport = 0f, zTeleport = 0f;
if (pos.x < GameManager.screenBottomLeft.x && vel.x <= 0f) // velocity check as sanity test
xTeleport = GameManager.screenWidth;
else if (pos.x > GameManager.screenTopRight.x && vel.x >= 0f)
xTeleport = -GameManager.screenWidth;
if (pos.z < GameManager.screenBottomLeft.z && vel.z <= 0f)
zTeleport = GameManager.screenHeight;
else if (pos.z > GameManager.screenTopRight.z && vel.z >= 0f)
zTeleport = -GameManager.screenHeight;
if (xTeleport != 0f || zTeleport != 0f)
transform.position = new Vector3 (pos.x + xTeleport, 0f, pos.z + zTeleport);
}
// method to spawn mini-asteroid fragments at the contact point(s) of a collision
private void OnCollisionEnter(Collision collision) {
// if collided with the spaceship, destroy it and recreate it at 0,0,0
if (collision.gameObject.CompareTag("spaceship")) {
Destroy(collision.gameObject);
GameManager.CreatePlayerSpaceship();
}
// if collided with the bullet, destroy it and asteroid and spawn small asteroids
if (collision.gameObject.CompareTag("bullet")) {
Destroy(collision.gameObject);
Destroy(this);
for (int i = 0; i <= numF4ragments; i++) {
GameObject fragment = Instantiate(miniAsteroid);
Instantiate(smallAsteroid, transform.position, transform.rotation);
}
}
// Arraylist to keep track of the mini asteroids created for a collision
ArrayList fragments = new ArrayList();
foreach (ContactPoint contact in collision.contacts) {
// instantiating a random number of mini asteroid between 1 and 5 inclusive
int numFragments = Random.Range(1, 5);
for (int i = 1; i <= numFragments; i++) {
GameObject fragment = Instantiate(miniAsteroid);
fragment.transform.position = contact.point;
fragments.Add(fragment);
}
}
StartCoroutine(DestroyFragments(fragments));
}
// coroutine to destroy all the fragments from a collision
IEnumerator DestroyFragments(ArrayList fragments) {
yield return new WaitForSeconds(3);
foreach (GameObject fragment in fragments) {
Destroy(fragment);
}
}
}

View File

@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public GameObject bullet;
public float speed = 20f;
// Start is called before the first frame update
void Start()
{
// set the bullet moving
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * speed;
// start periodically checking for being off-screen
InvokeRepeating ("CheckScreenEdges", 0.2f, 0.2f);
}
// Update is called once per frame
void Update()
{
}
private void CheckScreenEdges() {
Vector3 pos = bullet.transform.position;
Vector3 vel = bullet.GetComponent<Rigidbody>().velocity;
if (pos.x < GameManager.screenBottomLeft.x || pos.x > GameManager.screenTopRight.x || pos.z < GameManager.screenBottomLeft.z || pos.z > GameManager.screenTopRight.z) {
Destroy(bullet);
}
}
}

View File

@ -0,0 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
// inspector settings
public GameObject asteroidPrefab;
public GameObject spaceship;
public static GameObject staticSpaceship;
// class-level statics
public static GameManager instance;
public static int currentGameLevel;
public static Vector3 screenBottomLeft, screenTopRight;
public static float screenWidth, screenHeight;
//
void Awake() {
staticSpaceship = spaceship;
}
// Use this for initialization
void Start() {
instance = this;
Camera.main.transform.position = new Vector3 (0f, 30f, 0f);
Camera.main.transform.LookAt (Vector3.zero, new Vector3 (0f, 0f, 1f));
currentGameLevel = 0;
// find screen corners and size, in world coordinates
// for ViewportToWorldPoint, the z value specified is in world units from the camera
screenBottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0f,0f,30f));
screenTopRight = Camera.main.ViewportToWorldPoint (new Vector3(1f,1f,30f));
screenWidth = screenTopRight.x - screenBottomLeft.x;
screenHeight = screenTopRight.z - screenBottomLeft.z;
CreatePlayerSpaceship();
StartNextLevel();
}
public static void CreatePlayerSpaceship() {
Instantiate(staticSpaceship);
staticSpaceship.transform.position = new Vector3(0, 0, 0);
}
public static void StartNextLevel() {
currentGameLevel++;
// create some asteroids near the edges of the screen
for (int i = 0; i < currentGameLevel * 2 + 3; i++) {
GameObject go = Instantiate (instance.asteroidPrefab) as GameObject;
float x, z;
if (Random.Range (0f, 1f) < 0.5f)
x = screenBottomLeft.x + Random.Range (0f, 0.15f) * screenWidth; // near the left edge
else
x = screenTopRight.x - Random.Range (0f, 0.15f) * screenWidth; // near the right edge
if (Random.Range (0f, 1f) < 0.5f)
z = screenBottomLeft.z + Random.Range (0f, 0.15f) * screenHeight; // near the bottom edge
else
z = screenTopRight.z - Random.Range (0f, 0.15f) * screenHeight; // near the top edge
go.transform.position = new Vector3(x, 0f, z);
}
}
}

View File

@ -0,0 +1,86 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
// inspector settings
public Rigidbody rigidBody;
public GameObject miniAsteroid;
// Use this for initialization
void Start () {
// randomise size+mass
transform.localScale = new Vector3(Random.Range(0.06f,0.09f), Random.Range(0.06f,0.09f), Random.Range
(0.06f,0.09f));
rigidBody.mass = transform.localScale.x * transform.localScale.y * transform.localScale.z;
// randomise velocity
rigidBody.velocity = new Vector3 (Random.Range (-20f, 20f), 0f, Random.Range (-20f, 20f));
rigidBody.angularVelocity = new Vector3 (Random.Range (-20f, 20f), Random.Range (-
20f, 20f), Random.Range (-20f, 20f));
// start periodically checking for being off-screen
InvokeRepeating ("CheckScreenEdges", 0.2f, 0.2f);
}
private void CheckScreenEdges() {
Vector3 pos = transform.position;
Vector3 vel = rigidBody.velocity;
float xTeleport = 0f, zTeleport = 0f;
if (pos.x < GameManager.screenBottomLeft.x && vel.x <= 0f) // velocity check as sanity test
xTeleport = GameManager.screenWidth;
else if (pos.x > GameManager.screenTopRight.x && vel.x >= 0f)
xTeleport = -GameManager.screenWidth;
if (pos.z < GameManager.screenBottomLeft.z && vel.z <= 0f)
zTeleport = GameManager.screenHeight;
else if (pos.z > GameManager.screenTopRight.z && vel.z >= 0f)
zTeleport = -GameManager.screenHeight;
if (xTeleport != 0f || zTeleport != 0f)
transform.position = new Vector3 (pos.x + xTeleport, 0f, pos.z + zTeleport);
}
// method to spawn mini-asteroid fragments at the contact point(s) of a collision
private void OnCollisionEnter(Collision collision) {
// if collided with the spaceship, destroy it and recreate it at 0,0,0
if (collision.gameObject.CompareTag("spaceship")) {
Destroy(collision.gameObject);
GameManager.CreatePlayerSpaceship();
}
// if collided with the bullet, destroy it and asteroid and spawn small asteroids
if (collision.gameObject.CompareTag("bullet")) {
Destroy(collision.gameObject);
Destroy(this); // just destroying the small asteroid on collision
}
// Arraylist to keep track of the mini asteroids created for a collision
ArrayList fragments = new ArrayList();
foreach (ContactPoint contact in collision.contacts) {
// instantiating a random number of mini asteroid between 1 and 5 inclusive
int numFragments = Random.Range(1, 5);
for (int i = 1; i <= numFragments; i++) {
GameObject fragment = Instantiate(miniAsteroid);
fragment.transform.position = contact.point;
fragments.Add(fragment);
}
}
StartCoroutine(DestroyFragments(fragments));
}
// coroutine to destroy all the fragments from a collision
IEnumerator DestroyFragments(ArrayList fragments) {
yield return new WaitForSeconds(3);
foreach (GameObject fragment in fragments) {
Destroy(fragment);
}
}
}

View File

@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spaceship : MonoBehaviour
{
public GameObject spaceship;
public GameObject bullet; // spawning bullets from the Spaceship class as they "belong" to the spaceship
public float speed = 5.0f;
public float rotationalSpeed = 2.0f;
public float timeOfLastBullet;
// Start is called before the first frame update
void Start()
{
// start periodically checking for being off-screen
InvokeRepeating ("CheckScreenEdges", 0.2f, 0.2f);
}
// Update is called once per frame
void Update()
{
// move spaceship according to arrow keys
// applying just a force to the spaceship object creates some unusual handling, but i feel that this is correct as in space there should be 0 drag, and if a force is applied in one direction, it should remain until it's cancelled out
if (Input.GetKey(KeyCode.LeftArrow)) {
spaceship.GetComponent<Rigidbody>().AddTorque(new Vector3(0, -rotationalSpeed, 0));
}
else if (Input.GetKey(KeyCode.RightArrow)) {
spaceship.GetComponent<Rigidbody>().AddTorque(new Vector3(0, rotationalSpeed, 0));
}
else if (Input.GetKey(KeyCode.UpArrow)) {
spaceship.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, speed));
}
else if (Input.GetKey(KeyCode.DownArrow)) {
spaceship.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, -speed));
}
// shoot a bullet
if (Input.GetKeyUp(KeyCode.Space) && Time.time - timeOfLastBullet >= 0.25) { // only spawning a bullet once the key is released and once 0.25 seconds has elapsed since the last bullet
// spawning a bullet at the front tip of the spaceship
Instantiate(bullet, spaceship.transform.position + spaceship.transform.forward * spaceship.transform.localScale.z, spaceship.transform.rotation);
timeOfLastBullet = Time.time;
}
}
private void CheckScreenEdges() {
Vector3 pos = spaceship.transform.position;
Vector3 vel = spaceship.GetComponent<Rigidbody>().velocity;
float xTeleport = 0f, zTeleport = 0f;
if (pos.x < GameManager.screenBottomLeft.x && vel.x <= 0f) // velocity check as sanity test
xTeleport = GameManager.screenWidth;
else if (pos.x > GameManager.screenTopRight.x && vel.x >= 0f)
xTeleport = -GameManager.screenWidth;
if (pos.z < GameManager.screenBottomLeft.z && vel.z <= 0f)
zTeleport = GameManager.screenHeight;
else if (pos.z > GameManager.screenTopRight.z && vel.z >= 0f)
zTeleport = -GameManager.screenHeight;
if (xTeleport != 0f || zTeleport != 0f)
transform.position = new Vector3 (pos.x + xTeleport, 0f, pos.z + zTeleport);
}
}

View File

@ -0,0 +1,93 @@
%! TeX program = lualatex
\documentclass[a4paper, 11pt]{article}
% packages
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage[final, colorlinks = false, urlcolor = cyan]{hyperref}
\usepackage{changepage} % adjust margins on the fly
\usepackage{fontspec}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.9}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing, skip=0pt}}{}
\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{-}
\setmainfont{EB Garamond}
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}
\usepackage{titlesec}
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
%
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}
% margins
\addtolength{\hoffset}{-2.25cm}
\addtolength{\textwidth}{4.5cm}
\addtolength{\voffset}{-3.25cm}
\addtolength{\textheight}{5cm}
\setlength{\parskip}{0pt}
\setlength{\parindent}{0in}
% \setcounter{secnumdepth}{0}
\begin{document}
\hrule \medskip
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
\textbf{Name:} Andrew Hayes \\
\textbf{E-mail:} \href{mailto://a.hayes18@universityofgalway.ie}{a.hayes18@universityofgalway.ie} \hfill\\
\textbf{ID:} 21321503 \hfill
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\vspace{0.4em}
\Large
\textbf{CT3536} \\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\today
\end{minipage}
\medskip\hrule
\begin{center}
\normalsize
\textbf{Lab Assignment 05}
\end{center}
\hrule
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{../code/GameManager.cs}
\caption{\texttt{GameManager.cs}}
\end{code}
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{../code/Asteroid.cs}
\caption{\texttt{Asteroid.cs}}
\end{code}
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{../code/SmallAsteroid.cs}
\caption{\texttt{SmallAsteroid.cs}}
\end{code}
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{../code/Spaceship.cs}
\caption{\texttt{Spaceship.cs}}
\end{code}
\begin{code}
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{../code/Bullet.cs}
\caption{\texttt{Bullet.cs}}
\end{code}
\end{document}

View File

@ -0,0 +1,64 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections.Generic}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{UnityEngine}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{GameManager}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// inspector settings }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{asteroidPrefab}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{staticSpaceship}\PYG{p}{;}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// class-level statics }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{n}{GameManager}\PYG{+w}{ }\PYG{n}{instance}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{currentGameLevel}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{screenBottomLeft}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{screenTopRight}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{screenWidth}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{screenHeight}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// }
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Awake}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{staticSpaceship}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// Use this for initialization }
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Start}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{instance}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{this}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Camera}\PYG{p}{.}\PYG{n}{main}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{30f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Camera}\PYG{p}{.}\PYG{n}{main}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{LookAt}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Vector3}\PYG{p}{.}\PYG{n}{zero}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{1f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{currentGameLevel}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// find screen corners and size, in world coordinates }
\PYG{+w}{ }\PYG{c+c1}{// for ViewportToWorldPoint, the z value specified is in world units from the camera }
\PYG{+w}{ }\PYG{n}{screenBottomLeft}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Camera}\PYG{p}{.}\PYG{n}{main}\PYG{p}{.}\PYG{n}{ViewportToWorldPoint}\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0f}\PYG{p}{,}\PYG{l+m}{0f}\PYG{p}{,}\PYG{l+m}{30f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{screenTopRight}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Camera}\PYG{p}{.}\PYG{n}{main}\PYG{p}{.}\PYG{n}{ViewportToWorldPoint}\PYG{+w}{ }\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{1f}\PYG{p}{,}\PYG{l+m}{1f}\PYG{p}{,}\PYG{l+m}{30f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{screenWidth}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{screenTopRight}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{n}{screenBottomLeft}\PYG{p}{.}\PYG{n}{x}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{screenHeight}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{screenTopRight}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{n}{screenBottomLeft}\PYG{p}{.}\PYG{n}{z}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{CreatePlayerSpaceship}\PYG{p}{();}
\PYG{+w}{ }\PYG{n}{StartNextLevel}\PYG{p}{();}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{CreatePlayerSpaceship}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{staticSpaceship}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{staticSpaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k}{static}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{StartNextLevel}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{currentGameLevel}\PYG{o}{++}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// create some asteroids near the edges of the screen }
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{currentGameLevel}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{l+m}{2}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m}{3}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\P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{ }\PYG{l+m}{0.15f}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{screenHeight}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// near the top edge }
\PYG{+w}{ }\PYG{n}{go}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{z}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\end{Verbatim}

View File

@ -0,0 +1,88 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections.Generic}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{UnityEngine}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{Asteroid}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// inspector settings }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{Rigidbody}\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{miniAsteroid}\PYG{p}{;}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// Use this for initialization }
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Start}\PYG{+w}{ }\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// randomise size+mass }
\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}
\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{mass}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{y}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{z}\PYG{p}{;}\PYG{+w}{ }
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\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// start periodically checking for being off-screen }
\PYG{+w}{ }\PYG{n}{InvokeRepeating}\PYG{+w}{ }\PYG{p}{(}\PYG{l+s}{\PYGZdq{}CheckScreenEdges\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{CheckScreenEdges}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{pos}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{vel}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{velocity}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{screenBottomLeft}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZam{}\PYGZam{}}\PYG{+w}{ }\PYG{n}{vel}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{)}\PYG{+w}{ }\PYG{c+c1}{// velocity check as sanity test }
\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{screenWidth}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{n+nf}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZgt{}}\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{screenTopRight}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZam{}\PYGZam{}}\PYG{+w}{ }\PYG{n}{vel}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{\PYGZgt{}=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{)}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{screenWidth}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{screenBottomLeft}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }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PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{pos}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// method to spawn mini-asteroid fragments at the contact point(s) of a collision}
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{OnCollisionEnter}\PYG{p}{(}\PYG{n}{Collision}\PYG{+w}{ }\PYG{n}{collision}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// if collided with the spaceship, destroy it and recreate it at 0,0,0}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{.}\PYG{n}{CompareTag}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}spaceship\PYGZdq{}}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{CreatePlayerSpaceship}\PYG{p}{();}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// if collided with the bullet, destroy it and asteroid and spawn small asteroids}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{.}\PYG{n}{CompareTag}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}bullet\PYGZdq{}}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{k}{this}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// just destroying the small asteroid on collision}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// Arraylist to keep track of the mini asteroids created for a collision}
\PYG{+w}{ }\PYG{n}{ArrayList}\PYG{+w}{ }\PYG{n}{fragments}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ArrayList}\PYG{p}{();}
\PYG{+w}{ }\PYG{k}{foreach}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ContactPoint}\PYG{+w}{ }\PYG{n}{contact}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{n}{collision}\PYG{p}{.}\PYG{n}{contacts}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// instantiating a random number of mini asteroid between 1 and 5 inclusive}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{numFragments}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{1}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{5}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{n}{numFragments}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{fragment}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{miniAsteroid}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{fragment}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{contact}\PYG{p}{.}\PYG{n}{point}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{.}\PYG{n}{Add}\PYG{p}{(}\PYG{n}{fragment}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{n}{StartCoroutine}\PYG{p}{(}\PYG{n}{DestroyFragments}\PYG{p}{(}\PYG{n}{fragments}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// coroutine to destroy all the fragments from a collision }
\PYG{+w}{ }\PYG{n}{IEnumerator}\PYG{+w}{ }\PYG{n+nf}{DestroyFragments}\PYG{p}{(}\PYG{n}{ArrayList}\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{yield}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n+nf}{WaitForSeconds}\PYG{p}{(}\PYG{l+m}{3}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{foreach}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{fragment}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{fragment}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,36 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections}\PYG{p}{;}
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections.Generic}\PYG{p}{;}
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{UnityEngine}\PYG{p}{;}
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{Bullet}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}
\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{bullet}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{speed}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// Start is called before the first frame update}
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Start}\PYG{p}{()}
\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// set the bullet moving}
\PYG{+w}{ }\PYG{n}{bullet}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{velocity}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{bullet}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{forward}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{speed}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// start periodically checking for being off-screen }
\PYG{+w}{ }\PYG{n}{InvokeRepeating}\PYG{+w}{ }\PYG{p}{(}\PYG{l+s}{\PYGZdq{}CheckScreenEdges\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// Update is called once per frame}
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Update}\PYG{p}{()}
\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{CheckScreenEdges}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{pos}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{bullet}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+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end{Verbatim}

View File

@ -0,0 +1,69 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections}\PYG{p}{;}
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections.Generic}\PYG{p}{;}
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{UnityEngine}\PYG{p}{;}
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{Spaceship}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}
\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{bullet}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// spawning bullets from the Spaceship class as they "belong" to the spaceship}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{speed}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{5.0f}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{rotationalSpeed}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{2.0f}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{timeOfLastBullet}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// Start is called before the first frame update}
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Start}\PYG{p}{()}
\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// start periodically checking for being off-screen }
\PYG{+w}{ }\PYG{n}{InvokeRepeating}\PYG{+w}{ }\PYG{p}{(}\PYG{l+s}{\PYGZdq{}CheckScreenEdges\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// Update is called once per frame}
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Update}\PYG{p}{()}
\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// move spaceship according to arrow keys}
\PYG{+w}{ }\PYG{c+c1}{// applying just a force to the spaceship object creates some unusual handling, but i feel that this is correct as in space there should be 0 drag, and if a force is applied in one direction, it should remain until it's cancelled out}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Input}\PYG{p}{.}\PYG{n}{GetKey}\PYG{p}{(}\PYG{n}{KeyCode}\PYG{p}{.}\PYG{n}{LeftArrow}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{AddTorque}\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{n}{rotationalSpeed}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{n+nf}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Input}\PYG{p}{.}\PYG{n}{GetKey}\PYG{p}{(}\PYG{n}{KeyCode}\PYG{p}{.}\PYG{n}{RightArrow}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{AddTorque}\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{rotationalSpeed}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{n+nf}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Input}\PYG{p}{.}\PYG{n}{GetKey}\PYG{p}{(}\PYG{n}{KeyCode}\PYG{p}{.}\PYG{n}{UpArrow}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{AddRelativeForce}\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{speed}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{n+nf}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Input}\PYG{p}{.}\PYG{n}{GetKey}\PYG{p}{(}\PYG{n}{KeyCode}\PYG{p}{.}\PYG{n}{DownArrow}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{AddRelativeForce}\PYG{p}{(}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{n}{speed}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// shoot a bullet}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Input}\PYG{p}{.}\PYG{n}{GetKeyUp}\PYG{p}{(}\PYG{n}{KeyCode}\PYG{p}{.}\PYG{n}{Space}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZam{}\PYGZam{}}\PYG{+w}{ }\PYG{n}{Time}\PYG{p}{.}\PYG{n}{time}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{n}{timeOfLastBullet}\PYG{+w}{ }\PYG{o}{\PYGZgt{}=}\PYG{+w}{ }\PYG{l+m}{0.25}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{c+c1}{// only spawning a bullet once the key is released and once 0.25 seconds has elapsed since the last bullet}
\PYG{+w}{ }\PYG{c+c1}{// spawning a bullet at the front tip of the spaceship }
\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{bullet}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{forward}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{z}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{rotation}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{timeOfLastBullet}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Time}\PYG{p}{.}\PYG{n}{time}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{CheckScreenEdges}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{pos}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{vel}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{spaceship}\PYG{p}{.}\PYG{n}{GetComponent}\PYG{o}{\PYGZlt{}}\PYG{n}{Rigidbody}\PYG{o}{\PYGZgt{}}\PYG{p}{().}\PYG{n}{velocity}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k+kt}{float}\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}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 }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{+w}{ }\PYG{o}{||}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{)}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{pos}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,94 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{System.Collections.Generic}\PYG{p}{;}\PYG{+w}{ }
\PYG{k}{using}\PYG{+w}{ }\PYG{n+nn}{UnityEngine}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{k}{public}\PYG{+w}{ }\PYG{k}{class}\PYG{+w}{ }\PYG{n+nc}{Asteroid}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{MonoBehaviour}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// inspector settings }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{Rigidbody}\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{miniAsteroid}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{public}\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{smallAsteroid}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// spawn small asteroids in the place of the large asteroid}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// Use this for initialization }
\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{Start}\PYG{+w}{ }\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// randomise size+mass }
\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{p}{(}\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}
\PYG{p}{(}\PYG{l+m}{0.06f}\PYG{p}{,}\PYG{l+m}{0.09f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{mass}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{y}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{localScale}\PYG{p}{.}\PYG{n}{z}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// randomise velocity }
\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{velocity}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m}{20f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{),}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m}{20f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{angularVelocity}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m}{20f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{\PYGZhy{}}
\PYG{l+m}{20f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m}{20f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{20f}\PYG{p}{));}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// start periodically checking for being off-screen }
\PYG{+w}{ }\PYG{n}{InvokeRepeating}\PYG{+w}{ }\PYG{p}{(}\PYG{l+s}{\PYGZdq{}CheckScreenEdges\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0.2f}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{CheckScreenEdges}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{pos}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{n}{vel}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{rigidBody}\PYG{p}{.}\PYG{n}{velocity}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k+kt}{float}\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PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{xTeleport}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{+w}{ }\PYG{o}{||}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{)}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{Vector3}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{pos}\PYG{p}{.}\PYG{n}{x}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{xTeleport}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{0f}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{pos}\PYG{p}{.}\PYG{n}{z}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{zTeleport}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// method to spawn mini-asteroid fragments at the contact point(s) of a collision}
\PYG{+w}{ }\PYG{k}{private}\PYG{+w}{ }\PYG{k}{void}\PYG{+w}{ }\PYG{n+nf}{OnCollisionEnter}\PYG{p}{(}\PYG{n}{Collision}\PYG{+w}{ }\PYG{n}{collision}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// if collided with the spaceship, destroy it and recreate it at 0,0,0}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{.}\PYG{n}{CompareTag}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}spaceship\PYGZdq{}}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{GameManager}\PYG{p}{.}\PYG{n}{CreatePlayerSpaceship}\PYG{p}{();}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// if collided with the bullet, destroy it and asteroid and spawn small asteroids}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{.}\PYG{n}{CompareTag}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}bullet\PYGZdq{}}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{collision}\PYG{p}{.}\PYG{n}{gameObject}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{k}{this}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{n}{numF4ragments}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{fragment}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{miniAsteroid}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{smallAsteroid}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{transform}\PYG{p}{.}\PYG{n}{rotation}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// Arraylist to keep track of the mini asteroids created for a collision}
\PYG{+w}{ }\PYG{n}{ArrayList}\PYG{+w}{ }\PYG{n}{fragments}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ArrayList}\PYG{p}{();}
\PYG{+w}{ }\PYG{k}{foreach}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{ContactPoint}\PYG{+w}{ }\PYG{n}{contact}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{n}{collision}\PYG{p}{.}\PYG{n}{contacts}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// instantiating a random number of mini asteroid between 1 and 5 inclusive}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{numFragments}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Random}\PYG{p}{.}\PYG{n}{Range}\PYG{p}{(}\PYG{l+m}{1}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m}{5}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{n}{numFragments}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{fragment}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Instantiate}\PYG{p}{(}\PYG{n}{miniAsteroid}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{fragment}\PYG{p}{.}\PYG{n}{transform}\PYG{p}{.}\PYG{n}{position}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{contact}\PYG{p}{.}\PYG{n}{point}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{.}\PYG{n}{Add}\PYG{p}{(}\PYG{n}{fragment}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{n}{StartCoroutine}\PYG{p}{(}\PYG{n}{DestroyFragments}\PYG{p}{(}\PYG{n}{fragments}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// coroutine to destroy all the fragments from a collision }
\PYG{+w}{ }\PYG{n}{IEnumerator}\PYG{+w}{ }\PYG{n+nf}{DestroyFragments}\PYG{p}{(}\PYG{n}{ArrayList}\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{yield}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n+nf}{WaitForSeconds}\PYG{p}{(}\PYG{l+m}{3}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{foreach}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{GameObject}\PYG{+w}{ }\PYG{n}{fragment}\PYG{+w}{ }\PYG{k}{in}\PYG{+w}{ }\PYG{n}{fragments}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{Destroy}\PYG{p}{(}\PYG{n}{fragment}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,102 @@
\makeatletter
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
\let\PYG@ul=\relax \let\PYG@tc=\relax%
\let\PYG@bc=\relax \let\PYG@ff=\relax}
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
\PYG@tok{#1}\expandafter\PYG@toks\fi}
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
\PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}
\@namedef{PYG@tok@w}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\@namedef{PYG@tok@c}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cp}{\def\PYG@tc##1{\textcolor[rgb]{0.61,0.40,0.00}{##1}}}
\@namedef{PYG@tok@k}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kt}{\def\PYG@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\@namedef{PYG@tok@o}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ow}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@nb}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nf}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@ne}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.80,0.25,0.22}{##1}}}
\@namedef{PYG@tok@nv}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@no}{\def\PYG@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\@namedef{PYG@tok@nl}{\def\PYG@tc##1{\textcolor[rgb]{0.46,0.46,0.00}{##1}}}
\@namedef{PYG@tok@ni}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@na}{\def\PYG@tc##1{\textcolor[rgb]{0.41,0.47,0.13}{##1}}}
\@namedef{PYG@tok@nt}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nd}{\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@s}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sd}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@si}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@se}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.36,0.12}{##1}}}
\@namedef{PYG@tok@sr}{\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@ss}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sx}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@m}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@gh}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gu}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gd}{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\@namedef{PYG@tok@gi}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.52,0.00}{##1}}}
\@namedef{PYG@tok@gr}{\def\PYG@tc##1{\textcolor[rgb]{0.89,0.00,0.00}{##1}}}
\@namedef{PYG@tok@ge}{\let\PYG@it=\textit}
\@namedef{PYG@tok@gs}{\let\PYG@bf=\textbf}
\@namedef{PYG@tok@ges}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
\@namedef{PYG@tok@gp}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@go}{\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@gt}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\@namedef{PYG@tok@err}{\def\PYG@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
\@namedef{PYG@tok@kc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kd}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kr}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@bp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@fm}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@vc}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vg}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vi}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vm}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sa}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sb}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sc}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@dl}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s2}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sh}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s1}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@mb}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mf}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mh}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mi}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@il}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mo}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ch}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cm}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cpf}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@c1}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cs}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\def\PYGZbs{\char`\\}
\def\PYGZus{\char`\_}
\def\PYGZob{\char`\{}
\def\PYGZcb{\char`\}}
\def\PYGZca{\char`\^}
\def\PYGZam{\char`\&}
\def\PYGZlt{\char`\<}
\def\PYGZgt{\char`\>}
\def\PYGZsh{\char`\#}
\def\PYGZpc{\char`\%}
\def\PYGZdl{\char`\$}
\def\PYGZhy{\char`\-}
\def\PYGZsq{\char`\'}
\def\PYGZdq{\char`\"}
\def\PYGZti{\char`\~}
% for compatibility with earlier versions
\def\PYGZat{@}
\def\PYGZlb{[}
\def\PYGZrb{]}
\makeatother