Tabs.class.php 3.03 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
    private static $tabwidth = 40;
Orbmancer's avatar
Orbmancer committed
6
    private static $tabheight = 30;
Orbmancer's avatar
Orbmancer committed
7
8
9
10
11
12
13
14
15
16
    private $mapwidth;
    private $mapheight;
    private $tabsRaw;
    private $tabs;

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

Orbmancer's avatar
Orbmancer committed
17
        $this->tabs = array();
Orbmancer's avatar
Orbmancer committed
18

Orbmancer's avatar
Orbmancer committed
19
20
        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
Orbmancer's avatar
Orbmancer committed
21
                $this->tabs[$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
        $this->tabsRaw = $xml->Data->contents->__toString();

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

        $line = array();

Orbmancer's avatar
Orbmancer committed
33
        for($i=0; $i<count($tabs)-1; ++$i){
Orbmancer's avatar
Orbmancer committed
34
35
36
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);

37
            $line[] = (int) $tabs[$i];
Orbmancer's avatar
Orbmancer committed
38

39
            if(count($line) == 40){
Orbmancer's avatar
Orbmancer committed
40
                $this->tabs[$tab_x][$tab_y][] = $line;
Orbmancer's avatar
Orbmancer committed
41
42
43
                $line = array();
            }
        }
Orbmancer's avatar
Orbmancer committed
44
45
46
    }

    private function getTabX($index){
Orbmancer's avatar
Orbmancer committed
47
        return 1+(int) (floor($index / self::$tabwidth) % 5);
Orbmancer's avatar
Orbmancer committed
48
49
50
    }

    private function getTabY($index){
Orbmancer's avatar
Orbmancer committed
51
        return 1+(int) floor($index / (self::$tabwidth * $this->mapwidth * self::$tabheight));
Orbmancer's avatar
Orbmancer committed
52
53
54
    }

    public function whereIsMyLevelLocated(){
Orbmancer's avatar
Orbmancer committed
55
56
57
        $x = 0;
        $y = 0;
        $exit = false;
Orbmancer's avatar
Orbmancer committed
58
59
60

        for($i=1; $i<=5; ++$i) {
            for ($j = 1; $j <= 5; ++$j) {
Orbmancer's avatar
Orbmancer committed
61
                foreach($this->tabs[$i][$j] as $line) {
Orbmancer's avatar
Orbmancer committed
62
                    foreach($line as $block){
Orbmancer's avatar
Orbmancer committed
63
64
65
66
                        if($block != 0){
                            $x = $i;
                            $y = $j;
                            $exit = true;
Orbmancer's avatar
Orbmancer committed
67
                        }
Orbmancer's avatar
Orbmancer committed
68
69
                        if($exit)
                            break;
Orbmancer's avatar
Orbmancer committed
70
                    }
Orbmancer's avatar
Orbmancer committed
71
72
                    if($exit)
                        break;
Orbmancer's avatar
Orbmancer committed
73
                }
Orbmancer's avatar
Orbmancer committed
74
75
                if($exit)
                    break;
Orbmancer's avatar
Orbmancer committed
76
            }
Orbmancer's avatar
Orbmancer committed
77
78
            if($exit)
                break;
Orbmancer's avatar
Orbmancer committed
79
80
        }

Orbmancer's avatar
Orbmancer committed
81
82
83
84
        return array(
            'x' => $x,
            'y' => $y
        );
Orbmancer's avatar
Orbmancer committed
85
86
87
    }

    public function fillWithZeros($x, $y){
Orbmancer's avatar
Orbmancer committed
88
        for($j=0; $j<self::$tabheight; ++$j){
Orbmancer's avatar
Orbmancer committed
89
90
            $line = array();
            for($i=0; $i<self::$tabwidth; ++$i){
Orbmancer's avatar
Orbmancer committed
91
                $line[] = 0;
Orbmancer's avatar
Orbmancer committed
92
            }
Orbmancer's avatar
Orbmancer committed
93
            $this->tabs[$x][$y][] = $line;
Orbmancer's avatar
Orbmancer committed
94
95
96
        }
    }

97
98
99
100
101
102
103
    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
104
105
        $txt = '';

106
107
108
109
        for($j=1; $j<=5; ++$j){
            for($l=0; $l<=29; ++$l){
                for($i=1; $i<=5; ++$i){
                    foreach($this->tabs[$i][$j][$l] as $b => $block){
110
                        $txt.= $block.',';
Orbmancer's avatar
Orbmancer committed
111
112
113
114
115
116
117
118
                    }
                }
            }
        }

        return $txt;
    }

119
    public function setTab($tab, $x, $y){
Orbmancer's avatar
Orbmancer committed
120
        $this->tabs[$x][$y] = $tab;
121
122
123
124
    }

    public function getTab(){
        $where = $this->whereIsMyLevelLocated();
Orbmancer's avatar
Orbmancer committed
125
        return $this->tabs[$where['x']][$where['y']];
Orbmancer's avatar
Orbmancer committed
126
    }
Erwan Normand's avatar
Erwan Normand committed
127
}