示例#1
0
        private bool IsAroundCollider(ITransformComponent transform, IMoverComponent mover,
                                      CollidableComponent collider)
        {
            foreach (var entity in _entityManager.GetEntitiesInRange(transform.Owner, mover.GrabRange, true))
            {
                if (entity == transform.Owner)
                {
                    continue; // Don't try to push off of yourself!
                }

                if (!entity.TryGetComponent <CollidableComponent>(out var otherCollider))
                {
                    continue;
                }

                // TODO: Item check.
                var touching = ((collider.CollisionMask & otherCollider.CollisionLayer) != 0x0 ||
                                (otherCollider.CollisionMask & collider.CollisionLayer) != 0x0) && // Ensure collision
                               true;    // !entity.HasComponent<ItemComponent>(); // This can't be an item

                if (touching)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#2
0
        /// <summary>
        ///     Used for weightlessness to determine if we are near a wall.
        /// </summary>
        /// <param name="broadPhaseSystem"></param>
        /// <param name="transform"></param>
        /// <param name="mover"></param>
        /// <param name="collider"></param>
        /// <returns></returns>
        public static bool IsAroundCollider(SharedBroadphaseSystem broadPhaseSystem, ITransformComponent transform, IMobMoverComponent mover, IPhysBody collider)
        {
            var enlargedAABB = collider.GetWorldAABB().Enlarged(mover.GrabRange);

            foreach (var otherCollider in broadPhaseSystem.GetCollidingEntities(transform.MapID, enlargedAABB))
            {
                if (otherCollider == collider)
                {
                    continue;                            // Don't try to push off of yourself!
                }
                // Only allow pushing off of anchored things that have collision.
                if (otherCollider.BodyType != BodyType.Static ||
                    !otherCollider.CanCollide ||
                    ((collider.CollisionMask & otherCollider.CollisionLayer) == 0 &&
                     (otherCollider.CollisionMask & collider.CollisionLayer) == 0) ||
                    (otherCollider.Owner.TryGetComponent(out SharedPullableComponent? pullable) && pullable.BeingPulled))
                {
                    continue;
                }

                return(true);
            }

            return(false);
        }
示例#3
0
        private void UpdateKinematics(ITransformComponent transform, PlayerInputMoverComponent mover, PhysicsComponent physics)
        {
            if (mover.VelocityDir.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner))
            {
                if (physics.LinearVelocity != Vector2.Zero)
                {
                    physics.LinearVelocity = Vector2.Zero;
                }
            }
            else
            {
                physics.LinearVelocity  = mover.VelocityDir * (mover.Sprinting ? mover.SprintMoveSpeed : mover.WalkMoveSpeed);
                transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();

                // Handle footsteps.
                var distance = transform.GridPosition.Distance(mover.LastPosition);
                mover.StepSoundDistance += distance;
                mover.LastPosition       = transform.GridPosition;
                float distanceNeeded;
                if (mover.Sprinting)
                {
                    distanceNeeded = StepSoundMoveDistanceRunning;
                }
                else
                {
                    distanceNeeded = StepSoundMoveDistanceWalking;
                }
                if (mover.StepSoundDistance > distanceNeeded)
                {
                    mover.StepSoundDistance = 0;
                    PlayFootstepSound(transform.GridPosition);
                }
            }
        }
示例#4
0
 public override void Initialize()
 {
     base.Initialize();
     transform         = Owner.GetComponent <ITransformComponent>();
     transform.OnMove += OnMove;
     MapID             = transform.MapID;
 }
示例#5
0
        private void Charge(ITankInputComponent input, ITransformComponent transform, ILaunchForceComponent launchForce, ITime time)
        {
            float chargeSpeed = (launchForce.MaxLaunchForce - launchForce.MinLaunchForce) / launchForce.MaxChargeTime;

            if (launchForce.CurrentLaunchForce >= launchForce.MaxLaunchForce && !input.Fired)
            {
                launchForce.CurrentLaunchForce = launchForce.MaxLaunchForce;
                Fire(transform, launchForce.CurrentLaunchForce);
                input.Fired = true;
                launchForce.CurrentLaunchForce = launchForce.MinLaunchForce;
            }
            else if (input.GetFireButtonDown)
            {
                input.Fired = false;
                launchForce.CurrentLaunchForce = launchForce.MinLaunchForce;
            }
            else if (input.GetFireButton && !input.Fired)
            {
                launchForce.CurrentLaunchForce += chargeSpeed * time.DeltaTime;
            }
            else if (input.GetFireButtonUp && !input.Fired)
            {
                Fire(transform, launchForce.CurrentLaunchForce);
                input.Fired = true;
                launchForce.CurrentLaunchForce = launchForce.MinLaunchForce;
            }
        }
示例#6
0
 /// <summary>
 ///     Constructs a new state snapshot of a TransformComponent.
 /// </summary>
 /// <param name="position">Current position offset of the entity.</param>
 /// <param name="rotation">Current direction offset of the entity.</param>
 /// <param name="parent">Current parent transform of this entity.</param>
 public TransformComponentState(Vector2 position, Angle rotation, ITransformComponent parent)
     : base(NetIDs.TRANSFORM)
 {
     Position = position;
     Rotation = rotation;
     Parent   = parent;
 }
        public override void Initialize()
        {
            base.Initialize();

            Sprite    = Owner.GetComponent <SpriteComponent>();
            Transform = Owner.GetComponent <ITransformComponent>();
        }
示例#8
0
        private void UpdatePosition(EntityUid euid, ITransformComponent transform, SnapGridComponent snapComp)
        {
            if (snapComp.LastGrid != GridId.Invalid)
            {
                if (!_mapManager.TryGetGrid(snapComp.LastGrid, out var lastGrid))
                {
                    Logger.WarningS("go.comp.snapgrid", "Entity {0} snapgrid didn't find grid {1}. Race condition?", euid, transform.GridID);
                    return;
                }

                lastGrid.RemoveFromSnapGridCell(snapComp.LastTileIndices, euid);
            }

            if (!_mapManager.TryGetGrid(transform.GridID, out var grid))
            {
                // Either a race condition, or we're not on any grids.
                return;
            }

            var oldPos  = snapComp.LastTileIndices;
            var oldGrid = snapComp.LastGrid;

            var newPos  = grid.TileIndicesFor(transform.MapPosition);
            var newGrid = transform.GridID;

            grid.AddToSnapGridCell(newPos, euid);

            if (oldPos != newPos || oldGrid != newGrid)
            {
                RaiseLocalEvent(euid, new SnapGridPositionChangedEvent(newPos, oldPos, newGrid, oldGrid));
            }

            snapComp.LastTileIndices = newPos;
            snapComp.LastGrid        = newGrid;
        }
        protected void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, IPhysicsComponent physics,
                                        ICollidableComponent?collider = null)
        {
            physics.EnsureController <MoverController>();

            var weightless = !transform.Owner.HasComponent <MovementIgnoreGravityComponent>() &&
                             _physicsManager.IsWeightless(transform.GridPosition);

            if (weightless && collider != null)
            {
                // No gravity: is our entity touching anything?
                var touching = IsAroundCollider(transform, mover, collider);

                if (!touching)
                {
                    return;
                }
            }

            // TODO: movement check.
            var(walkDir, sprintDir) = mover.VelocityDir;
            var combined = walkDir + sprintDir;

            if (combined.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner) && !weightless)
            {
                if (physics.TryGetController(out MoverController controller))
                {
                    controller.StopMoving();
                }
            }
            else
            {
                //Console.WriteLine($"{IoCManager.Resolve<IGameTiming>().TickStamp}: {combined}");

                if (weightless)
                {
                    if (physics.TryGetController(out MoverController controller))
                    {
                        controller.Push(combined, mover.CurrentPushSpeed);
                    }

                    transform.LocalRotation = walkDir.GetDir().ToAngle();
                    return;
                }

                var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;
                //Console.WriteLine($"{walkDir} ({mover.CurrentWalkSpeed}) + {sprintDir} ({mover.CurrentSprintSpeed}): {total}");

                { if (physics.TryGetController(out MoverController controller))
                  {
                      controller.Move(total, 1);
                  }
                }

                transform.LocalRotation = total.GetDir().ToAngle();

                HandleFootsteps(mover);
            }
        }
 public bool IsMapTransform(ITransformComponent transform)
 {
     if (transform.Parent != null)
     {
         return(false);
     }
     return(true);
 }
