Jump to content

  • Log In with Google      Sign In   
  • Create Account


inheritance and state machine with flyweight pattern


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 gamer08   Members   -  Reputation: 115

Like
1Likes
Like

Posted 11 May 2013 - 08:43 AM

I everyone, i'm working on an indie game and i'm stuck with two problems

I have many similar units on screen so i decided to implement the flightWeight pattern so there is intrinsic and extrinsic parts

 

Q 1 : How to manage inheritance ?

If the game base class posses the object position and rotation for example and let says i have multiple inheritance level, where to put these attributes. I mean these attributes are state dependant so, it the should be the client to have these and manage it nut it the broke the inheritance. Any advices ?

 

Q 2 : How to manage state machine and behavior ?

My basic soldier unit have all the same same Ai behavior of cource ( they can attack, move, retreat, .. ) but their current state is not probably the same, one instance is moving when another is attacking. So wich class should posses the state machine and the behavior ?

 

I don't know if it's clear. Feel free to ask more details, i will explain

Thanks in advance for any advices or anything !

 



Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 2937

Like
1Likes
Like

Posted 13 May 2013 - 07:16 PM

Looks like no one wants to touch this.

 

Maybe you should try to explain your problem more clearly. Looking at both these questions I don't really see why you're having the problems that you're having. Maybe if we knew more about your current implementation we could give some advice.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#3 phil_t   Crossbones+   -  Reputation: 3234

Like
0Likes
Like

Posted 13 May 2013 - 08:26 PM

What's your motivation for using the flyweight pattern? What memory savings do you think you're achieving?



#4 gamer08   Members   -  Reputation: 115

Like
0Likes
Like

Posted 15 May 2013 - 10:16 AM

Well i'm working on a RTS bacteria game and these are the problem

 

1) I don't know what do to with the inheritance because i have multiple levels and normally my Game Base object should have the position the rotation,... but each instance of bacteria have their own position,rotation because this a dependant state property and my flyweight cannot handle it

 

so the question is : Is it normal the the inheritance can be duplicate or broken with flyweight pattern ?

 

2) Second, i don't yet implement the AI part and i would use probably state machine with different behavior  but i don't who should handle the machine and the behavior , is the Flyweight itselft ? or a client class ?. I mean each bacteria of the same type will have the same set of state and behavior, but they will have their own state machine or at least the current could be different. i am wrong ?

 

i don't know if it's more clear, i can explain with code i you want.

 

also if someone have a reference with example about pattern in video game, i have the GOF one's but it could be great to have an example how to implement flyweight or decorator

 

thanks for any help


Edited by gamer08, 15 May 2013 - 10:21 AM.


#5 phil_t   Crossbones+   -  Reputation: 3234

Like
0Likes
Like

Posted 15 May 2013 - 01:46 PM

Well i'm working on a RTS bacteria game and these are the problem

1) I don't know what do to with the inheritance because i have multiple levels and normally my Game Base object should have the position the rotation,... but each instance of bacteria have their own position,rotation because this a dependant state property and my flyweight cannot handle it


You just restated your problem without really giving us any more information.

My understanding is that you're storing position and rotation in some array in your main game class?
Why does this prevent you from using it for the bacteria class? Why does bacteria have its own position and rotation?

What language are you using? Maybe if you provided some code we could understand your problems better.

Edited by phil_t, 15 May 2013 - 01:47 PM.


#6 gamer08   Members   -  Reputation: 115

Like
0Likes
Like

Posted 15 May 2013 - 05:53 PM

Well i'm working on a RTS bacteria game and these are the problem

1) I don't know what do to with the inheritance because i have multiple levels and normally my Game Base object should have the position the rotation,... but each instance of bacteria have their own position,rotation because this a dependant state property and my flyweight cannot handle it


You just restated your problem without really giving us any more information.

My understanding is that you're storing position and rotation in some array in your main game class?
Why does this prevent you from using it for the bacteria class? Why does bacteria have its own position and rotation?

What language are you using? Maybe if you provided some code we could understand your problems better.

 

Ok first of all the langage is c#

second i use XNA 4.0

 

my Base class

abstract class Chemotaxis_GameObject
    {
        #region Constructor/Destructor

        protected Chemotaxis_GameObject(Texture2D texture, Vector2 origin)
        {
            this.texture = texture;
            this.origin = origin;
        }


        #endregion

        #region Attributes

        protected Texture2D texture;
       //protected Vector2 position;
       //protected float rotation;
        protected Vector2 origin;
       
        #endregion Attributes

        #region Functions

        public virtual void Update(GameTime gameTime){}
        public virtual void Render(SpriteBatch spriteBatch,Vector2 pos, float rot)
        {
            spriteBatch.Draw(texture, pos, null, Color.White, rot, origin, 1.0f, SpriteEffects.None, 1.0f);
        }

        #endregion
    }

 

