public void setCharacterPanelVisibility(bool value, unitScript unit = null, bool targetLeft = true)
 {
     if (value && unit)
     {
         loadCharacter(unit, unit.getOwner() == 0);
     }
     if (unit && unit.getOwner() == 0)
     {
         transform.FindChild("CharacterPanel").gameObject.SetActive(value);
     }
     if (!value && targetLeft)
     {
         transform.FindChild("CharacterPanel").gameObject.SetActive(false);
     }
     if (unit)
     {
         transform.FindChild(unit.getOwner() == 0 ? "StatPanel" : "EnemyStatPanel").gameObject.SetActive(value);
         transform.FindChild(unit.getOwner() == 0 ? "WeaponPanel" : "EnemyWeaponPanel").gameObject.SetActive(value);
     }
     else
     {
         transform.FindChild(targetLeft ? "StatPanel" : "EnemyStatPanel").gameObject.SetActive(value);
         transform.FindChild(targetLeft ? "WeaponPanel" : "EnemyWeaponPanel").gameObject.SetActive(value);
     }
 }
    void DeselectCurrent()
    {
        //Deselect current unit
        if (selected)
        {
            unitScript unit = selected.GetComponent <unitScript>();

            if (unit != null && unit.getOwner() == 0)
            {
                gui.GetComponent <GUIController>().setCharacterPanelVisibility(false);
                gui.GetComponent <GUIController>().setCombatPanelVisibility(false);

                unit.SendMessage("deselect");

                if (unit.getOwner() == 0)
                {
                    GridItem pos = selected.GetComponent <GridItem>();
                    map.UnHilightMap();
                    //map.toggleHighlight(false,Color.white, pos.getX(), pos.getY(), unit.getSpeed());
                    pathFinder.setPathfinding(false);
                    rangeRenderer.enabled = false;
                    attackMode            = false;
                }
            }
        }

        selected = null;
    }
    public void selectUnit(unitScript unit)
    {
        GUIController cont = gui.GetComponent <GUIController>();

        DeselectCurrent();
        selected = unit.gameObject;

        cont.setCharacterPanelVisibility(true, unit);

        if (unit.getOwner() == 0)
        {
            unit.SendMessage("select");
            // IF unit has not moved
            if (unit != null && !unit.hasMoved())
            {
                // Allow them to

                if (unit.getOwner() == 0 && unit.hasMoved() == false)
                {
                    GridItem pos = selected.GetComponent <GridItem>();

                    pathFinder.getMovementPaths(pos.getPos(), unit.getMovementDistance(), true);
                    //map.toggleHighlight(true, Color.cyan, pos.getX(), pos.getY(), unit.getMovementDistance());
                }
            }
        }
    }
    void leftButtonPressed()
    {
        RaycastHit hit;
        var        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, (1 << 8)))
        {
            DeselectCurrent();

            //set selected to hit
            selected = hit.collider.gameObject;

            //Select new unit
            if (selected)
            {
                unitScript unit = selected.GetComponent <unitScript>();

                if (unit != null)
                {
                    if (unit.getOwner() == 0)
                    {
                        selectUnit(unit);
                    }
                    else
                    {
                        DeselectCurrent();
                    }
                }
            }
        }
    }
