Rename year directories to allow natural ordering
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,79 @@
|
||||
%! 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}}{}
|
||||
|
||||
\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{Assignment 03 -- Starting a Proper Asteroids Game}
|
||||
\end{center}
|
||||
\hrule
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/GameManager.cs}
|
||||
\caption{\texttt{GameManager.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/Resources/Asteroid.cs}
|
||||
\caption{\texttt{Asteroid.cs}}
|
||||
\end{code}
|
||||
|
||||
\end{document}
|
@ -0,0 +1,74 @@
|
||||
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) {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour {
|
||||
|
||||
// inspector settings
|
||||
public GameObject asteroidPrefab;
|
||||
public GameObject spaceship;
|
||||
|
||||
// class-level statics
|
||||
public static GameManager instance;
|
||||
public static int currentGameLevel;
|
||||
public static Vector3 screenBottomLeft, screenTopRight;
|
||||
public static float screenWidth, screenHeight;
|
||||
//
|
||||
|
||||
// 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 void CreatePlayerSpaceship() {
|
||||
Instantiate(spaceship);
|
||||
spaceship.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Spaceship : MonoBehaviour
|
||||
{
|
||||
public GameObject spaceship;
|
||||
public float speed = 5.0f;
|
||||
public float rotationalSpeed = 2.0f;
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,84 @@
|
||||
%! 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}}{}
|
||||
|
||||
\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/Spaceship.cs}
|
||||
\caption{\texttt{Spaceship.cs}}
|
||||
\end{code}
|
||||
|
||||
\end{document}
|
Binary file not shown.
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,104 @@
|
||||
%! 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 07}
|
||||
\end{center}
|
||||
\hrule
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/GameManager.cs}
|
||||
\caption{\texttt{GameManager.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/Resources/Asteroid.cs}
|
||||
\caption{\texttt{Asteroid.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/GUIScript.cs}
|
||||
\caption{\texttt{GUIScript.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/ScreenEdgeChecker.cs}
|
||||
\caption{\texttt{ScreenEdgeChecker.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/Spaceship.cs}
|
||||
\caption{\texttt{Spaceship.cs}}
|
||||
\end{code}
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/SpeedLimiter.cs}
|
||||
\caption{\texttt{SpeedLimiter.cs}}
|
||||
\end{code}
|
||||
|
||||
|
||||
\begin{code}
|
||||
\inputminted[texcl, mathescape, breaklines, frame=single, linenos]{csharp}{/home/andrew/code/Unity/Asteroids/Assets/Resources/Bullet.cs}
|
||||
\caption{\texttt{Bullet.cs}}
|
||||
\end{code}
|
||||
\end{document}
|
Reference in New Issue
Block a user