]> git.lizzy.rs Git - dragonblocks.git/blob - engine/world.js
Add Map Manager
[dragonblocks.git] / engine / world.js
1 /*
2  * world.js
3  *
4  * Copyright 2021 Elias Fleckenstein <eliasfleckenstein@web.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23
24 dragonblocks.World = class
25 {
26         constructor(properties)
27         {
28                 this.name = properties.name;
29                 this.isLoaded = properties.isLoaded;
30
31                 if (this.isLoaded) {
32                         this.load();
33                 } else {
34                         this.mods = properties.mods;
35                         this.loadMods();
36
37                         this.maps = {};
38                         this.mapgen = properties.mapgen;
39                         this.loadMaps({});
40
41                         this.player = new dragonblocks.Player(null, this.maps["dragonblocks:map"]);
42                         this.player.setGamemode(properties.gamemode);
43                 }
44         }
45
46         serialize()
47         {
48                 let data = {
49                         mods: this.mods,
50                         player: this.player.serialize(),
51                         maps: {},
52                         mapgen: this.mapgen,
53                 };
54
55                 for (let name in this.maps)
56                         data.maps[name] = this.maps[name].serialize();
57
58                 return data;
59         }
60
61         deserialize(data)
62         {
63                 this.mods = data.mods;
64                 this.loadMods();
65
66                 this.maps = {};
67                 this.mapgen = data.mapgen;
68                 this.loadMaps(data.maps);
69
70                 this.player = new dragonblocks.Player(data.player, this.maps["dragonblocks:map"]);
71         }
72
73         save()
74         {
75                 if (dragonblocks.loggedin)
76                         return dragonblocks.backendCall("saveWorld", true, {name: this.name, world: JSON.stringify(this.serialize())});
77         }
78
79         load()
80         {
81                 this.deserialize($.getJSON("worlds/" + this.name + "/world.json").responseJSON);
82         }
83
84         loadMods()
85         {
86                 dragonblocks.loadMods(this.mods);
87         }
88
89         loadMaps(data)
90         {
91                 for (let name in dragonblocks.mapMgr.defs)
92                         this.maps[name] = dragonblocks.mapMgr.create(name, data[name], this.mapgen);
93         }
94 };
95
96 dragonblocks.World.Properties = class
97 {
98         constructor(isLoaded)
99         {
100                 this.name = "";
101                 this.isLoaded = isLoaded;
102
103                 if (! isLoaded) {
104                         this.mods = [];
105
106                         this.gamemode = dragonblocks.settings.defaultWorldOptions.gamemode;
107                         this.mapgen = dragonblocks.settings.defaultWorldOptions.mapgen;
108                 }
109         }
110
111         checkSpelling()
112         {
113                 return this.name.match(/^[a-zA-Z0-9]+$/);
114         }
115 };