Steffan's Game Development Portfolio

See Projects Go to git repositories
Real Time Strategy Game
August 2016 - November 2016

The Race Game was my assignment for the first semester of the second year during my education as Game Developer at ROC Friese poort. We had to make an RTS that was somewhat like Age of Empires or Starcraft. The focus of this project was on using inheritance.
Due to lack of time I sadly couldn't fully finish it before we had to start on the next project

External Links
RTS

Worker
The worker in a RTS game needs a lot of functionality. The worker builds buildings and collects resources
Worker.cs
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Worker : Unit {
  5.     public GameObject destination = null;
  6.     public Building currentBuilding = null;
  7.     public ResourceMine rm = null;
  8.     public AudioClip woodPlease = null;
  9.     public AudioClip foodPlease = null;
  10.     public AudioClip goldPlease = null;
  11.     public AudioClip stonePlease = null;
  12.     public List<AudioClip> waterSounds = new List<AudioClip>();
  13.     public List<AudioClip> foodSounds = new List<AudioClip>();
  14.     public List<AudioClip> miningSounds = new List<AudioClip>();
  15.     public List<AudioClip> woodSounds = new List<AudioClip>();
  16.     public float timeBetweenSounds = 1;
  17.     public float lastSound = 0;
  18.     AudioSource audioSource = null;
  19.     Resource inventory = new Resource(ResourceType.NONE, 0);
  20.     DockingPoint dp = null;
  21.     bool dockedAtResourceMine = false;
  22.     float timeTillResourceGathered = 0;
  23.  
  24.     protected override void Start()
  25.     {
  26.         base.Start();
  27.         audioSource = GetComponent<AudioSource>();
  28.     }
  29.  
  30.     protected override void Update()
  31.     {
  32.         base.Update();
  33.         if (Input.GetMouseButtonUp(1) && selected)
  34.         {
  35.             Ray ray = LevelController.Instance.mainCamera.ScreenPointToRay(Input.mousePosition);
  36.             RaycastHit rHit;
  37.             if (Physics.Raycast(ray, out rHit, 1000))
  38.             {
  39.                 ResourceMine resmine = rHit.collider.gameObject.GetComponent<ResourceMine>();
  40.                 if (resmine != null)
  41.                 {
  42.                     rm = resmine;
  43.                     ResourceType rt = resmine.resource.GetResourceType();
  44.                     switch (rt)
  45.                     {
  46.                         case ResourceType.FOOD:
  47.                             PlayAudioClip(foodPlease);
  48.                             break;
  49.                         case ResourceType.GOLD:
  50.                             PlayAudioClip(goldPlease);
  51.                             break;
  52.                         case ResourceType.IRON:
  53.                             PlayAudioClip(stonePlease);
  54.                             break;
  55.                         case ResourceType.ROCKS:
  56.                             PlayAudioClip(stonePlease);
  57.                             break;
  58.                         case ResourceType.WOOD:
  59.                             PlayAudioClip(woodPlease);
  60.                             break;
  61.                         default:
  62.                             break;
  63.                     }
  64.                     SetDestination(rm.gameObject);
  65.                 }
  66.             }
  67.         }
  68.         if (dockedAtResourceMine) {
  69.             ResourceType resT = rm.resource.GetResourceType();
  70.             switch (resT) {
  71.                 case ResourceType.FOOD:
  72.                     PlayRandomClipFromList(foodSounds);
  73.                     break;
  74.                 case ResourceType.GOLD:
  75.                     PlayRandomClipFromList(miningSounds);
  76.                     break;
  77.                 case ResourceType.IRON:
  78.                     PlayRandomClipFromList(miningSounds);
  79.                     break;
  80.                 case ResourceType.ROCKS:
  81.                     PlayRandomClipFromList(miningSounds);
  82.                     break;
  83.                 case ResourceType.WATER:
  84.                     PlayRandomClipFromList(waterSounds);
  85.                     break;
  86.                 case ResourceType.WOOD:
  87.                     PlayRandomClipFromList(woodSounds);
  88.                     break;
  89.                 default:
  90.                     break;
  91.  
  92.             }
  93.         }
  94.         if (dockedAtResourceMine && Time.time >= timeTillResourceGathered) {
  95.             Resource takenRes = this.rm.TakeResources();
  96.             dockedAtResourceMine = false;
  97.             timeTillResourceGathered = 0;
  98.             inventory = new Resource(takenRes.GetResourceType(), takenRes.GetAmount());
  99.             SetDestination(LevelController.Instance.players[playerID].GetClosestResourceDropOff(transform.position));
  100.         }
  101.     }
  102.  
  103.     public bool PlaceBuilding(GameObject g) {
  104.         SelectableUnit selUnit = g.GetComponent<SelectableUnit>();
  105.         Building b = g.GetComponent<Building>();
  106.         if (selUnit != null && b != null && b.placeHolderObject != null) {
  107.             if (LevelController.Instance.players[playerID].EnoughResources(selUnit.costs)) {
  108.                 GameObject g2 = (GameObject)Instantiate(b.placeHolderObject);
  109.                 BuildingPlaceHolder bp = g2.GetComponent<BuildingPlaceHolder>();
  110.                 if (bp != null)
  111.                 {
  112.                     bp.w = this;
  113.                 }
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.  
  119.     public void SetDestination(GameObject g) {
  120.         if (currentBuilding != null)
  121.         {
  122.             currentBuilding.RemoveWorker(this);
  123.             currentBuilding = null;
  124.         }
  125.         if (destination != null)
  126.         {
  127.             if (destination.GetComponent<BuildingPlaceHolder>() != null && !destination.GetComponent<BuildingPlaceHolder>().isStarted)
  128.                 Destroy(destination);
  129.             destination = null;
  130.         }
  131.         if (dp != null) {
  132.             dp.users--;
  133.             dp = null;
  134.         }
  135.         destination = g;
  136.         DockingPoints dps = g.GetComponent<DockingPoints>();
  137.         if (dps != null)
  138.         {
  139.             dp = dps.GetDockingPoint(transform);
  140.             dp.users++;
  141.             agent.SetDestination(dp.position + dps.transform.position);
  142.         }
  143.         else {
  144.             agent.SetDestination(g.transform.position);
  145.         }
  146.     }
  147.     void OnTriggerEnter(Collider c) {
  148.         if (destination != null) {
  149.             if (c.gameObject == destination) {
  150.                 agent.Stop();
  151.                 agent.ResetPath();
  152.                 BuildingPlaceHolder bp = c.gameObject.GetComponent<BuildingPlaceHolder>();
  153.                 Building b = c.gameObject.GetComponent<Building>();
  154.                 ResourceMine resmine = c.gameObject.GetComponent<ResourceMine>();
  155.                 destination = null;
  156.                 if (bp != null) {
  157.                     if (!bp.isStarted)
  158.                     {
  159.                         currentBuilding = bp.StartBuilding(this);
  160.                         currentBuilding.SetPlaceHolderBuild(bp.gameObject);
  161.                     }
  162.                     else {
  163.                         bp.AddWorker(this);
  164.                     }
  165.                 } else if (b != null) {
  166.                     if (!b.IsFinished())
  167.                     {
  168.                         b.AddWorker(this);
  169.                     } else if (b.resourceDropOff) {
  170.                         if (inventory.GetResourceType() != ResourceType.NONE)
  171.                         {
  172.                             if (rm != null) 
  173.                                 SetDestination(this.rm.gameObject);
  174.                             LevelController.Instance.players[playerID].GetResource(inventory.GetResourceType()).AddResources(inventory.GetAmount());
  175.                             inventory = new Resource(ResourceType.NONE, 0);
  176.                         }
  177.                     }
  178.                 } else if (resmine != null) {
  179.                     dockedAtResourceMine = true;
  180.                     timeTillResourceGathered = Time.time + rm.collectTime;
  181.                 }
  182.             }
  183.         }
  184.     }
  185.     void OnTriggerExit(Collider c) {
  186.         Building b = c.gameObject.GetComponent<Building>();
  187.         if (b != null) {
  188.             b.RemoveWorker(this);
  189.         }
  190.     }
  191.     public override void MoveTo(Vector3 location)
  192.     {
  193.         base.MoveTo(location);
  194.         if (rm != null)
  195.             rm = null;
  196.         if (dp != null)
  197.         {
  198.             dp.users--;
  199.             dp = null;
  200.         }
  201.         if (currentBuilding != null) {
  202.             currentBuilding.RemoveWorker(this);
  203.             currentBuilding = null;
  204.         }
  205.         if (destination != null) {
  206.             if (destination.GetComponent<BuildingPlaceHolder>() != null && !destination.GetComponent<BuildingPlaceHolder>().isStarted)
  207.                 Destroy(destination);
  208.             destination = null;
  209.         }
  210.     }
  211.     public void PlayAudioClip(AudioClip clip) {
  212.         if (audioSource != null) {
  213.             audioSource.PlayOneShot(clip);
  214.         }
  215.     }
  216.     public void PlayRandomClipFromList(List<AudioClip> clips) {
  217.         if (audioSource != null && clips.Count > 0 && Time.time - timeBetweenSounds > lastSound) {
  218.             if (!audioSource.isPlaying) {
  219.                 int random = Mathf.FloorToInt(Random.Range(0, clips.Count));
  220.                 audioSource.PlayOneShot(clips[random]);
  221.                 lastSound = Time.time;
  222.             }
  223.         }
  224.     }
  225. }
  226.  
Worker.cs