示例#1
0
        IEnumerator ConsumingFood()
        {
            ReusableWaitForSecondsEnumerator wait = new ReusableWaitForSecondsEnumerator(0.1f);

            while (true)
            {
                var mealStructs =
                    entitiesDB.QueryEntities <MealEntityStruct>(GameGroups.FOOD, out var foodcount);

                for (var j = 0; j < foodcount; j++)
                {
                    mealStructs[j].mealLeft -= mealStructs[j].eaters;

                    mealStructs[j].eaters = 0;

                    if (mealStructs[j].mealLeft <= 0)
                    {
                        _entityFunctions.RemoveEntity <FoodEntityDescriptor>(mealStructs[j].ID);
                    }
                }

                while (wait.IsDone() == false)
                {
                    yield return(Yield.It);
                }
            }
        }
示例#2
0
        IEnumerator CheckClick()
        {
            var timer = new ReusableWaitForSecondsEnumerator(0.01f);

            while (true)
            {
                if (Input.GetMouseButton(0) == true)
                {
                    //I am cheating a bit with the MouseToPosition function, but for the purposes of this demo
                    //creating a Camera Entity was an overkill
                    if (UnityUtilities.MouseToPosition(out Vector3 position))
                    {
                        //BuildEntity returns an EntityInitialized that is used to set the default values of the
                        //entity that will be built.
                        for (int i = 0; i < 100; i++)
                        {
                            var newposition = new float3(position.x + Random.Range(-10, 10), position.y,
                                                         position.z + Random.Range(-10, 10));


                            var init = _entityFactory.BuildEntity <FoodEntityDescriptor>(_foodPlaced++, GameGroups.FOOD);

                            init.Init(new MealEntityStruct(10000));
                            init.Init(new PositionEntityStruct
                            {
                                position = newposition
                            });
                            init.Init(new UnityEcsEntityStructStruct
                            {
                                uecsEntity     = _food,
                                spawnPosition  = newposition,
                                unityComponent = ComponentType.ReadWrite <UnityECSFoodGroup>()
                            });
                        }

                        yield break;
                    }
                }

                while (timer.IsDone() == false)
                {
                    yield return(Yield.It);
                }
            }
        }
示例#3
0
        public IEnumerator <TaskContract> Execute(byte[] bodyRaw)
        {
            int attemptNumber = 0;

            do
            {
                using (UnityWebRequest request = new UnityWebRequest())
                {
                    switch (method)
                    {
                    case Method.GET:
                        request.method = UnityWebRequest.kHttpVerbGET;
                        break;

                    case Method.POST:
                        request.method = UnityWebRequest.kHttpVerbPOST;
                        break;

                    default:
                        request.method = UnityWebRequest.kHttpVerbPOST;
                        break;
                    }

                    request.url             = URL;
                    request.uploadHandler   = new UploadHandlerRaw(bodyRaw);
                    request.downloadHandler = new UnityDownloadHandler(responseHandler);
                    request.SetRequestHeader("Content-Type", "application/json");
                    request.timeout = timeoutSeconds;

                    AsyncOperation op = request.SendWebRequest();

                    while (op.isDone == false)
                    {
                        yield return(Yield.It);
                    }

                    if (request.isNetworkError == false)
                    {
                        if (ProcessResponse(request) == true)
                        {
                            result = Result.Success;

                            Svelto.Console.LogDebug("web request completed");

                            yield break;
                        }
                        else
                        {
                            Svelto.Console.LogDebug("web request completed with failure ", URL);

                            try
                            {
                                responseHandler?.CompleteContent();

                                result = Result.ServerHandledError;
                            }
                            catch
                            {
                                result = Result.ServerException;
                            }

                            yield break; //no retry on server error!
                        }
                    }
                    else
                    if (++attemptNumber < maxAttempts)
                    {
                        var wait = new ReusableWaitForSecondsEnumerator(waitOnRetrySeconds);

                        while (wait.MoveNext() == true)
                        {
                            yield return(Yield.It);
                        }

                        Svelto.Console.LogDebug("web request retry");

                        continue; //retry on client error
                    }
                    else
                    {
                        result = Result.ClientFailure;

                        yield break;
                    }
                }
            }while (true);
        }
