Пример #1
0
 private void StopMovement(object sender, InputState inputState)
 {
     if (!inputState.IsActionActive(ActionType.StopMovement))
     {
         return;
     }
     if (!Selected)
     {
         return;
     }
     Velocity = Vector2.Zero;
     mPath    = null;
 }
Пример #2
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            if (unitData == null)
            {
                return;
            }

            if (!unitData.death && unitData.hp <= 0)
            {
                unitData.death = true;
            }

            if (_death != unitData.death)
            {
                _death = unitData.death;
                if (_death)
                {
                    SoliderPoolItem soliderPoolItem = GetComponent <SoliderPoolItem>();
                    UnitPath        unitPath        = GetComponent <UnitPath>();
                    unitPath.state = UnitPathState.Die;
                    if (unitData.isHitFly)
                    {
                        unitAgent.HitFly();
                        if (soliderPoolItem != null)
                        {
                            //							soliderPoolItem.DelayRelease(3F);
                            soliderPoolItem.Release();
                        }
                        else
                        {
                            delayDestory.delayTime = 3F;
                            delayDestory.enabled   = true;
                        }
                    }
                    else
                    {
                        if (soliderPoolItem != null)
                        {
                            soliderPoolItem.Release();
                        }
                        else
                        {
                            delayDestory.delayTime = 0F;
                            delayDestory.enabled   = true;
                        }
                    }
                }
            }
        }
Пример #3
0
    private void Start()
    {
        base.Init();

        _pathing = GetComponent<UnitPath>();
        _pathing.SetUnitInfo(this);

        _unitBehaviour = GetComponent<IUnit>();
        _pathcontroller = Camera.main.GetComponent<UnitSelectionControls>();
        _originalUnitColor = GetComponent<Renderer>().material.color;

        if (_unitBehaviour != null)
            _unitBehaviour.Init(this);
    }
Пример #4
0
        protected internal void RequestPath(Vector2 target, bool hasToReachTarget = false, bool loose = false)
        {
            target.X = Math.Min(Math.Max(target.X, 0), Game1.MapWidth - 0.1f);
            target.Y = Math.Min(Math.Max(target.Y, 0), Game1.MapHeight - 0.1f);
            mPath    = Pathfinder.GetPath(CollisionBoxCenter, target, hasToReachTarget);

            if (loose)
            {
                return;
            }
            if (Vector2.Distance(CollisionBoxCenter, target) > 0.5f && mPath.Empty())
            {
                mPath = Pathfinder.GetPath(CollisionBoxCenter, target, hasToReachTarget, false);
            }
        }
Пример #5
0
 protected void DeletePath()
 {
     mPath = null;
 }
