Ejemplo n.º 1
0
        public override void Init(GameManager gameMgr, int fID, bool free)
        {
            base.Init(gameMgr, fID, free);

            //get the unit's components
            HealthComp    = GetComponent <UnitHealth>();
            ConverterComp = GetComponent <Converter>();
            MovementComp  = GetComponent <UnitMovement>();
            WanderComp    = GetComponent <Wander>();
            EscapeComp    = GetComponent <EscapeOnAttack>();
            BuilderComp   = GetComponent <Builder>();
            CollectorComp = GetComponent <ResourceCollector>();
            HealerComp    = GetComponent <Healer>();
            AllAttackComp = GetComponents <UnitAttack>();

            //initialize them:
            if (ConverterComp)
            {
                ConverterComp.Init(gameMgr, this);
            }
            if (MovementComp)
            {
                MovementComp.Init(gameMgr, this);
            }
            if (WanderComp)
            {
                WanderComp.Init(gameMgr, this);
            }
            if (EscapeComp)
            {
                EscapeComp.Init(gameMgr, this);
            }
            if (BuilderComp)
            {
                BuilderComp.Init(gameMgr, this);
            }
            if (CollectorComp)
            {
                CollectorComp.Init(gameMgr, this);
            }
            if (HealerComp)
            {
                HealerComp.Init(gameMgr, this);
            }
            foreach (UnitAttack comp in AllAttackComp) //init all attached attack components
            {
                if (AttackComp == null)
                {
                    AttackComp = comp;
                }

                comp.Init(gameMgr, this, MultipleAttackMgr);
            }
            if (MultipleAttackMgr)
            {
                MultipleAttackMgr.Init(this);
            }
            if (TaskLauncherComp)                     //if the entity has a task launcher component
            {
                TaskLauncherComp.Init(gameMgr, this); //initialize it
            }
            if (animator == null)                     //no animator component?
            {
                Debug.LogError("[Unit] The " + GetName() + "'s Animator hasn't been assigned to the 'animator' field");
            }

            if (animator != null)                       //as long as there's an animator component
            {
                if (animatorOverrideController == null) //if there's no animator override controller assigned..
                {
                    animatorOverrideController = gameMgr.UnitMgr.GetDefaultAnimController();
                }
                ResetAnimatorOverrideController(); //set the default override controller
                //Set the initial animator state to idle
                SetAnimState(UnitAnimState.idle);
            }

            Rigidbody rigidbody = GetComponent <Rigidbody>();

            if (rigidbody == null) //no rigidbody component?
            {
                Debug.LogError("[Unit] The " + GetName() + "'s main object is missing a rigidbody component");
            }

            //rigidbody settings:
            rigidbody.isKinematic = true;
            rigidbody.useGravity  = false;

            //set the radius value:
            radius = MovementComp.GetAgentRadius();

            //if this is a free unit
            if (this.free)
            {
                UpdateFactionColors(gameMgr.UnitMgr.GetFreeUnitColor()); //set the free unit color
            }
            gameMgr.MinimapIconMgr?.Assign(selection);                   //ask the minimap icon manager to create the a minimap icon for this unit

            CustomEvents.OnUnitCreated(this);                            //trigger custom event
        }
Ejemplo n.º 2
0
        public override void Init(GameManager gameMgr, int fID, bool free)
        {
            base.Init(gameMgr, fID, free);

            //get the building-specifc components:
            PlacerComp       = GetComponent <BuildingPlacer>();
            HealthComp       = GetComponent <BuildingHealth>();
            NavObstacle      = GetComponent <NavMeshObstacle>();
            BoundaryCollider = GetComponent <Collider>();
            BorderComp       = GetComponent <Border>();
            DropOffComp      = GetComponent <BuildingDropOff>();
            WorkerMgr        = GetComponent <WorkerManager>();
            PortalComp       = GetComponent <Portal>();
            GeneratorComp    = GetComponent <ResourceGenerator>();
            AllAttackComp    = GetComponents <BuildingAttack>();

            //initialize them:
            PlacerComp.Init(gameMgr, this);
            foreach (BuildingAttack comp in AllAttackComp) //init all attached attack components
            {
                if (AttackComp == null)
                {
                    AttackComp = comp;
                }

                comp.Init(gameMgr, this, MultipleAttackMgr);
            }
            if (MultipleAttackMgr)
            {
                MultipleAttackMgr.Init(this);
            }
            WorkerMgr.Init();

            if (BoundaryCollider == null) //if the building collider is not set.
            {
                Debug.LogError("[Building]: The building parent object must have a collider to represent the building's boundaries.");
            }
            else
            {
                BoundaryCollider.isTrigger = true; //the building's main collider must always have "isTrigger" is true.
            }
            RallyPoint = gotoPosition;             //by default the rally point is set to the goto position
            if (gotoPosition != null)              //Hide the goto position
            {
                gotoPosition.gameObject.SetActive(false);
            }

            if (Placed == false) //Disable the player selection collider object if the building has not been placed yet.
            {
                selection.gameObject.SetActive(false);
            }

            if (placedByDefault == false)                   //if the building is not placed by default.
            {
                PlaneRenderer.material.color = Color.green; //start by setting the selection texture color to green which implies that it's allowed to place building at its position.
            }
            //if this is no plaement instance:
            if (!PlacementInstance)
            {
                PlacerComp.PlaceBuilding();                                      //place the building
                if (GodMode.Enabled && FactionID == GameManager.PlayerFactionID) //if god mode is enabled and this is the local player's building
                {
                    placedByDefault = true;
                }
            }

            if (placedByDefault) //if the building is supposed to be placed by default or we're in god mode and this is the player's building -> meaning that it is already in the scene
            {
                HealthComp.AddHealthLocal(HealthComp.MaxHealth, null);
            }
        }