Page 1 of 1

Adding weapons to my game?

Posted: Thu Oct 02, 2008 2:17 pm
by dandymcgee
I'm just curious as to how you added/would add weapons to your game (if you even use weapons)? Or will it just be casting spells of some sort? Whether or not you use weapons is somewhat irrelevant, I'm just not sure how I should do it.

Would you keep the same character sprites and just display the weapons during the battle phase? Would you not display them anywhere but in the inventory? Or would you have a set of each sprite holding each weapon so that they could walk around carrying it?

Clearly some of these choices require more work than others. I'm comparing Pokemon a game where you're using attacks instead of weapons so the sprite is always the same, to something far more complex like Diablo where they've drawn every frame of every character in every possible position with and without each weapon.

Any Ideas? :roll:

Thanks,
-Dan

Re: Adding weapons to my game?

Posted: Thu Oct 02, 2008 2:26 pm
by Falco Girgis
Hello. First of all, I'd like to say that this is definitely a delimna you're going to face. Pretty much every implementation of this is going to be easy for the programmer. It's going to be just drawing the weapon or drawing the character with the weapon. Big deal, right?

But this is a huge pain in the asshole for artists. One approach will require exponentially more sprites from them. If you want to show your character holding weapons and such out of battle, I'd personally recommend you take a look at Secret of Mana for SNES. It uses the same character sprites, but draws arms+weapons separately. That way you only have one character and an array of different arms depending on what he's holding (sword, spear, bow, whatever).

I thought that Secret of Mana handled it extremely well.

Re: Adding weapons to my game?

