-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapEditor.html
152 lines (144 loc) · 4.95 KB
/
mapEditor.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<html>
<head>
<title>Map Editor</title>
<style type="text/css">
p { margin:0; padding:0; }
img { margin:0; padding:0; border:1px dashed #AAA; }
img.rail { width:53px; height:53px; }
</style>
<script type="text/javascript">
TILE_SIZE = 79;
NORTH = 1;
SOUTH = 2;
WEST = 3;
EAST = 4;
</script>
<script type="text/javascript" src="libs/common.js"></script>
<script type="text/javascript" src="libs/tilemap.js"></script>
<script type="text/javascript">
var map = new TileMap();
var tiles = map.tiles;
var selectedTool = 0;
function load(mapId) {
map.onload = function() {
drawEditor();
}
map.load(mapId);
}
function newmap() {
var w = parseInt(e('nmapw').value);
var h = parseInt(e('nmaph').value);
var i, j;
map.level = new Array();
for(i=0; i<h; i++) {
map.level[i] = new Array();
for(j=0; j<w; j++) {
map.level[i][j] = 0;
}
}
drawEditor();
}
function save(mapId) {
// génération du csv
var csv = '';
var i, j;
for(i=0; i<map.level.length; i++) {
for(j=0; j<map.level[i].length; j++) {
if(j!=0) {
csv += ';';
}
csv+= map.level[i][j];
}
csv+="\n";
}
// sauvegarde du csv
var xhr;
xhr = getXHR();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
if(xhr.responseText == "OK") {
alert('Saved.');
} else {
alert(xhr.responseText);
}
} else {
alert("Error save code " + xhr.status + " : " + xhr.statusText);
}
}
};
var params = "content="+csv;
xhr.open("POST", 'map.php?PUT='+mapId, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(params);
}
function loadEditor() {
var tools = '<p></p><img class="rail" id="stimg" src="arts/blank.png" style="border:solid red 3px;"></img>';
for(var tid in tiles) {
if(tid >= 0 && tid < 4096) // condition pour éliminer une clé 'shuffle' du tableau qui sort d'on ne sait où (Oo)
tools += '<img class="rail" src="arts/'+tiles[tid]+'.png" onclick="selectedTool = parseInt('+tid+'); e(\'stimg\').src= \'arts/'+tiles[tid]+'.png\'; return false;" style="margin:0 0.5em; border:solid 1px black;"></img>';
}
tools += '<img class="rail" src="arts/cat1-down-1.png" onclick="selectedTool = 4096; e(\'stimg\').src= \'arts/cat1-down-1.png\'; return false;" style="margin:0 0.5em; border:solid 1px black;"></img>';
tools += '<img class="rail" src="arts/cat2-down-1.png" onclick="selectedTool = 8192; e(\'stimg\').src= \'arts/cat2-down-1.png\'; return false;" style="margin:0 0.5em; border:solid 1px black;"></img>';
tools += '<img class="rail" src="arts/bonus1.png" onclick="selectedTool = 12288; e(\'stimg\').src= \'arts/bonus1.png\'; return false;" style="margin:0 0.5em; border:solid 1px black;"></img>';
tools += '</p>';
e('blocks').innerHTML = tools;
}
function drawEditor() {
var editor = '';
var i, j;
for(i=0; i<map.level.length; i++) {
editor += '<p>';
for(j=0; j<map.level[i].length; j++) {
var style = '';
if(map.level[i][j] > 4095) {
// Tuile spéciale
switch(map.level[i][j] >>> 12) {
case 1 : style = 'background: url(arts/bg.png) repeat;'; break; // depart j1
case 2 : style = 'background: url(arts/bg3.png) repeat;'; break; // depart j2
case 3 : style = 'background: url(arts/bg4.png) repeat;'; break; // item
}
}
editor += '<img src="arts/'+tiles[map.tileType(map.level[i][j])]+'.png" onclick="updateBlock(parseInt('+i+'), parseInt('+j+'))" style="'+style+'"></img>';
}
editor += '</p>';
}
e('mapedit').innerHTML = editor;
}
function updateBlock(i,j) {
if(selectedTool < 4096) {
// Tuiles normales
map.level[i][j] = selectedTool;
} else {
// Spéciaux
if(map.level[i][j] == 0) {
alert("An item or a start position cannot be affected to an empty tile.");
} else {
map.level[i][j] += selectedTool;
}
}
drawEditor();
}
</script>
</head>
<body>
<h1>Map Editor</h1>
<h2><a href="monorail-cat.html">Play Monorail Cats</a></h2>
<p>New map : W <input type="text" id="nmapw" value="7" style="width:3em;"/>
| H <input type="text" id="nmaph" value="7" style="width:3em;"/>
<input type="button" value="create new map" onclick="newmap(); return false;">
</p>
<p>Map Id : <input type="text" id="mapId" /> <input type="button" value="Load" onclick="load(e('mapId').value); return false;" /> <input type="button" value="Save" onclick="save(e('mapId').value); return false;" /></p>
<hr></hr>
<p id="blocks">
</p>
<hr></hr>
<div id="mapedit">
</div>
<script type="text/javascript">
loadEditor();
</script>
</body>
</html>