Exemplo n.º 1
0
        uint?CheckTouch(UnitSide side, Vector3 tgtLeft, Vector3 tgtRight, Vector3[] checkCorners, uint[] ids)
        {
            if (this.HexDic == null)
            {
                return(null);
            }

            foreach (var id in ids)
            {
                if (this.HexDic.TryGetValue(id, out var hex) == false ||
                    hex.Side == side)
                {
                    continue;
                }

                HexUtils.SetHexCorners(this.Origin, id, checkCorners, HexDictionary.HexEdgeLength);

                if (HexUtils.CheckLine(tgtRight, tgtLeft, checkCorners, HexDictionary.HexEdgeLength / 10000))
                {
                    return(id);
                }
            }

            return(null);
        }
        private void UnitQuery(Entity entity,
                               ref BaseUnitStatus.Component status,
                               ref Position.Component position)
        {
            bool isActive = false;
            var  pos      = position.Coords.ToUnityVector() + this.Origin;

            foreach (var kvp in hexIndexes)
            {
                if (HexUtils.IsInsideHex(this.Origin, kvp.Key, pos) == false)
                {
                    continue;
                }

                isActive = kvp.Value.IsActive;
                break;
            }

            if (isActive)
            {
                if (status.State == UnitState.Sleep)
                {
                    status.State = UnitState.Alive;
                }
            }
            else
            {
                if (status.State == UnitState.Alive)
                {
                    status.State = UnitState.Sleep;
                }
            }
        }
        private void SearchChildren()
        {
            var list  = new List <UnitSnapshotPair>();
            var units = GetComponentsInChildren <UnitSnapshotComponent>(includeInactive: true);

            this.pair.units.Clear();

            foreach (var u in units)
            {
                if (u != null && HexUtils.HexAllowsUnitType(attribute, u.type))
                {
                    var hex = u.GetComponent <HexSnapshotAttachment>();
                    if (hex != null)
                    {
                        this.pair.hex = hex;
                    }

                    this.pair.units.Add(u);
                    u.gameObject.SetActive(true);
                }
                else
                {
                    u.gameObject.SetActive(false);
                }
            }

            hex.color = hexColorSettings.GetHexAttributeColor(attribute);
        }
        public void SetPosition(Vector3 pos, uint index, float edge)
        {
            this.index = index;
            this.transform.position = pos;

            var corners = new Vector3[7];

            HexUtils.SetHexCorners(pos, corners, edge);
            line.positionCount = 7;
            line.SetPositions(corners);
            hex.transform.localScale = edge * Vector3.one;
        }
        private bool SetStrategyTarget(Vector3 pos, float backBuffer, ref BaseUnitSight.Component sight, ref BaseUnitTarget.Component target)
        {
            bool isTarget = false;

            switch (target.Type)
            {
            case TargetType.Unit:
                if (target.TargetUnit.IsValid())
                {
                    sight.TargetPosition = target.TargetUnit.Position.ToFixedPointVector3();
                    sight.TargetSize     = target.TargetUnit.Size;
                    target.State         = TargetState.MovementTarget;
                    isTarget             = true;
                }
                else
                {
                    Debug.LogError("FrontLineInfo is InValid");
                }
                break;

            case TargetType.FrontLine:
                if (target.FrontLine.IsValid())
                {
                    sight.TargetPosition = target.FrontLine.GetOnLinePosition(this.Origin, pos, -backBuffer).ToWorldPosition(this.Origin);
                    sight.TargetSize     = 0.0f;
                    target.State         = TargetState.MovementTarget;
                    isTarget             = true;
                }
                else
                {
                    Debug.LogError("FrontLineInfo is InValid");
                }
                break;

            case TargetType.Hex:
                if (target.HexInfo.IsValid())
                {
                    sight.TargetPosition = HexUtils.GetHexCenter(this.Origin, target.HexInfo.HexIndex, HexDictionary.HexEdgeLength).ToWorldPosition(this.Origin);
                    sight.TargetSize     = HexDictionary.HexTargetRadius;
                    target.State         = TargetState.MovementTarget;
                    isTarget             = true;
                }
                else
                {
                    Debug.LogError("HexInfo is InValid");
                }
                break;
            }

            return(isTarget);
        }
Exemplo n.º 6
0
        void SetLandLine(Vector3 origin, UnitSide side, Dictionary <UnitSide, float> powers)
        {
            if (line == null || reader == null)
            {
                return;
            }

            var index = reader.Data.Index;

            HexUtils.SetHexCorners(origin, index, this.corners, HexDictionary.HexEdgeLength);

            line.SetLines(side, corners, PhysicsUtils.GroundMask, cutNumber, origin.y + height, origin.y, height / 100);

            text?.SetHexInfo(reader.Data.Index, reader.Data.HexId, side, powers);
        }
