File indexing completed on 2024-09-15 06:37:37
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "uientity.h" 0008 #include "seaview.h" 0009 #include "shot.h" 0010 #include "ship.h" 0011 0012 #include <QIcon> 0013 #include <QList> 0014 0015 UIEntity::UIEntity(Sea::Player player, Sea* sea, SeaView* view) 0016 : Entity(player, view, sea->battleShipsConfiguration()) 0017 , m_sea(sea) 0018 { 0019 } 0020 0021 UIEntity::~UIEntity() 0022 { 0023 m_seaview->setDelegate(nullptr); 0024 } 0025 0026 void UIEntity::notify(Sea::Player player, const Coord& c, const HitInfo& info) 0027 { 0028 drawShoot(Sea::opponent(player), c, info); 0029 } 0030 0031 void UIEntity::notifyGameOver(Sea::Player winner) 0032 { 0033 drawHiddenShips(winner); 0034 } 0035 0036 void UIEntity::start() 0037 { 0038 0039 } 0040 0041 void UIEntity::hit(Shot* shot) 0042 { 0043 shot->execute(HitInfo::INVALID); 0044 } 0045 0046 void UIEntity::drawHiddenShips(Sea::Player winner) 0047 { 0048 const QList<Ship *> enemyShips = m_sea->enemyShips(); 0049 0050 for (Ship * ship : enemyShips) { 0051 if (ship->alive()) { 0052 m_seaview->add(winner, ship); 0053 } 0054 } 0055 } 0056 0057 void UIEntity::drawShoot(Sea::Player player, const Coord& c, const HitInfo& info) 0058 { 0059 switch (info.type) { 0060 case HitInfo::HIT: 0061 m_seaview->hit(player, c); 0062 // registerHit(player, c); 0063 break; 0064 case HitInfo::MISS: 0065 m_seaview->miss(player, c); 0066 // registerMiss(player, c); 0067 break; 0068 default: 0069 return; 0070 } 0071 0072 if (info.shipDestroyed) { 0073 Coord shipPos = info.shipPos; 0074 if (shipPos.valid()) { 0075 // show destroyed opponent ship 0076 if (player != m_player) { 0077 m_seaview->add(player, info.shipDestroyed); 0078 } 0079 0080 m_seaview->sink(player, info.shipDestroyed); 0081 } 0082 } 0083 } 0084 0085 QIcon UIEntity::icon() const 0086 { 0087 return QIcon::fromTheme( QLatin1String( "user-identity" )); 0088 } 0089 0090 #include "moc_uientity.cpp"