In C#, you are not supposed to change the
variables of an object but its
properties instead. For example, Vector3 has the properties
x,
y,
z, and
length. Internally however, it only contains the variables
x_,
y_,
z_ and the properties are derived from them. x, y and z derive their values from x_, y_ and z_ individually. length however is derived from x_, y_ and z_ together.
Code: Select all
public class Vector3
{
...
public float length
{
get
{
return Mathf.Sqrt ( x * x + y * y + z * z );
}
set
{
float prev_length = length;
x *= value / prev_length;
y *= value / prev_length;
z *= value / prev_length;
}
}
...
}
What this does is provide another level of abstraction by allowing users to
configure an object through their properties rather than directly with their variables. This together with operator overloading and member functions to represent actions and reactions(Fight(), Fly(), OnDamaged() etc.), you have a large playground of syntax to design with. You can even add a
direction property that returns the normalised version of the Vector3 and also sets it to a different direction without changing the length.
Code: Select all
public void UpdateVelocity()
{
Vector3 position_to_target = target.position - position;
velocity.direction = position_to_target;
...
}
This one additional syntax incentivises you to design your classes in a way that you would not have otherwise. There is no C++ equivalent(in the same way there is no Java equivalent for operator overloading. I really hate that.)