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