示例#11
0
        #pragma warning restore 649

        public override void Initialize()
        {
            base.Initialize();

            Sprite    = Owner.GetComponent <SpriteComponent>();
            Transform = Owner.GetComponent <ITransformComponent>();
            var systemman = IoCManager.Resolve <IEntitySystemManager>();
        }
 public static void AttachParentToContainerOrGrid(this ITransformComponent transform)
 {
     if (transform.Parent == null ||
         !TryGetContainer(transform.Parent.Owner, out var container) ||
         !TryInsertIntoContainer(transform, container))
     {
         transform.AttachToGridOrMap();
     }
 }
示例#13
0
        private void Fire(ITransformComponent transform, float lauchForce)
        {
            GameObject go = _GameObjectFactory.Build(_ShellPrefab);

            go.transform.position = transform.Position;
            go.transform.rotation = transform.Rotation;
            go.GetComponent <Rigidbody>().velocity = lauchForce * transform.Forward;
            _EntityFactory.BuildEntity <ShellEntityDescriptor>(go.GetInstanceID(), new object[] { });
        }
        protected void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, IPhysicsComponent physics)
        {
            physics.EnsureController <MoverController>();

            var weightless = transform.Owner.IsWeightless();

            if (weightless)
            {
                // No gravity: is our entity touching anything?
                var touching = IsAroundCollider(transform, mover, physics);

                if (!touching)
                {
                    transform.LocalRotation = physics.LinearVelocity.GetDir().ToAngle();
                    return;
                }
            }

            // TODO: movement check.
            var(walkDir, sprintDir) = mover.VelocityDir;
            var combined = walkDir + sprintDir;

            if (combined.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner) && !weightless)
            {
                if (physics.TryGetController(out MoverController controller))
                {
                    controller.StopMoving();
                }
            }
            else
            {
                if (weightless)
                {
                    if (physics.TryGetController(out MoverController controller))
                    {
                        controller.Push(combined, mover.CurrentPushSpeed);
                    }

                    transform.LocalRotation = physics.LinearVelocity.GetDir().ToAngle();
                    return;
                }

                var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;

                {
                    if (physics.TryGetController(out MoverController controller))
                    {
                        controller.Move(total, 1);
                    }
                }

                transform.LocalRotation = total.GetDir().ToAngle();

                HandleFootsteps(mover);
            }
        }
        /// <summary>
        /// Sets another entity as the parent entity.
        /// </summary>
        /// <param name="parent"></param>
        private void AttachParent(ITransformComponent parent)
        {
            // nothing to attach to.
            if (parent == null)
            {
                return;
            }

            Parent = parent;
        }
