File indexing completed on 2023-11-26 07:40:44

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004     SPDX-FileCopyrightText: 2007-2008 Pierre-BenoƮt Besse <besse.pb@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "character.h"
0010 #include "arena.h"
0011 
0012 Character::Character(qreal p_x, qreal p_y, Arena* p_arena) : Element(p_x, p_y, p_arena), m_xSpeed(0), m_ySpeed(0)
0013 {
0014     initSpeed();
0015     m_maxSpeed = m_normalSpeed; // To avoid bugs, but will be overridden in the Player constructors
0016 }
0017 
0018 Character::~Character()
0019 = default;
0020 
0021 void Character::move()
0022 {
0023     // Take care of the Arena borders
0024     if (m_arena->getColFromX(m_x + m_xSpeed) == 0)                                  // First column
0025     {
0026         m_x = (m_arena->getNbColumns() - 1.5) * Granatier::CellSize;
0027     }
0028     else if (m_arena->getColFromX(m_x + m_xSpeed) == m_arena->getNbColumns() - 1)   // Last column
0029     {
0030         m_x = 1.5 * Granatier::CellSize;
0031     }
0032     else if (m_arena->getRowFromY(m_y + m_ySpeed) == 0)                            // First row
0033     {
0034         m_y = (m_arena->getNbRows() - 1.5) * Granatier::CellSize;
0035     }
0036     else if (m_arena->getRowFromY(m_y + m_ySpeed) == m_arena->getNbRows() - 1)     // Last row
0037     {
0038         m_y = 1.5 * Granatier::CellSize;
0039     }
0040     // Move the Character
0041     m_x += m_xSpeed;
0042     m_y += m_ySpeed;
0043     Q_EMIT moved(m_x, m_y);
0044 }
0045 
0046 void Character::die()
0047 {
0048     Q_EMIT dead();
0049 }
0050 
0051 qreal Character::getXSpeed() const
0052 {
0053     return m_xSpeed;
0054 }
0055 
0056 qreal Character::getYSpeed() const
0057 {
0058     return m_ySpeed;
0059 }
0060 
0061 qreal Character::getSpeed() const
0062 {
0063     return m_speed;
0064 }
0065 
0066 qreal Character::getNormalSpeed() const
0067 {
0068     return m_normalSpeed;
0069 }
0070 
0071 void Character::setXSpeed(qreal p_xSpeed)
0072 {
0073     m_xSpeed = p_xSpeed;
0074 }
0075 
0076 void Character::setYSpeed(qreal p_ySpeed)
0077 {
0078     m_ySpeed = p_ySpeed;
0079 }
0080 
0081 void Character::initSpeed()
0082 {
0083     // Player speed
0084     m_normalSpeed = 1;
0085     m_speed = m_normalSpeed;
0086 }
0087 
0088 bool Character::isInLineSight(Character* p_character) const
0089 {
0090     int curCallerRow;       // The current row of the Character
0091     int curCallerCol;       // The current column of the Character
0092     int curCharacterRow;    // The current row of the other Character
0093     int curCharacterCol;    // The current column of the other Character
0094 
0095     curCallerRow = m_arena->getRowFromY(m_y);
0096     curCallerCol = m_arena->getColFromX(m_x);
0097     curCharacterRow = m_arena->getRowFromY(p_character->getY());
0098     curCharacterCol = m_arena->getColFromX(p_character->getX());
0099 
0100     // If the two Characters are on the same row
0101     if (curCallerRow == curCharacterRow )
0102     {
0103         // If The Character is on the right of the other one and goes to the left
0104         if (curCallerCol > curCharacterCol && m_xSpeed < 0)
0105         {
0106             // Check there is a wall between them
0107             for (int i = curCharacterCol; i < curCallerCol; ++i)
0108             {
0109                 if (m_arena->getCell(curCallerRow, i).getType() != Granatier::Cell::GROUND)
0110                 {
0111                     return false;
0112                 }
0113             }
0114             // If not, the other Character is in the line sight
0115             return true;
0116         // If the Character is on the left of the other one and goes to the right
0117         }
0118         else if (curCallerCol < curCharacterCol && m_xSpeed > 0)
0119         {
0120             // Check there is a wall between them
0121             for (int i = curCallerCol; i < curCharacterCol; ++i)
0122             {
0123                 if (m_arena->getCell(curCallerRow, i).getType() != Granatier::Cell::GROUND)
0124                 {
0125                     return false;
0126                 }
0127             }
0128             // If not, the other Character is in the line sight
0129             return true;
0130         }
0131     // If the two Characters are on the same column
0132     }
0133     else if (curCallerCol == curCharacterCol)
0134     {
0135         // If The Character is on the bottom of the other one and goes up
0136         if (curCallerRow > curCharacterRow && m_ySpeed < 0)
0137         {
0138             // Check there is a wall between them
0139             for (int i = curCharacterRow; i < curCallerRow; ++i)
0140             {
0141                 if (m_arena->getCell(i, curCallerCol).getType() != Granatier::Cell::GROUND)
0142                 {
0143                     return false;
0144                 }
0145             }
0146             // If not, the other Character is in the line sight
0147             return true;
0148         // If the Character is on the top of the other one and goes down
0149         }
0150         else if (curCallerRow < curCharacterRow && m_ySpeed > 0)
0151         {
0152             // Check there is a wall between them
0153             for (int i = curCallerRow; i < curCharacterRow; ++i)
0154             {
0155                 if (m_arena->getCell(i, curCallerCol).getType() != Granatier::Cell::GROUND)
0156                 {
0157                     return false;
0158                 }
0159             }
0160             // If not, the other Character is in the line sight
0161             return true;
0162         }
0163     }
0164     // If the two Characters are not on the same row or column, they are not in the line of sight
0165     return false;
0166 }
0167 
0168 #include "moc_character.cpp"