Answer the question
In order to leave comments, you need to log in
How to model a complex game world using OOP?
Good day to all!
For the first time, I encountered the fact that I have almost no idea how to solve the problem of system modeling using OOP, and therefore I really need advice and recommendations from more experienced colleagues.
What is the actual question? I am constantly writing something for myself, cycling , so to speak, and now my eyes are directed to creating an RPG engine with incredibly high demands for realism. Well, so that you can pick up a stone from the ground, throw it at someone, or, for example, tear off a leg from a chair, sew a villain, or taste the snow (why not?) and much more.
All this is extremely intersects with reality, and therefore I started my path with the development of a system of ontological categories: `Entity`, `AbstractEntity`, `MaterialEntity`, `PhysicalObject`, and so on. And everything was fine until I seriously thought about what the surrounding objects really are?
For example, guitar . It's like a musical instrument , isn't it? Yes, that's how it's not. It can also be a weapon , and sometimes - firewood for the stove (if you really need it).
Unfortunately, OOP forces us to describe all behavioral characteristics in an object type, either using explicit methods or using interface implementations, but this, in my opinion, does not quite fit the current situation, because when analyzing this or that object, I come to the conclusion that the representation of an object as a whole (which is what OOP is about) is added together with the representation of its internal structure.
Those. instead of looking at a guitar and thinking: " Oh, it's a guitar, now how can I play it! ", I most likely think: " The visual properties of this object are associated with the already known concept of a guitar, which is associated with the concepts of a musical instrument, a tree, hilt, danger of breaking a string, etc. "
As a solution, I see: AOP + COP, but I'm not sure yet that this is not because my knowledge of OOP is too small.
In general, comrades and colleagues, I hope someone will help with advice and recommendations, at least with where to look for answers to your questions. Thank you!
Update 1: I 'll add some code.
public abstract class Entity
{
public int Guid;
}
public abstract class MaterialEntity : Entity
{
public Vector4 Position;
public Vector3 Size;
}
public abstract class PhysicalObject : Entity
{
public Mass Mass;
public Temperature Temperature;
}
// Существо.
public abstract class Creature : PhysicalObject
{
// Здоровье, мана и прочее.
}
// Предмет какой-нибудь.
public abstract class Thing : PhysicalObject
{
}
public class Weapon : Thing
{
public int Damage;
}
public abstract class MusicalInstrument : Thing
{
public abstract void PlayMusic();
}
Answer the question
In order to leave comments, you need to log in
And I would look at the problem from the other side. Just imagine your main character is still a baby, doesn’t know that you can play the guitar, throw firewood into the fire, don’t even know why a sandwich. He can pick up and eat a rock, a guitar, anything. So implement all the desired actions for the character, and give all objects only physical characteristics - size, weight, temperature, flammability, liquid. Let the hero play music on the stones! And your task will be to describe the possible logic of interactions, as in life, how to teach a child. Here the hero is trying to eat a guitar - it doesn’t fit into his mouth in size, puts a stone in - let him choke and die)
Well, on this sad note, I’ll probably finish))
your dogmas and patterns are wrong
"I coined the term 'object-oriented' and I assure you I didn't mean C++."
“I regret coining the term ‘objects’ many years ago because it forces people to focus on small ideas. The really big idea is messages.”
"The key to making systems large and scalable is to figure out how modules will communicate with each other, rather than worrying about their internal properties and behavior."
"I thought of objects as living cells, or as individual computers on a network that exchange messages."
“Object-oriented programming for me means only sending messages, holding and protecting locally, and hiding process-states, and linking everything extremely late. This can be done in Smalltalk and in LISP. Perhaps there are other systems where this is possible, but they are unknown to me.
https://habrahabr.ru/company/edison/blog/300922/
probably it would be possible to do this in the same way as components are assigned to GameObject in Unity. that is, universal objects for everything and any components that modify their behavior are added to them.
Design simple
Make it more complex
So far you have too much theorization and too little code
Well, so that you can pick up a stone from the ground, throw it at someone, or, for example, tear off a leg from a chair, sew a villain, or taste the snow (why not?) and much more.
In Java, for example, I would handle this with interfaces rather than abstract classes. For a class can implement many interfaces, and extend only one class. So, for example, the Guitar class can implement the Musical Instrument interface, which has a Play method, the Weapon interface, which has a Beat method, and so on.
Start designing the model with object attributes and relationships between objects/object containers. Leave behavioral characteristics for later. In other words, think about how to save and load the model into the database. You won’t hardcode that in your model at the start there are 5 guitars with 6 strings and all guitars are wooden).
This is such practical advice - to dance from real data.
Java has an Object object... and so, you don't need a more abstract entity.
Z.Y. on the topic - to implement an extensible model, you do not need to invent bicycles and crutches. read design patterns, read about IoC frameworks... everything is already described and painted there - how what and how much. the only problem is that few people manage to comply with the described rules completely, because "well, it's obviously easier and more reliable here." and then there are crutches, bicycles, plugs, unnecessary filters, etc .....
Why not take a look at how these engines are made? UnrealEngine, CryEngine and others. In fact, everything that you described is already there. It may be worth starting with an analysis of a well-made product in order to understand, in order to understand what and how.
A similar problem was solved by us in the field of security. There was some incident, the traces of which are known to us - smoke, for example. But we do not know whether it is a fire, or a chemical attack, or a grenade explosion during a terrorist attack. We must be able to model the facts we know, calculate the likelihood of certain consequences, and decide on the best course of action. All this cannot be squeezed into OOP. I myself am not a programmer, since my task is to create models for such cases. These models are created in the first and second level predicates. But those who put it in the code share their impressions that programming is better in functional programming languages, although, I repeat, I don’t understand anything about it
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question