private void GetUnits(List <ScenarioUnit> units, string section)
 {
     foreach (var kv in config.Enumerate(section))
     {
         var data = kv.Value.Split(',');
         if (data.Length < 7)
         {
             continue;
         }
         var house     = data[0];
         var technoId  = data[1];
         var health    = int.Parse(data[2]);
         var cell      = CPos.FromCell(ushort.Parse(data[3]));
         var facing    = new BinaryAngle(int.Parse(data[4]));
         var mission   = data[5];
         var triggerId = data[6];
         units.Add(new ScenarioUnit {
             house     = house,
             technoId  = technoId,
             health    = health,
             cell      = cell,
             subcell   = -1,
             facing    = facing,
             mission   = mission,
             triggerId = triggerId,
         });
     }
 }
        // [INFANTRY]
        // num=country,type,health,cell,sub_cell,action,facing,trig
        public List <ScenarioUnit> GetInfantry()
        {
            var units = new List <ScenarioUnit>();

            foreach (var kv in config.Enumerate("INFANTRY"))
            {
                var data = kv.Value.Split(',');
                if (data.Length < 8)
                {
                    continue;
                }
                var house     = data[0];
                var technoId  = data[1];
                var health    = int.Parse(data[2]);
                var cell      = CPos.FromCell(ushort.Parse(data[3]));
                var subcell   = int.Parse(data[4]);
                var mission   = data[5];
                var facing    = new BinaryAngle(int.Parse(data[6]));
                var triggerId = data[7];
                units.Add(new ScenarioUnit {
                    house     = house,
                    technoId  = technoId,
                    health    = health,
                    cell      = cell,
                    subcell   = subcell,
                    facing    = facing,
                    mission   = mission,
                    triggerId = triggerId,
                });
            }
            return(units);
        }
        // [STRUCTURES]
        // num=country,type,health,cell,facing,trig,sellabe,repairable
        public List <ScenarioStructure> GetStructures()
        {
            var structures = new List <ScenarioStructure>();

            foreach (var kv in config.Enumerate("STRUCTURES"))
            {
                var data = kv.Value.Split(',');
                if (data.Length < 8)
                {
                    continue;
                }
                var country    = data[0];
                var technoId   = data[1];
                var health     = int.Parse(data[2]);
                var cell       = CPos.FromCell(ushort.Parse(data[3]));
                var facing     = new BinaryAngle(int.Parse(data[4]));
                var triggerId  = data[5];
                var sellable   = data[6] == "1";
                var repairable = data[7] == "1";
                structures.Add(new ScenarioStructure {
                    country    = country,
                    technoId   = technoId,
                    health     = health,
                    cell       = cell,
                    facing     = facing,
                    triggerId  = triggerId,
                    sellable   = sellable,
                    repairable = repairable,
                });
            }
            return(structures);
        }
示例#4
0
        private void Process(Entity entity)
        {
            var pose            = this.world.Registry.Get <PoseComponent>(entity);
            var loco            = this.world.Registry.Get <DriveComponent>(entity);
            var position        = pose.Position;
            var facing          = pose.Facing;
            var speedType       = loco.SpeedType;
            var speed           = loco.Speed;
            var state           = loco.State;
            var flowField       = loco.FlowField;
            var movementVector  = loco.MovementVector;
            var destination     = loco.Destination;
            var currentPosition = position.V1;
            var nextPosition    = position.V1;
            var currentCell     = CPos.FromXPos(currentPosition);

            // The PreMove state determines the next cell in the path and whether the
            // final destination has been reached.
            if (state == DriveState.PreMove)
            {
                CardinalDirection dir;

                // destination reached -> Idle
                if (flowField == null || flowField.IsDestination(currentCell))
                {
                    state     = DriveState.Idle;
                    flowField = null;
                }
                // Get the next cell in the path and the movement direction and speed -> Moving
                else if (flowField.TryGetDirection(currentCell, out dir))
                {
                    var dirvec = dir.ToVector();
                    var spd    = flowField.SpeedAt(currentCell, speedType, speed);
                    movementVector = dirvec.Multiply(spd);
                    destination    = XPos.FromCell(currentCell.Translate(dirvec.X, dirvec.Y));
                    facing         = new BinaryAngle(dir);
                    state          = DriveState.Moving;
                }
                // Destination not reached but no next cell found -> Stuck
                else
                {
                    state     = DriveState.Stuck;
                    flowField = null;
                    Console.WriteLine("I'm stuck!");
                }
            }

            // The Moving state moves towards the destination cell at the predetermined speed,
            // then it will transition the state back to PreMove.
            if (state == DriveState.Moving)
            {
                var diff        = XPos.Sub(destination, currentPosition);
                var contheading = true;

                var movlepx = Lepton.FromPixel(movementVector.X);
                var movlepy = Lepton.FromPixel(movementVector.Y);

                // reaching end of cell
                if (Math.Abs(diff.LeptonsX) < Math.Abs(movlepx) || Math.Abs(diff.LeptonsY) < Math.Abs(movlepy))
                {
                    // check if next cell in the path follows the same direction
                    var dirvec = facing.CardinalDirection.ToVector();
                    var next   = currentCell.Translate(dirvec.X, dirvec.Y);
                    CardinalDirection nextdir;
                    var notAtEndOfPath = flowField.TryGetDirection(next, out nextdir);
                    contheading = notAtEndOfPath && (nextdir == facing.CardinalDirection);
                    state       = DriveState.PreMove;
                }

                if (contheading)
                {
                    nextPosition = XPos.Add(currentPosition, XPos.FromLeptons(movlepx, movlepy));
                }
                else
                {
                    nextPosition = destination;
                }
            }

            // Write the new state to the registry.
            position = position.Advance(nextPosition);
            pose     = new PoseComponent(position, facing);
            loco     = new DriveComponent(speedType, speed, state, flowField, movementVector, destination);
            this.world.Registry.Set(entity, pose);
            this.world.Registry.Set(entity, loco);
        }