using System; using System.Collections.Generic; using System.Linq; using Duality; using Duality.Components; namespace TopDownMovement { /// /// This component gives the GameObject it is attached to the ability /// to move within the scene. /// // This attribute indicates that this component requires another // component to be attached to the same GameObject. [RequiredComponent(typeof(Transform))] public class Movable : Component { // The speed at which this object will move. private float moveSpeed = 1f; public Movable mv { get; set; } /// /// This function is what does all the moving. It's parameters default /// to "false" (optional parameters) so we can just set the parameter /// we need and the rest will stay the same. /// /// Whether to move forwards. /// Whether to move to the right. /// Whether to move to the left. /// Whether to move backwards. public void Move (bool forward = false, bool right = false, bool left = false, bool backward = false) { // Here we define a "totalMovement" variable, which is the total movement // that will be applied to this Movable. Vector2 totalMovement = Vector2.Zero; // Here we add the respective movement axes multiplied with the movement // speed, based on the parameters passed to this function. if (forward == true) totalMovement += -Vector2.UnitY * this.moveSpeed; if (right == true) totalMovement += Vector2.UnitX * this.moveSpeed; if (left == true) totalMovement += -Vector2.UnitX * this.moveSpeed; if (backward == true) totalMovement += Vector2.UnitY * this.moveSpeed; // If the added movement is not zero, then move! if (totalMovement != Vector2.Zero) this.GameObj.Transform.MoveBy(totalMovement); } } }