Posted: Thu Oct 02, 2008 2:39 pm
by dandymcgee
Yeah I was thinking of drawing the character and then the weapon on top (didn't think of arms though, hehe). And as for the programmer/artist getting along, I'm both right now so that's the easy part :). I guess it comes down to personal preference, and the number of weapons I want to add. Just 5 - 10 weapons shouldn't be hard to add to a 12 frame (3 per direction) 2D 32x32 sprite. It'll be some work, but I think that's probably the direction I'll head at this point. Thanks for the advice.

Re: Adding weapons to my game?

Posted: Thu Oct 02, 2008 2:50 pm
by MarauderIIC
Right. You don't need an 'every possible' combination. Just a few character position templates and then draw the weapon into it. Sort of like FPS weapons.

Re: Adding weapons to my game?

Posted: Thu Oct 02, 2008 2:54 pm
by dandymcgee
Yeah, the arms of my sprite could almost hold a sword the way they are right now. Holding a gun would need a few adjustments, but there's no deadline for this project.

Re: Adding weapons to my game?

Posted: Thu Oct 02, 2008 4:03 pm
by trufun202
In one of several abandoned 2D rpgs of mine, I came up with the concept of "mounting" items.

Basically you have an item that inherits from IMountableItem and exposes several common methods including Update() and Draw().

Then, to add mountable items to the player:

Code: Select all

player.MountItem(sword);
player.MountItem(shield);
player.MountItem(hat);
then, to update the current frame of the player, and all mounted items:

Code: Select all

player.Update();

foreach(IMountableItem mount in player.Mounts)
{
     mount.Update();
}
then, to draw the player, and all mounted items:

Code: Select all

player.Draw();

foreach(IMountableItem mount in player.Mounts)
{
     mount.Draw();
}
But, as Falco mentioned, the real bitch is getting the sprites to line up correctly. I recommend making all the images the same dimensions as the player sprite, this way you don't have to worry about offsets. Just draw all of the mounts at the same X, Y as the player.

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 9:21 am
by dandymcgee
Hey trufun, thanks for taking the time to reply. Aside from reading about them, I have absolutely no experience with classes, so I'm going to make a stab at coding what I think you're recommending:

Code: Select all

class character {
        int x;
        int y;
    public:
        int MountItem (std::string);
        int Update ();
        int Draw ();
};

class mountable {
        int x;
        int y;
    public:
        int Update ();
        int Draw ();
};

int character::MountItem(std::string ItemName)
{
    if( ItemName == "sword" )
    {
        file.open("sword.bmp");
    }
    else if( ItemName == "shield" )
    {
        file.open("shield.bmp");
    }
    else if( ItemName == "hat" )
    {
        file.open("hat.bmp");
    }else{
        return 1;
    }

    return 0;
}

//character Update()
    //set x & y, applysurface()
//character Draw()
    //flip surface to screen

//mountable Update()
    //set x & y, applysurface()
//mountable Draw()
    //flip surface to screen

int main()
{
    character player;
    mountable sword;

    player.MountItem("sword")

    player.Update();
    sword.Update();

    player.Draw();
    sword.Draw();

    return 0;
}
Firstly, if you notice any severe newbie errors in this portion would be so kind as to point them out?

Also, how would I do this:

Code: Select all

foreach(IMountableItem mount in player.Mounts)
{
     mount.Update();
}
What types are "IMountableItem", "mount", and ".Mounts"? ".Mounts" would have to be an array or vector wouldn't it? And is "IMountableItem" a class?

Sorry if I've gotten this completely wrong, but as I've said this is the first time I've ever tried coding with classes.
-Dan

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 10:55 am
by trufun202
IMountableItem is an interface, and all of the mountable items (such as sword, shield, etc.) are classes that implement that interface. Interfaces allow you to specify which properties and methods are exposed from a class. So, you can work with the object without knowing specifically what *type* of object it is. The same is possible by using an abstract class.

For instance, you can Update() and Draw() all of the player's mounts without knowing if it's a sword, shield, etc.

But...If you're new to classes and OO design, this stuff can get pretty hairy.

What you've got can still get the job done, here are my tweaks:

Code: Select all

class mountable {
        int x;
        int y;
        std::string fileName;  //have the mountable object hold the file
    public:
        mountable(std::string fileName)
        {
              this.fileName = fileName;
        }
        void Init()
        {
               file.Open(fileName);
        }
        int Update ();
        int Draw ();
};

class character {
        int x;
        int y;
        std::list<mountable> mounts;    //Keep a list of mounts for this character
    public:
        int MountItem (std::string);
        int Update ();
        int Draw ();
};

int character::MountItem(std::string ItemName)
{
    mountable mount;

    if( ItemName == "sword" )
    {
         mount = new mountable("sword.bmp");
    }
    else if( ItemName == "shield" )
    {
        mount = new mountable("shield.bmp");
    }
    else if( ItemName == "hat" )
    {
        mount = new mountable("hat.bmp");
    }
    else
        return 1;

    mount.Init();   //Initialize this item

    mounts.insert(mounts.begin(), mount );   //Add this new item to the character's list of mounts

    return 0;
}

int character::Update()
{
      //TODO Update the player

      //Call the Update() method for each of the mounts
      std::for_each(mounts.begin(), mounts.end(), std::mem_fun(&mountable::Update));
}

int character::Draw()
{
      //TODO Draw the player

      //Call the Draw() method for each of the mounts
      std::for_each(mounts.begin(), mounts.end(), std::mem_fun(&mountable::Draw));
}

int main()
{
    character player;

    player.MountItem("sword")

    player.Update();

    player.Draw();

    return 0;
}

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 12:15 pm
by dandymcgee
Alright, I'm going to do some work with this, probably create a small test application before attempting to actually implement it into the game. I'll update this topic if I have any more questions. Thanks again for your kind efforts to help.

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 5:26 pm
by MarauderIIC
You should capitalize class names. "class Mountable". Then you can "Mountable mountable" later.

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 5:45 pm
by sparda
For instance, you can Update() and Draw() all of the player's mounts without knowing if
it's a sword, shield, etc.
Also, if by any chance you end up using inheritance and in that case you don't
know exactly how many of these different items (sword, shield, etc) are to be implemented
yet (or how many there will be), you should declare member functions like Update() and Draw()
virtual for posterity. I'm not sure if you, dandymcgee, are familiar with polymorphism, but should
really read up on it as its very useful in game programming.

Re: Adding weapons to my game?

Posted: Sun Oct 05, 2008 7:24 pm
by dandymcgee
Way over my head.. I think I'll take it one step at a time. :P

Re: Adding weapons to my game?

Posted: Mon Oct 06, 2008 12:02 am
by trufun202
dandymcgee wrote:Way over my head.. I think I'll take it one step at a time. :P
Yeah, I wouldn't recommend developing this type of game while you're still trying to learn OO design. For now, stick to what you know.

One of my first games was a puzzle game entirely based on a 2D Array. Basically it was a 2D array of integers and each number corresponded to a different colored orb. On Update(), I would traverse the array to check for falling orbs and 3 matching orbs. On Draw(), I had a giant switch statement and displayed different orbs based on the integer value.

This game has actually been really beneficial to me. I've actually written it in 4 or 5 languages. Its something simple that I can throw together quick and get familiar with a new language.