i'll keep it simple. Normally i would set the position, rotation,... in this class but the're all state dependant (extrinsic i think)

 

one of my flyweight abstract class

 

  abstract class Chemotaxis_Bacteria : Chemotaxis_GameObject
    {
        #region Constructor/Destructor

        protected Chemotaxis_Bacteria(Texture2D texture, Vector2 origin, Chemotaxis_Bacteria_Type type, Chemotaxis_Sensor sensor, float speed)
        :base(texture,origin)
        {
            this.type = type;
            this.sensor = sensor;
            this.speed = speed;
        }

        virtual ~Chemotaxis_Bacteria()
        { 
        }

        #endregion

        #region Attributes

        //protected Chemotaxis_StateMachine stateMachine;
        protected Chemotaxis_Bacteria_Type type;
        protected Chemotaxis_Sensor sensor;
        protected float speed;

        #endregion

        public abstract void Update(GameTime gameTime, ref Vector2 position,float rotation);
    }

 

so i implement this class on multiples concrete flyweight and use a factory to create and return them when a flyweight client is create

 

a flyweight client

 

 class Chemotaxis_BacteriaClient
    {
        #region Constructor/Destructor

        public Chemotaxis_BacteriaClient(Vector2 pos, float rot)
        {
            this.position = pos;
            this.rotation = rot;
            bacteria = Chemotaxis_BacteriaFactory.GetBacteria(Chemotaxis_Bacteria_Type.TYPE_A);
        }

        #endregion

        #region Attributes

        Vector2 position;
        float rotation;
        Chemotaxis_Bacteria bacteria;
        
        #endregion

        #region Functions

        public void Render(SpriteBatch spriteBatch)
        {
            bacteria.Render(spriteBatch, position, rotation);
        }

        public void Update(GameTime gameTime) 
        {
            bacteria.Update(gameTime,ref position,rotation);
        }

        #endregion
    }

as you can see this class hold the position, rotation and all state dependant properties. It's the first time i use a flyweight so i'm not sure if it's ok

 

so as you can see the code is split into many classes  intrasic and extrasic and doesn't fit well with my inheritance but it's probably Ok with this pattern. If someone can confirm it could be good   

 

and my second question is : Where shoud i put the AI behavior ? Probably in a client class if a follow the logic of the pattern but i'm not sure.

 

Finally the game client instances are create in the game main class

 

Thank for any help or advices



#7 phil_t   Crossbones+   -  Reputation: 3234

Like
1Likes
Like

Posted 15 May 2013 - 07:08 PM

I think I'm beginning to see your problem. You want to have different types of bacteria with different abilities, etc...  Right now you have it so that you're using inheritance to add behavior to Chemotaxis_GameObject, Chemotaxis_Bacteria, Chemotaxis_SuperAwesomeBacteria. But these are all the shared data for the flyweight "template" classes. At the same time, you have the per-object state in Chemotaxis_BacteriaClient. But some of the unique behavior you're adding via inheritance in your "template" classes requires per-object state, which Chemotaxis_BacteriaClient obviously doesn't contain.

 

So yes, there's a better way to do this. First of all, implement your custom behavior with composition, not inheritance. For instance, make an IBehavior interface, and have different implementations of it for the custom behavior of different bacteria. Chemotaxis_BacteriaClient would have an IBehavior member variable, which it could call into in its Update function, say.

 

Similarly, your flyweight "template" class Chemotaxis_Bacteria doesn't really need to have any methods on it. You could instead just call it Chemotaxis_BacteriaInfo, and have it contain type/sensor/speed/texture/origin. Chemotaxis_BacteriaClient could then lookup the texture it needs and do the rendering itself (or delegate to some IRender interface if you need custom rendering for different bacteria). Any custom code that depends on sensor/speed/etc... could also delegate to some IUpdateBacteria interface.

 

 

If you want to take this to its logical conclusion, I would really look into entity component systems instead. It's a different way of thinking, but once your game gets more complex and you have different types of bacteria and other things, it will result in much more flexible, much cleaner code.

 

There are a couple of threads about entity compoennt systems active right now. One in this forum, and one in the General Programming forum. Have a look.

http://www.gamedev.net/topic/643095-good-articles-on-entity-components-systems/



#8 gamer08   Members   -  Reputation: 115

Like
0Likes
Like

Posted 16 May 2013 - 07:01 AM

Thanks fo the awnser. For the AI behavior that' exacly what i though. i just need a confirmation thanks !

 

and yes i will probably refactoring, if i have time (deadlines in coming)

 

and thanks for the URL






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS