示例#1
0
        private Engine CreateEngine(CancellationToken cancellationToken, int processId)
        {
            var engine = new Engine(options => { options.DebugMode(); });

            engine.Step += (s, e) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                return(StepMode.Into);
            };
            engine.SetValue("KeyCode", TypeReference.CreateTypeReference(engine, typeof(KeyCode)));
            engine.SetValue("GetKey", new Func <KeyCode, bool>(code =>
            {
                var task = botCommands.GetKey(code);
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("GetKeyDown", new Func <KeyCode, bool>(code =>
            {
                var task = botCommands.GetKeyDown(code);
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("GetKeyUp", new Func <KeyCode, bool>(code =>
            {
                var task = botCommands.GetKeyUp(code);
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("Dir", TypeReference.CreateTypeReference(engine, typeof(Direction)));
            engine.SetValue("Pos", TypeReference.CreateTypeReference(engine, typeof(GridPosition)));
            engine.SetValue("TileType", TypeReference.CreateTypeReference(engine, typeof(TileType)));
            engine.SetValue("Coroutine",
                            new Action <uint, JsValue>((frameTime, jsfunc) =>
            {
                botCommands.Coroutine(frameTime, () => jsfunc.Invoke());
            }));
            engine.SetValue("Shot", new Action(() => botCommands.Shot()));
            engine.SetValue("Attack", new Action(() => botCommands.MeleeAttack()));

            engine.SetValue("Move", new Action <Direction, uint>((dir, mass) =>
                                                                 botCommands.Move(dir, mass).Wait()
                                                                 ));
            engine.SetValue("MoveDir", new Action <Direction>(dir =>
                                                              botCommands.MoveDirection(dir).Wait()
                                                              ));
            engine.SetValue("ShotDir", new Action <float>(dir =>
                                                          botCommands.MoveShotRotation(dir).Wait()
                                                          ));
            engine.SetValue("GetMyPos", new Func <GridPosition>(() =>
            {
                var task = botCommands.GetMyPosition();
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("GetPosAngle", new Func <GridPosition, float>(pos =>
            {
                var task = botCommands.GetPositionAngle(pos);
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("GetTileType", new Func <GridPosition, TileType>(pos =>
            {
                var task = botCommands.GetTileType(pos);
                task.Wait();
                return(task.Result);
            }));
            engine.SetValue("Print", new Action <object>(obj =>
                                                         ConsoleLogger.Log(DateTime.Now, processId, obj)
                                                         ));
            engine.SetValue("Wait", new Action <int>((milliSeconds) => Task.Delay(milliSeconds).Wait()));
            return(engine);
        }
 public void Coroutine(uint frameTime, Action action)
 {
     botCommands.Coroutine(frameTime, action);
 }