Example #1
0
    public bool Explode(Vector2 spawnPosition)
    {
        if (IsDestroyed)
        {
            return(false);
        }

        if (ExplosionPrefab != null)
        {
            var rotation  = InheritRotation ? transform.rotation : Quaternion.identity;
            var explosion = Instantiate(ExplosionPrefab, spawnPosition, rotation) as GameObject;
            explosion.transform.parent = transform.parent;              // TODO: spawn explosion on layer dedicated to explosions!
            PlayerOwnedObject.InheritOwner(this.gameObject, explosion);
        }

        TerminationSettings.Terminate(gameObject);
        IsDestroyed = true;

        /* Hide visual part of the object starting from appropriate root.
         * This allows to hide projectile and at the same time to keep showing particle systems. */
        var objectToHide = VisualToHide != null ? VisualToHide : gameObject;

        Common.SetVisibility(objectToHide, false);

        return(true);
    }
Example #2
0
    private void SpawnObjects()
    {
        float normalizedForce = 0;

        // TODO: configuration must be transferred across projectile objects chain.
        if (configuration != null)
        {
            configuration.TryGetFloat(ForceParameterName, out normalizedForce);
        }

        float forceAmount = Mathf.Lerp(
            Projectile.MinForceAmount,
            Projectile.MaxForceAmount,
            normalizedForce
            );

        var spawnerBody       = GetComponentInParent <Rigidbody2D> ();
        var spawnerGameObject = spawnerBody != null ? spawnerBody.gameObject : gameObject;

        float absMainDirAngle = MainDirectionAngle;

        if (!AngleIsAbsolute)
        {
            float spawnerAimAngle = Common.SignedAngle(Vector2.right, SpawnerAimDirection);
            absMainDirAngle += spawnerAimAngle;
        }

        float halfAngle = ArcHalfAngle * Mathf.Deg2Rad;
        float angle     = absMainDirAngle * Mathf.Deg2Rad - halfAngle;
        float dAngle    = halfAngle * 2 / (NumObjectsToSpawn - 1);

        var spawnedObjects = new List <GameObject> ();

        for (int i = 0; i < NumObjectsToSpawn; i++, angle += dAngle)
        {
            float deviatedAngle = angle + Random.value * Projectile.AngleRandomDeviation * ArcHalfAngle;
            var   dir           = new Vector2(Mathf.Cos(deviatedAngle), Mathf.Sin(deviatedAngle));
            var   spawnPos      = ( Vector2 )transform.position + dir * Projectile.SpawnDistance;
            var   rotation      = Quaternion.identity;

            if (Projectile.RotateAlongLaunchDir)
            {
                rotation = Common.RotateAlongDirection(dir);
            }

            var projGameObject = Instantiate(Projectile.Prefab, spawnPos, rotation) as GameObject;
            spawnedObjects.Add(projGameObject);
            // TODO: spawn projectiles on layer dedicated to projectiles!
            PlayerOwnedObject.InheritOwner(this.gameObject, projGameObject);

            var projBodies = projGameObject.GetComponentsInChildren <Rigidbody2D> ();

            foreach (var projBody in projBodies)
            {
                if (projBody != null)
                {
                    float deviatedForceAmount = forceAmount + Random.value * Projectile.ForceRandomDeviation * Projectile.MaxForceAmount;

                    if (deviatedForceAmount < 0)
                    {
                        deviatedForceAmount = 0;
                    }

                    var force = dir * deviatedForceAmount;
                    projBody.AddForce(force, Projectile.ForceMode);

                    float angularVel = Projectile.AngularVelocity;

                    if (Projectile.AngularVelocitySignDependsOnDir)
                    {
                        angularVel *= Mathf.Sign(dir.x);
                    }

                    if (Projectile.AngularVelocityDependsOnForce)
                    {
                        angularVel *= normalizedForce;
                    }

                    projBody.angularVelocity = angularVel;

                    if (Projectile.InheritSpawnerVelocity && spawnerBody != null)
                    {
                        var spawnerVelocity = spawnerBody.GetPointVelocity(projGameObject.transform.position);
                        projBody.velocity += spawnerVelocity;
                    }
                }
            }

            if (!Projectile.CollideWithSpawner)
            {
                PhysicsHelper.IgnoreCollision(spawnerGameObject, projGameObject);
            }
            // TODO: configuration must be transferred across projectile objects chain.
            if (configuration != null)
            {
                configuration.SendConfiguration(projGameObject);
            }
        }

        if (!CollideWithEachOther)
        {
            foreach (var obj1 in spawnedObjects)
            {
                foreach (var obj2 in spawnedObjects)
                {
                    if (obj1 == obj2)
                    {
                        continue;
                    }

                    PhysicsHelper.IgnoreCollision(obj1, obj2);
                }
            }
        }

        if (soundPlayer != null)
        {
            soundPlayer.PlayVariation(SpawnSounds);
        }
    }