Steffan's Game Development Portfolio

See Projects Go to git repositories
Waspslayer
January 2016 - February 2016

Waspslayer was a little side project I worked on during the second semester of the first year after I finished the pong project.
The concept of the game was inspired by my holiday during wasp season, I tried everything to get rid of these little buggers. After getting stung multiple times I came up with the idea to make a game in which you could kill wasps all you like without getting stung.

External Links
Waspslayer

Game WebGL build
The game was made for mobile devices, so I recommend playing it on a mobile device for an optimal experience.



GameController
The GameController class is a singleton class that contains all the important variables in the game and handles saving game data to a file and loading it when needed

GameController.cs
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.IO;
  8.  
  9. public class GameController : MonoBehaviour {
  10.  
  11.     //Access to class for all other classes and singleton, which makes sure the first gamecontroller to be inited to be kept over all the scenes and that gamecontrollers in later loaded scenes get destroyes
  12.     private static GameController instance = null;
  13.     public static GameController Instance
  14.     {
  15.         get { return instance; }
  16.     }
  17.  
  18.     //Variables
  19.     public int minLivingWasps = 10;
  20.     public int maxWasps = 200;
  21.     public int coins = 0;
  22.     public int pointsPerKil = 100;
  23.     public int maxMisClicks = 5;
  24.     public float minTimeBetweenHives = 20.0F;
  25.     public float maxTimeBetweenHives = 50.0F;
  26.     private float timeSinceLastHive = 0.0F;
  27.     public List<int> purchasedWeapons = new List<int> { 0 };
  28.     public Image weaponImageFrame;
  29.     public Weapon selectedWeapon = null;
  30.     public GameObject waspPrefab;
  31.     public GameObject hivePrefab;
  32.  
  33.     //Start game controller, run singleton check and run the load funtion
  34.     void Awake()
  35.     {
  36.         if (instance != null && instance != this)
  37.         {
  38.             Destroy(this.gameObject);
  39.             return;
  40.         }
  41.         else
  42.         {
  43.             instance = this;
  44.         }
  45.         DontDestroyOnLoad(this);
  46.  
  47.         //load settings from the save file
  48.         Load();
  49.         SetCoinsToText();
  50.         Weapon[] weapons = GameObject.FindObjectsOfType<Weapon>();
  51.         foreach (Weapon weapon in weapons) {
  52.             if (weapon.weaponID == 0 && selectedWeapon == null) {
  53.                 selectedWeapon = weapon;
  54.             }
  55.         }
  56.     }
  57.  
  58.     //Save file onDisable
  59.     void OnDisable(){
  60.         SaveData();
  61.     }
  62.  
  63.     // Update is called once per frame
  64.     void Update () {
  65.         spawnWasps();
  66.         timeSinceLastHive += Time.deltaTime;
  67.         if (timeSinceLastHive > maxTimeBetweenHives) {
  68.             spawnHive();
  69.         }
  70. 	}
  71.  
  72.     //Spawns wasps if the conditions are right
  73.     void spawnWasps() {
  74.         GameObject[] wasps = GameObject.FindGameObjectsWithTag("Wasp");
  75.         int livingWasps = 0;
  76.         foreach (GameObject wasp in wasps) {
  77.             Wasp script = wasp.GetComponent<Wasp>();
  78.             if (script.dead == false) {
  79.                 livingWasps++;
  80.             }
  81.         }
  82.         if (livingWasps < minLivingWasps) {
  83.             for (int i = 1; i <= (minLivingWasps - livingWasps); i++) {
  84.                 spawnWasp(waspPrefab.transform);
  85.  
  86.             }
  87.         }
  88.     }
  89.  
  90.     //Spawns an Hive, if the conditions are right
  91.     public void spawnHive() {
  92.         GameObject[] wasps = GameObject.FindGameObjectsWithTag("Wasp");
  93.         int livingWasps = 0;
  94.         foreach (GameObject wasp in wasps)
  95.         {
  96.             Wasp script = wasp.GetComponent<Wasp>();
  97.             if (script.dead == false)
  98.             {
  99.                 livingWasps++;
  100.             }
  101.         }
  102.         if (maxWasps > livingWasps) {
  103.             if (timeSinceLastHive > minTimeBetweenHives) {
  104.                 Instantiate(hivePrefab, hivePrefab.transform.position, hivePrefab.transform.rotation);
  105.                 timeSinceLastHive = 0;
  106.             }
  107.         }
  108.     }
  109.     //Spawn a Wasp
  110.     public void spawnWasp(Transform trans) {
  111.         GameObject wasp = (GameObject) Instantiate(waspPrefab, trans.position, trans.rotation);
  112.         wasp.transform.GetComponentInChildren<SphereCollider>().radius = selectedWeapon.hitRange;
  113.     }
  114.  
  115.  
  116.     //Write coin score to textfield
  117.     public void SetCoinsToText() {
  118.         Text coinsField = null;
  119.         coinsField = GameObject.FindGameObjectWithTag("CoinText").GetComponent<Text>();
  120.         if (coinsField != null) {
  121.             coinsField.text = coins.ToString();
  122.         }
  123.     }
  124.     //Select a new weapon
  125.     public void SetWeapon(Weapon weapon) {
  126.         selectedWeapon = weapon;
  127.         GameObject[] waspColliders = GameObject.FindGameObjectsWithTag("WaspRayCollider");
  128.         foreach (GameObject waspCollider in waspColliders) {
  129.             waspCollider.GetComponent<SphereCollider>().radius = weapon.hitRange;
  130.         }
  131.         weaponImageFrame.sprite = weapon.weaponImage;
  132.     }
  133.  
  134.     //Data file
  135.  
  136.     //Write data to file
  137.     public void SaveData()
  138.     {
  139.         BinaryFormatter bf = new BinaryFormatter();
  140.         FileStream file = File.Open(Application.persistentDataPath + "/gameData.dat", FileMode.OpenOrCreate);
  141.         GameData data = new GameData(coins, selectedWeapon.weaponID, purchasedWeapons);
  142.         bf.Serialize(file, data);
  143.         file.Close();
  144.     }
  145.  
  146.     //Load data from file
  147.     public void Load()
  148.     {
  149.         if (File.Exists(Application.persistentDataPath + "/gameData.dat"))
  150.         {
  151.             BinaryFormatter bf = new BinaryFormatter();
  152.             FileStream file = File.Open(Application.persistentDataPath + "/gameData.dat", FileMode.Open);
  153.             GameData data = (GameData)bf.Deserialize(file);
  154.             file.Close();
  155.  
  156.             coins = data.GetCoins();
  157.             purchasedWeapons = data.GetPurchasedWeapons();
  158.             Weapon[] weapons = GameObject.FindObjectsOfType<Weapon>();
  159.             foreach (Weapon weapon in weapons)
  160.             {
  161.                 if (weapon.weaponID == data.GetWeaponID() && selectedWeapon == null)
  162.                 {
  163.                     selectedWeapon = weapon;
  164.                     weaponImageFrame.sprite = weapon.weaponImage;
  165.                     weapon.infoBG.color = weapon.selectColor;
  166.                     weapon.itemBG.color = weapon.selectColor;
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172. }
  173.  
  174. //Data class, to be written to file
  175. [Serializable]
  176. class GameData
  177. {
  178.     private int coins = 0;
  179.     private int weaponID = 0;
  180.     private List<int> purchasedWeapons = new List<int> {0};
  181.  
  182.     public GameData(int coins, int weaponID, List<int> purchasedWeapons)
  183.     {
  184.         this.coins = coins;
  185.         this.weaponID = weaponID;
  186.         this.purchasedWeapons = purchasedWeapons;
  187.     }
  188.  
  189.     //Access to private fields from other classes
  190.     public int GetCoins() {
  191.         return coins;
  192.     }
  193.     public int GetWeaponID() {
  194.         return weaponID;
  195.     }
  196.     public List<int> GetPurchasedWeapons() {
  197.         return purchasedWeapons;
  198.     }
  199. }
GameController.cs


Wasp Class
The wasp class controls the flight of the wasp and let's it pick a random item from a list with points it can fly to every now and then

Wasp.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Wasp : MonoBehaviour
  5. {
  6.     public float maxSpeed = 250.0F;
  7.     public float startSpeed = 10.0F;
  8.     public float acceleration = 50.0F;
  9.     public float rotationSpeed = 3.0F;
  10.     public bool dead = false;
  11.     public float timeBeforeDestroy = 10.0F;
  12.     public AudioClip splat;
  13.     public AudioClip lightningSound;
  14.     private bool splatPlayed = false;
  15.     private float deadSince = 0.0F;
  16.     public Vector3 start;
  17.     public GameObject flyPoint;
  18.     private Vector3 directionVector;
  19.     private float speed = 0F;
  20.     // Use this for initialization
  21.     void Start()
  22.     {
  23.         speed = startSpeed;
  24.         start = transform.position;
  25.         flyPoint = getRandomFlyPoint();
  26.         transform.LookAt(flyPoint.transform.position);
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if (speed < maxSpeed) {
  33.             if (speed + acceleration * Time.deltaTime < maxSpeed)
  34.             {
  35.                 speed += acceleration * Time.deltaTime;
  36.             }
  37.             else {
  38.                 speed = maxSpeed;
  39.             }
  40.         }
  41.  
  42.         if (!dead)
  43.         {
  44.             if (Vector3.Distance(start, flyPoint.transform.position) > Vector3.Distance(start, transform.position))
  45.             {
  46.                 Quaternion targetRotation = Quaternion.LookRotation(flyPoint.transform.position - transform.position);
  47.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
  48.                 Vector3 directionVector = transform.forward;
  49.                 Vector3 aPos = new Vector3(directionVector.x * Time.deltaTime * speed, directionVector.y * Time.deltaTime * speed, directionVector.z * Time.deltaTime * speed);
  50.                 transform.position += aPos;
  51.             }
  52.             else
  53.             {
  54.                 start = transform.position;
  55.                 flyPoint = getRandomFlyPoint();
  56.             }
  57.         }
  58.         else
  59.         {
  60.             deadSince += Time.deltaTime;
  61.             Vector3 rotation = transform.rotation.eulerAngles;
  62.             if (deadSince < 1)
  63.             {
  64.                 rotation.z = 180;
  65.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(rotation), Time.deltaTime * 5);
  66.                 if (!splatPlayed) {
  67.                     AudioSource audio = GetComponentInChildren<AudioSource>();
  68.                     audio.loop = false;
  69.                     if (GameController.Instance.selectedWeapon.deathEffect == MouseTouchHandler.DeathEffect.Blood) {
  70.                         audio.PlayOneShot(splat, 1);
  71.                         splatPlayed = true;
  72.                     } else if (GameController.Instance.selectedWeapon.deathEffect == MouseTouchHandler.DeathEffect.Lightning) {
  73.                         audio.PlayOneShot(lightningSound, 1);
  74.                         splatPlayed = true;
  75.                     }
  76.                 }
  77.  
  78.             }
  79.             if (deadSince > timeBeforeDestroy) {
  80.                 Destroy(gameObject);
  81.             }
  82.         }
  83.     }
  84.  
  85.     GameObject getRandomFlyPoint()
  86.     {
  87.         GameObject[] flyPoints = GameObject.FindGameObjectsWithTag("FlyPoint");
  88.         int random = (int)Mathf.Round(Random.value * (flyPoints.Length - 1));
  89.         return flyPoints[random];
  90.  
  91.     }
  92. }
Wasp.cs