Posted By

philm on 01/13/14


Tagged

up c# Geared


Versions (?)

Geared Up - Boss Logic Script


 / Published in: C#
 

This is the boss logic script from a game called Geared Up, a grad show project at Algonquin college.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BossLogicScript : MonoBehaviour {
  5.  
  6. //For the Swarm
  7. bool isSpawningSwarm;
  8. int swarmSize;
  9. int spawnsLeft;
  10. float spawnInterval;
  11. float spawnTimer;
  12.  
  13. public GameObject SwarmEnemy; // the prefab for a swarm enemy
  14.  
  15. //For Swarm Door
  16. public GameObject backDoor;
  17.  
  18.  
  19. //For Cannons
  20. public GameObject cannon1;
  21. public GameObject cannon2;
  22. public GameObject cannon3;
  23. public GameObject cannon4;
  24.  
  25. private BossCannonScript cannon1Script;
  26. private BossCannonScript cannon2Script;
  27. private BossCannonScript cannon3Script;
  28. private BossCannonScript cannon4Script;
  29.  
  30. private float timeBetweenShots;
  31. private float shotTimer;
  32. private bool isShooting;
  33.  
  34.  
  35. //For different phase movements
  36. private Vector3 targetPosition;
  37. private Vector3 startPosition;
  38. private Vector3 cannonPhasePosition;
  39. private Vector3 spawnPhasePosition;
  40. private Quaternion initialRotation;
  41. private Quaternion swarmRotation;
  42. private float speed;
  43.  
  44. private bool isTorotate;
  45.  
  46. //so you can access the health script.
  47. HealthScript m_HealthScript;
  48.  
  49.  
  50. //for Ram Phase
  51. private bool isRamming;
  52. private int RamDirection;
  53. private bool hasRammedPlatform;
  54.  
  55.  
  56. //for DriveBy Phase
  57. private bool isDriveby;
  58. private int DriveSide;
  59.  
  60.  
  61. // Use this for initialization
  62. void Start ()
  63. {
  64. //Setup swarm variables.
  65. isSpawningSwarm = false;
  66. swarmSize = 50;
  67. spawnsLeft = 0;
  68. spawnInterval = 0.1f;
  69. spawnTimer = 0.0f;
  70.  
  71.  
  72. cannon1Script = cannon1.GetComponent("BossCannonScript") as BossCannonScript;
  73. cannon2Script = cannon2.GetComponent("BossCannonScript") as BossCannonScript;
  74. cannon3Script = cannon3.GetComponent("BossCannonScript") as BossCannonScript;
  75. cannon4Script = cannon4.GetComponent("BossCannonScript") as BossCannonScript;
  76. timeBetweenShots = 2.0f;
  77. shotTimer = 0.0f;
  78. isShooting = false;
  79.  
  80. //initial position
  81. startPosition = new Vector3(50,-30,150);
  82. transform.position = startPosition;
  83. transform.Rotate(new Vector3(0,1,0),-90);
  84. initialRotation = transform.rotation;
  85. swarmRotation = Quaternion.Euler(0,-180,0);
  86.  
  87. cannonPhasePosition = new Vector3(transform.position.x,transform.position.y,0);
  88. spawnPhasePosition = new Vector3(cannonPhasePosition.x + 70,cannonPhasePosition.y,cannonPhasePosition.z);
  89.  
  90. isTorotate = false;
  91.  
  92. //Setup Health Script
  93. m_HealthScript = this.gameObject.GetComponent("HealthScript") as HealthScript;
  94. m_HealthScript.AddHealth += new HealthScript.AddHealthDelegate(AddHealth);
  95. m_HealthScript.TakeDamage += new HealthScript.TakeDamageDelegate(TakeDamage);
  96. m_HealthScript.Death += new HealthScript.DeathDelegate(Death);
  97.  
  98.  
  99. isRamming = false;
  100. hasRammedPlatform = false;
  101. RamDirection = 0;
  102.  
  103. Random.seed = System.DateTime.Now.Second;
  104. }
  105.  
  106.  
  107. // Update is called once per frame
  108. void Update ()
  109. {
  110. //If the boss still has a phase to do.
  111. if(GlobalGameState.Instance.bossPhaseList.Count != 0)
  112. {
  113. //call the function for the respective next phase.
  114. switch(GlobalGameState.Instance.bossPhaseList[0])
  115. {
  116. case 0:
  117. MoveAndRemovePhase();
  118. break;
  119. case 1:
  120. CannonPhase();
  121. break;
  122. case 2:
  123. SwarmPhase();
  124. break;
  125. case 3:
  126. RamPhase();
  127. break;
  128. case 4:
  129. DriveByPhase();
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. //In case the boss didnt remove himself properly during the final phase.
  136. targetPosition = new Vector3(150, transform.position.y, transform.position.z);
  137. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  138.  
  139. if(transform.position == targetPosition)
  140. {
  141. Destroy(this.gameObject);
  142. }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. if(isShooting) //Handles all the shooting.
  149. {
  150. if(shotTimer < Time.time)
  151. {
  152. int picker = Random.Range(1,5);//1 to 4 since max in exclusive.
  153. Debug.Log(picker);
  154. switch(picker)
  155. {
  156. case 1:
  157. if(cannon1Script.isCharging == false)
  158. {
  159. cannon1Script.Fire();
  160. shotTimer = Time.time + timeBetweenShots;
  161. }
  162. break;
  163. case 2:
  164. if(cannon2Script.isCharging == false)
  165. {
  166. cannon2Script.Fire();
  167. shotTimer = Time.time + timeBetweenShots;
  168. }
  169. break;
  170. case 3:
  171. if(cannon3Script.isCharging == false)
  172. {
  173. cannon3Script.Fire();
  174. shotTimer = Time.time + timeBetweenShots;
  175. }
  176. break;
  177. case 4:
  178. if(cannon4Script.isCharging == false)
  179. {
  180. cannon4Script.Fire();
  181. shotTimer = Time.time + timeBetweenShots;
  182. }
  183. break;
  184. }
  185. }
  186. }
  187.  
  188. }
  189.  
  190.  
  191. public void SpawnOne()
  192. {
  193. //Create a swarmprefab and set its target.
  194. GameObject swarmUnit = ((GameObject)GameObject.Instantiate (SwarmEnemy, backDoor.gameObject.transform.position - new Vector3(-5,-10,0), transform.rotation));
  195. }
  196.  
  197.  
  198. private void CannonPhase()
  199. {
  200. targetPosition = cannonPhasePosition;
  201. speed = 10.0f;
  202.  
  203. //if in position set isShooting to true else move into position
  204. if(transform.position == targetPosition)
  205. {
  206. isShooting = true;
  207. //Check the health if 0 switch phases and set shooting to false.
  208. if(m_HealthScript.currentHealth <= 0)
  209. {
  210. isShooting = false;
  211. GlobalGameState.Instance.bossPhaseList.Remove(GlobalGameState.Instance.bossPhaseList[0]);
  212. }
  213. }
  214. else
  215. {
  216. isShooting = false;
  217. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  218. m_HealthScript.currentHealth = m_HealthScript.maxHealth;
  219. }
  220.  
  221.  
  222. }//End of CannonPhase()
  223.  
  224.  
  225.  
  226. private void SwarmPhase()
  227. {
  228. speed = 10.0f;
  229. float rotSpeed = 1.0f;
  230.  
  231. if(transform.position == startPosition)
  232. {
  233. targetPosition = cannonPhasePosition;
  234. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  235. isTorotate = false;
  236. }
  237. else if(transform.position == cannonPhasePosition)
  238. {
  239. targetPosition = spawnPhasePosition;
  240. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  241. isTorotate = true;
  242. }
  243. else if( transform.position == spawnPhasePosition)
  244. {
  245. if(isSpawningSwarm == false)
  246. {
  247. isSpawningSwarm = true;
  248. spawnsLeft = swarmSize;
  249. spawnTimer = 0.0f;
  250. }
  251. else
  252. {
  253. spawnTimer += Time.deltaTime;
  254.  
  255. if(spawnTimer > spawnInterval && spawnsLeft > 0)
  256. {
  257. SpawnOne(); //spawn one unit.
  258. spawnsLeft--;
  259. spawnTimer -= spawnInterval;
  260. }
  261. else if(spawnsLeft <= 0)
  262. {
  263. isSpawningSwarm = false;
  264. GlobalGameState.Instance.bossPhaseList.Remove(GlobalGameState.Instance.bossPhaseList[0]);
  265. }
  266. }
  267. }
  268. else
  269. {
  270. if(isTorotate)
  271. {
  272. transform.rotation = Quaternion.Lerp(transform.rotation,swarmRotation,Time.deltaTime * rotSpeed);
  273. }
  274. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  275. }
  276.  
  277.  
  278.  
  279. }//End of SwarmPhase()
  280.  
  281.  
  282.  
  283.  
  284. private void MoveAndRemovePhase()
  285. {
  286. //Get off screen and remove yourself
  287. targetPosition = new Vector3(150, transform.position.y, transform.position.z);
  288. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  289.  
  290. if(transform.position == targetPosition)
  291. {
  292. Destroy(this.gameObject);
  293. }
  294. }//End of MoveAndRemovePhase()
  295.  
  296.  
  297. private void RamPhase()
  298. {
  299.  
  300. if(isRamming == false)
  301. {
  302. RamDirection = Random.Range(1,3);
  303. isRamming = true;
  304. speed = 20.0f;
  305. hasRammedPlatform = false;
  306.  
  307.  
  308. if(RamDirection == 1) //Top
  309. {
  310. transform.position = new Vector3(-45,-16,200);
  311. targetPosition = new Vector3(transform.position.x, transform.position.y, -150);
  312. transform.rotation = Quaternion.Euler(0,-90,0);
  313. GameObject.Instantiate(Resources.Load ("Prefabs/Popups/IncomingDown"), new Vector3(-47,5,30), Quaternion.Euler(-90,180,0));
  314. cannon1Script.faceLeft();
  315. }
  316. else //Bottom
  317. {
  318. transform.position = new Vector3(-45,-16,-230);
  319. targetPosition = new Vector3(transform.position.x, transform.position.y, 180);
  320. transform.rotation = Quaternion.Euler(0,90,0);
  321. GameObject.Instantiate(Resources.Load ("Prefabs/Popups/IncomingUp"), new Vector3(-45,5,-38), Quaternion.Euler(-90,180,0));
  322. cannon1Script.faceRight();
  323. }
  324. }
  325. else
  326. {
  327. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  328. }
  329.  
  330.  
  331. if(transform.position == targetPosition)
  332. {
  333.  
  334. GlobalGameState.Instance.bossPhaseList.Remove(GlobalGameState.Instance.bossPhaseList[0]);
  335. isRamming = false;
  336. }
  337. }//End of RamPhase()
  338.  
  339. private void DriveByPhase()
  340. {
  341. if(isDriveby == false)
  342. {
  343. DriveSide = Random.Range(1,3);
  344. isDriveby = true;
  345. speed = 10.0f;
  346. hasRammedPlatform = false;
  347.  
  348. if(DriveSide == 1) //Top
  349. {
  350. transform.position = new Vector3(100,0,15);
  351. targetPosition = new Vector3(transform.position.x - 400, transform.position.y, transform.position.z);
  352. transform.rotation = Quaternion.Euler(0,0,0);
  353. }
  354. else //Bottom
  355. {
  356. transform.position = new Vector3(100,0,-40);
  357. targetPosition = new Vector3(transform.position.x - 400, transform.position.y, transform.position.z);
  358. transform.rotation = Quaternion.Euler(0,0,0);
  359. }
  360. }
  361. else
  362. {
  363. transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  364. }
  365.  
  366.  
  367. if(transform.position == targetPosition)
  368. {
  369. GlobalGameState.Instance.bossPhaseList.Remove(GlobalGameState.Instance.bossPhaseList[0]);
  370. isRamming = false;
  371. }
  372. }//End of DriveByPhase()
  373.  
  374.  
  375. float TakeDamage(float damage, GameObject sender)
  376. {
  377. return damage;
  378. }
  379.  
  380. float AddHealth(float amount, GameObject sender)
  381. {
  382. return amount;
  383. }
  384.  
  385.  
  386. void Death(GameObject sender)
  387. {
  388.  
  389. }
  390.  
  391.  
  392. void OnCollisionEnter(Collision collider)
  393. {
  394. if(collider.gameObject.tag == "Platform")
  395. {
  396. if(GlobalGameState.Instance.bossPhaseList[0] == 1 || GlobalGameState.Instance.bossPhaseList[0] == 2)
  397. {
  398. ((HealthScript)collider.gameObject.GetComponent("HealthScript")).ApplyDamage(1.0f, this.gameObject);
  399. }
  400.  
  401.  
  402. if(GlobalGameState.Instance.bossPhaseList[0] == 3)
  403. {
  404. if(hasRammedPlatform == false)
  405. {
  406. ((HealthScript)collider.gameObject.GetComponent("HealthScript")).ApplyDamage(20.0f, this.gameObject);
  407. hasRammedPlatform = true;
  408. }
  409.  
  410. float z = 0;
  411. if(RamDirection == 1)
  412. {
  413. z = -1;
  414. }
  415. else
  416. {
  417. z = 1;
  418. }
  419. ((Platform_Logic)collider.gameObject.GetComponent("Platform_Logic")).Bump(0.2f,new Vector3(1,0,z),3.0f);
  420. }
  421.  
  422.  
  423. if(GlobalGameState.Instance.bossPhaseList[0] == 4)
  424. {
  425. if(hasRammedPlatform == false)
  426. {
  427. ((HealthScript)collider.gameObject.GetComponent("HealthScript")).ApplyDamage(20.0f, this.gameObject);
  428. hasRammedPlatform = true;
  429. }
  430.  
  431. float z = 0;
  432. if(DriveSide == 1)
  433. {
  434. z = -1;
  435. }
  436. else
  437. {
  438. z = 1;
  439. }
  440. ((Platform_Logic)collider.gameObject.GetComponent("Platform_Logic")).Bump(0.2f,new Vector3(-1,0,z),3.0f);
  441. }
  442.  
  443. }
  444. }
  445. }

Report this snippet  

You need to login to post a comment.