コード例 #1
0
ファイル: AISetup.cs プロジェクト: manio143/JumpyJetV2
        public override async Task Execute()
        {
            await Task.Yield(); // don't execute initialization on call

            Start();

            // Wait for scripts to get initialized????
            await Script.NextFrame();

            await newGameEvent.ReceiveAsync();

            while (true)
            {
                await Script.NextFrame();

                DebugText.Print($"Generation: {generation}\nAlive: {dead.Count(d => !d)}/{ai.Count}\nScore: {score}\nHighscore: {highscore}", new Int2(20, 300));

                var profiler = Profiler.Begin(AIProfilingKey);

                // get current state
                float dist = 0, height = 0;
                PipesScript.ProvideAiInformation(ref dist, ref height);

                for (int i = 0; i < characterScripts.Length; i++)
                {
                    if (dead[i])
                    {
                        continue;
                    }

                    var character = characterScripts[i];

                    if (!character.isRunning || character.isDying)
                    {
                        dead[i] = true;
                        // disable collisions to save some processing time
                        characterPhysics[i].CanCollideWith = (CollisionFilterGroupFlags)0;
                        neat.AddWithScore(ai[i], score);
                        continue;
                    }

                    var position = character.Movement.Position.Y;

                    // we try to find a function that given to positions
                    // tries to jump so that they come close together
                    var aiResult = ai[i].Compute(neat.options, new double[] { position, height });
                    if (aiResult[0] > 0.5)
                    {
                        character.Jump();
                    }
                }

                score++;

                highscore = score > highscore ? score : highscore;

                if (dead.All(d => d))
                {
                    GlobalEvents.GameOver.Broadcast();
                    ResetRound();
                }

                profiler.End();
            }
        }