summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-03-30 04:39:13 +0300
committerAndrei Karas <akaras@inbox.ru>2011-03-30 04:39:13 +0300
commitd629f56517f55d1fd756c4774d9a368d66f615df (patch)
treea3675eb7af6b95bd33a8c9887bda5952a11fced5
parenta08839164df3ae37d216d810123f17eb968da1d4 (diff)
downloadmv-d629f56517f55d1fd756c4774d9a368d66f615df.tar.gz
mv-d629f56517f55d1fd756c4774d9a368d66f615df.tar.bz2
mv-d629f56517f55d1fd756c4774d9a368d66f615df.tar.xz
mv-d629f56517f55d1fd756c4774d9a368d66f615df.zip
Add background music map ranges.
-rw-r--r--src/localplayer.cpp9
-rw-r--r--src/map.cpp78
-rw-r--r--src/map.h60
-rw-r--r--src/resources/mapreader.cpp5
-rw-r--r--src/sound.cpp2
-rw-r--r--src/sound.h3
6 files changed, 140 insertions, 17 deletions
diff --git a/src/localplayer.cpp b/src/localplayer.cpp
index 0533290b3..e94a9bed1 100644
--- a/src/localplayer.cpp
+++ b/src/localplayer.cpp
@@ -3226,6 +3226,15 @@ void LocalPlayer::updateCoords()
socialWindow->updatePortals();
if (viewport)
viewport->hideBeingPopup();
+ if (mMap)
+ {
+ std::string str = mMap->getObjectData(getTileX(), getTileY(),
+ MapItem::MUSIC);
+ if (str.empty())
+ str = mMap->getMusicFile();
+ if (str != sound.getCurrentMusicFile())
+ sound.playMusic(str);
+ }
}
if (mShowNavigePath)
diff --git a/src/map.cpp b/src/map.cpp
index 36034cbcb..bc8102cbd 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -391,6 +391,8 @@ Map::Map(int width, int height, int tileWidth, int tileHeight):
}
mSpecialLayer = new SpecialLayer(width, height);
mTempLayer = new SpecialLayer(width, height, true);
+ mObjects = new ObjectsLayer(width, height);
+
config.addListener("OverlayDetail", this);
config.addListener("guialpha", this);
@@ -420,6 +422,8 @@ Map::~Map()
mSpecialLayer = 0;
delete mTempLayer;
mTempLayer = 0;
+ delete mObjects;
+ mObjects = 0;
delete_all(mMapPortals);
}
@@ -1340,6 +1344,15 @@ std::string Map::getUserMapDirectory() const
+ getProperty("_realfilename");
}
+void Map::addRange(const std::string &name, int type,
+ int x, int y, int dx, int dy)
+{
+ if (!mObjects)
+ return;
+
+ mObjects->addObject(name, type, x / 32, y / 32, dx / 32, dy / 32);
+}
+
void Map::addPortal(const std::string &name, int type,
int x, int y, int dx, int dy)
{
@@ -1432,6 +1445,25 @@ void Map::setPvpMode(int mode)
}
}
+std::string Map::getObjectData(unsigned x, unsigned y, int type)
+{
+ if (!mObjects)
+ return "";
+
+ MapObjectList *list = mObjects->getAt(x, y);
+ if (!list)
+ return "";
+
+ std::list<MapObject>::iterator it = list->objects.begin();
+ while (it != list->objects.end())
+ {
+ if ((*it).type == type)
+ return (*it).data;
+ }
+
+ return "";
+}
+
SpecialLayer::SpecialLayer(int width, int height, bool drawSprites):
mWidth(width), mHeight(height)
{
@@ -1674,3 +1706,49 @@ void MapItem::draw(Graphics *graphics, int x, int y, int dx, int dy)
}
}
+ObjectsLayer::ObjectsLayer(unsigned width, unsigned height) :
+ mWidth(width), mHeight(height)
+{
+ const unsigned size = width * height;
+ mTiles = new MapObjectList*[size];
+ std::fill_n(mTiles, size, static_cast<MapObjectList*>(0));
+}
+
+ObjectsLayer::~ObjectsLayer()
+{
+ delete mTiles;
+ mTiles = 0;
+}
+
+void ObjectsLayer::addObject(std::string name, int type,
+ unsigned x, unsigned y,
+ unsigned dx, unsigned dy)
+{
+ if (!mTiles)
+ return;
+
+ if (x + dx > mWidth)
+ dx = mWidth - x;
+ if (y + dy > mHeight)
+ dy = mHeight - y;
+
+ for (unsigned y1 = y; y1 < y + dy; y1 ++)
+ {
+ unsigned idx1 = x + y1 * mWidth;
+ unsigned idx2 = idx1 + dx;
+
+ for (unsigned i = idx1; i < idx2; i ++)
+ {
+ if (!mTiles[i])
+ mTiles[i] = new MapObjectList();
+ mTiles[i]->objects.push_back(MapObject(type, name));
+ }
+ }
+}
+
+MapObjectList *ObjectsLayer::getAt(unsigned x, unsigned y)
+{
+ if (x >= mWidth || y >= mHeight)
+ return 0;
+ return mTiles[x + y * mWidth];
+} \ No newline at end of file
diff --git a/src/map.h b/src/map.h
index 7c606dfbd..8fd2c33c9 100644
--- a/src/map.h
+++ b/src/map.h
@@ -47,6 +47,7 @@ class SimpleAnimation;
class Tileset;
class SpecialLayer;
class MapItem;
+class ObjectsLayer;
typedef std::vector<Tileset*> Tilesets;
typedef std::vector<MapLayer*> Layers;
@@ -75,23 +76,24 @@ struct MetaTile
unsigned char blockmask; /**< Blocking properties of this tile */
};
-/*
-struct MapBox
+class MapObject
{
- MapBox() : name(""), x(0), y(0)
- {
- }
-
- MapBox(std::string name1, int x1, int y1):
- name(name1), x(x1), y(y1)
- {
- }
-
- std::string name;
- int x;
- int y;
+ public:
+ MapObject(int type, std::string data)
+ {
+ this->type = type;
+ this->data = data;
+ }
+
+ int type;
+ std::string data;
+};
+
+class MapObjectList
+{
+ public:
+ std::list<MapObject> objects;
};
-*/
/**
* Animation cycle of a tile image which changes the map accordingly.
@@ -401,6 +403,9 @@ class Map : public Properties, public ConfigListener
void addPortal(const std::string &name, int type,
int x, int y, int dx, int dy);
+ void addRange(const std::string &name, int type,
+ int x, int y, int dx, int dy);
+
void addPortalTile(const std::string &name, int type, int x, int y);
void updatePortalTile(const std::string &name, int type,
@@ -423,6 +428,11 @@ class Map : public Properties, public ConfigListener
void setPvpMode(int mode);
+ ObjectsLayer* getObjectsLayer()
+ { return mObjects; }
+
+ std::string getObjectData(unsigned x, unsigned y, int type);
+
protected:
friend class Actor;
@@ -509,6 +519,7 @@ class Map : public Properties, public ConfigListener
SpecialLayer *mSpecialLayer;
SpecialLayer *mTempLayer;
+ ObjectsLayer *mObjects;
};
@@ -560,7 +571,8 @@ class MapItem
ARROW_DOWN = 5,
ARROW_LEFT = 6,
ARROW_RIGHT = 7,
- PORTAL = 8
+ PORTAL = 8,
+ MUSIC = 9
};
MapItem();
@@ -609,4 +621,20 @@ class MapItem
int mY;
};
+class ObjectsLayer
+{
+ public:
+ ObjectsLayer(unsigned width, unsigned height);
+ ~ObjectsLayer();
+
+ void addObject(std::string name, int type, unsigned x, unsigned y,
+ unsigned dx, unsigned dy);
+
+ MapObjectList *getAt(unsigned x, unsigned y);
+ private:
+ MapObjectList **mTiles;
+ unsigned mWidth;
+ unsigned mHeight;
+};
+
#endif
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp
index d930bf083..25e9750de 100644
--- a/src/resources/mapreader.cpp
+++ b/src/resources/mapreader.cpp
@@ -355,6 +355,11 @@ Map *MapReader::readMap(xmlNodePtr node, const std::string &path)
// map->addPortal(_("Spawn: ") + objName, MapItem::PORTAL,
// objX, objY, objW, objH);
}
+ else if (objType == "MUSIC")
+ {
+ map->addRange(objName, MapItem::MUSIC,
+ objX, objY, objW, objH);
+ }
else
{
logger->log1(" Warning: Unknown object type");
diff --git a/src/sound.cpp b/src/sound.cpp
index be366b496..e3b1a9bcc 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -214,7 +214,7 @@ void Sound::playMusic(const std::string &filename)
haltMusic();
- if ((mMusic = loadMusic(filename)))
+ if (!filename.empty() && (mMusic = loadMusic(filename)))
Mix_PlayMusic(mMusic, -1); // Loop forever
}
diff --git a/src/sound.h b/src/sound.h
index 4f11f38ac..0de9f0636 100644
--- a/src/sound.h
+++ b/src/sound.h
@@ -107,6 +107,9 @@ class Sound : public ConfigListener
void volumeRestore();
+ std::string getCurrentMusicFile()
+ { return mCurrentMusicFile; }
+
private:
/** Logs various info about sound device. */
void info();