Part 39: Monsters part 1: Groups in the editor
Downloads
Monster graphics
<?xml version="1.0" encoding="utf-8"?>
<monsters>
<!-- 00 None -->
<monster name = MONSTER00">
</monster>
<!-- 01 Giant Scorpion -->
<monster name = MONSTER01">
<img_front>01_Giant_Scorpion_front.png</img_front>
<img_side>01_Giant_Scorpion_side.png</img_side>
<img_back>01_Giant_Scorpion_back.png</img_back>
<img_attack>01_Giant_Scorpion_attack.png</img_attack>
</monster>
<!-- 02 Swamp Slime -->
<monster name = MONSTER02">
<img_front>02_Swamp_Slime_front.png</img_front>
<img_attack>02_Swamp_Slime_attack.png</img_attack>
</monster>
[...]
</monsters>
Monsters appears in the game as groups of 1 to 4 on a tile.
The monsters tab
Tab {
id:monstersTab
title: qsTr("Monsters")
source:"/MonstersTab.qml"
clip: true
}
"MonsterTab.qml" is nearly the same as the files used for the other tabs.
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
ScrollView
{
ListView
{
model:monstersList
currentIndex: bridge.monsterType
highlightMoveDuration: 0
delegate: Item {
width: parent.width - 12
height: 25
Text {
x: 6
width: parent.width - 12
anchors.verticalCenter: parent.verticalCenter
text: modelData
}
MouseArea {
anchors.fill: parent
onClicked: bridge.monsterType = index
}
}
highlight: Rectangle { color: "#70c0ff" }
}
}
"MainForm.ui.qml" also contains the selection info part that appears below the tab.
ScrollView {
id: monsterSelScroll
visible: bridge.monsterSelScrollVis
anchors.fill: parent
GridLayout {
id: mSelGrid
objectName: "oSelGrid"
rowSpacing: 4
columnSpacing: 4
rows: 3
columns: 2
anchors.fill: parent
Label {
id: mPosLabel
height: 20
text: qsTr("Position:")
verticalAlignment: Text.AlignVCenter
renderType: Text.QtRendering
}
Label {
id: mPosValue
height: 20
text: bridge.selPosition
verticalAlignment: Text.AlignVCenter
}
Label {
id: mTypeLabel
height: 20
text: qsTr("Type:")
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
}
ComboBox {
id: mTypeValue
model: monstersList
currentIndex: bridge.selMonsterType
}
}
}
Storing the monsters
class CMonsterGroup
{
public:
CMonsterGroup();
CMonsterGroup(const CMonsterGroup& rhs);
virtual ~CMonsterGroup();
void setType(uint8_t type);
uint8_t getType();
CMonsterGroup& operator=(const CMonsterGroup& rhs);
void load(FILE* handle);
void save(FILE* handle);
CParam* findParamByName(QString name);
CVec2 mPos;
std::vector<CParam*> mParams;
private:
void deleteParams();
uint8_t mType;
};
//---------------------------------------------------------------------------------------------
class CMap
{
[...]
CMonsterGroup* addMonsterGroup(CVec2 pos);
CMonsterGroup* findMonsterGroup(CVec2 pos);
void removeMonsterGroup(CVec2 pos);
[...]
std::vector<CMonsterGroup> mMonsterGroups;
[...]
For now the groups only hold a type. We'll add more parameters later.
The tools
class drawMonsterTool : public QUndoCommand
{
public:
explicit drawMonsterTool(QUndoCommand *parent = 0);
bool init(CVec2 pos, int type);
void undo() override;
void redo() override;
private:
CVec2 mPos;
int mType;
CMonsterGroup mOldMonster;
};
This tool is a kind of mix between the drawTile and the addObject ones.
drawMonsterTool::drawMonsterTool(QUndoCommand *parent)
: QUndoCommand(parent)
{
}
bool drawMonsterTool::init(CVec2 pos, int type)
{
mPos = pos / TILE_SIZE;
if (mPos.x < 0 || mPos.x >= map.mSize.x ||
mPos.y < 0 || mPos.y >= map.mSize.y)
return false;
CMonsterGroup* group = map.findMonsterGroup(mPos);
if (group != NULL)
mOldMonster = *group;
else
mOldMonster.setType(0);
if (mOldMonster.getType() == type)
return false;
mType = type;
return true;
}
void drawMonsterTool::undo()
{
if (mOldMonster.getType() == 0)
{
map.removeMonsterGroup(mPos);
}
else
{
CMonsterGroup* group = map.addMonsterGroup(mPos);
*group = mOldMonster;
}
bridge.updateQMLImage();
}
void drawMonsterTool::redo()
{
CMonsterGroup* group = map.addMonsterGroup(mPos);
group->setType(mType);
bridge.updateQMLImage();
}
The copy, cut and paste tools needed some modifications too.