Exemplo n.º 7
0
        private void ResourceQuery(Entity entity,
                                   ref HexPowerResource.Component resource,
                                   ref Position.Component position)
        {
            var pos = position.Coords.ToUnityVector() + this.Origin;

            foreach (var kvp in this.HexDic)
            {
                var index = kvp.Key;
                if (HexUtils.IsInsideHex(this.Origin, index, pos, HexDictionary.HexEdgeLength) == false)
                {
                    continue;
                }

                resourceDictionary.TryGetValue(index, out var current);
                resourceDictionary[index] = current + resource.Level * HexDictionary.HexResourceRate;
            }
        }
Exemplo n.º 8
0
        private void PlayerCheck()
        {
            if (base.HexDic == null)
            {
                return;
            }

            if (CheckTime(ref playerQuerySet.inter) == false)
            {
                return;
            }

            HashSet <uint> activeIndexes = new HashSet <uint>();

            Entities.With(playerQuerySet.group).ForEach((Entity entity,
                                                         ref PlayerInfo.Component player,
                                                         ref Position.Component position,
                                                         ref SpatialEntityId entityId) =>
            {
                var pos = position.Coords.ToUnityVector() + this.Origin;
                foreach (var kvp in this.HexDic)
                {
                    if (HexUtils.IsInsideHex(this.Origin, kvp.Key, pos))
                    {
                        activeIndexes.Add(kvp.Key);
                    }
                }
            });

            foreach (var kvp in this.HexDic)
            {
                bool isActivate = activeIndexes.Contains(kvp.Key);
                if (kvp.Value.isActive != isActivate)
                {
                    this.UpdateSystem.SendEvent(new HexPower.HexActiveChange.Event(new HexActiveChange()
                    {
                        IsActive = isActivate
                    }), kvp.Value.EntityId.EntityId);
                }
            }
        }
Exemplo n.º 9
0
        public void DrawGrid()
        {
            var comps = parent.GetComponentsInChildren <HexGridComponent>();

            var canvasSize = parent.rect.size.magnitude;
            var hexSize    = canvasSize / (2 * layerNumber + 1);
            int total      = layerNumber * (layerNumber + 1) * 3 + 1;

            for (uint i = 0; i < total; i++)
            {
                var center = HexUtils.GetHexCenter(Vector3.zero, i, edge: edgeLength);

                center *= hexSize / (edgeLength * 2);

                HexGridComponent grid = null;
                if (i < comps.Length)
                {
                    grid = comps[i];
                }
                else
                {
                    grid = Instantiate(baseHex, parent);
                }

                grid.gameObject.SetActive(true);
                grid.MyRect.localPosition = new Vector3(center.x, center.z, 0);

                var size = grid.MyRect.sizeDelta.x;
                grid.MyRect.localScale = Vector3.one * hexSize / size;

                grid.DrawGrid(i);
            }

            for (int i = total; i < comps.Length; i++)
            {
                comps[i].gameObject.SetActive(false);
            }
        }
Exemplo n.º 10
0
        private void PowerQuery(Entity entity,
                                ref HexPower.Component power,
                                ref HexBase.Component hex,
                                ref SpatialEntityId entityId)
        {
            var hexSide = hex.Side;

            if (hexSide == UnitSide.None)
            {
                return;
            }

            power.SidePowers.TryGetValue(hexSide, out var current);

            if (current <= 0)
            {
                power.SidePowers[hexSide] = 0;
                current = 0;
            }

            if (resourceDictionary.TryGetValue(hex.Index, out var resourceValue))
            {
                power.SidePowers[hexSide] = current + (float)(resourceValue * deltaTime);
            }

            var ids = HexUtils.GetNeighborHexIndexes(hex.Index);

            targetIds.Clear();
            foreach (var id in ids)
            {
                if (base.HexDic.ContainsKey(id) == false)
                {
                    continue;
                }

                var  h      = base.HexDic[id];
                bool isFlow = false;
                if (h.Side == hexSide)
                {
                    if (current > h.CurrentPower + current * flowValueRate)
                    {
                        isFlow = true;
                    }
                }
                else if (h.Side == UnitSide.None)
                {
                    isFlow = true;
                }

                if (isFlow)
                {
                    targetIds.Add(id);
                }
            }

            var totalFlow = (float)(flowValueRate * deltaTime * current);
            var count     = targetIds.Count;
            var flow      = totalFlow / count;

            foreach (var id in targetIds)
            {
                float val = flow;
                if (power.SidePowers[hexSide] > flow)
                {
                    power.SidePowers[hexSide] -= flow;
                }
                else
                {
                    val = power.SidePowers[hexSide];
                    power.SidePowers[hexSide] = 0;
                }

                var h   = base.HexDic[id];
                var key = h.EntityId.EntityId;
                if (flowDictionary.ContainsKey(key))
                {
                    var powerDic = flowDictionary[key];
                    if (powerDic.ContainsKey(hexSide))
                    {
                        powerDic[hexSide] += val;
                    }
                    else
                    {
                        powerDic[hexSide] = val;
                    }
                }
                else
                {
                    flowDictionary[key] = new Dictionary <UnitSide, float>();
                    flowDictionary[key].Add(hexSide, val);
                }
            }
        }
