/// <summary> /// This function is for dealing with shield-ship or shield-wreckage collides. /// </summary> /// <param name="x"></param> void OnTriggerStay2D(Collider2D x) { if (x.gameObject.tag != "Enemy" && x.gameObject.tag != "Wreckage") { return; } // Colliding damage is defined here. float dmgps = 3000; float dmg = dmgps * Time.fixedDeltaTime; sp -= dmg; // *Not* RevieveDamage. SpecObject s = x.gameObject.GetComponent <SpecObject>(); if (s != null) { s.RecieveDamage(DamageType.Energy, dmg); Player player = this.transform.parent.gameObject.GetComponent <Player>(); player.v = (Vector2)(this.gameObject.transform.parent.position - x.gameObject.transform.position).normalized; } // **No physics effects yet.** // Shuold have some special FX when colliding. }
void OnTriggerStay2D(Collider2D x) { if (x.gameObject.tag != "Enemy" && x.gameObject.tag != "Wreckage") { return; } if (!shield.shutdown) { return; // Shield will protect it from collide. } // Colliding damage is defined here. float dmgps = 3000; float dmg = dmgps * Time.fixedDeltaTime; hp -= dmg; // *Not* RevieveDamage. SpecObject s = x.gameObject.GetComponent <SpecObject>(); if (s != null) { s.RecieveDamage(DamageType.Energy, dmg * 2f); v = (Vector2)(this.gameObject.transform.position - x.gameObject.transform.position).normalized * 2f; } // **No physics effects yet.** }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="loc">The correct hit location.</param> void Hit(Collider2D x, Vector2 loc) { GameObject t = x.gameObject; // Should do nothing with the friendly. if (this.gameObject.tag == "AllyCannonball") { if (t.tag == "Ally" || t.tag == "AllyCannonball" | t.tag == "Player") { return; } } else if (this.gameObject.tag == "EnemyCannonball") { if (t.tag == "Enemy" || t.tag == "EnemyCannonball") { return; } } /*else if(this.gameObject.tag != "UsedCannonball") * { * Debug.Log("WARNING: Cannonball tag is not set properly : " + this.gameObject.name); * return; * }*/ if (hitted) { return; // This gameobjcet will be destroyed when timeout in FixedUpdate. } // Notice that all *can-be-hit* cannonball & missiles *should* mount a SpecObject. SpecObject s = t.GetComponent <SpecObject>(); if (s == null) { return; } s.RecieveDamage(damageType, damage); this.GetComponent <AudioSource>().Play(); // Not to destroy this object immediatly for playing sounds. //Destroy(this.gameObject); Collider2D cd = this.gameObject.GetComponent <Collider2D>(); if (cd != null) { Destroy(cd); } SpriteRenderer rd = this.gameObject.GetComponent <SpriteRenderer>(); if (rd != null) { Destroy(rd); } this.gameObject.tag = "UsedCannonball"; hitted = true; // FX objects... if (explodeFX != null) { // Maintain direction... Vector3 dir; float a; this.gameObject.transform.rotation.ToAngleAxis(out a, out dir); if (dir.z < 0) { dir = -dir; a = -a; } Vector2 vdir; vdir.y = Mathf.Min(Mathf.Cos(a * Mathf.Deg2Rad) * speed, 0f); vdir.x = -Mathf.Sin(a * Mathf.Deg2Rad) * speed; if (vdir.magnitude > Global.flySpeed * 0.5f) { vdir = vdir.normalized * Global.flySpeed * 0.5f; } // Create and assign fx objects... for (int i = 0; i < explodeFX.Length; i++) { GameObject fx = Instantiate(explodeFX[i]); fx.transform.position = loc; Flasher fl = fx.GetComponent <Flasher>(); if (fl != null) { fl.baseSpeed = vdir; } } } // particle objects... if (explodeParticles != null) { for (int i = 0; i < explodeParticles.Length; i++) { GameObject pc = Instantiate(explodeParticles[i]); pc.transform.position = loc; // Turn the color to red as enemies' ship damaged. SpriteRenderer prd = pc.GetComponent <SpriteRenderer>(); Shield sd = x.gameObject.GetComponent <Shield>(); if (prd != null && sd == null) { SpriteRenderer spr = s.gameObject.GetComponent <SpriteRenderer>(); if (spr != null) { float hrate = 0.5f + 0.5f * s.hp / s.hpMax; prd.color = new Color( spr.color.r, spr.color.g * hrate, spr.color.b * hrate, spr.color.a); } } } } }