void OnModified(FuelModifier modifier)
        {
            var current = writer.Data.Fuel;
            var max     = writer.Data.MaxFuel;

            switch (modifier.Type)
            {
            case FuelModifyType.Consume:
            case FuelModifyType.Absorb:     current -= modifier.Amount;  break;

            case FuelModifyType.Feed:       current += modifier.Amount;  break;
            }

            current = Mathf.Clamp(current, 0, max);

            writer.SendUpdate(new FuelComponent.Update()
            {
                Fuel = current,
            });
        }
예제 #2
0
        bool DealOrder(UnitInfo unit, FuelModifyType type, ref FuelComponent.Component fuel)
        {
            FuelComponent.Component?comp = null;
            if (TryGetComponent(unit.id, out comp) == false)
            {
                return(false);
            }

            var t_fuel = comp.Value;

            int num = 0;

            switch (type)
            {
            case FuelModifyType.Feed:
                num        = Mathf.Clamp(t_fuel.EmptyCapacity(), 0, fuel.Fuel);
                fuel.Fuel -= num;
                break;

            case FuelModifyType.Absorb:
                num        = Mathf.Clamp(fuel.EmptyCapacity(), 0, t_fuel.Fuel);
                fuel.Fuel += num;
                break;

            default:
                return(false);
            }

            var modify = new FuelModifier {
                Type   = type,
                Amount = num,
            };

            this.UpdateSystem.SendEvent(new FuelComponent.FuelModified.Event(modify), unit.id);
            return(true);
        }
        protected override void OnUpdate()
        {
            Entities.With(group).ForEach((Unity.Entities.Entity entity,
                                          ref FuelServer.Component server,
                                          ref FuelComponent.Component fuel,
                                          ref BaseUnitStatus.Component status,
                                          ref SpatialEntityId entityId) =>
            {
                if (status.State != UnitState.Alive)
                {
                    return;
                }

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

                if (fuel.Fuel == 0)
                {
                    return;
                }

                var inter = server.Interval;
                if (inter.CheckTime() == false)
                {
                    return;
                }

                server.Interval = inter;

                float range  = server.Range;
                int baseFeed = server.FeedRate;
                int current  = fuel.Fuel;
                current     += server.GainRate;

                var trans = EntityManager.GetComponentObject <Transform>(entity);
                var pos   = trans.position;
                var list  = getAllyUnits(status.Side, pos, range, UnitType.Soldier, UnitType.Commander);
                foreach (var unit in list)
                {
                    FuelComponent.Component?comp = null;
                    if (TryGetComponent(unit.id, out comp))
                    {
                        var f   = comp.Value.Fuel;
                        var max = comp.Value.MaxFuel;
                        if (f >= max)
                        {
                            continue;
                        }

                        var num = Mathf.Clamp(max - f, 0, baseFeed);
                        if (current < num)
                        {
                            continue;
                        }

                        current -= num;

                        var modify = new FuelModifier
                        {
                            Type   = FuelModifyType.Feed,
                            Amount = num,
                        };
                        this.UpdateSystem.SendEvent(new FuelComponent.FuelModified.Event(modify), unit.id);
                    }

                    GunComponent.Component?gun = null;
                    if (TryGetComponent(unit.id, out gun))
                    {
                        var dic = gun.Value.GunsDic;
                        foreach (var kvp in dic)
                        {
                            var info = kvp.Value;
                            var b    = info.StockBullets;
                            var max  = info.StockMax;
                            if (b >= max)
                            {
                                continue;
                            }

                            var feed = baseFeed / rate;
                            var num  = Mathf.Clamp(max - b, 0, feed);
                            if (current < num * feed)
                            {
                                continue;
                            }

                            current   -= num * feed;
                            var supply = new SupplyBulletInfo {
                                GunId     = info.GunId,
                                GunTypeId = info.GunTypeId,
                                Attached  = kvp.Key,
                                Amount    = num,
                            };
                            this.UpdateSystem.SendEvent(new GunComponent.BulletSupplied.Event(supply), unit.id);
                            break;
                        }
                    }
                }

                if (fuel.Fuel != current)
                {
                    fuel.Fuel = current;
                }
            });
        }