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

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

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

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

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

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

        for($i=1; $i<=5; ++$i){
Orbmancer's avatar
Orbmancer committed
109
            for($j=1; $j<=5; ++$j) {
Orbmancer's avatar
Orbmancer committed
110
                foreach($this->tabs[$i][$j] as $l => $line) {
Orbmancer's avatar
Orbmancer committed
111
                    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 setTab($tab, $x, $y){
Orbmancer's avatar
Orbmancer committed
122
        $this->tabs[$x][$y] = $tab;
123
124
125
126
    }

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