Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Self-aware' classes?

long time reader first time poster. Please be gentle.

I avidly create games in VB.net. Being a hobby programmer I give my games away for free. Since I'm a hobbiest without formal training I do try and educate myself on proper programming principles and follow them when programming such as OOP. There are however some things I have obviously missed being self-taught that perhaps I would know if I were formally educated.

Once such issue that has really been my bane is how different classes (from now on referred to as objects) 'know' about each other WITHOUT programming specific interaction (stay with me here... please...)

Okay imagine this, you have a space game and the following objects

  • clsShip
  • clsMissile
  • clsAsteroid
  • clsSpaceJunk

Now each one of these objects has a Health, X,Y and Z private members and properties that are all inherited from clsSpaceEntity for argument sake. Now one way of programming collision between them would be to code out the following logic in the main game loop

for each ship in a list of ships
check each clsMissile in a list to see if it collides with it and if so, reduce health
check each clsAsteroidin a list to see if it collides with it and if so, reduce health
check each clsSpaceJunka list to see if it collides with it and if so, reduce health
... and so on and so on for every object next

ect ect ect...

Now this might seam okay if your talking about the simple example above but some of my games have tens or even HUNDREDS of objects that interact in this basic way.

Now my question to the experienced programmers out there.

Is there a way in OOP to do the following...

for each thing that inherits from clsSpaceEntity
check against every other object that inherits this type (except itself) and if they collide then reduce health
next

?

This sort of ability for a type of objects/class or whatever to be 'aware' of another and how they are the same/different and interact would save me tonnes and tonnes and TONNES of coding.

Any assistance/help or feedback on this would be very appreciated. Thank you for your time, sorry for the long read.

like image 310
Purge Anony Avatar asked Nov 01 '11 00:11

Purge Anony


People also ask

What is self-awareness classes?

What is Self Awareness? Self-awareness means being aware of one's own character and feelings. This course will provide learners with the knowledge and skills to self-reflect, understand emotional intelligence and the various learning styles.

What are the 5 areas of self-awareness?

Key areas for self-awareness include our personality traits, personal values, habits, emotions, and the psychological needs that drive our behaviors.

Can self-awareness be taught?

The good news is that self-awareness skills can be taught and practiced on a regular basis. Of course, it's important to note that self-awareness skills don't develop all at once. Learning them is a process that takes time and practice.

What are the 4 keys to self-awareness?

These four keys – being intentional, thinking differently, building skills, and changing your context – can make a vital difference in moving from passive self-awareness to dynamic action.


1 Answers

Instead of having a separate List(Of T) for each derived object, you should make a single List(Of SpaceEntity) that holds all of your entities.

You can then make a nested For Each loop that loops through every pair of entities and does collision checking. (after an If x <> y check)

You can further enhance your design by giving SpaceEntity a MustOverride function to handle collisions with other objects, without polluting your collision loop with separate logic for each entity.
You should probably use the visitor pattern to allow entities to react differently to collisions with different other kinds of entities.

Note that each collision will be sent to both objects that collided separately; you need to handle that in your logic.

like image 67
SLaks Avatar answered Nov 01 '22 17:11

SLaks