-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.phtml
More file actions
executable file
·160 lines (138 loc) · 4.59 KB
/
map.phtml
File metadata and controls
executable file
·160 lines (138 loc) · 4.59 KB
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
153
154
155
156
157
158
159
160
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet/less" href="source/less/style.less">
<link rel="stylesheet/less" href="source/less/map.less">
<script async type="text/javascript" src="source/js/less.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="map">
<main>
<table>
<tbody>
<!-- content set via js -->
</tbody>
</table>
<input id="console-input" type="text">
</main>
</body>
</html>
<script>
var map = {
init: function() {
map.addTr();
map.attachHandlers();
},
attachHandlers: function() {
$('#console-input').on('keyup', function(event) {
if (event.keyCode == 13) {
map.handleInput(event);
$(event.target).val('');
}
});
},
/**
* Js solution of making a bunch of tr's
* @return undefined
*/
addTr: function() {
var tBody = $('body.map').find('tbody');
for (i = 0; i < 10; i++) {
tBody.append('<tr></tr>');
}
map.addTd();
},
/**
* Js solution of making a bunch of td's for each tr`
* @return undefined
*/
addTd: function() {
var selector = $('body.map tbody tr');
$(selector).each(function(index, tr) {
for (i = 0; i < 10; i++) {
$(tr).append('<td></td>');
}
});
map.setActiveTile();
},
/**
* Set active tile on the map. If no location is given the default is
* 5,5.
* @var location array
* @return undefined
*/
setActiveTile: function(location) {
var table = $('body.map').find('tbody');
var location = location || [5,5];
table.find('tr:nth-child(' + location[0] + ')')
.find('td:nth-child(' + location[1] + ')')
.addClass('active');
},
/**
* Handles the input of the map
* @var event obj
* @return undefined
*/
handleInput: function(event) {
var direction = $('#console-input').val();
if (! (direction == 'n' || 's' || 'e' || 'w')) {
console.log('geen windrichting');
return;
} else {
map.handleMove(direction);
}
},
/**
* Handles the movement on the map, i.e. change the active tile location.
* Only reacts to cardinal directions.
* @var direction string
* @return undefined
*/
handleMove: function(direction) {
var activeTile = $('body.map').find('td.active');
switch (direction) {
case 'n':
var location = map.getActiveTileLocation();
location[0] = location[0] - 1;
map.setActiveTile(location);
$(activeTile).removeClass('active');
break;
case 's':
var location = map.getActiveTileLocation();
location[0] = location[0] + 1;
map.setActiveTile(location);
$(activeTile).removeClass('active');
break;
case 'w':
var location = map.getActiveTileLocation();
location[1] = location[1] - 1;
map.setActiveTile(location);
$(activeTile).removeClass('active');
break;
case 'e':
var location = map.getActiveTileLocation();
location[1] = location[1] + 1;
map.setActiveTile(location);
$(activeTile).removeClass('active');
break;
}
},
/**
* Gets the coordinates of the active map tile.
* @return location array
*/
getActiveTileLocation: function() {
var location = [];
$('body.map tbody tr').each(function(rowIndex, row) {
$(row).find('td').each(function(cellIndex, cell) {
if ($(cell).hasClass('active')) {
location = [rowIndex + 1, cellIndex + 1];
}
});
});
return location;
}
};
map.init();
</script>