Tabs.class.php 3.32 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
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);
Orbmancer's avatar
Orbmancer committed
36
            echo 'TAB INDEX: '.$i.' : '.$tab_x.'|'.$tab_y.'<br/>';
Orbmancer's avatar
Orbmancer committed
37

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

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

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

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

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

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

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

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

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

        for($i=1; $i<=5; ++$i){
Orbmancer's avatar
Orbmancer committed
108
            for($j=1; $j<=5; ++$j) {
109
                //echo 'TOSTRING, i='.$i.' j='.$j.' LINE COUNT='.count($this->tabs[$i][$j]).'<br/>';
Orbmancer's avatar
Orbmancer committed
110
                foreach($this->tabs[$i][$j] as $l => $line) {
111
                    //echo 'BLOCK COUNT = '.count($line).'<br/>';
Orbmancer's avatar
Orbmancer committed
112
                    foreach($line as $b => $block){
113
                        $txt.= $block.',';
Orbmancer's avatar
Orbmancer committed
114
115
116
117
118
119
120
121
                    }
                }
            }
        }

        return $txt;
    }

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

    public function getTab(){
        $where = $this->whereIsMyLevelLocated();
Orbmancer's avatar
Orbmancer committed
128
129
        echo 'WHERE:'.$where['x'].'|'.$where['y'].'<br/>';
        return $this->tabs[$where['x']][$where['y']];
Orbmancer's avatar
Orbmancer committed
130
    }
Erwan Normand's avatar
Erwan Normand committed
131
}