Tabs.class.php 3.27 KB
Newer Older
Erwan Normand's avatar
Erwan Normand committed
1
2
<?php

Erwan Normand's avatar
Rename  
Erwan Normand committed
3
class Tabs
Erwan Normand's avatar
Erwan Normand committed
4
{
Orbmancer's avatar
Orbmancer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    private static $tabwidth = 40;
    private static $tabheight = 31;
    private $mapwidth;
    private $mapheight;
    private $tabsRaw;
    private $tabs;

    public function __construct() {
        $this->tabsRaw = null;
        $this->mapwidth = 5;
        $this->mapheight = 5;

        $this->contents = array();

    }

    public function importXML($string) {
        $xml = simplexml_load_string($string);
        $this->tabsRaw = $xml->Data->tabs->__toString();

        $this->contents = $this->extractContent($this->tabsRaw);
    }

    private function getTabX($index){
        return (int) floor($index / self::$tabwidth) % 5 +1;
    }

    private function getTabY($index){
        return (int) floor($index / (self::$tabwidth * $this->mapwidth * self::$tabheight)) +1;
    }

    public function settabs($tabs) {
        $this->tabs = $tabs;
    }

    public function gettabs() {
        return $this->tabs;
    }

    public function getTab($x, $y) {

    }

    public function whereIsMyLevelLocated(){

        for($i=1; $i<=5; ++$i) {
            for ($j = 1; $j <= 5; ++$j) {
                foreach($this->contents[$i][$j] as $line) {
                    foreach($line as $block){
                        if($block != '0'){
                            return array(
                                'x' => $i,
                                'y' => $j
                            );
                        }
                    }
                }
            }
        }

        return array();
    }

    public function fillWithZeros($x, $y){
        for($j=0; $j<self::$tabheight; +$j){
            $line = array();
            for($i=0; $i<self::$tabwidth; ++$i){
                $line[] = '0';
            }
            $this->contents[$x][$y][] = $line;
        }
    }

    public function extractContent($txt){
        $tabs = explode(',', $txt);
        $output = array();
Orbmancer's avatar
Orbmancer committed
81
82
83

        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
Orbmancer's avatar
Orbmancer committed
84
                $output[$i][$j] = array();
Orbmancer's avatar
Orbmancer committed
85
86
87
88

        $line = array();
        $prev_y = 1;

Orbmancer's avatar
Orbmancer committed
89
        for($i=0; $i<count($tabs); ++$i){
Orbmancer's avatar
Orbmancer committed
90
91
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);
Orbmancer's avatar
Orbmancer committed
92
            $line[] = $tabs[$i];
Orbmancer's avatar
Orbmancer committed
93
94

            if($prev_y != $tab_y){
Orbmancer's avatar
Orbmancer committed
95
                $output[$tab_x][$tab_y][] = $line;
Orbmancer's avatar
Orbmancer committed
96
97
98
                $line = array();
            }
        }
Orbmancer's avatar
Orbmancer committed
99

Orbmancer's avatar
Orbmancer committed
100
        return $output;
Orbmancer's avatar
Orbmancer committed
101
102
    }

Orbmancer's avatar
Orbmancer committed
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
132
    public function pushOnlyNonZeros($tab){
        $txt = '';

        for($i=1; $i<=5; ++$i){
            for($j=1; $j<=5; ++$i) {
                foreach($this->contents[$i][$j] as $l => $line) {
                    foreach($line as $b => $block){
                        if($block != '0'){
                            $txt.= $block.',';
                        }else{
                            $txt.= $tab[$i][$j][$l][$b];
                        }
                    }
                }
            }
        }

        return $txt;
    }

    public function merge($txt, $x, $y){
        $where = $this->whereIsMyLevelLocated();
        $this->contents[$x][$y] = $this->contents[$where['x']][$where['y']];

        if($where['x'] != $x || $where['y'] != $y)
            $this->fillWithZeros($where['x'], $where['y']);

        $tab = $this->extractContent($txt);

        return $this->pushOnlyNonZeros($tab);
Orbmancer's avatar
Orbmancer committed
133
    }
Erwan Normand's avatar
Erwan Normand committed
134
}