Tabs.class.php 4.48 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
    private $mapwidth;
    private $mapheight;
    private $tabsRaw;
    private $tabs;
11
    private $edEntities;
Orbmancer's avatar
Orbmancer committed
12
13
14
15
16
17

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

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

Orbmancer's avatar
Orbmancer committed
20
21
        for($i=1; $i<=5; ++$i)
            for($j=1; $j<=5; ++$j)
Orbmancer's avatar
Orbmancer committed
22
                $this->tabs[$i][$j] = array();
23

Orbmancer's avatar
Orbmancer committed
24
25
26
27
28
    }

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

Orbmancer's avatar
Orbmancer committed
29
        $this->tabsRaw = $xml->Data->contents->__toString();
30
31
32
33
34
        $this->edEntities = $xml->Data->edEntities->children();
        /*foreach($this->edEntities as $k => $v) {
            var_dump($v);
            echo '<br/>';
        }*/
Orbmancer's avatar
Orbmancer committed
35
36
37
38
39

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

        $line = array();

Orbmancer's avatar
Orbmancer committed
40
        for($i=0; $i<count($tabs)-1; ++$i){
Orbmancer's avatar
Orbmancer committed
41
42
43
            $tab_x = $this->getTabX($i);
            $tab_y = $this->getTabY($i);

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

46
            if(count($line) == 40){
Orbmancer's avatar
Orbmancer committed
47
                $this->tabs[$tab_x][$tab_y][] = $line;
Orbmancer's avatar
Orbmancer committed
48
49
50
                $line = array();
            }
        }
Orbmancer's avatar
Orbmancer committed
51
52
53
    }

    private function getTabX($index){
Orbmancer's avatar
Orbmancer committed
54
        return 1+(int) (floor($index / self::$tabwidth) % 5);
Orbmancer's avatar
Orbmancer committed
55
56
57
    }

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

    public function whereIsMyLevelLocated(){
Orbmancer's avatar
Orbmancer committed
62
63
64
        $x = 0;
        $y = 0;
        $exit = false;
Orbmancer's avatar
Orbmancer committed
65
66
67

        for($i=1; $i<=5; ++$i) {
            for ($j = 1; $j <= 5; ++$j) {
Orbmancer's avatar
Orbmancer committed
68
                foreach($this->tabs[$i][$j] as $line) {
Orbmancer's avatar
Orbmancer committed
69
                    foreach($line as $block){
Orbmancer's avatar
Orbmancer committed
70
71
72
73
                        if($block != 0){
                            $x = $i;
                            $y = $j;
                            $exit = true;
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
                }
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
87
        }

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

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

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


        $this->edEntities = simplexml_load_string('<edEntities></edEntities>');
111
112
113
    }

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

116
117
118
119
        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){
120
                        $txt.= $block.',';
Orbmancer's avatar
Orbmancer committed
121
122
123
124
125
                    }
                }
            }
        }

126
127
128
129
        return array(
            'content' => $txt,
            'edEntities' => $this->edEntities
        );
Orbmancer's avatar
Orbmancer committed
130
131
    }

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    public function setTab($data, $x, $y){
        $this->tabs[$x][$y] = $data['content'];

        //var_dump($data['edEntities']);

        if(!empty($data['edEntities']))
            foreach($data['edEntities'] as $key => $value){
                //echo 'EDIT:'.$value.'<br/>';
                $child = $this->edEntities->addChild('edentity', $value);

                foreach($value->attributes() as $k => $v){
                    $newval = $v;
                    if($k == 'x') {
                        $newval = ((int)$v) - 40 * ($data['from_x'] - $x);
                        //echo 'ancient: '.$v.' new_x:'.$newval.' from='.$data['from_x'].' x='.$x.'<br/>';
                    }
                    if($k == 'y') {
                        $newval = ((int)$v) - 30 * ($data['from_y'] - $y);
                        //echo 'ancient: '.$v.' new_y:'.$newval.' from='.$data['from_y'].' y='.$y.'<br/>';
                    }

                    $child->addAttribute($k, $newval);
                }
            }
156
157
158
159
    }

    public function getTab(){
        $where = $this->whereIsMyLevelLocated();
160
161
162
163
164
165
166

        return array(
            'content' => $this->tabs[$where['x']][$where['y']],
            'edEntities' => $this->edEntities,
            'from_x' => $where['x'],
            'from_y' => $where['y']
        );
Orbmancer's avatar
Orbmancer committed
167
    }
Erwan Normand's avatar
Erwan Normand committed
168
}