]> git.lizzy.rs Git - dragonblocks.git/blob - engine/ressources.js
4aec9a652857a7b8b92181c0aed1e8fc09f5abcd
[dragonblocks.git] / engine / ressources.js
1 /*
2  * ressources.js
3  * 
4  * Copyright 2020 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 dragonblocks.Texture = class{
24         constructor(path){
25                 this.name = path.slice(path.lastIndexOf("/") + 1, path.length);
26                 this.path = path;
27                 dragonblocks.textures[this.name] = this;
28         }
29 }
30 dragonblocks.textures = {};
31 dragonblocks.loadTexture = function(path){
32         new dragonblocks.Texture(path);
33 };
34 {
35         let textures = $.getJSON({
36                 url: "api.php",
37                 method: "POST",
38                 data: {call: "getTextures"}
39         }).responseJSON;
40         for(let i in textures)
41                 dragonblocks.loadTexture(textures[i]);
42 }
43 dragonblocks.getTexture = function(texture){
44         if(! texture)
45                 return "none";
46         if(dragonblocks.textures[texture])
47                 return "url(" + dragonblocks.textures[texture].path + ")";
48         else
49                 return texture;
50 };
51 dragonblocks.resolveTextures = function(elem){
52         if(elem.nodeName == "IMG" && elem.attributes["texture"]){
53                 let texture = elem.attributes["texture"].nodeValue;
54                 elem.src = dragonblocks.textures[texture] ? dragonblocks.textures[texture].path : texture;
55         }
56         for(let child of elem.children)
57                 dragonblocks.resolveTextures(child);
58 }
59 dragonblocks.Sound = class{
60         constructor(path){
61                 this.name = path.slice(path.lastIndexOf("/") + 1, path.length);
62                 this.path = path;
63                 dragonblocks.sounds[this.name] = this;
64         }
65 }
66 dragonblocks.sounds = {};
67 dragonblocks.loadSound = function(path){
68         new dragonblocks.Sound(path);
69 };
70 dragonblocks.getSound = function(sound){
71         if(! sound)
72                         return "";
73         if(dragonblocks.sounds[sound])
74                 return dragonblocks.sounds[sound].path;
75         else
76                 return sound;
77 };
78 {
79         let sounds = $.getJSON({
80                 url: "api.php",
81                 method: "POST",
82                 data: {call: "getSounds"}
83         }).responseJSON;
84         for(let i in sounds)
85                 dragonblocks.loadSound(sounds[i]);
86 }
87 dragonblocks.playSound = function(sound){
88         new Audio(dragonblocks.getSound(sound)).play();
89 }