Tabs.class.php 3.19 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
    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();

Orbmancer's avatar
Orbmancer committed
19
20
21
        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
                $this->contents[$i][$j] = array();
Orbmancer's avatar
Orbmancer committed
22
23
24
25
26
    }

    public function importXML($string) {
        $xml = simplexml_load_string($string);

Orbmancer's avatar
Orbmancer committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        $this->tabsRaw = $xml->Data->contents->__toString();

        $tabs = explode(',', $this->tabsRaw);

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

        for($i=0; $i<count($tabs); ++$i){
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);
            $line[] = (int) $tabs[$i];


            if($prev_y != $tab_y){
                $this->contents[$tab_x][$tab_y][] = $line;
                $line = array();
            }
        }
Orbmancer's avatar
Orbmancer committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    }

    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(){
Orbmancer's avatar
Orbmancer committed
64
65
66
        $x = 0;
        $y = 0;
        $exit = false;
Orbmancer's avatar
Orbmancer committed
67
68
69
70
71

        for($i=1; $i<=5; ++$i) {
            for ($j = 1; $j <= 5; ++$j) {
                foreach($this->contents[$i][$j] as $line) {
                    foreach($line as $block){
Orbmancer's avatar
Orbmancer committed
72
73
74
75
76
                        if($block != 0){
                            $x = $i;
                            $y = $j;
                            $exit = true;
                            break;
Orbmancer's avatar
Orbmancer committed
77
78
                        }
                    }
Orbmancer's avatar
Orbmancer committed
79
80
                    if($exit)
                        break;
Orbmancer's avatar
Orbmancer committed
81
                }
Orbmancer's avatar
Orbmancer committed
82
83
                if($exit)
                    break;
Orbmancer's avatar
Orbmancer committed
84
            }
Orbmancer's avatar
Orbmancer committed
85
86
            if($exit)
                break;
Orbmancer's avatar
Orbmancer committed
87
88
        }

Orbmancer's avatar
Orbmancer committed
89
90
91
92
        return array(
            'x' => $x,
            'y' => $y
        );
Orbmancer's avatar
Orbmancer committed
93
94
95
    }

    public function fillWithZeros($x, $y){
Orbmancer's avatar
Orbmancer committed
96
        for($j=0; $j<self::$tabheight; ++$j){
Orbmancer's avatar
Orbmancer committed
97
98
            $line = array();
            for($i=0; $i<self::$tabwidth; ++$i){
Orbmancer's avatar
Orbmancer committed
99
                $line[] = 0;
Orbmancer's avatar
Orbmancer committed
100
101
102
103
104
            }
            $this->contents[$x][$y][] = $line;
        }
    }

105
106
107
108
109
110
111
    public function fillBlank(){
        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
                $this->fillWithZeros($i, $j);
    }

    public function toString(){
Orbmancer's avatar
Orbmancer committed
112
113
114
        $txt = '';

        for($i=1; $i<=5; ++$i){
Orbmancer's avatar
Orbmancer committed
115
            for($j=1; $j<=5; ++$j) {
Orbmancer's avatar
Orbmancer committed
116
117
                foreach($this->contents[$i][$j] as $l => $line) {
                    foreach($line as $b => $block){
118
                        $txt.= $block.',';
Orbmancer's avatar
Orbmancer committed
119
120
121
122
123
124
125
126
                    }
                }
            }
        }

        return $txt;
    }

127
128
129
130
131
132
133
    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
134
    }
Erwan Normand's avatar
Erwan Normand committed
135
}