Foundation.java 4.32 KB
Newer Older
Florian Grabowski's avatar
Florian Grabowski committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package de._82grfl1bif.KPI_Visualizer.structures;

import de._82grfl1bif.KPI_Visualizer.helpers.Layout;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.util.Vector;

import javax.management.AttributeNotFoundException;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Foundation extends Structure{

    private ArrayList<Structure> children = new ArrayList<>();
    private Layout layout;

    public Foundation(int depth, Material material){ //Foundations are uniform Height
20
        this.width.x = 0;
Florian Grabowski's avatar
Florian Grabowski committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        this.depth = depth;
        this.material = material;
    }

    public ArrayList<Structure> getChildren() {
        return children;
    }

    public void addChildren(ArrayList<Structure> children) {
        for (Structure s:children) {
            s.setDepth(this.depth);
        }
        this.children.addAll(children);
    }

    public  void removeChildren(){
        this.children = new ArrayList<>();
    }

    public void optimizeLayout(){
        int cWith = 0;
        for (Structure s : children) {
            s.setLocation(location.clone().add(new Vector(1,depth,((cWith)+1))));
44
            cWith += (s.getWidth().x + 2);
Florian Grabowski's avatar
Florian Grabowski committed
45
        }
46
        this.width.x = cWith;
Florian Grabowski's avatar
Florian Grabowski committed
47
48
49
50
51
52
53
54
55
56
57
58
59
    }

    private static class DepthComparator implements Comparator<Foundation>{
        @Override
        public int compare(Foundation o1, Foundation o2) {
            return Integer.compare(o1.getDepth(), o2.getDepth());
        }
    }

    private static class WidthComparator implements Comparator<Structure>{

        @Override
        public int compare(Structure o1, Structure o2) {
60
            return Integer.compare(o1.getWidth().x, o2.getWidth().x);
Florian Grabowski's avatar
Florian Grabowski committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        }
    }

    private boolean baseFoundation = true;

    public void organizeFoundation(){
        if(this.depth == 0 && baseFoundation){ //rootElement prepwork and recursion call
            ArrayList<Foundation> allFoundations = collectAllFoundations();
            baseFoundation = false;
            allFoundations.sort(new DepthComparator());
            Collections.reverse(allFoundations); //higher Depths first
            for (Foundation f:allFoundations) {
                //if(!this.equals(f))
                f.organizeFoundation();
            }
        }else{ //normal Call and recursion
            this.children.sort(new WidthComparator());
            Collections.reverse(this.children); //Widest Building or Foundation first
            this.layout = new Layout(this.children);
            this.layout.createLayout();
81
            this.width.x = layout.getSize();
Florian Grabowski's avatar
Florian Grabowski committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        }
    }

    private ArrayList<Foundation> collectAllFoundations(){ //called on the base Foundation
        ArrayList<Foundation> fCollection = new ArrayList<>();
        fCollection.add(this);
        int currentDepth = this.depth;
        int lastDepthAdded = -1;
        while (lastDepthAdded != 0){ //while there are new nextDepths in this Depth.
            for (int i = 0; i < fCollection.size();i++) {
                if (!(fCollection.get(i).getDepth() < currentDepth)){ //if f is not part of a historical depth
                    fCollection.addAll(fCollection.get(i).getNextDepth());
                    lastDepthAdded = fCollection.get(i).getNextDepth().size();
                }
            }
            currentDepth += 1;
        }
        return fCollection;
    }

    private ArrayList<Foundation> getNextDepth(){
        ArrayList<Foundation> result = new ArrayList<>();
        for (Structure s:this.children) {
            if (s.getClass() == Foundation.class){
                result.add((Foundation) s);
            }else {
                s.setDepth(this.depth);
            }
        }
        for (Foundation f:result) {
            f.setDepth((this.depth)+1);
        }
        return result;
    }

    public void correctAllLocations(Location location){
        for (Structure s:this.children) {
            try {
                Point temp = this.layout.getCoordinateOf(s);
                s.setLocation(location.clone().add(temp.x,0,temp.y));
                if (s.getClass() == Foundation.class){
                    Foundation f = (Foundation)s;
                    f.correctAllLocations(f.getLocation());
                }
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}