예제 #1
0
        public void SetUnitPosition(int id, Coordinates coordinates)
        {
            try
            {
                var unit = PopulationList.FirstOrDefault(u => u.Id == id);
                if (unit == null || unit.Id == 0)
                {
                    unit = PopulationList.FirstOrDefault(u => u.Id == 0);
                    if (unit == null)
                    {
                        throw new Exception("SetUnitPosition: unit not found");
                    }
                    // Cas où il s'agit d'une nouvelle unité qui n'a pas encore d'Id
                    else
                    {
                        unit.Id = id;
                    }
                }

                unit.Position = coordinates;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="resource"></param>
        public void FetchResource(int workerId, int buildingId)
        {
            if (buildingId == 0 || workerId == 0)
            {
                throw new ArgumentNullException($"Manager FetchResource: workerId = {workerId} or buildingId = {buildingId} is missing");
            }

            // TODO: faire un try/catch pour valider le workerId comme étant un Id de Worker
            var worker   = PopulationList.FirstOrDefault(wk => wk.Id == workerId) as Worker;
            var building = BuildingList.FirstOrDefault(bld => bld.Id == buildingId) as PassiveBuilding;

            if (building == null || worker == null)
            {
                throw new ArgumentException("Manager FetchResource: workerId or buildingId does not exist");
            }

            // Annule la tâche en cours
            CancelTask(workerId);

            // Créé la tâche de collecter les ressources
            worker.FetchResource(building);

            // Capte l'evénement de collecte de ressources par ce worker
            worker.ResourceCollected += OnWorkerCompletedCollect;
            // worker.ResourceFetched += AddResourcesToStock;
        }
예제 #3
0
        /// <summary>
        /// Annule toutes les tâches en cours d'un worker
        /// </summary>
        /// <param name="workerId"></param>
        public void CancelTask(int workerId)
        {
            // TODO: faire un try/catch pour valider le workerId comme étant un Id de Worker
            var worker = PopulationList.FirstOrDefault(wk => wk.Id == workerId) as Worker;

            if (worker.IsWorking)
            {
                worker.CancelAllActions();
            }
        }