]> git.lizzy.rs Git - dragonblocks.git/blob - engine/pixel_manipulator.js
Initial Commit
[dragonblocks.git] / engine / pixel_manipulator.js
1 /*
2  * pixel_manipulator.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.PixelManipulator = class{
24         constructor(arr){
25                 let pos = null;
26                 this.content = [];
27                 for(let y = 0; y < arr.length; y++){
28                         for(let x = 0; x < arr[y].length; x++){
29                                 if(arr[y][x][0] == "ยง"){
30                                         pos = {x: x, y: y};
31                                         arr[y][x] = arr[y][x].slice(1, arr[y][x].length);
32                                 }
33                                 if(arr[y][x] == "")
34                                         continue;
35                                 this.content.push({
36                                         x: x,
37                                         y: y,
38                                         node: arr[y][x]
39                                 });
40                         }
41                 }
42                 if(! pos)
43                         pos = {x: 0, y: 0};
44                 for(let pixel of this.content){
45                         pixel.x = pixel.x - pos.x;
46                         pixel.y = pixel.y - pos.y;
47                 }
48                 this.functions = [];
49                 
50         }
51         apply(x, y){
52                 for(let pixel of this.content){
53                         if(! dragonblocks.getNode(pixel.x + x, pixel.y + y))
54                                 continue;
55                         let doApply = true
56                         for(let func of this.functions)
57                                 if(func(dragonblocks.getNode(pixel.x + x, pixel.y + y).toNode(), pixel.x + x, pixel.y + y, pixel.node) == false)
58                                         doApply = false;
59                         if(doApply)
60                                 dragonblocks.setNode(pixel.x + x, pixel.y + y, pixel.node);
61                 }
62         }
63         replace(toReplace, replaceWith){
64                 for(let pixel of this.content){
65                         if(pixel.node == toReplace)
66                                 pixel.node = replaceWith;
67                 }
68         }
69         addFunction(func){
70                 this.functions.push(func);
71         }
72 }
73 dragonblocks.getPixelManipulator = function(arr){
74         return new dragonblocks.PixelManipulator(arr);
75 }