Exemplo n.º 1
0
 private void FindHealthBar()
 {
     for (int i = 0; i < transform.childCount; i++)
     {
         if (transform.GetChild(i).CompareTag("UnitHealthBar"))
         {
             healthBar = transform.GetChild(i).GetComponent <UnitHealthBar>();
         }
     }
 }
Exemplo n.º 2
0
 void Awake()
 {
     if (_player)
     {
         _healthBarPlayer    = GameObject.Find("Healthbar Player").GetComponent<UnitHealthBar>();
     }
     if (_bull)
     {
         _healthBarBull      = GameObject.Find("Healthbar Bull").GetComponent<UnitHealthBar>();
     }
     health = maxHealth;
 }
Exemplo n.º 3
0
    private void OnUnitAdded(HexUnit newUnit)
    {
        if (_healthBars.ContainsKey(newUnit.gameObject))
        {
            throw new Exception("Unit already added to HealthBar list. Possible double spawn?");
        }
        GameObject healtBarGo = PoolManager.instance.GetObjectForName(_barPrefab.name, false);

        healtBarGo.transform.SetParent(_transform, false);
        UnitHealthBar unitHealth = healtBarGo.GetComponent <UnitHealthBar>();

        unitHealth.GetRectTransform.anchorMin = -Vector2.one;
        unitHealth.GetRectTransform.anchorMax = -Vector2.one;
        _healthBars.Add(newUnit.gameObject, unitHealth);
    }
Exemplo n.º 4
0
    protected override void OnEnable()
    {
        base.OnEnable();
        mainCamera    = Camera.main;
        currentHealth = initialHealth;

        // Instantiate attached health bar, assign values, then hide.
        if (unitHealthBar == null)
        {
            Transform unitHealthBarParent = GameManager.HUD.transform.Find("Unit Health Bars");
            unitHealthBarObject        = Instantiate(unitHealthBarPrefab, unitHealthBarParent);
            unitHealthBar              = unitHealthBarObject.GetComponent <UnitHealthBar>();
            unitHealthBar.attachedUnit = transform;
            unitHealthBar.SetTotalHealth(initialHealth);
        }

        unitHealthBarObject.SetActive(false);
    }
Exemplo n.º 5
0
        private void InstantiateHealthBar()
        {
            var healthBarGameObj = Instantiate(healthBarPrefab, transform.position, Quaternion.identity);

            healthBarGameObj.transform.SetParent(transform);
            healthBarGameObj.transform.localPosition = new Vector3() + healthBarOffset;
            var billboard = healthBarGameObj.GetComponent <Billboard>();

            var teamCamera = Camera.main;

            if (teamCamera == null)
            {
                throw new Exception("No cameras available");
            }

            billboard.Cam = teamCamera.transform;
            healthBar     = healthBarGameObj.GetComponentInChildren <UnitHealthBar>();
            healthBar.SetMaxHealth(maxHitPoints);
        }
Exemplo n.º 6
0
    private void Start()
    {
        // setting variables from Unit scriptable object
        animator = GetComponent <Animator>();
        animator.runtimeAnimatorController = unitData.animator;
        isRanged         = unitData.isRanged;
        health           = unitData.health;
        damage           = unitData.damage;
        hitboxIncrease   = unitData.hitboxIncrease;
        movementSpeed    = unitData.movementSpeed;
        trainingTime     = unitData.trainingTime;
        cost             = unitData.cost;
        rewardGold       = unitData.rewardGold;
        rewardExperience = unitData.rewardExperience;
        if (isEnemy)
        {
            isEnemyInt     = -1;
            gameObject.tag = "Enemy";
            GetComponent <SpriteRenderer>().flipX = true;
        }
        else
        {
            isEnemyInt     = 1;
            gameObject.tag = "Friendly";
        }

        // increase hitbox size
        GetComponent <BoxCollider2D>().size = new Vector3(1 + hitboxIncrease, 1 + hitboxIncrease, 1);

        // adding health bar on top of unit from Resources\Bar
        GameObject temp = (GameObject)Instantiate(Resources.Load("Bar"));

        healthBar = temp.GetComponent <UnitHealthBar>();
        temp.transform.SetParent(gameObject.transform);
        barRenderers = temp.transform.GetComponentsInChildren <SpriteRenderer>();
        foreach (SpriteRenderer rend in barRenderers)
        {
            rend.enabled = false;
        }

        values  = FindObjectOfType <Values>();
        enemyAI = FindObjectOfType <EnemyAI>();
    }