示例#16
0
        /// <summary>
        /// Sets another entity as the parent entity.
        /// </summary>
        /// <param name="parent"></param>
        protected virtual void AttachParent(ITransformComponent parent)
        {
            // nothing to attach to.
            if (parent == null)
            {
                return;
            }

            Parent = parent;
        }
        /// <summary>
        /// Detaches this entity from its parent.
        /// </summary>
        private void DetachParent()
        {
            // nothing to do
            if (Parent == null)
            {
                return;
            }

            Parent = null;
        }
示例#18
0
        private void UpdateKinematics(ITransformComponent transform, IMoverComponent mover, PhysicsComponent physics)
        {
            if (mover.VelocityDir.LengthSquared < 0.001 || !ActionBlockerSystem.CanMove(mover.Owner))
            {
                if (physics.LinearVelocity != Vector2.Zero)
                {
                    physics.LinearVelocity = Vector2.Zero;
                }
            }
            else
            {
                physics.LinearVelocity  = mover.VelocityDir * (mover.Sprinting ? mover.CurrentSprintSpeed : mover.CurrentWalkSpeed);
                transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();

                // Handle footsteps.
                if (_mapManager.GridExists(mover.LastPosition.GridID))
                {
                    // Can happen when teleporting between grids.
                    var distance = transform.GridPosition.Distance(_mapManager, mover.LastPosition);
                    mover.StepSoundDistance += distance;
                }

                mover.LastPosition = transform.GridPosition;
                float distanceNeeded;
                if (mover.Sprinting)
                {
                    distanceNeeded = StepSoundMoveDistanceRunning;
                }
                else
                {
                    distanceNeeded = StepSoundMoveDistanceWalking;
                }
                if (mover.StepSoundDistance > distanceNeeded)
                {
                    mover.StepSoundDistance = 0;

                    if (!mover.Owner.HasComponent <FootstepSoundComponent>())
                    {
                        return;
                    }

                    if (mover.Owner.TryGetComponent <InventoryComponent>(out var inventory) &&
                        inventory.TryGetSlotItem <ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item) &&
                        item.Owner.TryGetComponent <FootstepModifierComponent>(out var modifier))
                    {
                        modifier.PlayFootstep();
                    }
                    else
                    {
                        PlayFootstepSound(transform.GridPosition);
                    }
                }
            }
