SFE_shieldedController.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // this script handles a space object's HP and Shield parameters.
  2. #pragma strict
  3. var impulseForce:float=10;
  4. var HPmin:float=3;
  5. var HPmax:float=6;
  6. var shieldMin:float=5;
  7. var shieldMax:float=5;
  8. var shieldObject:GameObject;
  9. var shieldGraphicsStuff:String="---------------------------------";
  10. var normalShieldGfxPower:float=1;
  11. var onHitShieldGfxPower:float=10;
  12. var onHitShieldGfxCooldownSpeed:float=1;
  13. private var power:float;
  14. var onHitShieldGenerate:GameObject;
  15. var onDestroyShieldGenerate:GameObject;
  16. private var HP:float;
  17. private var shield:float;
  18. var explosion:GameObject;
  19. function Start () {
  20. //setting HP and shield parameters between random values
  21. HP=Random.Range(HPmin, HPmax);
  22. shield=Random.Range(shieldMin, shieldMax);
  23. power=normalShieldGfxPower;
  24. shieldObject.renderer.material.SetFloat("_AllPower", normalShieldGfxPower);
  25. }
  26. function Update () {
  27. if (shieldObject)
  28. {
  29. shieldObject.renderer.material.SetFloat("_AllPower", power);
  30. if (power>normalShieldGfxPower) power-=Time.deltaTime*onHitShieldGfxCooldownSpeed;
  31. if (power<normalShieldGfxPower) power=normalShieldGfxPower;
  32. }
  33. }
  34. function OnCollisionEnter(collision : Collision) {
  35. if (shieldObject) power=onHitShieldGfxPower;
  36. /*
  37. this basically does the following:
  38. checks if there are shields remaining, if no, then the damage goes to HP
  39. else it gets substracted from the shield, and if the shield is below 0, then destroys the shield
  40. it is not super great as damage now doesn't "overbleed" through the shield; even if a 10 strength attack hits
  41. an 1 strength shield, the shield still completely blocks it before being destroyed, but this whole
  42. scene is just an effect demo package, so meh
  43. */
  44. if (shield<=0)
  45. {
  46. if (collision.gameObject.GetComponent(SFE_BulletController))
  47. HP-=collision.gameObject.GetComponent(SFE_BulletController).damage;
  48. if (collision.gameObject.GetComponent(SFE_LaserController))
  49. HP-=collision.gameObject.GetComponent(SFE_LaserController).damage;
  50. }
  51. if (shield>0)
  52. {
  53. if ((shield>0) && (onHitShieldGenerate)) {
  54. var contact = collision.contacts[0];
  55. var rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
  56. var pos = contact.point;
  57. Instantiate(onHitShieldGenerate, pos, rot);
  58. }
  59. if (collision.gameObject.GetComponent(SFE_BulletController))
  60. shield-=collision.gameObject.GetComponent(SFE_BulletController).damage;
  61. if (collision.gameObject.GetComponent(SFE_LaserController))
  62. shield-=collision.gameObject.GetComponent(SFE_LaserController).damage;
  63. if (shield<=0) {
  64. Destroy(shieldObject);
  65. if (onDestroyShieldGenerate) Instantiate(onDestroyShieldGenerate, transform.position, transform.rotation);
  66. }
  67. }
  68. if (HP<=0) //yep, if the object does not have any HP left, it gets destroyed, and an explosion is created
  69. {
  70. Instantiate(explosion, transform.position, transform.rotation);
  71. Destroy (gameObject);
  72. }
  73. }