File indexing completed on 2024-02-18 04:03:23

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 "arena.h"
0010 #include "granatier_random.h"
0011 
0012 #include <QPoint>
0013 
0014 #include <cstdlib>
0015 
0016 Arena::Arena()
0017 = default;
0018 
0019 Arena::~Arena()
0020 {
0021     for(int i = 0 ; i < m_nbRows; ++i)
0022     {
0023         delete[] m_cells[i];
0024     }
0025     delete[] m_cells;
0026 }
0027 
0028 void Arena::init(const int p_nbRows, const int p_nbColumns)
0029 {
0030     m_nbRows = p_nbRows;
0031     m_nbColumns = p_nbColumns;
0032     m_cells = new Cell*[m_nbRows];
0033     for (int i = 0; i < m_nbRows; ++i)
0034     {
0035         m_cells[i] = new Cell[m_nbColumns];
0036     }
0037 
0038     m_emptyCell.setType(Granatier::Cell::HOLE);
0039 }
0040 
0041 QString Arena::getName () const
0042 {
0043     return m_strArenaName;
0044 }
0045 
0046 void Arena::setName (const QString &p_strArenaName)
0047 {
0048     m_strArenaName = p_strArenaName;
0049 }
0050 
0051 void Arena::setCellType(const int p_row, const int p_column, const Granatier::Cell::Type p_type)
0052 {
0053     if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
0054     {
0055         return;
0056     }
0057     m_cells[p_row][p_column].setType(p_type);
0058 }
0059 
0060 void Arena::setCellElement(const int p_row, const int p_column, Element * p_element)
0061 {
0062     if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
0063     {
0064         return;
0065     }
0066     m_cells[p_row][p_column].setElement(p_element);
0067 }
0068 
0069 void Arena::removeCellElement(const int p_row, const int p_column, Element * p_element)
0070 {
0071     if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
0072     {
0073         return;
0074     }
0075     m_cells[p_row][p_column].removeElement(p_element);
0076 }
0077 
0078 void Arena::addPlayerPosition(const QPointF &p_position)
0079 {
0080     m_playerPosition.append(p_position);
0081 
0082     int nShuffle;
0083     for (int i = 0; i < m_playerPosition.size(); i++)
0084     {
0085         nShuffle = granatier::RNG::fromRange<int>(0, m_playerPosition.size()-1);
0086         m_playerPosition.swapItemsAt(i, nShuffle);
0087     }
0088 }
0089 
0090 QPointF Arena::getPlayerPosition(int p_player) const
0091 {
0092     if(m_playerPosition.count() > p_player)
0093     {
0094         return m_playerPosition.at(p_player);
0095     }
0096     return m_playerPosition.at(0);    //to have a valid position
0097 }
0098 
0099 Cell Arena::getCell(const int p_row, const int p_column) const
0100 {
0101     if (p_row < 0 || p_row >= m_nbRows || p_column < 0 || p_column >= m_nbColumns)
0102     {
0103         return m_emptyCell;
0104     }
0105     return m_cells[p_row][p_column];
0106 }
0107 
0108 QPoint Arena::getCoords(Cell* p_cell) const
0109 {
0110     for (int i = 0; i < m_nbRows; ++i)
0111     {
0112         for (int j = 0; j < m_nbColumns; ++j)
0113         {
0114             if (&m_cells[i][j] == p_cell)
0115             {
0116                 return {j, i};
0117             }
0118         }
0119     }
0120     return {};
0121 }
0122 
0123 int Arena::getRowFromY(const qreal p_y) const
0124 {
0125     int nRow = static_cast<int>(p_y / Granatier::CellSize);
0126     if (p_y < 0)
0127     {
0128         nRow -= 1;
0129     }
0130     return nRow;
0131 }
0132 
0133 int Arena::getColFromX(const qreal p_x) const
0134 {
0135     int nCol = static_cast<int>(p_x / Granatier::CellSize);
0136     if (p_x < 0)
0137     {
0138         nCol -= 1;
0139     }
0140     return nCol;
0141 }
0142 
0143 int Arena::getNbColumns() const
0144 {
0145     return m_nbColumns;
0146 }
0147 
0148 int Arena::getNbRows() const
0149 {
0150     return m_nbRows;
0151 }
0152 
0153 #include "moc_arena.cpp"