Example #1
0
 public static void ValidateResourceDefId(this GameDef gameDef, ResourceDefId resourceDefId, string hint)
 {
     if (!gameDef.Resources.Any(x => x.Id.Equals(resourceDefId)))
     {
         throw new InvalidGameDefException($"Resource '{resourceDefId}' not found. Check '{hint}'!");
     }
 }
Example #2
0
 public decimal GetAmount(PlayerId playerId, ResourceDefId resourceDefId)
 {
     if (Res(playerId).TryGetValue(resourceDefId, out var value))
     {
         return(value);
     }
     return(0);
 }
Example #3
0
        private void DeductResourceUnchecked(PlayerId playerId, ResourceDefId resourceDefId, decimal value)
        {
            if (value < 0)
            {
                throw new InvalidGameDefException("Resource cost cannot be negative.");
            }
            if (!Res(playerId).ContainsKey(resourceDefId))
            {
                throw new CannotAffordException(Cost.FromSingle(resourceDefId, value));
            }

            // deduct cost
            Res(playerId)[resourceDefId] -= value;
        }
Example #4
0
        public decimal AddResources(PlayerId playerId, ResourceDefId resourceDefId, decimal value)
        {
            // TODO: synchronize
            var playerRes = Res(playerId);

            if (!playerRes.ContainsKey(resourceDefId))
            {
                playerRes.Add(resourceDefId, 0);
            }
            else
            {
                playerRes[resourceDefId] += value;
            }
            return(playerRes[resourceDefId]);
        }
Example #5
0
        public void SetProperty(string name, string value)
        {
            switch (name)
            {
            case "worker-units":
                workerUnit = new UnitDefId(value);
                gameDef.ValidateUnitDefId(workerUnit, $"{Name}.{name}");
                break;

            case "growth-resource":
                growthResource = new ResourceDefId(value);
                gameDef.ValidateResourceDefId(growthResource, $"{Name}.{name}");
                break;

            case "constraint-resource":
                constraintResource = new ResourceDefId(value);
                gameDef.ValidateResourceDefId(constraintResource, $"{Name}.{name}");
                break;

            default:
                throw new InvalidGameDefException($"Property '{name}' not valid for GameTickModule '{this.Name}'.");
            }
        }
Example #6
0
 public void DeductCost(PlayerId playerId, ResourceDefId resourceDefId, decimal value) => DeductCost(playerId, Cost.FromSingle(resourceDefId, value));
Example #7
0
 private void VerifyResource(GameDef gameDef, PlayerId playerId, ResourceDefId resourceDefId)
 {
     gameDef.ValidateResourceDefId(resourceDefId, $"Player '{playerId}' Resources");
 }
Example #8
0
 public static ResourceDef?GetResourceDef(this GameDef gameDef, ResourceDefId resourceDefId)
 {
     return(gameDef.Resources.SingleOrDefault(x => x.Id.Equals(resourceDefId)));
 }