Ejemplo n.º 1
0
    public override void Use(HexCell target = null)
    {
        HexUnit targetUnit = target.hexUnits.Find(C => C.HexUnitType == HexUnit.UnitType.COMBAT);

        if (targetUnit)
        {
            targetUnit.GetComponent <Unit>().HitPoints -= (config as DamageUnitConfig).GetDamage();
            targetUnit.GetComponent <Unit>().UpdateUI();
            if (targetUnit.GetComponent <Unit>().HitPoints <= 0)
            {
                targetUnit.DieAnimationAndRemove();
            }
        }
        PlayParticleEffect();
        PlayAbilitySound();
        PlayAnimation();
    }
Ejemplo n.º 2
0
    public HexUnit CreateCityStateUnit(string combatUnitConfig, HexCell cell, int cityStateID)
    {
        HexUnit    hexUnit    = Instantiate(combatUnitPrefab).GetComponent <HexUnit>();
        CombatUnit combatUnit = hexUnit.GetComponent <CombatUnit>();

        combatUnit.SetCombatUnitConfig(GetCombatUnitConfig(combatUnitConfig));
        hexUnit.UnitPrefabName = name;
        hexUnit.Grid           = hexGrid;
        hexUnit.Location       = cell;
        hexUnit.Orientation    = Random.Range(0f, 360f);
        hexUnit.HexUnitType    = HexUnit.UnitType.COMBAT;
        hexGrid.AddUnit(hexUnit);
        CityState cityState = cityStates.Find(c => c.CityStateID == cityStateID);

        cityState.AddUnit(hexUnit.GetComponent <CombatUnit>());
        return(hexUnit);
    }
Ejemplo n.º 3
0
    private void RemoveUnit(GameObject go)
    {
        HexUnit hexUnit = go.GetComponent <HexUnit>();

        hexUnit.OwnerId = -1;
        _units.Remove(hexUnit);
        hexUnit.GetComponent <Recycle>().onRecyclingCallback -= RemoveUnit;
        Messenger.Broadcast(ON_UNIT_REMOVED, go);
    }
Ejemplo n.º 4
0
 public void AddUnit(HexUnit newUnit)
 {
     if (newUnit == null)
     {
         throw new NullReferenceException("newUnit is null!");
     }
     newUnit.OwnerId = Id;
     newUnit.GetComponent <Recycle>().onRecyclingCallback += RemoveUnit;
     _units.Enqueue(newUnit);
     Messenger.Broadcast(ON_UNIT_ADDED, newUnit);
 }
Ejemplo n.º 5
0
 public HexUnit GetFightableUnit(HexUnit unit)
 {
     foreach (HexUnit hexUnit in hexUnits)
     {
         if (hexUnit.GetComponent <Unit>().CanAttack(unit.GetComponent <Unit>()))
         {
             return(hexUnit);
         }
     }
     return(null);
 }
Ejemplo n.º 6
0
    public override void Use(HexCell target = null)
    {
        HexUnit targetUnit = target.hexUnits.Find(C => C.HexUnitType == HexUnit.UnitType.COMBAT);

        if (targetUnit)
        {
            targetUnit.GetComponent <Unit>().SetMovementLeft(0);
        }
        PlayParticleEffect();
        PlayAbilitySound();
        PlayAnimation();
    }
    public override void Use(HexCell target = null)
    {
        HexUnit targetAgent = target.hexUnits.Find(C => C.HexUnitType == HexUnit.UnitType.AGENT);

        if (targetAgent)
        {
            targetAgent.GetComponent <Unit>().HitPoints -= (config as AssassinateAgentConfig).GetDamage();
        }
        PlayParticleEffect();
        PlayAbilitySound();
        PlayAnimation();
    }
Ejemplo n.º 8
0
    public bool CanUnitMoveToCell(HexUnit unit)
    {
        HexUnit hexUnit = hexUnits.Find(c => c.HexUnitType == unit.HexUnitType);

        if (hexUnit)
        {
            return(false);
        }
        if (City && unit.HexUnitType == HexUnit.UnitType.COMBAT && unit.GetComponent <Unit>().CityState != City.GetCityState())
        {
            return(false);
        }
        return(true);
    }
