private void LaunchProjectile(WeaponProjectile projectile) { // Create the projectile if (projectile != null && this.CastProjectiles) { WeaponProjectile.Create( projectile, this, this.LaunchPoint, (this.CurrentDirection == Direction.Left ? -1 : 1)); } }
protected static void IgnoreOwnerCollisions(WeaponProjectile projectile, Character owner) { // Prevent hitting the player who cast it if (owner != null) { Collider2D[] colliders = owner.GetComponentsInChildren <Collider2D>(); Collider2D[] projectileColliders = projectile.GetComponentsInChildren <Collider2D>(); for (int i = 0; i < colliders.Length; i++) { for (int j = 0; j < projectileColliders.Length; j++) { Physics2D.IgnoreCollision(colliders[i], projectileColliders[j]); } } } }
/// <summary> /// Instantiate a new instance of the WeaponProjectile class using the supplied parameters /// </summary> /// <param name="instance">The instance to use as the base</param> /// <param name="owner">The character that owns this projectile</param> /// <param name="launchPoint">Where to spawn the projectile</param> /// <param name="directionX">The direction to move</param> /// <returns>The new projectile</returns> public static WeaponProjectile Create(WeaponProjectile instance, Character owner, Transform launchPoint, int directionX) { WeaponProjectile projectile = GameObject.Instantiate <WeaponProjectile>(instance); projectile.Owner = owner; // Set the start position projectile.launchPoint = launchPoint; projectile.transform.position = projectile.launchPoint.position; projectile.DirectionX = directionX; // Make sure we can't collide with the person who shot this projectile WeaponProjectile.IgnoreOwnerCollisions(projectile, owner); // Flip the sprite if necessary if (projectile.ShouldFlipDirection && directionX < 0) { Vector3 rotation = projectile.transform.localRotation.eulerAngles; rotation.y -= 180; projectile.transform.localEulerAngles = rotation; } return(projectile); }
protected override void Start() { base.Start(); this.spriteRenderer = this.GetComponentInChildren <SpriteRenderer>(); this.outlineSpriteRenderer = this.spriteRenderer.transform.GetChild(0).GetComponentInChildren <SpriteRenderer>(); // Get the sprite from the hand of the character GameObject weaponPart = null; Sprite weaponSprite = this.spriteRenderer.sprite; Sprite weaponOutlineSprite = this.outlineSpriteRenderer.sprite; // Search the character for the appropriate weapon sprite Collider2D weaponBox = this.GetComponentInChildren <Collider2D>(); if (this.Owner != null) { SpriteRenderer[] parts = this.Owner.GetComponentsInChildren <SpriteRenderer>(); for (int i = 0; i < parts.Length; i++) { if (parts[i].name == (this.IsMainItem ? "HeldItemMainSprite" : "HeldItemOffSprite")) { weaponPart = parts[i].gameObject; weaponSprite = parts[i].sprite; // Now search this weapon's children for the outline sprite for (int j = 0; j < parts[i].transform.childCount; j++) { var child = parts[i].transform.GetChild(j); if (child.name.EndsWith("-Outline")) { weaponOutlineSprite = child.GetComponentInChildren <SpriteRenderer>().sprite; break; } } weaponBox = null; break; } } } // Update our sprite to match the one we are throwing if (weaponSprite != null) { if (this.spriteRenderer != null) { this.spriteRenderer.sprite = weaponSprite; this.outlineSpriteRenderer.sprite = weaponOutlineSprite; this.outlineSpriteRenderer.color = this.Owner.GetComponent <PixelPartsOutline>().OutlineTint; this.outlineSpriteRenderer.enabled = this.Owner.GetComponent <PixelPartsOutline>().UseOutline; } } // If a collision box is not found, we generate one using PolygonCollider2D so that it fits the shape of the sprite if (weaponBox == null) { // Create a new collider that will take the shape of the sprite var spriteCollider = this.spriteRenderer.gameObject.AddComponent <PolygonCollider2D>(); spriteCollider.enabled = false; // Now copy it into different gameobjects that will not rotate var collisionO = new GameObject("CollisionOrigin"); collisionO.transform.SetParent(this.CollisionHolder, false); var cO = collisionO.gameObject.AddComponent <PolygonCollider2D>(); cO.points = spriteCollider.points; cO.enabled = true; var collisionR = new GameObject("CollisionRotated"); collisionR.transform.SetParent(this.CollisionHolder, false); var cR = collisionR.gameObject.AddComponent <PolygonCollider2D>(); cR.points = spriteCollider.points; cR.transform.localEulerAngles = new Vector3(0, 0, 90); cR.enabled = true; // Make the generated colliders a bit smaller this.CollisionHolder.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); // Center the sprite based on the new collider Rigidbody2D body = this.GetComponent <Rigidbody2D>(); if (body != null && weaponPart != null) { Vector3 dirScale = new Vector3(this.DirectionX, 1, 1); this.spriteRenderer.transform.localPosition = Vector3.Scale(-body.centerOfMass, dirScale); this.RotateTransform.localEulerAngles = new Vector3(0, 0, this.StartRotation); // If we are going to rotate, adjust the start position so we don't just hit the ground immediately if (this.RotationSpeed != 0 || this.StartRotation != 0) { var minY = Mathf.Min(cO.bounds.extents.y, cR.bounds.extents.y) * 2; var maxY = Mathf.Max(cO.bounds.extents.y, cR.bounds.extents.y) * 2; if (this.RotationSpeed != 0 && this.launchPoint.localPosition.y < maxY) { this.transform.localPosition += Vector3.up * (maxY - this.launchPoint.localPosition.y); } else if (this.RotationSpeed == 0 && this.launchPoint.localPosition.y < minY) { this.transform.localPosition += Vector3.up * (minY - this.launchPoint.localPosition.y); } else if (this.RotationSpeed == 0) { if (this.StartRotation != 0) { if (this.launchPoint.localPosition.y < cR.bounds.extents.y * 2) { this.transform.localPosition += Vector3.up * (cR.bounds.extents.y * 2 - this.launchPoint.localPosition.y); } cO.enabled = false; } else if (this.StartRotation == 0) { if (this.launchPoint.localPosition.y < cO.bounds.extents.y * 2) { this.transform.localPosition += Vector3.up * (cO.bounds.extents.y * 2 - this.launchPoint.localPosition.y); } cR.enabled = false; } } // Reset the slider joint that prevents any Y movement SliderJoint2D joint = this.GetComponent <SliderJoint2D>(); if (joint != null) { joint.anchor = new Vector2(this.transform.position.x, -this.transform.position.y); } } } // Since we updated the collider we need to re-ignore the collisions WeaponProjectile.IgnoreOwnerCollisions(this, this.Owner); } }