Пример #6
0
        private static void SetWaveTactic(IReadOnlyCollection <Zombie> wave, double passedTime)
        {
            IAttackable target   = null;
            var         position = wave.First().Position;

            // Avoid multiple zombies attacking the same target and blocking themselves
            var targets      = new IAttackable[(int)Math.Ceiling((float)wave.Count / 2)];
            var flockTargets = false;


            // Choose tactic: attack economy vs. trying to finish
            if (sRandom.NextDouble() < passedTime / (passedTime + 5) * Game1.Difficulty || !ObjectManager.Instance.GetAnimals().Any())
            {
                UnitPath farmerPath    = null;
                UnitPath farmhousePath = null;

                if (ObjectManager.Instance.GetFarmer() != null &&
                    Pathfinder.ExistsPath(position, ObjectManager.Instance.GetFarmer().Position))
                {
                    farmerPath = Pathfinder.GetPath(position, ObjectManager.Instance.GetFarmer().Position);
                }
                if (ObjectManager.Instance.GetFarmhouse() != null &&
                    Pathfinder.ExistsPath(position, ObjectManager.Instance.GetFarmhouse().Position))
                {
                    farmhousePath = Pathfinder.GetPath(position, ObjectManager.Instance.GetFarmhouse().Position);
                }

                if (farmerPath != null && (farmhousePath == null || farmerPath.Length <= farmhousePath.Length))
                {
                    target = ObjectManager.Instance.GetFarmer();
                }
                else if (farmhousePath != null)
                {
                    target = ObjectManager.Instance.GetFarmhouse();
                }
                else
                {
                    foreach (var tower in ObjectManager.Instance.GetTowers())
                    {
                        if (target == null || Vector2.Distance(position, tower.Position) <
                            Vector2.Distance(position, target.Position))
                        {
                            target = tower;
                        }
                    }

                    if (target == null || !Pathfinder.ExistsPath(position, target.Position))
                    {
                        flockTargets = true;

                        var fences   = ObjectManager.Instance.GetFences().ToArray();
                        var maxIndex = 0;
                        for (var i = 0; i < targets.Length && i < fences.Length; i++)
                        {
                            targets[i] = fences[i];
                            if (Vector2.Distance(position, fences[i].Position) <
                                Vector2.Distance(position, targets[maxIndex].Position))
                            {
                                maxIndex = i;
                            }
                        }

                        for (var i = targets.Length; i < fences.Length; i++)
                        {
                            if (!(Vector2.Distance(position, fences[i].Position) <
                                  Vector2.Distance(position, targets[maxIndex].Position)))
                            {
                                continue;
                            }

                            targets[maxIndex] = fences[i];
                            for (var j = 0; j < targets.Length; j++)
                            {
                                if (Vector2.Distance(position, targets[j].Position) >
                                    Vector2.Distance(position, targets[maxIndex].Position))
                                {
                                    maxIndex = j;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                flockTargets = true;

                var animals  = ObjectManager.Instance.GetAnimals().ToArray();
                var maxIndex = 0;
                for (var i = 0; i < targets.Length && i < animals.Length; i++)
                {
                    targets[i] = animals[i];
                    if (Vector2.Distance(position, animals[i].Position) <
                        Vector2.Distance(position, targets[maxIndex].Position))
                    {
                        maxIndex = i;
                    }
                }

                for (var i = targets.Length; i < animals.Length; i++)
                {
                    if (!(Vector2.Distance(position, animals[i].Position) <
                          Vector2.Distance(position, targets[maxIndex].Position)))
                    {
                        continue;
                    }

                    targets[maxIndex] = animals[i];
                    for (var j = 0; j < targets.Length; j++)
                    {
                        if (Vector2.Distance(position, targets[j].Position) >
                            Vector2.Distance(position, targets[maxIndex].Position))
                        {
                            maxIndex = j;
                        }
                    }
                }
            }

            if (flockTargets)
            {
                var flockedTargets = targets.Where(t => t != null).ToArray();
                for (var i = 0; i < wave.Count; i++)
                {
                    wave.ElementAt(i).Target = flockedTargets[i % flockedTargets.Length];
                }
            }
            else
            {
                foreach (var zombie in wave)
                {
                    zombie.Target = target;

                    if (!zombie.PathEmpty)
                    {
                        continue;
                    }
                    var closestPosition = target.Position;
                    while (!Pathfinder.ExistsPath(position, closestPosition))
                    {
                        closestPosition = 0.9f * (closestPosition - zombie.Position) + zombie.Position;
                    }
                    zombie.Charge(closestPosition);
                    zombie.PreferredTargetType = target.GetType();
                }
            }
        }
Пример #7
0
 public DefaultUnit(DefaultUnitVisual visual, DefaultUnitModel model, UnitPath path) : base(visual, model)
 {
     _pathMapper = new UnitPath.UnitPathMapper(path);
 }
Пример #8
0
        protected override void OnStart()
        {
            base.OnStart();

            unitPath = GetComponent <UnitPath>();
        }
Пример #9
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            if (!sendArmData.sending)
            {
                return;
            }
            if (!War.isGameing)
            {
                return;
            }
            if (_forced == false && unitData.freezedSendArm)
            {
                return;
            }
            if (unitData.unitNum <= 0 || unitData.death)
            {
                Stop();
                return;
            }


            armOnceWidth = legionData.soliderData.armOnceWidth;
            radius       = legionData.soliderData.radius;
            gapV         = legionData.soliderData.gapV;
            gapH         = radius * 2;
            if (gapV < 0)
            {
                gapV = gapH;
            }
            if (armOnceWidth < 0)
            {
                armOnceWidth = 8;
            }
            onceCount = Mathf.FloorToInt(armOnceWidth / gapH);


            targetCount = sendArmData.targets.Count;
            for (int j = 0; j < targetCount; j++)
            {
                SendArmDataTarget targetData = sendArmData.targets[j];

                int count = Mathf.Min(targetData.num, onceCount);
                count = Mathf.Min(count, unitData.unitNum);
                //Debug.Log ("j=" + j + " count=" + count + "  targetData.num=" + targetData.num);
                if (count <= 0)
                {
                    sendArmData.WillRemoveTarget(targetData);
                    continue;
                }

                if (count > 8)
                {
                    count = 8;
                }

                if (Time.time > targetData.nextTime)
                {
                    PathGroup pathGroup = War.scene.GetPathCache(onceCount, transform, targetData.target.transform);
                    isInitPathCache = pathGroup != null;
                    List <UnitPath> unitPathList  = new List <UnitPath>();
                    UnitPathGroup   unitPathGroup = null;
                    if (!isInitPathCache)
                    {
                        GameObject groupGO = War.factory.CreatePathGroup();
                        groupGO.transform.position = transform.position;
                        unitPathGroup = groupGO.GetComponent <UnitPathGroup>();
                    }

                    float moveSpeed = 1f;
                    for (int i = 0; i < count; i++)
                    {
                        int pathIndex = (int)((i + 1) / 2) * (i % 2 == 0 ? 1 : -1);

                        targetData.num      += -1;
                        sendArmData.sendNum += -1;
                        unitData.AddUnitNum(-1);

//											GameObject soliderGO = War.factory.CreateSolider(unitData.legionId);
                        GameObject soliderGO       = War.soliderPool.Get();
                        UnitData   soliderUnitData = legionData.soliderData.Clone();
                        soliderUnitData.id   = targetData.idBegin++;
                        soliderUnitData.from = unitData;
                        soliderUnitData.to   = targetData.target.unitData;
                        unitData.soliderPropContainer.UnitApp(soliderUnitData);
                        legionData.soliderPropContainer.UnitApp(soliderUnitData);
                        legionData.soliderInitPropContainer.UnitApp(soliderUnitData, true);

                        soliderGO.AddEComponent(soliderUnitData);
                        soliderUnitData.Init();
                        if (targetData.forced)
                        {
                            soliderUnitData.moveSpeed = targetData.speed;
                        }
                        UnitAgent unitAgent = soliderGO.GetComponent <UnitAgent>();
                        unitAgent.Walk();
                        unitAgent.angel = HMath.angle(transform.position.z, transform.position.x, targetData.target.transform.position.z, targetData.target.transform.position.x);


                        soliderGO.name = "War_Solider-" + soliderUnitData.id;


                        UnitPath unitPath = soliderGO.GetComponent <UnitPath>();
                        unitPath.index               = pathIndex;
                        unitPath.maxSpeed            = targetData.forced ? targetData.speed : soliderUnitData.moveSpeed;
                        soliderGO.transform.position = transform.position;

                        if (isInitPathCache)
                        {
                            unitPathList.Add(unitPath);
                        }
                        else
                        {
                            unitPathGroup.list.Add(unitPath);
                        }



                        moveSpeed = unitPath.maxSpeed;
                        if (targetData.forced)
                        {
                            soliderUnitData.moveSpeed = moveSpeed;
                            soliderUnitData.Props[PropId.InitMoveSpeed] = moveSpeed;
                        }

                        soliderUnitData.to.AddFromLegionUnit(soliderUnitData.legionId, 1);

                        SoliderPoolItem soliderPoolItem = soliderGO.GetComponent <SoliderPoolItem>();
                        if (soliderPoolItem != null)
                        {
                            soliderPoolItem.Rest();
                        }
                    }

                    //				Debug.Log(string.Format("<color=green>isInitPathCache={0}</color>", isInitPathCache));
                    if (isInitPathCache)
                    {
                        pathGroup = War.scene.GetPathCache(onceCount, transform, targetData.target.transform);
                        int unitPathListCount = unitPathList.Count;
                        pathGroup = pathGroup.Clone(unitPathListCount);

                        for (int i = 0; i < unitPathListCount; i++)
                        {
                            unitPathList[i].SetPath(pathGroup.paths[i]);
                        }
                    }
                    else
                    {
                        unitPathGroup.gap       = gapH;
                        unitPathGroup.pathNum   = count;
                        unitPathGroup.onceCount = onceCount;
                        unitPathGroup.MoveTo(unitCtl, targetData.target);
                    }

                    if (float.IsNaN(moveSpeed) || moveSpeed <= 0)
                    {
                        moveSpeed = 1f;
                    }
                    targetData.nextTime = Time.time + (gapV + sendArmData.sendUnitSpeed) / moveSpeed;
                }
            }

            sendArmData.ExeRemoveTargets();
        }