File indexing completed on 2024-12-01 06:51:43

0001 /***************************************************************************
0002                                cmaplevel.cpp
0003                              -------------------
0004     begin                : Tue Mar 20 2001
0005     copyright            : (C) 2001 by Kmud Developer Team
0006     email                : kmud-devel@kmud.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "cmaplevel.h"
0019 
0020 #include "cmapmanager.h"
0021 #include "cmapzone.h"
0022 #include "cmaptext.h"
0023 #include "cmaproom.h"
0024 #include "cmapview.h"
0025 
0026 #include <KLocalizedString>
0027 
0028 CMapLevel::CMapLevel(CMapManager *mapManager, int pos): m_mapManager(mapManager)
0029 {
0030   m_mapManager->m_levelCount++;
0031   setLevelID(m_mapManager->m_levelCount);
0032   name = i18n("Level %1").arg(pos+1);
0033 
0034   // insert the level at the requested location
0035   getZone()->insertLevel(this, pos);
0036 }
0037 
0038 CMapLevel::~CMapLevel()
0039 {
0040   CMapView *view = m_mapManager->getActiveView();
0041   if (view->getCurrentlyViewedLevel() == this)
0042   {
0043     CMapLevel *show = getPrevLevel();
0044     if (!show) show = getNextLevel();
0045     if (!show) show = getZone()->firstLevel();
0046     view->showPosition(show,true);
0047   }
0048   getZone()->removeLevel(this);
0049 
0050   QList<CMapElement *> lst = getAllElements();
0051   foreach (CMapElement *el, lst)
0052     delete el;
0053 }
0054 
0055 /** Used to find a room with the ID */
0056 CMapRoom *CMapLevel::findRoom(unsigned int id)
0057 {
0058   foreach (CMapRoom *room, m_roomList)
0059     if (room->getRoomID() == id)
0060       return room;
0061   return nullptr;
0062 }
0063 
0064 /** Used to find a room with the ID */
0065 CMapText *CMapLevel::findText(unsigned int id)
0066 {
0067   foreach (CMapText *text, m_textList)
0068     if (text->getTextID() == id)
0069       return text;
0070   return nullptr;
0071 }
0072 
0073 /** Used to get the number of the level */
0074 int CMapLevel::getNumber(void) const
0075 {
0076   return getZone()->levelIndex(this);
0077 }
0078 
0079 unsigned int CMapLevel::getLevelID(void) const
0080 {
0081     return m_ID;
0082 }
0083 
0084 void CMapLevel::setLevelID(unsigned int id)
0085 {
0086   if (id > m_mapManager->m_levelCount)
0087     m_mapManager->m_zoneCount = id;
0088 
0089   m_ID = id;
0090 }
0091 
0092 QString CMapLevel::getName() const
0093 {
0094   if (name.length()) return name;
0095   return i18n("Level %1").arg(getLevelID());
0096 }
0097 
0098 void CMapLevel::setName(const QString &name)
0099 {
0100   this->name = name;
0101   getZone()->setLevelName(this, getName());
0102 }
0103 
0104 QList<CMapElement *> CMapLevel::getAllElements()
0105 {
0106   QList<CMapElement *> lst;
0107 
0108   foreach (CMapRoom *room, m_roomList)
0109     lst.push_back(room);
0110   foreach (CMapText *text, m_textList)
0111     lst.push_back(text);
0112 
0113   return lst;
0114 }
0115 
0116 /** Used to get the pointer to the previous level */
0117 CMapLevel *CMapLevel::getPrevLevel(void)
0118 {
0119   int idx = getZone()->levelIndex(this);
0120   if (idx <= 0) return nullptr;
0121   return getZone()->getLevel(idx - 1);
0122 }
0123 
0124 /** Used to get the pointer to the next level */
0125 CMapLevel *CMapLevel::getNextLevel(void)
0126 {
0127   int idx = getZone()->levelIndex(this);
0128   if (idx < 0) return nullptr;
0129   return getZone()->getLevel(idx + 1);
0130 }
0131 
0132 /** Used to get the zone that the level is in */
0133 CMapZone *CMapLevel::getZone(void) const
0134 {
0135   return m_mapManager->getZone();
0136 }
0137 
0138 QList<CMapElement *> CMapLevel::elementsUnderMouse(QPoint mousePos)
0139 {
0140   QList<CMapElement *> opts = getAllElements();
0141   QList<CMapElement *> res;
0142   foreach (CMapElement *el, opts)
0143     if (el->mouseInElement(mousePos))
0144       res.push_back(el);
0145 
0146   return res;
0147 }
0148 
0149 CMapElement *CMapLevel::findElementAt(QPoint pos, int type)
0150 {
0151   QList<CMapElement *> lst = elementsUnderMouse(pos);
0152   if (lst.empty()) return nullptr;
0153   foreach (CMapElement *el, lst)
0154   {
0155     if ((type >= 0) && (el->getElementType() != type)) continue;
0156     return el;
0157   }
0158   return nullptr;
0159 }
0160 
0161 CMapRoom *CMapLevel::findRoomAt(QPoint pos)
0162 {
0163   foreach (CMapRoom *room, m_roomList)
0164     if (room->mouseInElement(pos))
0165       return room;
0166 
0167   return nullptr;
0168 }
0169 
0170