Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a Hero class  (Read 2273 times)

0 Members and 1 Guest are viewing this topic.

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Creating a Hero class
« on: September 14, 2012, 12:31:19 pm »
Hi guys!

I am a beginner when it comes to SFML, and since SFML2 does not have many tutorials i ran into some problems when designing my 2d game.

My question is this:

What is the best way to create a Hero class, considering the need of using sf::Sprite?

The hero would be animated based on a spritesheet, and would need to store data including states, frames, positions, hitpoints etc.

I came up with 3 possible solutions and i wonder which one would fit best with SFML design philosophy.

1) Hero inherits from sf::Sprite
2) Aggregation, so sf::Sprite is a member of Hero
3) Keeping sf::Sprite and Hero separate - use Hero class to store all the date and pass it to sf::Sprite

I would appreciate any answer, especially with some explanation, why any of those options might be the best.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Creating a Hero class
« Reply #1 on: September 14, 2012, 02:31:34 pm »
1) not a good idea IMO : a Hero is not a Sprite.
2 & 3)  depends on your application design strategy. For example, if you plan to use MVC pattern then go with 3). Personally I'll go with 2).

As a matter of fact, I'm currently rewriting sftools animation API. I'll probably write a simple example later that might help you.
SFML / OS X developer

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Creating a Hero class
« Reply #2 on: September 19, 2012, 11:10:45 am »
Thanks for the reply.

I went with option 2 as planned, it seemed straightforward, but i quickly ran into a problem.

So here's my hero class:

class Hero
{
public:
        Hero (const sf::Texture & texture, float posX, float posY, int state );
        Hero::~Hero();
        void draw (sf::RenderWindow* window);
        void move (int state);
        void display(int state);
        int getFrame();
        void resetFrame();
        void set();
        int state;

private:

        sf::Sprite m_sprite;
        int position;
        int frame;

};
 

ATM i'm doing the animation the standard way: using Rect on each frame, and drawing that frame in the loop.

It works nicely when there's only a few moves. But the problem is, main character's sprite sheet is pretty large. There are lot moves, and they are quite detailed, up to 20 frames per move. This means, they can't fit into one spritesheet.

So while this way of doing things is perfectly fine for background elements, or enemies with smaller pattern of moves, the hero class would need some changes.

I was wondering what the best course of action would be. Should i add more member sprites to the class, and switch them, depending on state ( running, attacking etc. )? That seems kinda clunky, since i would need to change which sprite gets drawn in main loop of the game.

Or should i just remove the member sprites, and just move on to option 3, leaving hero class with just data regarding positions and status, etc.?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Creating a Hero class
« Reply #3 on: September 19, 2012, 11:58:04 am »
If you sort the spritesheet good enough you can just set the needed offset, the number of sprites and the animation speed and then the animation can work on its own. The properties need to be dedined somewhere, either you hardcode them or load them from an external file at runtime.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Creating a Hero class
« Reply #4 on: September 19, 2012, 02:02:41 pm »
I guess you have multiple sf::Texture loaded ? So you can simply use sf::Sprite::SetTexture when you change your state (you'll need to hide 'state' field and add a 'setState' to make thing easier).
SFML / OS X developer

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Creating a Hero class
« Reply #5 on: September 19, 2012, 03:26:13 pm »
@Hiura:

Yeah, that seems like best option;)

Thanks.