Exemplo n.º 1
0
        /// <summary>
        /// Explode when the missile collides with something.
        /// </summary>
        /// <param name="hit">Information about the collision.</param>
        private void OnControllerColliderHit(ControllerColliderHit hit)
        {
            GameObject explosion = Instantiate(ExplosionPrefab);

            explosion.transform.position = gameObject.transform.position;
            ActionTimer despawnTimer = explosion.AddComponent <ActionTimer>();

            despawnTimer.Action = delegate {
                Destroy(explosion);
            };
            despawnTimer.Expiry = ExplosionExpiry;

            Collider[] damageTargets = Physics.OverlapSphere(
                transform.position, ExplosionRadius, DamageLayers);
            foreach (Collider collider in damageTargets)
            {
                GameObject   explodee     = collider.gameObject;
                FenceChecker fenceChecker = explodee.GetComponent <FenceChecker>();

                if ((fenceChecker != null) && (!fenceChecker.Fenced()))
                {
                    BuildingExploder exploder = explodee.GetComponent <BuildingExploder>();
                    if (exploder != null)
                    {
                        exploder.Explode(gameObject.transform.position);
                    }
                }
            }

            Destroy(gameObject);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Setup <see cref="MapsService"/>. Give buildings mesh colliders and make them explodeable.
        /// </summary>
        private void MapsServiceSetup()
        {
            MapsService = GetComponent <MapsService>();

            MapsService.Events.ExtrudedStructureEvents.DidCreate.AddListener(args => {
                // Assign every building a mesh collider, so player and bullets can collide with them.
                MeshFilter meshFilter = args.GameObject.GetComponent <MeshFilter>();
                MeshCollider collider = args.GameObject.AddComponent <MeshCollider>();

                collider.sharedMesh = meshFilter.sharedMesh;

                // Assign every building a building exploder.
                BuildingExploder exploder = args.GameObject.AddComponent <BuildingExploder>();
                exploder.ChunkMaterial    = BuildingChunkMaterial;

                // Allow the fencing status of the building to be checked before damaging it.
                args.GameObject.AddComponent <FenceChecker>();
            });
        }