1 /** @file world.cpp  World base class.
2  *
3  * @authors Copyright © 2015 Daniel Swanson <danij@dengine.net>
4  *
5  * @par License
6  * GPL: http://www.gnu.org/licenses/gpl.html
7  *
8  * <small>This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version. This program is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details. You should have received a copy of the GNU
15  * General Public License along with this program; if not, see:
16  * http://www.gnu.org/licenses</small>
17  */
18 
19 #include "doomsday/world/world.h"
20 #include "doomsday/world/materials.h"
21 #include "doomsday/world/map.h"
22 #include "doomsday/DoomsdayApp"
23 #include "doomsday/players.h"
24 #include "api_player.h"
25 
26 #include <de/App>
27 #include <de/Context>
28 
29 using namespace de;
30 using namespace world;
31 
32 static World *theWorld = nullptr;
33 
DENG2_PIMPL(World)34 DENG2_PIMPL(World)
35 {
36     BaseMap *map = nullptr;
37     world::Materials materials;
38 
39     Impl(Public *i) : Base(i)
40     {
41         theWorld = thisPublic;
42     }
43 
44     ~Impl()
45     {
46         theWorld = nullptr;
47     }
48 
49     DENG2_PIMPL_AUDIENCE(MapChange)
50 };
51 
DENG2_AUDIENCE_METHOD(World,MapChange)52 DENG2_AUDIENCE_METHOD(World, MapChange)
53 
54 World::World() : d(new Impl(this))
55 {
56     // Let players know that a world exists.
57     DoomsdayApp::players().forAll([this] (Player &plr)
58     {
59         plr.setWorld(this);
60         return LoopContinue;
61     });
62 }
63 
reset()64 void World::reset()
65 {
66     DoomsdayApp::players().forAll([] (Player &plr)
67     {
68         ddplayer_t &ddpl = plr.publicData();
69 
70         // Mobjs go down with the map.
71         ddpl.mo            = nullptr;
72         ddpl.extraLight    = 0;
73         ddpl.fixedColorMap = 0;
74         //ddpl.inGame        = false;
75         ddpl.flags         &= ~DDPF_CAMERA;
76 
77         // States have changed, the state pointers are unknown.
78         for (ddpsprite_t &pspr : ddpl.pSprites)
79         {
80             pspr.statePtr = nullptr;
81         }
82 
83         return LoopContinue;
84     });
85 }
86 
timeChanged(Clock const &)87 void World::timeChanged(Clock const &)
88 {
89     // Nothing to do.
90 }
91 
setMap(BaseMap * map)92 void World::setMap(BaseMap *map)
93 {
94     d->map = map;
95 }
96 
hasMap() const97 bool World::hasMap() const
98 {
99     return d->map != nullptr;
100 }
101 
map() const102 BaseMap &World::map() const
103 {
104     DENG2_ASSERT(hasMap());
105     return *d->map;
106 }
107 
materials()108 Materials &World::materials()
109 {
110     return d->materials;
111 }
112 
materials() const113 Materials const &World::materials() const
114 {
115     return d->materials;
116 }
117 
get()118 World &World::get()
119 {
120     DENG2_ASSERT(theWorld);
121     return *theWorld;
122 }
123 
notifyMapChange()124 void World::notifyMapChange()
125 {
126     DENG2_FOR_AUDIENCE2(MapChange, i) i->worldMapChanged();
127 }
128