File indexing completed on 2023-10-01 08:02:05
0001 /* 0002 SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kapman.h" 0008 0009 #include <KgDifficulty> 0010 0011 const qreal Kapman::MAX_SPEED_RATIO = 1.5; 0012 0013 Kapman::Kapman(qreal p_x, qreal p_y, Maze *p_maze) 0014 : Character(p_x, p_y, p_maze) 0015 { 0016 m_type = Element::KAPMAN; 0017 m_maxSpeed = m_normalSpeed * MAX_SPEED_RATIO; 0018 } 0019 0020 Kapman::~Kapman() = default; 0021 0022 void Kapman::init() 0023 { 0024 goRight(); 0025 updateDirection(); 0026 // Stop animation 0027 Q_EMIT stopped(); 0028 } 0029 0030 void Kapman::goUp() 0031 { 0032 m_askedXSpeed = 0; 0033 m_askedYSpeed = -m_speed; 0034 } 0035 0036 void Kapman::goDown() 0037 { 0038 m_askedXSpeed = 0; 0039 m_askedYSpeed = m_speed; 0040 } 0041 0042 void Kapman::goRight() 0043 { 0044 m_askedXSpeed = m_speed; 0045 m_askedYSpeed = 0; 0046 } 0047 0048 void Kapman::goLeft() 0049 { 0050 m_askedXSpeed = -m_speed; 0051 m_askedYSpeed = 0; 0052 } 0053 0054 void Kapman::updateDirection() 0055 { 0056 setXSpeed(m_askedXSpeed); 0057 setYSpeed(m_askedYSpeed); 0058 m_askedXSpeed = 0; 0059 m_askedYSpeed = 0; 0060 // Signal to the kapman item that the direction changed 0061 Q_EMIT directionChanged(); 0062 } 0063 0064 void Kapman::updateMove() 0065 { 0066 // If the kapman does not move 0067 if (m_xSpeed == 0 && m_ySpeed == 0) { 0068 // If the user asks for moving 0069 if (m_askedXSpeed != 0 || m_askedYSpeed != 0) { 0070 // Check the next cell with the asked direction 0071 if (getAskedNextCell().getType() == Cell::CORRIDOR) { 0072 // Update the direction 0073 updateDirection(); 0074 // Move the kapman 0075 move(); 0076 } 0077 } 0078 } 0079 // If the kapman is already moving 0080 else { 0081 // If the kapman wants to go back it does not wait to be on a center 0082 if ((m_xSpeed != 0 && m_askedXSpeed == -m_xSpeed) || (m_ySpeed != 0 && m_askedYSpeed == -m_ySpeed)) { 0083 // Go back 0084 updateDirection(); 0085 // If the kapman just turned at a corner and instantly makes a half-turn, do not run into a wall 0086 if (isOnCenter() && getNextCell().getType() != Cell::CORRIDOR) { 0087 // Stop moving 0088 stopMoving(); 0089 } else { 0090 // Move the kapman 0091 move(); 0092 } 0093 } else { 0094 // If the kapman gets on a cell center 0095 if (onCenter()) { 0096 // If there is an asked direction (not a half-turn) and the corresponding next cell is accessible 0097 if ((m_askedXSpeed != 0 || m_askedYSpeed != 0) && (m_askedXSpeed != m_xSpeed || m_askedYSpeed != m_ySpeed) 0098 && (getAskedNextCell().getType() == Cell::CORRIDOR)) { 0099 // Move the kapman on the cell center 0100 moveOnCenter(); 0101 // Update the direction 0102 updateDirection(); 0103 } else { 0104 // Check the next cell with the kapman current direction 0105 if (getNextCell().getType() != Cell::CORRIDOR) { 0106 // Move the kapman on the cell center 0107 moveOnCenter(); 0108 // Stop moving 0109 stopMoving(); 0110 } else { 0111 // Move the kapman 0112 move(); 0113 } 0114 } 0115 } else { 0116 // Move the kapman 0117 move(); 0118 } 0119 } 0120 } 0121 } 0122 0123 void Kapman::winPoints(Element *p_element) 0124 { 0125 // Emits a signal to the game 0126 Q_EMIT sWinPoints(p_element); 0127 } 0128 0129 void Kapman::die() 0130 { 0131 Q_EMIT eaten(); 0132 } 0133 0134 void Kapman::emitGameUpdated() 0135 { 0136 Q_EMIT gameUpdated(); 0137 } 0138 0139 qreal Kapman::getAskedXSpeed() const 0140 { 0141 return m_askedXSpeed; 0142 } 0143 0144 qreal Kapman::getAskedYSpeed() const 0145 { 0146 return m_askedYSpeed; 0147 } 0148 0149 Cell Kapman::getAskedNextCell() 0150 { 0151 // Get the current cell coordinates from the character coordinates 0152 int curCellRow = m_maze->getRowFromY(m_y); 0153 int curCellCol = m_maze->getColFromX(m_x); 0154 Cell nextCell; 0155 0156 // Get the next cell function of the character asked direction 0157 if (m_askedXSpeed > 0) { 0158 nextCell = m_maze->getCell(curCellRow, curCellCol + 1); 0159 } else if (m_askedXSpeed < 0) { 0160 nextCell = m_maze->getCell(curCellRow, curCellCol - 1); 0161 } else if (m_askedYSpeed > 0) { 0162 nextCell = m_maze->getCell(curCellRow + 1, curCellCol); 0163 } else if (m_askedYSpeed < 0) { 0164 nextCell = m_maze->getCell(curCellRow - 1, curCellCol); 0165 } 0166 0167 return nextCell; 0168 } 0169 0170 void Kapman::stopMoving() 0171 { 0172 setXSpeed(0); 0173 setYSpeed(0); 0174 m_askedXSpeed = 0; 0175 m_askedYSpeed = 0; 0176 Q_EMIT stopped(); 0177 } 0178 0179 void Kapman::initSpeedInc() 0180 { 0181 // Kapman speed increase when level up 0182 switch ((int)Kg::difficultyLevel()) { 0183 case KgDifficultyLevel::Easy: 0184 m_speedIncrease = Character::LOW_SPEED_INC / 2; 0185 break; 0186 case KgDifficultyLevel::Medium: 0187 m_speedIncrease = Character::MEDIUM_SPEED_INC / 2; 0188 break; 0189 case KgDifficultyLevel::Hard: 0190 m_speedIncrease = Character::HIGH_SPEED_INC / 2; 0191 break; 0192 } 0193 } 0194 0195 #include "moc_kapman.cpp"