-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.html
254 lines (234 loc) · 8.76 KB
/
editor.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editor</title>
</head>
<body>
<canvas id="myCanvas" width="1280" height="960" style="border:1px solid #000000;"></canvas>
<script src="./js/map.js"></script>
<script src="./js/images.js"></script>
<script src="./js/types.js"></script>
<script src="./js/utils.js"></script>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var map = new Map(30, 15, "map")
for (let x in map.grid) {
for (let y in map.grid[x]) {
map.grid[x][y].type = "void"
}
}
let selected = "snow"
let selectedDecor = ""
let fill = true
let drawing = false
function fillAreaEditor(x, y, type) {
let tile = map.grid[x][y]
let tileType = tile.type
tile.type = type
check(tile)
function check(sTile) {
if (sTile.up !== undefined) {
if (sTile.up.type === tileType) {
sTile.up.type = type
check(sTile.up)
}
} if (sTile.left !== undefined) {
if (sTile.left.type === tileType) {
sTile.left.type = type
check(sTile.left)
}
} if (sTile.down !== undefined) {
if (sTile.down.type === tileType) {
sTile.down.type = type
check(sTile.down)
}
} if (sTile.right !== undefined) {
if (sTile.right.type === tileType) {
sTile.right.type = type
check(sTile.right)
}
}
}
}
canvas.addEventListener('mousedown', function (e) {
getCursorPosition(canvas, e)
})
canvas.addEventListener('mouseup', function (e) {
getCursorPos(canvas, e)
})
canvas.addEventListener('mousemove', function (e) {
getMousePost(canvas, e)
})
var mx = null
var my = null
function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
var mx_ = event.clientX - rect.left
var my_ = event.clientY - rect.top
console.log(mx_, my_)
mx = mx_ - 2
my = my_
drawing = true
for (let x in map.grid) {
for (let y in map.grid[x]) {
if (mx > x * 16 && mx < x * 16 + 17 && my > y * 16 && my < y * 16 + 17) {
if (selected !== "") {
if (fill === false) {
map.grid[x][y].type = selected
} else if (fill === true) {
fillAreaEditor(x, y, selected)
}
}
if (selectedDecor === "del") {
if (fill === false) {
map.grid[x][y].decor = ""
}
} else if (selectedDecor !== "") {
if (fill === false) {
map.grid[x][y].decor = selectedDecor
}
}
//console.log(map.grid[x][y])
}
}
}
render()
}
function getCursorPos() {
const rect = canvas.getBoundingClientRect()
var mx_ = event.clientX - rect.left
var my_ = event.clientY - rect.top
console.log(mx_, my_)
mx = mx_ - 2
my = my_
drawing = false
} function getMousePost() {
const rect = canvas.getBoundingClientRect()
var mx_ = event.clientX - rect.left
var my_ = event.clientY - rect.top
//console.log(mx_, my_)
mx = mx_ - 2
my = my_
if (drawing === true) {
for (let x in map.grid) {
for (let y in map.grid[x]) {
if (mx > x * 16 && mx < x * 16 + 17 && my > y * 16 && my < y * 16 + 17) {
if (selected !== "") {
if (fill === false) {
map.grid[x][y].type = selected
}
}
if (selectedDecor === "del") {
if (fill === false) {
map.grid[x][y].decor = ""
}
} else if (selectedDecor !== "") {
if (fill === false) {
map.grid[x][y].decor = selectedDecor
}
}
//console.log(map.grid[x][y])
}
}
}
}
render()
}
render()
function render() {
ctx.fillStyle = "#000000"
ctx.fillRect(0, 0, 16 * map.width, 16 * map.height)
for (let x in map.grid) {
for (let y in map.grid[x]) {
let tile = map.grid[x][y]
if (tile.type === "snow") {
ctx.fillStyle = "#eeeeee"
} else if (tile.type === "ice") {
ctx.fillStyle = "#37bfb2"
} else if (tile.type === "water1") {
ctx.fillStyle = "#2260bd"
} else {
ctx.fillStyle = "#000000"
}
ctx.fillRect(16 * x, 16 * y, 15, 15)
if (tile.decor === "borealTree1") {
ctx.fillStyle = "#5c3a23"
ctx.fillRect(16 * x + 4, 16 * y + 4, 7, 7)
} else if (tile.decor === "rock") {
ctx.fillStyle = "#666666"
ctx.fillRect(16 * x + 4, 16 * y + 4, 7, 7)
} else {
ctx.fillStyle = "#000000"
}
}
}
}
document.addEventListener('keydown', logKey);
document.addEventListener('keyup', logDown);
function logKey(e) {
if (e.key === "1") {
selected = "snow"
selectedDecor = ""
} if (e.key === "2") {
selected = "ice"
selectedDecor = ""
} if (e.key === "3") {
selected = "water1"
selectedDecor = ""
} if (e.key === "8") {
selected = ""
selectedDecor = "rock"
} if (e.key === "9") {
selected = ""
selectedDecor = "borealTree1"
} if (e.key === "0") {
selected = "void"
selectedDecor = "del"
}
if (e.key === "e") {
console.log(mapString())
} if (e.key === "q") {
if (fill === true) {
fill = false
} else if (fill === false) {
fill = true
}
}
}
function mapString() {
let string = String(map.width + "" + map.height)
for (let x in map.grid) {
for (let y in map.grid[x]) {
let tile = map.grid[x][y]
if (tile.type === "void") {
string = string + "a"
} else if (tile.type === "snow") {
string = string + "b"
} else if (tile.type === "ice") {
string = string + "c"
} else if (tile.type === "water1") {
string = string + "d"
}
}
}
for (let x in map.grid) {
for (let y in map.grid[x]) {
let tile = map.grid[x][y]
if (tile.decor === "") {
string = string + "a"
} else if (tile.decor === "borealTree1") {
string = string + "b"
} else if (tile.decor === "rock") {
string = string + "c"
}
}
}
return String(string)
}
</script>
<p id="mapLink"></p>
</body>
</html>