Ejemplo n.º 9
0
    public HexUnit CreateAgent(string agentConfig, HexCell cell, Player player)
    {
        HexUnit hexUnit = Instantiate(agentPrefab).GetComponent <HexUnit>();
        Agent   agent   = hexUnit.GetComponent <Agent>();

        agent.SetAgentConfig(GetAgentConfig(agentConfig));
        hexGrid.AddUnit(hexUnit);
        hexUnit.Grid        = hexGrid;
        hexUnit.Location    = cell;
        hexUnit.Orientation = Random.Range(0f, 360f);
        hexUnit.HexUnitType = HexUnit.UnitType.AGENT;

        if (player.IsHuman)
        {
            hexUnit.Controllable = true;
        }

        hexUnit.HexUnitType = HexUnit.UnitType.AGENT;
        player.AddAgent(agent);
        return(hexUnit);
    }
Ejemplo n.º 10
0
    public static CombatUnit Load(BinaryReader reader, GameController gameController, HexGrid grid, int header, int cityStateID)
    {
        HexCoordinates coordinates      = HexCoordinates.Load(reader);
        float          orientation      = reader.ReadSingle();
        string         unitName         = reader.ReadString();
        string         combatUnitConfig = "Swordsman";

        if (header >= 4)
        {
            combatUnitConfig = reader.ReadString();
        }


        HexUnit    hexUnit    = gameController.CreateCityStateUnit(combatUnitConfig, grid.GetCell(coordinates), cityStateID);
        CombatUnit combatUnit = hexUnit.GetComponent <CombatUnit>();

        if (header >= 3)
        {
            combatUnit.HitPoints = reader.ReadInt32();
            combatUnit.SetMovementLeft(reader.ReadInt32());
        }
        return(combatUnit);
    }
Ejemplo n.º 11
0
 void DoSelection()
 {
     grid.ClearPath();
     UpdateCurrentCell();
     if (currentCell)
     {
         if (currentCell.GetTopUnit() && currentCell.GetTopUnit().Controllable)
         {
             selectedUnit = currentCell.GetTopUnit();
             HUD.Unit     = selectedUnit.GetComponent <Unit>();
         }
         else if (currentCell.City)
         {
             selectedUnit = null;
             HUD.City     = currentCell.City;
         }
         else if (currentCell.OpCentre)
         {
             selectedUnit = null;
             HUD.OpCentre = currentCell.OpCentre;
         }
     }
 }
Ejemplo n.º 12
0
    public static Agent Load(BinaryReader reader, GameController gameController, HexGrid grid, int header, Player player)
    {
        HexCoordinates coordinates = HexCoordinates.Load(reader);
        float          orientation = reader.ReadSingle();
        string         unitName    = reader.ReadString();
        string         agentConfig = "Builder";

        if (header >= 4)
        {
            agentConfig = reader.ReadString();
        }
        HexUnit unit = gameController.CreateAgent(agentConfig, grid.GetCell(coordinates), player);

        Agent agent = unit.GetComponent <Agent>();

        if (header >= 3)
        {
            agent.HitPoints = reader.ReadInt32();
            agent.SetMovementLeft(reader.ReadInt32());
        }

        return(agent);
    }
