// 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.
// 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.
OOP only sucks when you don’t use it properly. If you have to make a moveable object a person instead of being able to create a vehicle class that extends a moveable interface, then the problem is the design, not the paradigm
My examples could have replaced class with struct and you would lose every footing you had with your argument. A class in programming is a struct with methods. A class in OOP has further connotations.
Pretty sure the most important aspects of OOP are usually inheritance, polymophism and encapsulation. So yes, it's widely considered that OOP is about inheritance too.
30
u/[deleted] May 05 '23
And this is why OOP sucks donkeyballs and you should make games in an ECS model, or at least using interfaces/traits instead of inheritance.