Exemplo n.º 11
0
        private void DeviceQuery(Unity.Entities.Entity entity,
                                 ref DominationStamina.Component domination,
                                 ref BaseUnitStatus.Component status,
                                 ref SpatialEntityId entityId)
        {
            if (status.Side != UnitSide.None)
            {
                return;
            }

            if (status.Type != UnitType.Stronghold)
            {
                return;
            }

            float range    = domination.Range;
            var   staminas = domination.SideStaminas;

            var trans = EntityManager.GetComponentObject <Transform>(entity);
            var pos   = trans.position;
            var list  = getAllUnits(pos, range, allowDead: false, AttackLogicDictionary.DominationUnitTypes);

            sumsDic.Clear();
            foreach (var unit in list)
            {
                DominationDevice.Component?comp = null;
                if (TryGetComponent(unit.id, out comp) == false)
                {
                    continue;
                }

                var speed = 1.5f;

                switch (comp.Value.Type)
                {
                case DominationDeviceType.Capturing:
                    AffectCapture(unit.side, speed, sumsDic);
                    break;

                case DominationDeviceType.Jamming:
                    AffectJamming(unit.side, speed, sumsDic);
                    break;
                }
            }

            if (this.HexIndexes != null)
            {
                foreach (var kvp in this.HexIndexes)
                {
                    if (HexUtils.IsInsideHex(this.Origin, kvp.Key, pos, HexDictionary.HexEdgeLength) == false)
                    {
                        continue;
                    }

                    var hex = kvp.Value;
                    if (HexUtils.TryGetOneSidePower(hex.SidePowers, out var side, out var val))
                    {
                        if (sumsDic.ContainsKey(side))
                        {
                            sumsDic[side] += val;
                        }
                        else
                        {
                            sumsDic[side] = val;
                        }
                    }
                    break;
                }
            }

            // check over
            if (sumsDic.Count == 0)
            {
                return;
            }

            UnitSide firstSide  = UnitSide.None;
            float    firstValue = 0.0f;
            float    underSum   = 0.0f;

            foreach (var kvp in sumsDic)
            {
                if (kvp.Value > firstValue)
                {
                    underSum  += firstValue;
                    firstSide  = kvp.Key;
                    firstValue = kvp.Value;
                }
                else
                {
                    underSum += kvp.Value;
                }
            }

            if (firstValue <= underSum)
            {
                return;
            }

            var over = firstValue - underSum;

            if (staminas.ContainsKey(firstSide) == false)
            {
                staminas[firstSide] = over;
            }
            else
            {
                staminas[firstSide] += over;
            }

            this.keys.Clear();
            this.keys.AddRange(staminas.Keys);

            foreach (var k in this.keys)
            {
                if (k != firstSide && staminas.ContainsKey(k))
                {
                    var val = staminas[k];
                    staminas[k] = Mathf.Max(0.0f, val - over);
                }
            }

            // capture
            if (staminas[firstSide] >= domination.MaxStamina)
            {
                Capture(entityId.EntityId, firstSide);
                staminas.Clear();
            }

            domination.SideStaminas = staminas;
        }
