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
33
        $this->tabsRaw = $xml->Data->contents->__toString();

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

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

Orbmancer's avatar
Orbmancer committed
34
        for($i=0; $i<count($tabs)-1; ++$i){
Orbmancer's avatar
Orbmancer committed
35
36
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);
Orbmancer's avatar
Orbmancer committed
37
            echo 'TAB INDEX: '.$i.' : '.$tab_x.'|'.$tab_y.'<br/>';
Orbmancer's avatar
Orbmancer committed
38
39
40
41
            $line[] = (int) $tabs[$i];


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

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

    private function getTabY($index){
Orbmancer's avatar
Orbmancer committed
53
        return 1+ (int) floor($index / (self::$tabwidth * $this->mapwidth * self::$tabheight));
Orbmancer's avatar
Orbmancer committed
54
55
56
57
58
59
60
61
62
63
64
    }

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

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

    public function whereIsMyLevelLocated(){
Orbmancer's avatar
Orbmancer committed
65
66
67
        $x = 0;
        $y = 0;
        $exit = false;
Orbmancer's avatar
Orbmancer committed
68
69
70

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

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

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

107
108
109
110
111
112
113
    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
114
115
116
        $txt = '';

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

        return $txt;
    }

129
    public function setTab($tab, $x, $y){
Orbmancer's avatar
Orbmancer committed
130
        $this->tabs[$x][$y] = $tab;
131
132
133
134
    }

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