예제 #1
0
 static void MoveY(Ihn ihn, Entity entity, ComponentPosition pos, ComponentVelocity velocity)
 {
     pos.Y += velocity.Y;
     if (CollisionHelper.Colliding(ihn, entity))
     {
         pos.Y -= velocity.Y;
         int runs = 0;
         while (!CollisionHelper.Colliding(ihn, entity) && runs < Math.Abs(velocity.Y) + 2)
         {
             runs++;
             pos.Y += velocity.Y > 0 ? 1 : -1;
         }
         pos.Y -= velocity.Y > 0 ? 1 : -1;
     }
 }
예제 #2
0
        /// <summary>
        /// Moves the entity
        /// </summary>
        /// <param name="ihn">Ihn entity is contained in</param>
        /// <param name="entity">Entity to update</param>
        public void Update(Ihn ihn, Entity entity)
        {
            var pos  = entity.GetComp <ComponentPosition>();
            var move = entity.GetComp <ComponentKeyboardMovement>();
            ComponentVelocity velocity = null;

            if (entity.HasComp <ComponentVelocity>())
            {
                velocity = entity.GetComp <ComponentVelocity>();
            }

            bool n;
            bool e;
            bool s;
            bool w;

            n = KeyHelper.KeyDown(Keys.W);
            e = KeyHelper.KeyDown(Keys.D);
            s = KeyHelper.KeyDown(Keys.S);
            w = KeyHelper.KeyDown(Keys.A);

            Vector2 momentum = new Vector2();

            if (velocity == null)
            {
                pos.X      += e ? move.Speed * move.HorizontalModifier : 0;
                momentum.X += e ? move.Speed * move.HorizontalModifier : 0;
                if (CollisionHelper.Colliding(ihn, entity))
                {
                    pos.X -= e ? move.Speed * move.HorizontalModifier : 0;
                }
                pos.X      += w ? -move.Speed * move.HorizontalModifier : 0;
                momentum.X += w ? -move.Speed * move.HorizontalModifier : 0;
                if (CollisionHelper.Colliding(ihn, entity))
                {
                    pos.X -= w ? -move.Speed * move.HorizontalModifier : 0;
                }
                pos.Y      += s ?  move.Speed * move.VerticalModifier : 0;
                momentum.Y += s ? move.Speed * move.VerticalModifier : 0;
                if (CollisionHelper.Colliding(ihn, entity))
                {
                    pos.Y -= s ? move.Speed * move.VerticalModifier : 0;
                }
                pos.Y      += n ? -move.Speed * move.VerticalModifier : 0;
                momentum.Y += n ? -move.Speed * move.VerticalModifier : 0;
                if (CollisionHelper.Colliding(ihn, entity))
                {
                    pos.Y -= n ? -move.Speed * move.VerticalModifier : 0;
                }
            }
            else
            {
                velocity.X += e ? move.Speed * move.HorizontalModifier : 0;
                momentum.X += e ? move.Speed * move.HorizontalModifier : 0;
                velocity.X += w ? -move.Speed * move.HorizontalModifier : 0;
                momentum.X += w ? -move.Speed * move.HorizontalModifier : 0;
                velocity.Y += s ?  move.Speed * move.VerticalModifier : 0;
                momentum.Y += s ? move.Speed * move.VerticalModifier : 0;
                velocity.Y += n ? -move.Speed * move.VerticalModifier : 0;
                momentum.Y += n ? -move.Speed * move.VerticalModifier : 0;
            }
            if (entity.HasComp <ComponentDirection>() && (momentum.X != 0 || momentum.Y != 0))
            {
                var dir = entity.GetComp <ComponentDirection>();
                dir.Dir = DirectionHelper.VectorToDirection(momentum);
            }
        }