Need Help with my Class Structure

Need Help with my Class Structure
0.0 0

#1

I have a class called GBody
It has many things inside like sprite, physics body, various required variables etc

I have a class called GGRoup
Group is a collection of GBodies, so I have a vector inside of GBodies

I have already added include GBody in GGroup

However, a GBody can possible be in N Groups and I cannot also include GGroup in GBody as causes error

I may have 100 Units on map.
20 and place in group 1
20 in group 2
20 in group 3
20 in group 4
20 in group 5

I have a group called “Selected” as when bodies are highlighted and selected they get placed in the selected body vector for

Each declared group has a function to select it such as a HUD button

Things that can happen

If I select group 5 button on HUD it highlights all group 5 bodies from its vector (Selected = group5)

If i drag my square selected over small parts of group clusters highlighting many bodies from many groups and tell them all move over somewhere, they all leave there group formation and form together where I told them

This is a standard strategy mechanic

Part of the problem is how I update every object. Cycle through all groups->cycling through all bodies to call update doesn’t seem best choice. Cycling through a world vector of objects to tell them update seems better. But then they may need to know what groups they are part of, but we cannot include group into body because body is already included in group. Bodies that are part of a group are to move together and require group center and group heading so seem that they require group info

Any tips of class structure please ? Looking for help as i am not sure. Project will be shared at the end

I have been thinking to have some kinda world vector for action groups. Once anything is selected and told move, they all get put into a new action group, and we update action groups bodies separately. Once group reaches target and formation control is not required, we can empty action group and delete.

My brain is melting


#2

Draw it as a diagram and post it :slight_smile:


#3

Good Idea. I will make clear Photoshop image tomorrow and add to post. Ill make a good one


#4

It will help you think and us to visualize it. Sometimes you can read and read and all you really needed was a picture.


#5

To begin, I want to clarify. Does GGroup has a vector of objects or a vector of pointers to objects?
If it is a question of pointers, then I do not see why not GGroup has vector of pointers to GBodys, and GBody has pointer to its GGroup (or maybe 2 pointers - the primary group, and the selected group).
This is how the parent-child relationship works in cocos2d-x. See Node::addChild().

When you select units and send somewhere then you already have a ready list of candidates to update their status, do not you?
Or maybe Observer pattern will be a good option.


#6

I will check out this observer pattern. I will also draw a image to demonstrate my structure so far. Maybe if you and Slack can have look, as the over all project is intended to share with everyone and hopefully be some use.

Pointers to objects

A body (Game object) can be in many groups and be called upon by there group object so it has to have vector of pointers to objects
Each body requires group information as to help with the groups formations such as flocking, square formation lines or circle. So the group needs to mediate to its group objects.
But if a body is actually in many groups which it can be, such as group 1 and selected group, we cannot cycle through groups and tell each group to tell all its bodies to update as there may be double updating.

Keeping every body in a single group only seems tedious

My plan now seems to be keep a vector of action groups. The vector of action groups is cycled through and updated, which tells all included bodies to update, but this will be a velocity update only. So when anything is selected and told move, a action group is created and added to the action group vector. Once group reacts target, that action group is cleared and set to not active (Variable). So we can now make this action group available again. The array will only ever hold a certain amount of groups, which will be at most , the most amount of active action groups that were active at any one time, other wise there recycled.

And also I cycle through every object in scene in a every object vector and tell them update. The two different types of updates will do different things , one handles velocity, group formations… While the other handles all the other body things

This means objects that are in a action group are been updated twice per frame for different reasons .

Ill be back with a image tomorrow


#7

Maybe pointers to groups then?

As I said, I am not very good with strategies. Could you give an example of what groups this can be? Like Officers, Squadron 1, Selected or what?

It sounds good.

In addition, as I understand units can die and new ones can be hired, but the maximum number of units must be fixed, right? Then Object Pool may be useful for you as well.

I am waiting for your images tomorrow. Good night. :slight_smile:


#8

Like what?


#9

Like rotating towards enemy, moving towards nearby enemy to attack, aim and start shooting nearby enemy. move into a better position to start attack enemy(like gathering around a enemy, outnumbered)

Not entirely sure yet but there is few i think


#10

Got it, thanks!