コード例 #1
0
ファイル: World.cs プロジェクト: lospec/lowresjam2020
        private void SpawnEnemy(EnemySpawn enemySpawn)
        {
            if (enemySpawn.enemies.Length == 0)
            {
                GD.PushWarning(
                    $"{enemySpawn} enemy spawn has no enemy names attached.");
                return;
            }

            if (enemySpawn.enemyNumber <= 0)
            {
                GD.PushWarning(
                    $"{enemySpawn} enemy spawn's enemyNumber not greater than 0");
                return;
            }

            for (var i = 0; i < enemySpawn.enemyNumber; i++)
            {
                var enemy     = (BaseEnemy)_baseEnemyScene.Instance();
                var safe      = false;
                var enemyName = enemySpawn.enemies.RandomElement();
                if (string.IsNullOrWhiteSpace(enemyName))
                {
                    GD.PushError(
                        $"{enemySpawn.Name} enemy spawn has a null enemy attached");
                    return;
                }

                var data = Autoload.Get <Data>();
                if (!data.enemyData.ContainsKey(enemyName))
                {
                    GD.PushError(
                        $"{enemySpawn} enemy spawn has a {enemyName} enemy with no data attached.");
                    return;
                }

                enemy.OnDependency = (ref BaseEnemy.EnemyDependencies dependency) =>
                {
                    dependency.EnemyName      = enemyName;
                    dependency.PlayerInstance = _player;
                };

                while (!safe)
                {
                    enemy.Position = enemySpawn.GetRandomGlobalPosition();
                    _map.CallDeferred("add_child", enemy);
                    safe = enemy.IsInAllowedTile();

                    if (!safe)
                    {
                        enemy.QueueFree();
                    }
                }

                enemy.Connect(nameof(BaseEnemy.Died), this, nameof(OnEnemy_Death),
                              new Array {
                    enemySpawn
                });
            }
        }