Exemplo n.º 7
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /// <summary>
    //  Creates a healthbar widget for the object. (This also attachs it to the object afterwards).
    /// </summary>
    // <param name="thisObject"></param>
    // <param name="camera"></param>
    public void CreateHealthBar(WorldObject thisObject, Camera camera)
    {
        // Create a health bar and allocate it to the unit
        ///GameObject healthBarObj = ObjectPooling.Spawn(GameManager.Instance.UnitHealthBar.gameObject);
        GameObject healthBarObj = Instantiate(GameManager.Instance.UnitHealthBar);

        _HealthBar = healthBarObj.GetComponent <UnitHealthBar>();
        _HealthBar.SetObjectAttached(thisObject);
        _HealthBar.SetCameraAttached(camera);
        healthBarObj.transform.SetParent(GameManager.Instance.WorldSpaceCanvas.gameObject.transform, false);
        healthBarObj.gameObject.SetActive(true);

        // Set healthbar widget size & anchoring
        RectTransform healthRectTransform = _HealthBar.HealthSlider.GetComponent <RectTransform>();

        healthRectTransform.sizeDelta        = new Vector2(_WidgetHealthbarScaleX, _WidgetHealthbarScaleY);
        healthRectTransform.anchoredPosition = new Vector2(0, _WidgetHealthbarOffset);
        RectTransform shieldRectTransform = _HealthBar.ShieldSlider.GetComponent <RectTransform>();

        shieldRectTransform.sizeDelta        = new Vector2(_WidgetShieldbarScaleX, _WidgetShieldbarScaleY);
        shieldRectTransform.anchoredPosition = new Vector2(0, _WidgetShieldbarOffset);

        // Build logo properties
        if (ShowBuildLogoInHealthBar)
        {
            _HealthBar.BuildLogoImage.gameObject.SetActive(true);

            if (HealthBarLogo != null)
            {
                _HealthBar.BuildLogoImage.sprite = HealthBarLogo;
            }
            else
            {
                _HealthBar.BuildLogoImage.sprite = Logo;
            }
            _HealthBar.BuildLogoImage.GetComponent <RectTransform>().anchoredPosition = new Vector2(BuildLogoOffset, 0);
        }
        else
        {
            _HealthBar.BuildLogoImage.gameObject.SetActive(false);
        }
    }
Exemplo n.º 8
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /// <summary>
    //  Creates a healthbar widget for the object. (This also attachs it to the object afterwards).
    /// </summary>
    /// <param name="thisObject"></param>
    /// <param name="camera"></param>
    public void CreateHealthBar(WorldObject thisObject, Camera camera)
    {
        // Create a health bar and allocate it to the unit
        GameObject healthBarObj = ObjectPooling.Spawn(GameManager.Instance.UnitHealthBar.gameObject);

        _HealthBar = healthBarObj.GetComponent <UnitHealthBar>();
        _HealthBar.SetObjectAttached(thisObject);
        _HealthBar.SetCameraAttached(camera);
        healthBarObj.transform.SetParent(GameManager.Instance.WorldSpaceCanvas.gameObject.transform, false);
        healthBarObj.gameObject.SetActive(true);

        // Set healthbar widget size & anchoring
        RectTransform healthRectTransform = _HealthBar.HealthSlider.GetComponent <RectTransform>();

        healthRectTransform.sizeDelta        = new Vector2(_WidgetHealthbarScaleX, _WidgetHealthbarScaleY);
        healthRectTransform.anchoredPosition = new Vector2(0, _WidgetHealthbarOffset);
        RectTransform shieldRectTransform = _HealthBar.ShieldSlider.GetComponent <RectTransform>();

        shieldRectTransform.sizeDelta        = new Vector2(_WidgetShieldbarScaleX, _WidgetShieldbarScaleY);
        shieldRectTransform.anchoredPosition = new Vector2(0, _WidgetShieldbarOffset);
    }
Exemplo n.º 9
0
    protected override void OnEnable()
    {
        base.OnEnable();
        mainCamera    = Camera.main;
        currentHealth = initialHealth;

        // Instantiate attached health bar, assign values, then hide.
        if (unitHealthBar == null)
        {
            Vector3 healthBarPosition = mainCamera.WorldToScreenPoint(transform.position);
            healthBarPosition.x -= 30f;
            healthBarPosition.y -= 30f;
            Transform unitHealthBarParent = GameManager.HUD.transform.Find("Unit Health Bars");
            unitHealthBarObject        = Instantiate(unitHealthBarPrefab, healthBarPosition, Quaternion.identity, unitHealthBarParent);
            unitHealthBar              = unitHealthBarObject.GetComponent <UnitHealthBar>();
            unitHealthBar.attachedUnit = transform;
            unitHealthBar.SetTotalHealth(initialHealth);
        }

        unitHealthBarObject.SetActive(false);
    }
Exemplo n.º 10
0
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /// <summary>
    //  Sets reference to the object's health bar widget.
    /// </summary>
    /// <param name="healthBar"></param>
    public void SetHealthBar(UnitHealthBar healthBar)
    {
        _HealthBar = healthBar;
    }