Ejemplo n.º 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}'!");
     }
 }
Ejemplo n.º 2
0
 public decimal GetAmount(PlayerId playerId, ResourceDefId resourceDefId)
 {
     if (Res(playerId).TryGetValue(resourceDefId, out var value))
     {
         return(value);
     }
     return(0);
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 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]);
        }
Ejemplo n.º 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}'.");
            }
        }
Ejemplo n.º 6
0
 public void DeductCost(PlayerId playerId, ResourceDefId resourceDefId, decimal value) => DeductCost(playerId, Cost.FromSingle(resourceDefId, value));
Ejemplo n.º 7
0
 private void VerifyResource(GameDef gameDef, PlayerId playerId, ResourceDefId resourceDefId)
 {
     gameDef.ValidateResourceDefId(resourceDefId, $"Player '{playerId}' Resources");
 }
Ejemplo n.º 8
0
 public static ResourceDef?GetResourceDef(this GameDef gameDef, ResourceDefId resourceDefId)
 {
     return(gameDef.Resources.SingleOrDefault(x => x.Id.Equals(resourceDefId)));
 }