]> git.lizzy.rs Git - dragonblocks.git/blob - engine/world.js
Abstract MapDisplay from Map
[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
36                         this.loadMods();
37
38                         this.map = new dragonblocks.Map();
39
40                         this.player = new dragonblocks.Player(null, this.map);
41                         this.player.setGamemode(properties.gamemode);
42
43                         dragonblocks.mapgen.generate(properties.mapgen, this.map);
44                 }
45         }
46
47         serialize()
48         {
49                 return {
50                         mods: this.mods,
51                         map: this.map.serialize(),
52                         player: this.player.serialize(),
53                 };
54         }
55
56         deserialize(data)
57         {
58                 this.mods = data.mods;
59
60                 this.loadMods();
61
62                 this.map = new dragonblocks.Map(data.map);
63                 this.player = new dragonblocks.Player(data.player, this.map);
64         }
65
66         save()
67         {
68                 if (dragonblocks.loggedin)
69                         return dragonblocks.backendCall("saveWorld", true, {name: this.name, world: JSON.stringify(this.serialize())});
70         }
71
72         load()
73         {
74                 this.deserialize($.getJSON("worlds/" + this.name + "/world.json").responseJSON);
75         }
76
77         loadMods()
78         {
79                 dragonblocks.loadMods(this.mods);
80         }
81 };
82
83 dragonblocks.World.Properties = class
84 {
85         constructor(isLoaded)
86         {
87                 this.name = "";
88                 this.isLoaded = isLoaded;
89
90                 if (! isLoaded) {
91                         this.mods = [];
92
93                         this.gamemode = dragonblocks.settings.defaultWorldOptions.gamemode;
94                         this.mapgen = dragonblocks.settings.defaultWorldOptions.mapgen;
95                 }
96         }
97
98         checkSpelling()
99         {
100                 return this.name.match(/^[a-zA-Z0-9]+$/);
101         }
102 };