Ejemplo n.º 13
0
    IEnumerator TravelPath()
    {
        Vector3 lookTowards = new Vector3(0, 0, 0);

        if (!currentTravelLocation)
        {
            currentTravelLocation = pathToTravel[0];
        }
        int currentColumn = currentTravelLocation.ColumnIndex;
        int nextColumn;

        if (Location.IsVisible == true || HexVision.HasVision)
        {
            Vector3 a, b, c = pathToTravel[0].Position;
            yield return(LookAt(pathToTravel[1].Position));

            animator.SetBool("Walking", true);

            HexVision.ClearCells();


            float t = Time.deltaTime * travelSpeed;
            for (int i = 1; i < pathToTravel.Count; i++)
            {
                currentTravelLocation = pathToTravel[i];
                a = c;
                b = pathToTravel[i - 1].Position;

                nextColumn = currentTravelLocation.ColumnIndex;
                if (currentColumn != nextColumn)
                {
                    if (nextColumn < currentColumn - 1)
                    {
                        a.x -= HexMetrics.innerDiameter * HexMetrics.wrapSize;
                        b.x -= HexMetrics.innerDiameter * HexMetrics.wrapSize;
                    }
                    else if (nextColumn > currentColumn + 1)
                    {
                        a.x += HexMetrics.innerDiameter * HexMetrics.wrapSize;
                        b.x += HexMetrics.innerDiameter * HexMetrics.wrapSize;
                    }
                    Grid.MakeChildOfColumn(transform, nextColumn);
                    currentColumn = nextColumn;
                }

                c = (b + currentTravelLocation.Position) * 0.5f;

                //if(pathToTravel[i].IsVisible)
                //{
                //    EnableMesh(true);
                //}
                //else
                //{
                //    EnableMesh(false);
                //}
                HexVision.AddCells(Grid.GetVisibleCells(pathToTravel[i], VisionRange));
                if (currentTravelLocation.IsVisible == true)
                {
                    for (; t < 1f; t += Time.deltaTime * travelSpeed)
                    {
                        transform.localPosition = Bezier.GetPoint(a, b, c, t);
                        Vector3 d = Bezier.GetDerivative(a, b, c, t);
                        d.y = 0f;
                        transform.localRotation = Quaternion.LookRotation(d);
                        yield return(null);
                    }

                    t -= 1f;
                }
                HexVision.ClearCells();
            }
            a = c;
            b = Location.Position;
            c = b;

            HexVision.AddCells(Grid.GetVisibleCells(location, VisionRange));

            //if (location.IsVisible)
            //{
            //    EnableMesh(true);
            //}


            if (unit.IsSomethingToAttack())
            {
                if (unit.AttackCity)
                {
                    City city = unit.AttackCity;
                    lookTowards = city.GetHexCell().Position;
                    float attackTime = 0;
                    animator.SetBool("Attacking", true);
                    for (; attackTime < fightSpeed; attackTime += Time.deltaTime)
                    {
                        yield return(null);
                    }
                    animator.SetBool("Attacking", false);

                    if (city.HitPoints <= 0)
                    {
                        city.GetCityState().KillLocalUnits(city);
                        city.SetCityState(GetComponent <Unit>().CityState);
                    }
                    city.UpdateUI();
                }
                else if (unit.AttackUnit)
                {
                    HexUnit unitToFight = unit.AttackUnit;
                    lookTowards = unitToFight.Location.Position;
                    yield return(unitToFight.LookAt(location.Position));

                    if (unitToFight)
                    {
                        float attackTime = 0;
                        animator.SetBool("Attacking", true);
                        unitToFight.animator.SetBool("Attacking", true);
                        for (; attackTime < fightSpeed; attackTime += Time.deltaTime)
                        {
                            yield return(null);
                        }
                        animator.SetBool("Attacking", false);
                        unitToFight.animator.SetBool("Attacking", false);
                        Unit unitToFightUnitComp = unitToFight.GetComponent <Unit>();
                        unitToFightUnitComp.ShowHealthChange(unitToFightUnitComp.GetLastHitPointChange());
                        unitToFightUnitComp.UpdateUI();
                        if (unitToFightUnitComp.HitPoints <= 0)
                        {
                            unitToFight.DieAnimationAndRemove();
                        }
                    }
                }
                Unit unitComp = GetComponent <Unit>();
                unitComp.ShowHealthChange(unitComp.GetLastHitPointChange());
                unitComp.UpdateUI();
                if (unitComp.HitPoints <= 0)
                {
                    DieAnimationAndRemove();
                }



                nextColumn = Location.ColumnIndex;

                if (currentColumn != nextColumn)
                {
                    if (nextColumn < currentColumn - 1)
                    {
                        a.x -= HexMetrics.innerDiameter * HexMetrics.wrapSize;
                    }
                    else if (nextColumn > currentColumn + 1)
                    {
                        a.x += HexMetrics.innerDiameter * HexMetrics.wrapSize;
                    }
                    Grid.MakeChildOfColumn(transform, nextColumn);
                    currentColumn = nextColumn;
                }
            }



            currentTravelLocation = null;

            for (; t < 1f; t += Time.deltaTime * (travelSpeed * 2))
            {
                transform.localPosition = Bezier.GetPoint(a, b, c, t);
                Vector3 d = Bezier.GetDerivative(a, b, c, t);
                d.y = 0f;
                transform.localRotation = Quaternion.LookRotation(d);
                yield return(null);
            }
        }
        else
        {
            currentTravelLocation = null;
            nextColumn            = Location.ColumnIndex;
            if (currentColumn != nextColumn)
            {
                Grid.MakeChildOfColumn(transform, nextColumn);
                currentColumn = nextColumn;
            }
        }

        transform.localPosition = location.Position;
        location.UpdateVision();
        orientation = transform.localRotation.eulerAngles.y;
        if (unit.IsSomethingToAttack())
        {
            yield return(LookAt(lookTowards));
        }
        ListPool <HexCell> .Add(pathToTravel);

        pathToTravel = null;
        animator.SetBool("Walking", false);
    }
