private void Update(object sender, EventArgs e) { if (Gui.Focus != null && Gui.Focus.Enabled == false) { Gui.Focus = null; } if (RootControl.Enabled) { RootControl.Update(Ihn.Instance); } #if DEBUG if (KeyHelper.KeyDown(Keys.F4) && (KeyHelper.KeyDown(Keys.LeftAlt) || KeyHelper.KeyDown(Keys.RightAlt))) { Ihn.Instance.Exit(); } #endif }
/// <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); } }