示例#5
0
    public bool isEnemyOnTile(string player)
    {
        if (unitsOnTerrain[0])
        {
            unitScript enemyScript = unitsOnTerrain[0].GetComponent <unitScript>();

            return(player != enemyScript.getOwner());
        }
        else
        {
            return(!unitsOnTerrain[0]);
        }
    }
    void rightButtonPressed()
    {
        if (selected)
        {
            unitScript unit = selected.GetComponent <unitScript>();
            if (unit != null && unit.getOwner() == 0)
            {
                RaycastHit hit;
                var        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity, (1 << 8)))
                {
                    unitSelectedRightClick(hit, unit);
                }
            }
        }
    }
    void unitSelectedRightClick(RaycastHit hit, unitScript unit)
    {
        clickableTile tile = hit.collider.gameObject.GetComponent <clickableTile>();

        if (tile)
        {
            GridItem tilePos = tile.GetComponent <GridItem>();
            GridItem pos     = selected.GetComponent <GridItem>();

            if (!unit.hasMoved())
            {
                //draw path only if not in attack mode
                Vector2[] path = pathFinder.drawPath(tilePos.getPos(), !attackMode);


                if (path != null)
                {
                    if (!attackMode && !unit.hasMoved())
                    {
                        map.UnHilightMap();
                        //map.toggleHighlight(false, Color.white, pos.getX(), pos.getY(), unit.getSpeed());
                        unit.setPath(path);
                        pathFinder.setPathfinding(false);
                    }
                }
            }
            if (attackMode && unit.getCurrentWeapon().type == WeaponType.shield)
            {
                activateShield(unit, tilePos);
            }
        }
        //If not tile may be unit
        unitScript clickedUnit = hit.collider.gameObject.GetComponent <unitScript>();

        // if is enemy unit
        if (attackMode && !unit.hasAttacked() && clickedUnit && clickedUnit.getOwner() != 0)
        {
            Attack(unit, clickedUnit);
        }
    }
    void mouseOver()
    {
        //Mouse over tile while unit selected
        if (selected)
        {
            unitScript unit = selected.GetComponent <unitScript>();
            if (unit != null && unit.getOwner() == 0)
            {
                RaycastHit hit;
                var        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit, Mathf.Infinity, (1 << 8)))
                {
                    clickableTile tile = hit.collider.gameObject.GetComponent <clickableTile>();

                    if (tile && !unit.hasMoved() && !attackMode)
                    {
                        GridItem tilePos = tile.GetComponent <GridItem>();
                        //print(tilePos);
                        if (pathFinder != null)
                        {
                            pathFinder.drawPath(tilePos.getPos());
                        }
                        else
                        {
                            print("pathfinder dead");
                        }
                    }

                    unitScript enemyUnit = hit.collider.GetComponent <unitScript>();

                    if (enemyUnit && attackMode == true && enemyUnit.getOwner() != 0)
                    {
                        GridItem playerPos = unit.GetComponent <GridItem>();
                        GridItem enemyPos  = hit.collider.GetComponent <GridItem>();

                        MeleeWeapon currentMeleeWeapon = null;
                        if (unit.getCurrentWeapon().type == WeaponType.melee)
                        {
                            currentMeleeWeapon = (MeleeWeapon)unit.getCurrentWeapon();


                            bool withinMeleeRange = currentMeleeWeapon != null && Vector2.Distance(enemyPos.getPos(), playerPos.getPos()) <= currentMeleeWeapon.range;
                            if (withinMeleeRange || currentMeleeWeapon == null)
                            {
                                GUIController cont = gui.GetComponent <GUIController>();

                                cont.setCombatPanelVisibility(true, unit, enemyUnit, meleeChanceToHit(unit, enemyUnit));
                            }
                        }
                        else if (unit.getCurrentWeapon().type == WeaponType.ranged)
                        {
                            if (canSee(unit, enemyUnit))
                            {
                                rangeRenderer.enabled = true;
                                updateRangefinder(unit, enemyUnit.GetComponent <GridItem>());
                                GUIController cont     = gui.GetComponent <GUIController>();
                                float         distance = Vector2.Distance(unit.GetComponent <GridItem>().getPos(), enemyUnit.GetComponent <GridItem>().getPos());

                                cont.setCombatPanelVisibility(true, unit, enemyUnit, rangedChanceToHit(unit, enemyUnit));
                                cont.loadRangedData(isShielded(unit, enemyUnit), getBaseHitChance(unit), getRangePenalty(unit, distance), getCoverPenalty(unit, enemyUnit, distance));
                            }
                            else if (rangeRenderer.enabled == true)
                            {
                                rangeRenderer.enabled = false;
                            }
                        }
                    }
                    else
                    {
                        GUIController cont = gui.GetComponent <GUIController>();

                        cont.setCombatPanelVisibility(false);
                        rangeRenderer.enabled = false;
                    }
                }
            }
        }

        //We also want to set the enemyStatPanel

        RaycastHit result;
        Ray        line = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(line, out result, Mathf.Infinity, (1 << 8)))
        {
            //set selected to hit
            GameObject enemy = result.collider.gameObject;

            //Select new unit
            if (enemy)
            {
                var        guiCont = gui.GetComponent <GUIController>();
                unitScript unit    = enemy.GetComponent <unitScript>();

                if (unit != null && unit.getOwner() == 1)
                {
                    guiCont.setCharacterPanelVisibility(true, unit, false);
                }
                else
                {
                    guiCont.setCharacterPanelVisibility(false, null, false);
                }
            }
        }
    }