示例#19
0
        protected override void AttachParent(ITransformComponent parent)
        {
            if (parent == null)
            {
                return;
            }

            base.AttachParent(parent);
            SceneNode.GetParent().RemoveChild(SceneNode);
            ((IGodotTransformComponent)parent).SceneNode.AddChild(SceneNode);
            UpdateSceneVisibility();
        }
示例#20
0
        public static void AttachToGrandparent(this ITransformComponent transform)
        {
            var grandParent = transform.Parent?.Parent;

            if (grandParent == null)
            {
                transform.AttachToGridOrMap();
                return;
            }

            transform.AttachParent(grandParent);
        }
示例#21
0
        public static Vector2 GlobalLinearVelocity(this IEntity entity)
        {
            Vector2 result = new Vector2();

            for (ITransformComponent transform = entity.Transform; transform.Parent != null; transform = transform.Parent)
            {
                if (transform.Owner.TryGetComponent(out PhysicsComponent? physicsComponent))
                {
                    result += physicsComponent.LinearVelocity;
                }
            }

            return(result);
        }
        private static LowWallComponent?FindLowWall(IMapManager mapManager, ITransformComponent transform)
        {
            var grid   = mapManager.GetGrid(transform.GridID);
            var coords = transform.Coordinates;

            foreach (var entity in grid.GetLocal(coords))
            {
                if (transform.Owner.EntityManager.ComponentManager.TryGetComponent(entity, out LowWallComponent? lowWall))
                {
                    return(lowWall);
                }
            }
            return(null);
        }
示例#23
0
 /// <summary>
 ///     Does this entity contain the entity in the argument
 /// </summary>
 public bool ContainsEntity(ITransformComponent transform)
 {
     if (transform.IsMapTransform)     //Is the entity on the map
     {
         if (this == transform.Parent) //Is this the direct container of the entity
         {
             return(true);
         }
         else
         {
             return(ContainsEntity(transform.Parent)); //Recursively search up the entitys containers for this object
         }
     }
     return(false);
 }
示例#24
0
 private static void UpdateKinematics(ITransformComponent transform, PlayerInputMoverComponent mover, PhysicsComponent physics)
 {
     if (mover.VelocityDir.LengthSquared < 0.001)
     {
         if (physics.LinearVelocity != Vector2.Zero)
         {
             physics.LinearVelocity = Vector2.Zero;
         }
     }
     else
     {
         physics.LinearVelocity  = mover.VelocityDir * (mover.Sprinting ? mover.SprintMoveSpeed : mover.WalkMoveSpeed);
         transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();
     }
 }
        private static bool TryInsertIntoContainer(this ITransformComponent transform, IContainer container)
        {
            if (container.Insert(transform.Owner))
            {
                return(true);
            }

            if (container.Owner.Transform.Parent != null &&
                TryGetContainer(container.Owner, out var newContainer))
            {
                return(TryInsertIntoContainer(transform, newContainer));
            }

            return(false);
        }
