Tabs.class.php 3.48 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
    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 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;
        }
    }

74
75
76
77
78
79
    public function fillBlank(){
        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
                $this->fillWithZeros($i, $j);
    }

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

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

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

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

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

Orbmancer's avatar
Orbmancer committed
102
        return $output;
Orbmancer's avatar
Orbmancer committed
103
104
    }

105
    public function toString(){
Orbmancer's avatar
Orbmancer committed
106
107
108
109
110
111
        $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){
112
                        $txt.= $block.',';
Orbmancer's avatar
Orbmancer committed
113
114
115
116
117
118
119
120
                    }
                }
            }
        }

        return $txt;
    }

121
    /*public function merge($txt, $x, $y){
Orbmancer's avatar
Orbmancer committed
122
123
124
125
126
127
128
        $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);
129
130
131
132
133
        $txt = $this->pushOnlyNonZeros($tab);
        unset($tab);

        return $txt;
    }*/
Orbmancer's avatar
Orbmancer committed
134

135
136
137
138
139
140
141
    public function setTab($tab, $x, $y){
        $this->contents[$x][$y] = $tab;
    }

    public function getTab(){
        $where = $this->whereIsMyLevelLocated();
        return $this->contents[$where['x']][$where['y']];
Orbmancer's avatar
Orbmancer committed
142
    }
Erwan Normand's avatar
Erwan Normand committed
143
}