Ejemplo n.º 14
0
    bool Search(HexCell fromCell, HexCell toCell, HexUnit unit, bool allowUnexplored)
    {
        int speed = unit.Speed;

        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = speed - unit.GetComponent <Unit>().GetMovementLeft();
        searchFrontier.Enqueue(fromCell);
        //if(toCell.City)
        //{
        //    int trap = 0;
        //}
        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            int currentTurn = ((current.Distance - 1) / speed) + 1;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (
                    neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase
                    )
                {
                    continue;
                }
                if (!unit.IsValidDestination(neighbor, allowUnexplored))
                {
                    if (neighbor != toCell || !unit.IsValidAttackDestination(neighbor))
                    {
                        continue;
                    }
                }
                int moveCost = unit.GetMoveCost(current, neighbor, d, allowUnexplored);
                if (moveCost < 0)
                {
                    continue;
                }

                int distance = current.Distance + moveCost;
                int turn     = (distance - 1) / speed;
                if (turn > currentTurn)
                {
                    distance = turn * speed + moveCost;
                }

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic =
                        neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(false);
    }
Ejemplo n.º 15
0
 public void DestroyUnit(HexUnit unit)
 {
     gameController.DestroyUnit(unit.GetComponent <Unit>());
 }
Ejemplo n.º 16
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="roomId">房间id</param>
    /// <param name="ownerId">所属玩家id</param>
    /// <param name="actorId">自己的id</param>
    /// <param name="posX"></param>
    /// <param name="posZ"></param>
    /// <param name="orientation"></param>
    /// <param name="unitName"></param>
    /// <param name="cellIndex">该单位在地形块HexCell中的Index,因为根据PosX,PosZ可能得不到正确的cell,只能用这个数据确保正确</param>
    /// <param name="actorInfoId">兵种ID,在actor_info表中的id</param>
    /// <param name="name"></param>
    /// <param name="hp"></param>
    /// <param name="attackPower"></param>
    /// <param name="defencePower"></param>
    /// <param name="speed"></param>
    /// <param name="filedOfVision"></param>
    /// <param name="shootingRange"></param>
    /// <param name="attackDuration"></param>
    /// <param name="attackInterval"></param>
    /// <param name="ammoBase"></param>
    /// <param name="ammoBaseMax"></param>
    /// <returns></returns>
    public ActorVisualizer CreateUnit(long roomId, long ownerId, long actorId, int posX, int posZ, float orientation,
                                      string unitName, int cellIndex, int actorInfoId,
                                      string name, int hp, int hpMax, float attackPower, float defencePower, float speed, float filedOfVision, float shootingRange,
                                      float attackDuration, float attackInterval, int ammoBase, int ammoBaseMax)
    {
        HexCell cell = GetCell(cellIndex);

        if (!cell)
        {
            GameRoomManager.Instance.Log($"HexmapHelper CreateUnit :创建Actor失败!格子越界 - <{posX},{posZ}> - {unitName}");
            return(null);
        }

        if (cellIndex == 0)
        {
            Debug.LogError("HexmapHelper CreateUnit Error - CellIndex is lost!!!");
            return(null);
        }

        if (cell.Unit)
        {
            HexCell newCell = null;
            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = cell.GetNeighbor(d);
                if (neighbor && !neighbor.Unit)
                {
                    newCell = neighbor;
                    break;
                }
            }

            if (newCell)
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!物体位置重合了,重新放置! - 原坐标:<{posX},{posZ}> - 新坐标:<{newCell.coordinates.X},{newCell.coordinates.Z}> - {unitName}");
                cell = newCell;
            }
            else
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!原来这个格子没有物体,现在有了物体, 附近也没有空地! - <{posX},{posZ}> - {unitName}");
                return(null);
            }
        }

        // 区分颜色
        string newUnitName = unitName;

        if (ownerId != GameRoomManager.Instance.CurrentPlayer.TokenId)
        { // 如果不是自己,蓝色就换成红色(绿色换成黄色)
            if (unitName.IndexOf("BLUE") > 0)
            {
                newUnitName = unitName.Replace("BLUE", "RED");
            }
            else if (unitName.IndexOf("GREEN") > 0)
            {
                newUnitName = unitName.Replace("GREEN", "YELLOW");
            }
        }

        string unitPathName = $"Arts/BeffioPrefabs/Soldiers/{newUnitName}";
        var    go           = Resources.Load <HexUnit>(unitPathName);

        if (go != null)
        {
            HexUnit hu = Instantiate(go);
            if (hu != null)
            {
                hexGrid.AddUnit(hu, cell, orientation);
                var av = hu.GetComponent <ActorVisualizer>();
                if (av != null)
                {
                    av.RoomId      = roomId;
                    av.OwnerId     = ownerId;
                    av.ActorId     = actorId;
                    av.PosX        = posX;
                    av.PosZ        = posZ;
                    av.Orientation = orientation;
                    av.Species     = unitName;
                    av.CellIndex   = cellIndex;
                    av.ActorInfoId = actorInfoId;

                    av.Name          = name;
                    av.Hp            = hp;
                    av.HpMax         = hpMax;
                    av.AttackPower   = attackPower;
                    av.DefencePower  = defencePower;
                    av.Speed         = speed;
                    av.FieldOfVision = filedOfVision;
                    av.ShootingRange = shootingRange;

                    av.AttackDuration = attackDuration;
                    av.AttackInterval = attackInterval;
                    av.AmmoBase       = ammoBase;
                    av.AmmoBaseMax    = ammoBaseMax;
                }

                // 关闭预制件上没用的东西,看以后这东西能否用得上,如果没用,就完全干掉
                if (hu.GetComponentInChildren <ThirdPersonUserControl>())
                {
                    hu.GetComponentInChildren <ThirdPersonUserControl>().enabled = false;
                }
                if (hu.GetComponentInChildren <ThirdPersonCharacter>())
                {
                    hu.GetComponentInChildren <ThirdPersonCharacter>().enabled = false;
                }
                if (hu.GetComponentInChildren <CapsuleCollider>())
                {
                    hu.GetComponentInChildren <CapsuleCollider>().enabled = false;
                }
                EnableFollowCamera(av, false);

                if (!GameRoomManager.Instance.RoomLogic.ActorManager.AllActors.ContainsKey(actorId))
                {
                    ActorBehaviour ab = new ActorBehaviour()
                    {
                        RoomId      = roomId,
                        OwnerId     = ownerId,
                        ActorId     = actorId,
                        PosX        = posX,
                        PosZ        = posZ,
                        Orientation = orientation,
                        Species     = unitName,
                        CellIndex   = cellIndex,
                        ActorInfoId = actorInfoId,

                        Name          = name,
                        Hp            = hp,
                        HpMax         = hpMax,
                        AttackPower   = attackPower,
                        DefencePower  = defencePower,
                        Speed         = speed,
                        FieldOfVision = filedOfVision,
                        ShootingRange = shootingRange,

                        AttackDuration = attackDuration,
                        AttackInterval = attackInterval,
                        AmmoBase       = ammoBase,
                        AmmoBaseMax    = ammoBaseMax,
                    };
                    GameRoomManager.Instance.RoomLogic.ActorManager.AddActor(ab, hu);
                }
                GameRoomManager.Instance.Log($"MSG: CreateATroopReply - 创建了一个Actor - {unitName}");
                return(av);
            }
        }

        return(null);
    }