示例#26
0
        private Vector2f?calculateNewPosition(IEntity entity, Vector2f newPosition, ITransformComponent transform)
        {
            //Check for collision
            var  collider = entity.GetComponent <ColliderComponent>();
            bool collided = collider.TryCollision(newPosition - transform.Position, true);

            if (!collided)
            {
                return(newPosition);
            }

            // When modifying the movement diagonally we need to scale it down
            // as the magnitude is too high for cardinal movements
            float diagonalMovementScale = 0.75f;

            Vector2f newPositionX = newPosition;

            newPositionX.X = transform.Position.X;
            bool collidedX = collider.TryCollision(newPositionX - transform.Position, true);

            if (!collidedX)
            {
                // Add back the lost speed from colliding with the wall
                // but because we was moving diagonally we need to scale it down
                newPositionX.Y += (newPositionX.Y - transform.Position.Y) * diagonalMovementScale;
                return(newPositionX);
            }

            Vector2f newPositionY = newPosition;

            newPositionY.Y = transform.Position.Y;
            bool collidedY = collider.TryCollision(newPositionY - transform.Position, true);

            if (!collidedY)
            {
                // Add back the lost speed from colliding with the wall
                // but because we was moving diagonally we need to scale it down
                newPositionY.X += (newPositionY.X - transform.Position.X) * diagonalMovementScale;
                return(newPositionY);
            }

            return(null);
        }
        /// <summary>
        ///     Verify that the subscribed clients are still in range of the entity.
        /// </summary>
        /// <param name="transformComp">Transform Component of the entity being checked.</param>
        /// <param name="uiComp">UserInterface Component of entity being checked.</param>
        private void CheckRange(ITransformComponent transformComp, ServerUserInterfaceComponent uiComp)
        {
            foreach (var ui in uiComp.Interfaces)
            {
                // We have to cache the set of sessions because Unsubscribe modifies the original.
                _sessionCache.Clear();
                _sessionCache.AddRange(ui.SubscribedSessions);

                if (_sessionCache.Count == 0)
                {
                    continue;
                }

                var uiPos = transformComp.WorldPosition;
                var uiMap = transformComp.MapID;

                foreach (var session in _sessionCache)
                {
                    var attachedEntity = session.AttachedEntity;

                    // The component manages the set of sessions, so this invalid session should be removed soon.
                    if (attachedEntity == null || !attachedEntity.IsValid())
                    {
                        continue;
                    }

                    if (uiMap != attachedEntity.Transform.MapID)
                    {
                        ui.Close(session);
                        continue;
                    }

                    var distanceSquared = (uiPos - attachedEntity.Transform.WorldPosition).LengthSquared;
                    if (distanceSquared > MaxWindowRangeSquared)
                    {
                        ui.Close(session);
                    }
                }
            }
        }
        private bool IsAroundCollider(ITransformComponent transform, IMoverComponent mover,
            IPhysicsComponent collider)
        {
            foreach (var entity in _entityManager.GetEntitiesInRange(transform.Owner, mover.GrabRange, true))
            {
                if (entity == transform.Owner)
                {
                    continue; // Don't try to push off of yourself!
                }

                if (!entity.TryGetComponent<IPhysicsComponent>(out var otherCollider) ||
                    !otherCollider.CanCollide ||
                    (collider.CollisionMask & otherCollider.CollisionLayer) == 0)
                {
                    continue;
                }

                // Don't count pulled entities
                if (otherCollider.HasController<PullController>())
                {
                    continue;
                }

                // TODO: Item check.
                var touching = ((collider.CollisionMask & otherCollider.CollisionLayer) != 0x0
                                || (otherCollider.CollisionMask & collider.CollisionLayer) != 0x0) // Ensure collision
                               && !entity.HasComponent<IItemComponent>(); // This can't be an item

                if (touching)
                {
                    return true;
                }
            }

            return false;
        }
示例#29
0
 public override void Shutdown()
 {
     transform.OnMove -= OnMove;
     transform         = null;
     base.Shutdown();
 }
示例#30
0
 public static bool InvalidateTileAir(this ITransformComponent transform, AtmosphereSystem?atmosSystem = null)
 {
     return(InvalidateTileAir(transform.Coordinates));
 }