public Account(bool isVerified, bool isOpen, bool isFrozen, decimal initialAmount, string holder, Action onUnFreeze) { if (isVerified) { _state |= AccountState.Verfied; } if (isOpen) { _state |= AccountState.Open; } else { _state |= AccountState.Close; _state &= ~AccountState.Open; } if (isFrozen) { this.Freezable = new Frozen(onUnFreeze); } else { this.Freezable = new UnFrozen(); } _amount = initialAmount; _holder = holder; this.onUnFreeze = onUnFreeze; }
private void GoToTempCommands() { if (command.Length > 3) { throw new ArgumentException("Wrong command"); } else if ("down" == command[2].ToLower()) { if (deviceList[command[0]] is IFreezable) { IFreezable device = (IFreezable)deviceList[command[0]]; device.FreezDown(); } else { throw new ArgumentException("Wrong device"); } } else if ("up" == command[2].ToLower()) { if (deviceList[command[0]] is IFreezable) { IFreezable device = (IFreezable)deviceList[command[0]]; device.FreezUp(); } else { throw new ArgumentException("Wrong device"); } } else { throw new ArgumentException("Wrong command"); } }
public static void ThrowIfFrozen(IFreezable freezable) { if (freezable.IsFrozen) { throw new InvalidOperationException("Cannot mutate frozen " + freezable.GetType().Name); } }
public static void Freeze(object item) { if (item is IFreezable) { IFreezable f = item as IFreezable; f.Freeze(); } }
// we do not need it anymore, because changing the state become the interface responsibility // private Action ManageUnfreezing { get; set; } public Account4(Action onUnfreeze) { // Account class will not be responsible for it any more. //this.OnUnfreeze = onUnfreeze; // we do not need it anymore, because changing the state become the interface responsibility // this.ManageUnfreezing = this.StayUnfrozen; this.Freezable = new Active(onUnfreeze); }
public void Deposit(decimal amount) { if (IsClosed) { return; } Freezable = Freezable.Deposit(); Balance += amount; }
public static void Freeze(object item) { IFreezable f = item as IFreezable; if (f != null) { f.Freeze(); } }
private void OnTriggerEnter2D(Collider2D collision) { IFreezable freezable = collision.gameObject.GetComponent <IFreezable>(); if (freezable != null) { Freeze(freezable); //Debug.Log("Freezing " + collision.gameObject.name); } }
public void Intercept(IInvocation invocation) { IFreezable obj = (IFreezable)invocation.InvocationTarget; if (obj.IsFrozen && invocation.Method.Name.StartsWith("set_", StringComparison.OrdinalIgnoreCase)) { throw new NotSupportedException("Target is frozen"); } invocation.Proceed(); }
public static bool SetRefProperty <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value) where TProperty : class { if (ReferenceEquals(thisProperty, value)) { return(false); } that.ThrowIfFrozen(); thisProperty = value; return(true); }
public static bool SetValueProperty <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value) where TProperty : struct { if (thisProperty.Equals(value)) { return(false); } that.ThrowIfFrozen(); thisProperty = value; return(true); }
private void Freeze(IFreezable freezable) { if (freezable != null && !collisions.Contains(freezable)) { if (isActive) { freezable.Freeze(); } collisions.Add(freezable); } }
public Exception ValidateValue(object value, string locationName, LocationKind locationKind) { IFreezable freezable = value as IFreezable; if (freezable != null) { freezable.Freeze(); } return(null); }
public void Freeze() { if (!IsVerified) { return; } if (IsClosed) { return; } Freezable = Freezable.Freeeze(); }
protected override void OnCollisionEnter2D(Collision2D collision) { IFreezable freezable = collision.gameObject.GetComponent <IFreezable>(); IDamageable damageable = collision.gameObject.GetComponent <IDamageable>(); if (freezable != null) { damageable.TakeDamage(player.bow.dmg); freezable.Freze(duration); } Destroy(gameObject); }
private void OnTriggerExit2D(Collider2D collision) { IFreezable freezable = collision.gameObject.GetComponent <IFreezable>(); if (freezable != null) { if (isActive) { freezable.UnFreeze(); } collisions.Remove(freezable); } }
private void OnParticleCollision(GameObject other) { IFreezable fre = other.GetComponent <IFreezable>(); rand = Random.Range(1, 100); if (fre != null) { fre.SlownDown(5); if (rand == 50) { fre.Freze(3); } } }
public static bool SetRefPropertyOnce <TProperty>(this IFreezable that, ref TProperty thisProperty, TProperty value, [CallerMemberName] string callerMemberName = null) where TProperty : class { if (ReferenceEquals(thisProperty, value)) { return(false); } if ((object)thisProperty != null) { throw new System.ArgumentException($"{that.GetType().Name}.{callerMemberName} is already set."); } thisProperty = value; return(true); }
public void Withdraw(decimal amount) { if (!IsVerified) { return; } if (IsClosed) { return; } Freezable = Freezable.Withdraw(); //Withdraw money Balance -= amount; }
public static bool SetValueProperty <TProperty>(this IFreezable that, ref Nullable <TProperty> thisProperty, Nullable <TProperty> value) where TProperty : struct { if (thisProperty.HasValue && value.HasValue && thisProperty.Value.Equals(value.Value)) { return(false); } if (!thisProperty.HasValue && !value.HasValue) { return(false); } that.ThrowIfFrozen(); thisProperty = value; return(true); }
//[System.Diagnostics.DebuggerStepThrough] //[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] public static bool SetStringProperty(this IFreezable that, ref string thisProperty, string value, string nameOfProperty = null) { //if (value == string.Empty) { value = null; } if (ReferenceEquals(thisProperty, value)) { return(false); } if (string.Equals(thisProperty, value, System.StringComparison.Ordinal)) { return(false); } that.ThrowIfFrozen(nameOfProperty); thisProperty = value; return(true); }
public static TProperty CreateOrGetCachedObject <TProperty, TArg0>(this IFreezable that, ref TProperty thisProperty, TArg0 arg0, Func <TArg0, TProperty> generator) where TProperty : class { var result = thisProperty; if (result is null) { result = generator(arg0); if (@that.IsFrozen()) { thisProperty = result; } } return(result); }
public void Freeze() { if (this.IsClosed) { return; } if (!this.IsVerified) { return; } // we do not need it anymore, because changing the state become the interface responsibility //this.ManageUnfreezing = this.Unfreeze; this.Freezable = this.Freezable.Freeze(); }
public void Deposit(decimal amount) { if (this.IsClosed) { return; } // we do not need it anymore, because changing the state become the interface responsibility //this.ManageUnfreezing(); // state change is now operated by Freezable.Deposit() // it will change the state and return the result for us. this.Freezable = this.Freezable.Deposit(); this.Balance += amount; }
public static void ThrowIfNotFrozen(this IFreezable that, string name = null) { if ((object)that != null) { if (!(that.IsFrozen())) { if (name is null) { throw new InvalidOperationException($"{that.GetType().FullName} is NOT frozen."); } else { throw new InvalidOperationException($"{name} is NOT frozen."); } } } }
public static void TestFreeze(IFreezable sut) { var properties = sut.GetType().GetRuntimeProperties() .Where(x => x.CanWrite && x.CanRead && x.Name != "IsFrozen"); foreach (var property in properties) { var value = property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null; property.SetValue(sut, value); var result = property.GetValue(sut); Assert.Equal(value, result); } sut.Freeze(); foreach (var property in properties) { var value = property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null; Assert.Throws <InvalidOperationException>(() => { try { property.SetValue(sut, value); } catch (Exception ex) { throw ex.InnerException; } }); } }
public static void SetOrThrowIfFrozen <T>(this IFreezable that, ref T target, T value, string name = null) { if ((object)that != null) { if (that.IsFrozen()) { if (name is null) { throw new InvalidOperationException($"{that.GetType().FullName} is frozen."); } else { throw new InvalidOperationException($"{name} is frozen."); } } } target = value; }
public override void Attack(GameObject source, Vector3 contactPoint, float AttackArg1, float AttackArg2, float AttackArg3, float AttackArg4, Collider collider, GameObject effect, GameObject effect2) { //float Damage, float speedDecreasePercent, float time Collider[] colliders = Physics.OverlapSphere(contactPoint, AttackArg4); int hitNum = 0; try{ foreach (Collider hit in colliders) { IFreezable freezableObject = hit.GetComponent <IFreezable>(); if (freezableObject != null) { freezableObject.TakeIce(AttackArg1, AttackArg2, AttackArg3, contactPoint, effect); hitNum++; } else if (string.Compare(hit.tag, "Enemy") == 0) { IFreezable freezableObject2 = hit.transform.parent.parent.parent.parent.GetComponent <IFreezable>(); if (freezableObject2 != null) { freezableObject2.TakeIce(AttackArg1, AttackArg2, AttackArg3, contactPoint, effect); } hitNum++; } } }catch (NullReferenceException e) {} Destroy(Instantiate(effect.gameObject, source.transform.position + 0.2f * Vector3.up, Quaternion.identity) as GameObject, 3f); if (hitNum > 0) { if (effect2 != null) { Destroy(Instantiate(effect2.gameObject, source.transform.position + 0.2f * Vector3.up, Quaternion.identity) as GameObject, 3f); } } //Destroy(source); source.SetActive(false); }
protected override void OnCollisionEnter2D(Collision2D collision) { IFreezable freezable = collision.gameObject.GetComponent <IFreezable>(); IDamageable damageable = collision.gameObject.GetComponent <IDamageable>(); if (freezable != null) { damageable.TakeDamage(player.bow.dmg); int rand = Random.Range(1, 100); if (rand < slowChance) { freezable.SlownDown(slowDuration); } if (rand < frezeChance) { freezable.Freze(frezeDuration); } } Destroy(gameObject); }
public void Deposit(decimal amount) { if (_state.HasFlag(AccountState.Close)) { Console.WriteLine("La cuenta se encuentra cerrada"); throw new AccountClosedException(); } if (_state.HasFlag(AccountState.Open) && !_state.HasFlag(AccountState.Verfied)) { Console.WriteLine("La cuenta no se encuentra verificada"); throw new AccountNotVerifiedException(); } if (_state.HasFlag(AccountState.Open) && _state.HasFlag(AccountState.Verfied)) { deposit(amount); } this.Freezable = this.Freezable.Deposit(); }