private void OnHover_HandleStatsDisplayer()
 {
     //Destroy lastStatsDisplayer if tilePos changes
     if (lastDisplayer != null)
     {
         Destroy(lastDisplayer);
     }
     //Spawn statsdisplayer if unit is on tile
     if (tileManager.CheckForUnit(mousePos))
     {
         Unit       unit = tileManager.GetUnitAtPosition(mousePos);
         GameObject go   = StatsDisplayer.NewStatsDisplayer(statsDisplayer, unit);
         unit.healthBar.SetActive(false);
         lastDisplayer = go;
         lastUnit      = unit;
     }
 }
Beispiel #2
0
    // Update is called once per frame
    void Update()
    {
        if (unitIsMoving || RoundManager.state != GameCycle.Play)
        {
            return;
        }

        //Get tile mouse is focusing on
        Vector3    mousePos      = Input.mousePosition;
        Vector3    mousePosWorld = Camera.main.ScreenToWorldPoint(mousePos);
        Vector3Int tilePos       = masksMap.WorldToCell(mousePosWorld);

        Unit currentUnit = RoundManager.currentUnit;

        #region Move, Attack and Select Units
        if (Input.GetMouseButtonDown(0))
        {
            if (masksMap.GetTile(tilePos) == greenMask)
            {
                currentUnit.WalkPath(PathEngine.Instance.GetPath(currentUnit, tilePos));
            }
            else if (masksMap.GetTile(tilePos) == redMask)
            {
                //Check if unit is in range;
                Vector3Int[] attackPositions = currentUnit.attackPositions;
                bool         canAttack       = false;
                if (attackPositions == null)
                {
                    return;
                }
                foreach (Vector3Int v in attackPositions)
                {
                    if (currentUnit.GetPos() + v == tilePos)
                    {
                        currentUnit.Attack(GetUnitAtPosition(tilePos));
                        canAttack = true;
                    }
                }
                if (currentAttackPath != null && !canAttack)
                {
                    currentUnit.WalkPath(currentAttackPath);
                }
            }
            else if (CheckForUnit(tilePos))
            {
                Unit unitAtPos = GetUnitAtPosition(tilePos);
                if (unitAtPos.team == RoundManager.currentTeam)
                {
                    RoundManager.currentUnit = unitAtPos;
                    currentUnit = unitAtPos;
                    MaskGenerator.Instance.GenerateMask(currentUnit);
                }
            }
        }
        #endregion

        #region Display Path and Spawn StatsDisplayer
        if (tilePos != lastTilePos)
        {
            lastTilePos = tilePos;
            //Reset last unit
            if (lastUnit != null && !lastUnit.isDead)
            {
                lastUnit.healthBar.SetActive(true);
            }

            //Generate Path to location
            if (masksMap.GetTile(tilePos) == greenMask)
            {
                Path path = PathEngine.Instance.GetPath(currentUnit, tilePos);
                if (path != null)
                {
                    PathEngine.Instance.DisplayPath(path, currentUnit);
                }
            }

            //Generate Path to attack position
            if (masksMap.GetTile(tilePos) == redMask)
            {
                Vector3Int[] attackPositions = currentUnit.attackPositions;
                if (attackPositions == null)
                {
                    return;
                }
                foreach (Vector3Int v in attackPositions)
                {
                    Path attackPath = PathEngine.Instance.GetPath(currentUnit, tilePos + v);
                    if (attackPath != null && masksMap.GetTile(tilePos + v))
                    {
                        PathEngine.Instance.DisplayPath(attackPath, currentUnit);
                        currentAttackPath = attackPath;
                    }
                }
            }

            //Destroy lastStatsDisplayer if tilePos changes
            if (lastStatsDisplayer != null)
            {
                Destroy(lastStatsDisplayer);
            }
            //Spawn statsdisplayer if unit is on tile
            if (CheckForUnit(tilePos))
            {
                Unit unit = GetUnitAtPosition(tilePos);
                print(unit);
                GameObject go = StatsDisplayer.NewStatsDisplayer(statsDisplayer, unit);
                unit.healthBar.SetActive(false);
                lastStatsDisplayer = go;
                lastUnit           = unit;
            }
        }
        #endregion

        //Destroy lastStatsDisplayer if unit is dead
        if (lastUnit != null && lastStatsDisplayer != null)
        {
            if (lastUnit.isDead)
            {
                Destroy(lastStatsDisplayer);
            }
        }
    }