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 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);
        }
    }
예제 #3
0
    void testMove(unitScript unit)
    {
        pathFinder.getMovementPaths(unit.GetComponent <GridItem>().getPos(), unit.getMovementDistance(), false);
        var  locations = pathFinder.getReachableLocations();
        bool f         = true;

        foreach (Vector2 location in locations)
        {
            float chance = Random.Range(0.0f, 1.0f);
            if (!f && chance > 0.5)
            {
                if (!unit.hasMoved())
                {
                    Vector2[] path = pathFinder.drawPath(location, false);
                    unit.setPath(path);
                }
            }
            if (f)
            {
                f = !f;
            }
        }
    }
    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);
                }
            }
        }
    }