Skip to main content

IMovable

Written by: Akram Taghavi-Burris | © Copyright 2024
Status

In our game, the IMovable interface provides a clear and consistent structure for defining required movement across different game objects. By enforcing specific movement-related methods and properties, we can standardize how objects move, regardless of their unique behaviors or characteristics.

note

Why "IMovable": Interfaces are typically named with a prefix of "I" to clearly identify that it is an interface. The remainder of the name often describes a capability or behavior, usually in the form of an adjective, such as "Movable," indicating something that can move.

In the context of our game, using an interface such as IMovable can be beneficial in the following ways:

  • Clearly Defined Scope: The IMovable interface explicitly defines the core properties and methods required for movement, such as Speed, Direction, Move(), and ReverseDirection(). This ensures any implementing class has essential movement tools without unrelated functionality.

  • Establishes Consistency: By defining a common interface for movement, the IMovable ensures all game objects consistently manage speed, direction, and movement behavior, reducing potential errors across entities.

  • Enhances Modularity: IMovable decouples movement logic from specific implementations. Any game object requiring movement can implement the interface, preserving unique behavior and allowing for easy updates to movement logic.

  • Facilitates Reusability and Scalability: The interface simplifies reusing movement functionality across various game objects. Adding new objects that require movement (e.g., vehicles or projectiles) only requires implementing the IMovable interface without rewriting complex logic.

  • Simplifies Testing and Maintenance: An interface streamlines testing and maintenance. Since all movable objects adhere to the same contract, global changes can be made by updating the interface or its implementations, making debugging easier.

Defining the Interface

In game development, interfaces provide a structured approach to define movement behavior for game objects without imposing specific implementations. Unlike classes, interfaces do not extend from other classes; they are standalone definitions.

Class vs Interface
//Declaration of a class
public class ClassName : MonoBehaviour { }

//Declaration of an Interface
public interface IMovable { }

Properties and Methods

When designing an interface such as IMovable, it's essential to consider the properties and methods required by all game objects that implement this interface (i.e., game objects that need to move).

  • Move Method: The primary purpose of the IMovable interface is to define how objects move, which is why a Move() method is required. While the implementation of this method may vary across different classes, it typically relies on fundamental concepts such as speed and direction to update the object's position effectively.

  • Speed Property: This property allows us to get or set the speed of the object. By defining speed as a property, we can easily adjust how fast our objects move without altering the underlying logic.

  • Direction Property: The direction property represents the movement vector of the object. This enables flexible movement in any direction, allowing us to dynamically change the vector based on game conditions.

info

Property vs Field: In interfaces, properties are preferred over fields because they define a contract that requires implementing classes to provide their own backing fields. This approach allows for greater flexibility and encapsulation, enabling access control and validation when getting or setting values. By using properties in an interface, we ensure that any class implementing the interface adheres to the expected behavior while allowing for custom implementations that can include logic for managing state or enforcing constraints.

  • ReverseDirection Method: At some point, the direction of a moving object may need to change. The ReverseDirection() method allows for quick reversal of movement direction, making it useful for dynamic gameplay mechanics, such as bouncing off walls or retreating from threats.

  • Stop Method: A Stop() method can be useful for halting the movement of an object without altering its speed or direction properties. This method can enable gameplay mechanics where objects need to be temporarily halted, such as pausing during gameplay or stopping for user input.

  • Reset Method: Adding a Reset() method allows developers to return the object's state to its initial conditions, including position, speed, and direction. This functionality is particularly useful in scenarios where objects need to restart their movement patterns or be reset after a specific event, such as a player respawning.

IMovable
using UnityEngine;

public interface IMovable
{
// Property to get or set the speed of the object
float Speed { get; set; }

// Property to get or set the direction of movement for the object
Vector3 Direction { get; set; }

// Method to move the object based on its speed and direction
void Move();

// Method to reverse the current direction of the object
void ReverseDirection();

// Method to stop the movement of the object
void Stop();

// Method to reset the object to its initial state
void Reset();
}