J
J
JoshuaLight2017-02-07 14:34:25
Programming
JoshuaLight, 2017-02-07 14:34:25

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
{

}

Actually, how to classify further, and is it necessary, given how many different types of behavior can show through here and there?
For example, you can put clothes on a creature. Clothes are Thing . But it can be worn not only on a creature, but also on a mannequin, which is also a Thing .
Another example: weapons. If you continue like this:
public class Weapon : Thing
{
  public int Damage;
}

public abstract class MusicalInstrument : Thing
{
  public abstract void PlayMusic();
}

How then to show that the guitar can be both a musical instrument and a weapon, depending on the desire and context? It seems that inheritance doesn't help.
If we encapsulate behavior in objects, as in the Strategy pattern, then where should we place them? Answering such questions, I came to the conclusion that only the KOP will help, because. I want dynamic entities with dynamic behavior, but maybe there are practices that I don't know about yet that allow me to deal with this kind of complexity.
Update 2 : unfortunately and to my surprise, the question was misunderstood by many and most likely the reason for this was its ambiguity and complexity of interpretation.
One way or another, for those who, nevertheless, found something familiar in it, which any game developer would definitely encounter, trying to realize the childhood dream of a realistic game universe, something similar to, say, the TES series (than it and I was so attracted at one time) and many others, I decided to provide a few tips regarding where to look for the answer:
1. COP - component-oriented programming. Examples: Unity. Hence ES (Entity systems) - Artemis (C#).
2. AOP - aspect-oriented programming. Judging by the description, in theory , it can help.
3. It seems ( read only half ), in the book by Chris Partridge Business objects: Re-engineering for Re-useabsolutely the same problems are revealed, and a complete description of how you can reflash the paradigm of thinking in a different way, more similar to reality, is given.
4. There are also suspicions that the answers will be in the book Object Thinking .
Thank you all for your replies and good luck!

Answer the question

In order to leave comments, you need to log in

12 answer(s)
E
Egor Padalka, 2017-02-08
@ehs

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))

R
Rafael™, 2017-02-07
@maxminimus

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/

Y
Yuri, 2017-02-07
@riky

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.

S
sim3x, 2017-02-07
@sim3x

Design simple
Make it more complex
So far you have too much theorization and too little code

X
xmoonlight, 2017-02-07
@xmoonlight

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.

1. Solve the problem of processing an object as a composite element from different materials, including all physical and chemical interactions.
2. Estimate the current PCs, the capabilities of the engines, the resources expended and the power of the video cards per one average object.
3. Estimate the labor costs for creating a prototype of such an object: warming up at the guitar, etc.
4. If the desire does not disappear, but the capacities allow, a lot of DISTRIBUTED human resources will be needed to fill the world with such items.
The amount of work is huge!
About the code:
An object is a collection of instances of objects of "atomic classes" that has its own "tree" of dependencies and a list of states (guitar, firewood, ruler, machine gun, etc.) and takes one of these states at a particular moment in time.
The states can be reversible and not reversible (therefore, the "tree" of dependencies): that is, it is impossible to play on a guitar broken for firewood, and it is also impossible to assemble a working guitar from firewood.

J
Jomeisama, 2017-02-10
@Jomeisama

Object Oriented Thinking (2014)
By Matt Weisfeld

K
Kirill Romanov, 2017-02-07
@Djaler

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.

W
woodapiary, 2017-02-07
@woodapiary

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.

C
Cawich, 2017-02-07
@Cawich

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 .....

M
Maxim Timofeev, 2017-02-08
@webinar

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.

M
maxstroy, 2017-02-10
@maxstroy

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

A
Andrey Vorobyov, 2017-02-16
@Santacruz

composition vs inheritance

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question