public void OnTick(EngineEntity entity)
        {
            var movable = entity.Current <IMovable>();

            if (!movable.TargetPosition.HasValue)
            {
                return;
            }

            var transform = entity.Current <ITransform>();
            var diff      = movable.TargetPosition.Value - transform.Position;

            var distance = diff.Magnitude;

            // Detect when arrived at destination
            if (distance < movable.MoveSpeed * _timeService.Step)
            {
                _logger.Info($"Entity `{entity.Id}` has arrived at destination {movable.TargetPosition}");

                entity.Modify <IWriteableTransform>().Position     = movable.TargetPosition.Value;
                entity.Modify <IWriteableMovable>().TargetPosition = null;
                return;
            }

            var moveAmount = movable.MoveSpeed * _timeService.Step;

            entity.Modify <IWriteableTransform>().Position = transform.Position + moveAmount * diff.Normalized;
        }
Exemplo n.º 2
0
        public void EntityCommitsChangesToDirtyComponents()
        {
            var entity = new EngineEntity(0, new IComponent[] { new SimpleComponent() }, Mock.Of <IEventDispatcher>());

            var initialValue = entity.Current <ISimpleComponent>().Value;

            entity.Modify <IWriteableSimpleComponent>().Value = 5;
            entity.Current <ISimpleComponent>().Value.ShouldBe(initialValue);
            entity.Commit();
            entity.Current <ISimpleComponent>().Value.ShouldBe(5);
        }
Exemplo n.º 3
0
        public void EntityReturnsCorrectComponent()
        {
            var entity = new EngineEntity(0, new IComponent[] { new SimpleComponent() }, Mock.Of <IEventDispatcher>());

            var c = entity.Current <ISimpleComponent>();

            c.ShouldNotBeNull();
            c.ShouldBeOfType <SimpleComponent>();
        }
Exemplo n.º 4
0
        public void EntityThrowsOnNonExistantAccess()
        {
            var entity = new EngineEntity(0, new IComponent[] { new SimpleComponent() }, Mock.Of <IEventDispatcher>());

            Should.Throw <ComponentAccessException>(() => entity.Current <IOtherSimpleComponent>());
        }