示例#4
0
        IEnumerator CheckClick()
        {
            var timer = new ReusableWaitForSecondsEnumerator(0.01f);

            while (true)
            {
                //note: in a complex project an engine shouldn't ever poll input directly, it should instead poll
                //entity states
                if (Input.GetMouseButton(0) || Input.GetMouseButton(1) == true)
                {
                    var _random = new Random((uint)DateTime.Now.Ticks);
                    //I am cheating a bit with the MouseToPosition function, but for the purposes of this demo
                    //creating a Camera Entity was an overkill
                    if (UnityUtilities.MouseToPosition(out Vector3 position))
                    {
                        //BuildEntity returns an EntityInitialized that is used to set the default values of the
                        //entity that will be built.
                        for (int i = 0; i < MaxMeals; i++)
                        {
                            NativeEntityInitializer init;

                            var randX       = position.x + _random.NextFloat(-50, 50);
                            var randZ       = position.z + _random.NextFloat(-50, 50);
                            var newposition = new float3(randX, position.y, randZ);

                            bool isRed;

                            if (Input.GetMouseButton(0))
                            {
                                init = _entityFactory.BuildEntity(
                                    new EGID(_foodPlaced++, GameGroups.RED_FOOD_NOT_EATEN.BuildGroup)
                                    , Thread.CurrentThread.ManagedThreadId);
                                isRed = true;
                            }
                            else
                            {
                                init = _entityFactory.BuildEntity(
                                    new EGID(_foodPlaced++, GameGroups.BLUE_FOOD_NOT_EATEN.BuildGroup)
                                    , Thread.CurrentThread.ManagedThreadId);

                                isRed = false;
                            }

                            init.Init(new PositionEntityComponent
                            {
                                position = newposition
                            });
                            //these structs are used for ReactOnAdd callback to create unity Entities later
                            init.Init(new UnityEcsEntityComponent
                            {
                                uecsEntity      = isRed ? _redfood : _bluefood
                                , spawnPosition = newposition
                                ,
                            });
                        }

                        while (timer.IsDone() == false)
                        {
                            yield return(Yield.It);
                        }
                    }
                }

                yield return(Yield.It);
            }
        }
示例#5
0
        IEnumerator CheckClick()
        {
            var timer = new ReusableWaitForSecondsEnumerator(0.01f);

            while (true)
            {
                if (Input.GetMouseButton(0) || Input.GetMouseButton(1) == true)
                {
                    var _random = new Random((uint)DateTime.Now.Ticks);
                    //I am cheating a bit with the MouseToPosition function, but for the purposes of this demo
                    //creating a Camera Entity was an overkill
                    if (UnityUtilities.MouseToPosition(out Vector3 position))
                    {
                        //BuildEntity returns an EntityInitialized that is used to set the default values of the
                        //entity that will be built.
                        for (int i = 0; i < MaxMeals; i++)
                        {
                            EntityComponentInitializer init;

                            var randX       = position.x + _random.NextFloat(-50, 50);
                            var randZ       = position.z + _random.NextFloat(-50, 50);
                            var newposition = new float3(randX, position.y, randZ);

                            bool isRed;

                            if (Input.GetMouseButton(0))
                            {
                                init = _entityFactory.BuildEntity <FoodEntityDescriptor>(_foodPlaced++,
                                                                                         GroupCompound <GameGroups.FOOD, GameGroups.RED,
                                                                                                        GameGroups.NOTEATING> .BuildGroup);

                                isRed = true;
                            }
                            else
                            {
                                init = _entityFactory.BuildEntity <FoodEntityDescriptor>(_foodPlaced++,
                                                                                         GroupCompound <GameGroups.FOOD, GameGroups.BLUE,
                                                                                                        GameGroups.NOTEATING> .BuildGroup);

                                isRed = false;
                            }

                            init.Init(new PositionEntityComponent
                            {
                                position = newposition
                            });
                            //these structs are used for ReactOnAdd callback to create unity Entities later
                            init.Init(new UnityEcsEntityComponent
                            {
                                uecsEntity    = isRed ? _redfood : _bluefood,
                                spawnPosition = newposition,
                            });
                        }

                        while (timer.IsDone() == false)
                        {
                            yield return(Yield.It);
                        }
                    }
                }

                yield return(Yield.It);
            }
        }