public void Move_RuleViolation_RaisesEvent() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "A".AsSpatialObject(1, 1); ISpatialRule rule = null; grid.RuleViolated += (s, e) => { rule = e.Rule; }; grid.AddConstraint((x, m) => m.Any(p => p.Y > 1), 1, 23); // Add a rule which prevents Y from exceeding 2 grid.SetObject(thing1); thing1.Move(Direction.Right); // 2, 1 Assert.That(rule == null); thing1.Move(Direction.Down); // try to move => 2, 2 Assert.That(thing1.TopLeft.Y, Is.EqualTo(1), "The Y value should remain unchanged"); Assert.That(thing1.TopLeft.X, Is.EqualTo(2), "The X value should remain unchanged"); Assert.That(rule.Id == 23); }
/// <summary> /// Adds a (type specific) constraint which will be executed before an object is moved /// </summary> /// <param name="condition">A predicate function which takes a /// thing and a proposed move as arguments and returns a boolean value. /// The rule will fire (will be violated) if the condition returns true</param> /// <param name="id">An optional id assigned to the rul</param> /// <param name="type">The type of rule</param> public ISpatialRule AddConstraint <R>(Func <ISpatial2D, IEnumerable <Point2D>, bool> condition, R type = default(R), int?id = null) { ISpatialRule rule = null; Write(() => { var idv = id.HasValue ? id.Value : (_movementRules.Any() ? _movementRules.Max(r => r.Value.Id) + 1 : 1); _movementRules[idv] = rule = new TypedSpatialRule <R>(idv, type, condition); }); return(rule); }
/// <summary> /// Adds a constraint which will be executed before an object is moved /// </summary> /// <param name="condition">A predicate function which takes a /// thing and a proposed move as arguments and returns a boolean value. /// The rule will fire (will be violated) if the condition returns true</param> /// <param name="id">An optional id assigned to the rul</param> public ISpatialRule AddConstraint(Func <ISpatial2D, IEnumerable <Point2D>, bool> condition) { ISpatialRule rule = null; Write(() => { var idv = _movementRules.Any() ? _movementRules.Max(r => r.Value.Id) + 1 : 1; _movementRules[idv] = rule = new TypedSpatialRule <byte>(idv, 0, condition); }); return(rule); }
public RuleViolationEvent(ISpatialRule rule, ISpatial2D target) { Rule = rule; Target = target; }