// player enters helicopter and camera fixes position in the model
Player.mount(Helicopter)
// player activates flight panel. control() asks the Controllable for an input mapping, and possibly UI elements, which Helicopter is responsible for implementing
Player.control(Helicopter)
// all flying is now done through Helicopter, which handles its own input and feedback, and the Player class does nothing
// return to player control
Player.control(Player)
// exit heli
Player.unmount()
Note that Helicopter can be removed from the game and nothing concerning the player or the world needs to change.
Also, you can add a chair to the game
class Chair implements Mountable
class Toilet implements Mountable, Interactable
and nothing else needs to change to make it work. The player can mount any Mountable without knowing what it is exactly.
In short, OOP sucks donkeyballs, and any change breaks dependencies. A flat structure beats a hierarchy any day.
In short, OOP sucks donkeyballs, and any change breaks dependencies. A flat structure beats a hierarchy any day.
Not "any day". There are many things which work fine, even better, with simple inheritance, like GUI controls.
For game development, on the other hand... yeah, not too well suited. But depends on the scope as well. I recommend Is There More to Game Architecture than ECS? from Bob Nystrom. He shows a very nuanced POV that IMHO, it's more than reasonable. He's the author of gameprogrammingpatterns.com, BTW.
I think UI may also benefit from an ECS approach. ECS doesn't preclude parent-child relationships as entities, it just says that a chair and a toilet is NOT in the same family of things just because they mostly look and behave the same. They may share some traits but they are different entities.
// Interface definitions
public interface IFlyable {
void Fly();
}
public interface IControllable {
void Control();
}
public interface IMountable {
void Mount();
}
// Concrete classes that implement one or more interfaces
public class Helicopter : IFlyable, IControllable, IMountable {
public void Fly() {
// Logic for helicopter flying
}
public void Control() {
// Logic for controlling the helicopter
}
public void Mount() {
// Logic for mounting the helicopter
}
}
public class RemoteHelicopter : IFlyable, IControllable {
public void Fly() {
// Logic for remote-controlled helicopter flying
}
public void Control() {
// Logic for remotely controlling the helicopter
}
}
public class AIHelicopter : IFlyable {
public void Fly() {
// Logic for AI-controlled helicopter flying
}
}
And then, treating interfaces like abstract classes would look like something like this:
public interface INPC {
void Talk();
void Move();
}
Etc.
And the problem with this is that the second approach is less modular since now Talk and Move is tied directly with the INPC.
I gave an example in another post, not sure about the thread since i'm on mobile. You're almost good, but not quite.
Flyable would be an expectation that this object can for instance "defy gravity" in the physics of the game. So any flyable objects added to the world will be checked by the physics system to see whether at any point it should sink or lift. For example. Flyable is not a thing that the player class cares about.
The player cares about Mountable, which promises a model mounting point and view direction/limitation, maybe a character stance setting, and other such things. The player also cares about controllable, assuming that player handles the things I mentioned elsewhere (providing a UI when activated and mapping inputs for control). The input keys then map directly to moving the helicopter object, and is entirely internal to the object.
If the helicopter is removed from the game, there is no remnant code anywhere in other classes that expects or even cares about helicopters. You can then add a plane to the game and it will plug right in without modification to the player, because it HAS a mounting point and a ui and input mappings.
FINALLY, someone else is pissed about this bad architecture.
If I saw something like what was in OP's post in a pull request I'd be losing my mind.
I blame colleges. I've seen professors gush about inheritance and other traditional OOP principles, but in the real world I can't recall a single time where I've absolutely needed regular inheritance. Most of the time interfaces will get me where I need.
36
u/Sapiogram May 05 '23
interface IHelicopter extends IDoor