Skip to content

Commit

Permalink
Initial add enemy support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harinlen committed Jun 5, 2015
1 parent 5ebbe43 commit 53b5908
Show file tree
Hide file tree
Showing 23 changed files with 1,659 additions and 85 deletions.
12 changes: 10 additions & 2 deletions Robot.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ SOURCES += \
robotmanagewidget.cpp \
about.cpp \
gridwidget.cpp \
robotbase.cpp
robotbase.cpp \
enemy.cpp \
enemyaddwidget.cpp \
enemymanagewidget.cpp \
enemymanagement.cpp

HEADERS += \
mainwindow.h \
Expand All @@ -61,7 +65,11 @@ HEADERS += \
about.h \
gridwidget.h \
robotbase.h \
mainpage.h
mainpage.h \
enemy.h \
enemyaddwidget.h \
enemymanagewidget.h \
enemymanagement.h

RESOURCES += \
res.qrc
70 changes: 70 additions & 0 deletions enemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "enemy.h"

Enemy::Enemy() :
m_destory(false)
{
}

Enemy::Enemy(QPointF pos) :
Enemy()
{
setPos(pos);
}

Enemy::Enemy(qreal x, qreal y) :
Enemy()
{
setPos(x, y);
}

QPointF Enemy::m_target=QPointF(0,0);

QPointF Enemy::target()
{
return m_target;
}

void Enemy::setTarget(const QPointF &target)
{
m_target = target;
}

void Enemy::moveOneStep()
{
QLineF routine(m_pos, m_target);
if(routine.length()<0.9)
{
//The enemy reach the target point.
return;
}
setPos(routine.pointAt(1/routine.length()));
}
bool Enemy::destory() const
{
return m_destory;
}

void Enemy::setDestory(bool destory)
{
m_destory = destory;
}


83 changes: 83 additions & 0 deletions enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef ENEMY_H
#define ENEMY_H

#include "robotbase.h"

/*!
* \brief The Enemy class record all the basic information of a enemy.
*/
class Enemy : public RobotBase
{
public:
/*!
* \brief Construct a Enemy class. The start position of this type of
* construct will be QPointF(0, 0);
*/
Enemy();

/*!
* \brief Construct the enemy with the give position.
* \param pos The start position of the robot.
*/
Enemy(QPointF pos);

/*!
* \brief Construct the enemy with the give position.
* \param x The start x position of the enemy.
* \param y The start y position of the enemy.
*/
Enemy(qreal x, qreal y);

/*!
* \brief Get the target point of the enemy.
* \return The target point.
*/
static QPointF target();

/*!
* \brief Set the target point of the enemy.
* \param target Enemy's target point.
*/
static void setTarget(const QPointF &target);

/*!
* \brief Ask the enemy move one step to the target point.
*/
void moveOneStep();

/*!
* \brief Whether this enemy is destoried by robot.
* \return The destory status.
*/
bool destory() const;

/*!
* \brief Set the destory status.
* \param destory Destory status.
*/
void setDestory(bool destory);

private:
static QPointF m_target;
bool m_destory;
};

#endif // ENEMY_H
130 changes: 130 additions & 0 deletions enemyaddwidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QGroupBox>

#include "enemyaddwidget.h"

EnemyAddWidget::EnemyAddWidget(QWidget *parent) :
QWidget(parent),
m_editArea(new QGroupBox(this)),
m_okay(new QPushButton(this)),
m_cancel(new QPushButton(this))
{
//Intial labels.
for(int i=0; i<3; i++)
{
m_labels[i]=new QLabel(this);
}
m_xData=new QLineEdit(this);
m_yData=new QLineEdit(this);

//Link edit widget.
connect(m_xData, SIGNAL(textChanged(QString)),
this, SLOT(onActionParameterChange()));
connect(m_yData, SIGNAL(textChanged(QString)),
this, SLOT(onActionParameterChange()));

//Initial layouts.
QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::LeftToRight,
this);
mainLayout->setContentsMargins(0,0,0,0);
setLayout(mainLayout);

//Generate the control layout.
QBoxLayout *controlLayout=new QBoxLayout(QBoxLayout::TopToBottom,
mainLayout->widget());
controlLayout->setSpacing(2);
m_editArea->setLayout(controlLayout);
mainLayout->addWidget(m_editArea, 1);

QBoxLayout *positionLayout=new QBoxLayout(QBoxLayout::LeftToRight,
mainLayout->widget());
positionLayout->setSpacing(5);
positionLayout->addWidget(m_labels[0]);
positionLayout->addWidget(m_xData, 1);
positionLayout->addSpacing(10);
positionLayout->addWidget(m_labels[1]);
positionLayout->addWidget(m_yData, 1);
controlLayout->addLayout(positionLayout);
controlLayout->addStretch();

//Generate the button layout.
QBoxLayout *buttonLayout=new QBoxLayout(QBoxLayout::TopToBottom,
mainLayout->widget());
buttonLayout->setSpacing(2);
buttonLayout->addWidget(m_okay);
m_cancel->setShortcut(QKeySequence(Qt::Key_Escape));
buttonLayout->addWidget(m_cancel);
buttonLayout->addStretch();
mainLayout->addLayout(buttonLayout);

connect(m_okay,
static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),
[=]{emit requireAddEnemy(QPointF(m_xData->text().toDouble(),
m_yData->text().toDouble()));});
connect(m_cancel,
static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),
[=]{emit requireClose();});

retranslate();
}

void EnemyAddWidget::enabledWidget()
{
m_okay->setDefault(true);
setEnabled(true);
}

void EnemyAddWidget::disabledWidget()
{
m_okay->setDefault(false);
setEnabled(false);
}

void EnemyAddWidget::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
//Reset the data.
m_xData->clear();
m_yData->clear();
//Update the preview.
onActionParameterChange();
//Set the focus to x data edit.
m_xData->setFocus();
}

void EnemyAddWidget::retranslate()
{
m_okay->setText(tr("Ok"));
m_cancel->setText(tr("Cancel"));

m_labels[0]->setText(tr("X:"));
m_labels[1]->setText(tr("Y:"));

m_editArea->setTitle(tr("Robot Initial Status"));
}

void EnemyAddWidget::onActionParameterChange()
{
emit requirePreviewEnemy(QPointF(m_xData->text().toDouble(),
m_yData->text().toDouble()));
}
Loading

0 comments on commit 53b5908

Please sign in to comment.