Boy I've really outdone myself this time. I don't know if I should be actually handing you code instead of just giving you ideas on how to do it, but I personally love the idea of your project.
Now, you can:
-Have multiple weapons
-Drag a weapon on top of another and not pick it up
-Drag weapons as fast as you possibly can
No grid control yet
, but I'll definitely work on trying it.
BUT! I have organized everything into a neat little class, so all you have to do is add a weapon via the constructor and call Update() and Draw() when needed. Isn't that awesome?!?!?
Just tell me if I'm being too helpful by handing you this code lol.
#include "DarkGDK.h"
#include "Main.h"
bool ItemSelected; // Boolean to represent if an item has been selected.
int SelectedItem; // Integer to represent the sprite that is currently being dragged.
class Weapon
{
private:
void DisplayStats() // Displays stats your way
{
color (50, 50, 50);
dbBox ( dbMouseX() - 64, dbMouseY() - 128, dbMouseX() + 64, dbMouseY() - 32 ) ;
color (0, 255, 0);
dbText (dbMouseX() - 60, dbMouseY() - 124, w_name);
color (255, 255, 255);
dbText (dbMouseX() - 60, dbMouseY() - 108, "Dmg : ");
dbText (dbMouseX() - 12, dbMouseY() - 108, GetDamageRange());
color (150, 150, 205);
dbText (dbMouseX() - 60, dbMouseY() - 76, w_special1);
dbText (dbMouseX() - 60, dbMouseY() - 60, w_special2);
}
char* GetDamageRange() // Returns a char* containing the min and max hit values for example: "25-35" is what it might look like.
{
char* str = dbStr(w_minDMG); // Make str equal to min damage
strcat(str, "-"); // Append this to str
strcat(str, dbStr(w_maxDMG)); // Append this to str
return str;
}
bool MouseOver() // On MouseOver
{
if(dbMouseX() > dbSpriteX(w_spriteNum) && dbMouseX() < dbSpriteX(w_spriteNum) + dbSpriteWidth(w_spriteNum) && dbMouseY() > dbSpriteY(w_spriteNum) && dbMouseY() < dbSpriteY(w_spriteNum) + dbSpriteHeight(w_spriteNum))
return true;
return false;
}
bool MouseClick() // On MouseClick
{
if(MouseOver())
if(dbMouseClick())
return true;
return false;
}
void color (int r, int g, int b) // Coloring
{
dbInk ( dbRGB (r,g,b), dbRGB (0,0,0) );
}
public:
char* w_name; // Name
int w_minDMG; // Min Damage
int w_maxDMG; // Max Damage
int w_spriteNum; // Sprite Number
int w_centerX; // Center sprite X
int w_centerY; // Center sprite Y
int w_imageNum; // Image Number
char* w_special1; // Special perk 1
char* w_special2; // Special perk 2
Weapon(char* Name, int MinDMG, int MaxDMG, char* Special1, char* Special2, int SpriteNumber, int CenterX, int CenterY, int ImageNumber) // Constructor
{
w_name = Name;
w_minDMG = MinDMG;
w_maxDMG = MaxDMG;
w_special1 = Special1;
w_special2 = Special2;
w_spriteNum = SpriteNumber;
w_centerX = CenterX;
w_centerY = CenterY;
w_imageNum = ImageNumber;
}
void Update() // Detects and handles MouseOvers and MouseClicks
{
if(!ItemSelected) // If no item is currently being dragged
{
if(MouseOver()) // On MouseOver
{
DisplayStats(); // Display Stats of weapon
if(dbMouseClick() == 1) // If Left Mouse Click
{
ItemSelected = true; // An item IS currently being dragged
SelectedItem = w_spriteNum; // The item is the sprite id.
w_centerX = dbMouseX(); // Move the sprite's center X to the mouse's X
w_centerY = dbMouseY(); // Move the sprite's center Y to the mouse's Y
}
}
}
else // If an item is currently being selected
{
if(SelectedItem == w_spriteNum) // If it has the sprite id of the item being dragged
{
DisplayStats(); // Display Stats of weapon
if(dbMouseClick() == 1) // If Left Mouse Click
{
w_centerX = dbMouseX(); // Move the sprite's center X to the mouse's X
w_centerY = dbMouseY(); // Move the sprite's center Y to the mouse's Y
}
else // If left mouse button is being released
{
ItemSelected = false; // No item being dragged
SelectedItem = 0; // No item being dragged
}
}
}
}
void Draw() // Draws the sprites
{
dbSprite(w_spriteNum, w_centerX - dbSpriteWidth(w_spriteNum) / 2, w_centerY - dbSpriteHeight(w_spriteNum) / 2, w_imageNum); // Draws the sprite using it's center, so we have to subtract it's width and height.
dbSetSprite(w_spriteNum, 0, 1); // Necessary? I added this because you had it before.
}
};
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 100 );
dbLoadImage ("hammer1.bmp", 1);
Weapon* steelHammer = new Weapon("Steel Hammer", 25, 35, "Strength + 2", "Dexterity + 1", 1, 192, 192, 1); // Make a weapon with these stats. See the Weapon constructor.
Weapon* steelHammer2 = new Weapon("Steel Hammer2", 40, 80, "Strength + 5", "Dexterity - 4", 2, 260, 260, 1); // Make a weapon with these stats. See the Weapon constructor.
Weapon* weapons[2] = {steelHammer, steelHammer2}; // Make an array of weapons that contains the two weapons we made.
// Go ahead and add more weapons. There can be as many as your computer can handle :)
char* MouseX;
char* MouseY;
ItemSelected = false; // No item selected.
SelectedItem = 0;
while ( LoopGDK ( ) )
{
dbCLS (dbRGB (0,0,0));
for(int i = 0; i < (sizeof(weapons) / sizeof(weapons[0])); i++) // Cycles through each weapon in the array. Calculates array size by diving total size by a piece.
{
weapons[i]->Update(); // Detects and handles MouseOver and MouseClick Events.
weapons[i]->Draw(); // Draws the weapon.
}
/*MouseX = "X = ";
MouseY = "Y : ";
strcat(MouseX, dbStr(dbMouseX()));
strcat(MouseY, dbStr(dbMouseY()));
dbText(0, 0, MouseX);
dbText(0, 10, MouseY);*/ // Uncomment this block of code to have the Mouse's coordinates written in the top-left corner of the screen.
// It should work, but it is throwing a memory access violation error and I don't know why.
dbCenterText (320, 464, "Inventory system *early alpha* by Markos Tanix");
dbSync ( );
}
return;
}