Part 25: Objects - Part 5: Weight and informations
Downloads
Selected champion
void CInterface::drawChampionName(QImage* image, int num, CVec2 pos, bool isFullName)
{
CCharacter* c = &game.characters[num];
std::string name = c->firstName;
if (isFullName == true)
name += " " + c->lastName;
QColor color = (num == selectedChampion ? HIGHLIGHT_TEXT_COLOR : DEFAULT_TEXT_COLOR);
drawText(image, pos, eFontStandard, name.c_str(), color);
}
It draws the champion's first or full name with the right color.
#define MAIN_INTERFACE_COLOR QColor(0, 204, 204)
#define DEFAULT_TEXT_COLOR QColor(173, 173, 173)
#define HIGHLIGHT_TEXT_COLOR QColor(255, 170, 0)
#define INVENTORY_BG_COLOR QColor(68, 68, 68)
#define INVENTORY_SLOT_COLOR QColor(102, 102, 102)
#define OVERLOAD1_COLOR QColor(255, 255, 0)
#define OVERLOAD2_COLOR QColor(255, 0, 0)
To change the selected champion, we need to define mouse areas around the champions' names in CInterface::drawChampion():
CRect nameRect(basePos, basePos + CVec2(42, 6));
mouse.addArea(eMouseArea_SelectChamp, nameRect, eCursor_Arrow, (void*)num);
And when we test it, we simply have to change the value of selectedChampion.
void CInterface::update(SMouseArea* clickedArea)
{
[...]
if (clickedArea != NULL)
{
if (mouse.mButtonPressing == true)
{
switch (clickedArea->type)
{
[...]
// click on a champion's name
case eMouseArea_SelectChamp:
selectedChampion = (int)clickedArea->param1;
break;
Weight
<?xml version="1.0" encoding="utf-8"?>
<items>
<!-- ======================== Weapons ======================== -->
<!-- EYE OF TIME -->
<item name="ITEM001">
[...]
<weight>1</weight>
</item>
<!-- STORMRING -->
<item name="ITEM002">
[...]
<weight>1</weight>
</item>
<!-- TORCH -->
<item name="ITEM003">
[...]
<weight>11</weight>
</item>
[...]
</items>
I also wrote a function to compute the load of a character.
void CCharacter::updateLoad()
{
int type;
stats[eStatLoad].value = 0;
for (int i = 0; i < eBodyPartCount; ++i)
{
type = bodyObjects[i].getType();
if (type != 0)
stats[eStatLoad].value += objects.mObjectInfos[type - 1].weight;
}
if (&game.characters[interface.selectedChampion] == this)
{
type = mouse.mObjectInHand.getType();
if (type != 0)
stats[eStatLoad].value += objects.mObjectInfos[type - 1].weight;
}
}
We simply sum up the weights of all the objects carried by the character.
void CGame::update(SMouseArea* clickedArea)
{
[...]
if (clickedArea != NULL)
{
if (mouse.mButtonPressing == true)
{
if (clickedArea->type == eMouseAreaG_Champion)
{
[...]
}
else if (clickedArea->type == eMouseAreaG_DoorButton)
{
[...]
}
else if (clickedArea->type == eMouseAreaG_PickObject)
{
[...]
characters[interface.selectedChampion].updateLoad();
}
else if (clickedArea->type == eMouseAreaG_DropObject)
{
[...]
characters[interface.selectedChampion].updateLoad();
}
}
}
}
...and when we take or put an object in a character's sheet.
void CInterface::update(SMouseArea* clickedArea)
{
[...]
if (clickedArea != NULL)
{
if (mouse.mButtonPressing == true)
{
switch (clickedArea->type)
{
[...]
// object slot in inventory
case eMouseArea_InvObject:
{
[...]
// update the loads of the characters
c->updateLoad();
c = &game.characters[selectedChampion];
c->updateLoad();
}
break;
Overload
int CCharacter::overload()
{
int value = stats[eStatLoad].value;
int max = stats[eStatLoad].maxValue;
if (value > max)
return 2;
else if (value > (625 * max) / 1000)
return 1;
else
return 0;
}
And I used it to choose the color when we draw the load:
void CInterface::drawInventory(QImage* image)
{
[...]
// display load
CCharacter* c = &game.characters[currentChampion];
QColor loadColor;
if (c->overload() == 0)
loadColor = DEFAULT_TEXT_COLOR;
else if (c->overload() == 1)
loadColor = OVERLOAD1_COLOR;
else
loadColor = OVERLOAD2_COLOR;
drawText(image, CVec2(104, 161), eFontStandard, getText("STATS09").c_str(), loadColor);
static char temp[256];
CCharacter::SStat stat = getStat(currentChampion, CCharacter::eStatLoad);
sprintf(temp, "%5.1f/%3d KG", (float)stat.value / 10.0f, stat.maxValue / 10);
drawText(image, CVec2(148, 161), eFontStandard, temp, loadColor);
[...]
}
But that was the easy part. Apart from the color of the text, the overload level also have an
if (isClickingOnArrow(clickedArea, eMouseArea_ArrowForward) == true || keyboard.keys[CKeyboard::eForward] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowForward;
setDelayedArrow(eArrowForward);
}
else if (isClickingOnArrow(clickedArea, eMouseArea_ArrowBackward) == true || keyboard.keys[CKeyboard::eBackward] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowBackward;
setDelayedArrow(eArrowBackward);
}
else if (isClickingOnArrow(clickedArea, eMouseArea_ArrowLeft) == true || keyboard.keys[CKeyboard::eLeft] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowLeft;
setDelayedArrow(eArrowLeft);
}
else if (isClickingOnArrow(clickedArea, eMouseArea_ArrowRight) == true || keyboard.keys[CKeyboard::eRight] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowRight;
setDelayedArrow(eArrowRight);
}
else if (isClickingOnArrow(clickedArea, eMouseArea_ArrowTurnLeft) == true || keyboard.keys[CKeyboard::eTurnLeft] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowTurnLeft;
player.turnLeft();
}
else if (isClickingOnArrow(clickedArea, eMouseArea_ArrowTurnRight) == true || keyboard.keys[CKeyboard::eTurnRight] == true)
{
arrowHighlightTime = ARROWS_HIGHLIGHT_TIME;
currentArrow = eArrowTurnRight;
player.turnRight();
}
This function only sets 2 variables. delayedArrow is the id of the arrow we clicked and delayedArrowTime
void CInterface::setDelayedArrow(EArrows arrow)
{
delayedArrow = arrow;
int maxOverload = 0;
for (int i = 0; i < 4; ++i)
{
int overload = game.characters[i].overload();
if (overload > maxOverload)
maxOverload = overload;
}
if (maxOverload == 0)
delayedArrowTime = OVERLOAD0_DELAY;
else if (maxOverload == 1)
delayedArrowTime = OVERLOAD1_DELAY;
else
delayedArrowTime = OVERLOAD2_DELAY;
}
So in updateArrow(), if delayedArrowTime is not equal to 0, we won't test the keyboard and arrows, and at the end
void CInterface::updateArrows(SMouseArea* clickedArea)
{
if (delayedArrowTime == 0)
{
if (isClickingOnArrow...
[...]
}
// delayed movements
if (delayedArrowTime != 0)
{
delayedArrowTime--;
if (delayedArrowTime == 0)
{
switch(delayedArrow)
{
case eArrowForward:
player.moveForward();
break;
case eArrowBackward:
player.moveBackward();
break;
case eArrowLeft:
player.moveLeft();
break;
case eArrowRight:
player.moveRight();
break;
default:
break;
}
}
}
}
In the map you will see that I added a bunch of objects so that you can test the overloading.
Objects infos
void CInterface::drawInfos(QImage* image)
{
// draw info image
QImage infoBg = fileCache.getImage("gfx/interface/EmptyInfo.png");
graph2D.drawImage(image, CVec2(80, 85), infoBg);
int objType = mouse.mObjectInHand.getType();
if (objType == 0)
{
// charater's infos
[...]
}
else
{
// object's infos
QImage eye = fileCache.getImage("gfx/interface/Eye.png");
graph2D.drawImage(image, CVec2(83, 90), eye);
QImage circle = fileCache.getImage("gfx/interface/Circle.png");
graph2D.drawImage(image, CVec2(106, 87), circle);
CObjects::CObjectInfo& infos = objects.mObjectInfos[objType - 1];
QImage objSheet = fileCache.getImage(infos.imageFile.toLocal8Bit().constData());
CRect objRect = interface.getItemRect(infos.imageNum);
graph2D.drawImageAtlas(image, CVec2(111, 92), objSheet, objRect);
// object name
drawText(image, CVec2(134, 98), eFontStandard, infos.name.c_str(), DEFAULT_TEXT_COLOR);
int y = 116;
if (objType == 144)
{
// waterskin full
drawText(image, CVec2(108, y), eFontStandard, getText("OBJ_INFO00").c_str(), DEFAULT_TEXT_COLOR);
y += 7;
}
else if (objType == 145)
{
// waterskin empty
drawText(image, CVec2(108, y), eFontStandard, getText("OBJ_INFO03").c_str(), DEFAULT_TEXT_COLOR);
y += 7;
}
else if (infos.positions.contains("c"))
{
// consumable
drawText(image, CVec2(108, y), eFontStandard, getText("OBJ_INFO04").c_str(), DEFAULT_TEXT_COLOR);
y += 7;
}
// weight
static char temp[256];
sprintf(temp, "%s %.1f KG.", getText("OBJ_INFO05").c_str(), (float)infos.weight / 10.0f);
drawText(image, CVec2(108, y), eFontStandard, temp, DEFAULT_TEXT_COLOR);
}
}