Exemplo n.º 12
0
        protected Dictionary <uint, HexDetails> BorderHexList(UnitSide side, Dictionary <uint, HexDetails> indexes)
        {
            if (this.HexDic == null)
            {
                return(null);
            }

            foreach (var kvp in this.HexDic)
            {
                if (kvp.Value.Side != side)
                {
                    continue;
                }

                var index = kvp.Value.Index;

                HexUtils.SetHexCorners(this.Origin, index, baseCorners, HexDictionary.HexEdgeLength);
                var ids = HexUtils.GetNeighborHexIndexes(index);

                List <FrontLineInfo> lines = null;
                foreach (var cornerIndex in cornerIndexes)
                {
                    var right = baseCorners[cornerIndex];
                    var left  = baseCorners[cornerIndex + 1];

                    var id = CheckTouch(side, left, right, checkCorners, ids);
                    if (id != null)
                    {
                        if (lines == null)
                        {
                            if (linesQueue.Count > 0)
                            {
                                lines = linesQueue.Dequeue();
                            }
                            else
                            {
                                lines = new List <FrontLineInfo>();
                            }
                        }

                        lines.Add(new FrontLineInfo()
                        {
                            LeftCorner  = left.ToWorldPosition(this.Origin).ToCoordinates(),
                            RightCorner = right.ToWorldPosition(this.Origin).ToCoordinates()
                        });
                    }
                }

                if (lines == null)
                {
                    continue;
                }

                HexDetails hexDetails = null;
                if (hexDetailQueue.Count > 0)
                {
                    hexDetails = hexDetailQueue.Dequeue();
                }
                else
                {
                    hexDetails = new HexDetails();
                }

                hexDetails.frontLines = lines;
                hexDetails.staminas   = kvp.Value.Powers;

                indexes = indexes ?? new Dictionary <uint, HexDetails>();
                indexes.Add(index, hexDetails);
            }

            return(indexes);
        }
Exemplo n.º 13
0
        private void UpdateRecovery()
        {
            if (this.HexDic == null)
            {
                return;
            }

            if (CheckTime(ref inter) == false)
            {
                return;
            }

            var interval = 1.0f;

            Entities.With(unitGroup).ForEach((Entity entity,
                                              ref BaseUnitHealth.Component health,
                                              ref BaseUnitStatus.Component status,
                                              ref SpatialEntityId entityId) =>
            {
                if (status.State == UnitState.Dead)
                {
                    return;
                }

                var trans = EntityManager.GetComponentObject <Transform>(entity);
                var pos   = trans.position;

                int?hexMasterId = null;
                foreach (var kvp in base.HexDic)
                {
                    if (kvp.Value.Side != status.Side)
                    {
                        continue;
                    }

                    if (HexUtils.IsInsideHex(this.Origin, kvp.Key, pos, HexDictionary.HexEdgeLength))
                    {
                        hexMasterId = kvp.Value.HexId;
                        break;
                    }
                }

                if (hexMasterId == null)
                {
                    return;
                }

                if (health.Health >= health.MaxHealth)
                {
                    return;
                }

                // rate from hexMasterId
                var rate   = 1.0f / 100;
                var amount = health.RecoveryAmount;
                amount    += rate * interval;

                var floor = Mathf.FloorToInt(amount);
                if (floor > 0)
                {
                    health.Health = Mathf.Min(health.Health + floor, health.MaxHealth);
                    amount       -= floor;
                }

                health.RecoveryAmount = amount;
            });
        }
Exemplo n.º 14
0
        private void Query(Transform trans,
                           ref TurretHub.Component turret,
                           ref BaseUnitStatus.Component status,
                           ref HexFacility.Component hex,
                           ref SpatialEntityId entityId)
        {
            if (status.State != UnitState.Alive)
            {
                return;
            }

            if (UnitUtils.IsBuilding(status.Type) == false)
            {
                return;
            }

            var datas = turret.TurretsDatas;

            if (IsNeedRenewTurrets(datas) == false)
            {
                return;
            }

            var units = getAllUnits(trans.position, HexDictionary.HexEdgeLength, allowDead: true, GetSingleUnitTypes(UnitType.Turret));

            removeKeys.Clear();
            foreach (var k in datas.Keys)
            {
                removeKeys.Add(k);
            }

            bool changed = false;

            var hexIndex = hex.HexIndex;

            foreach (var u in units)
            {
                if (hexIndex != uint.MaxValue && HexUtils.IsInsideHex(this.Origin, hexIndex, u.pos, HexDictionary.HexEdgeLength) == false)
                {
                    continue;
                }

                if (TryGetComponent <TurretComponent.Component>(u.id, out var comp) == false)
                {
                    continue;
                }

                int masterId = comp.Value.MasterId;

                if (datas.ContainsKey(u.id))
                {
                    var val = datas[u.id];
                    if (CheckDiffTurretInfo(ref val, u.side, masterId, u.id))
                    {
                        datas[u.id] = val;
                        changed     = true;
                    }

                    removeKeys.Remove(u.id);
                }
                else
                {
                    datas[u.id] = new TurretInfo(u.side, masterId, u.id);
                    changed     = true;
                }
            }

            if (removeKeys.Count == 0 && changed == false)
            {
                return;
            }

            foreach (var k in removeKeys)
            {
                datas.Remove(k);
            }

            turret.TurretsDatas = datas;
        }