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