forked from qiuxiang/boxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
170 lines (152 loc) · 4.64 KB
/
app.js
File metadata and controls
170 lines (152 loc) · 4.64 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
161
162
163
164
165
166
167
168
169
170
var Application = function () {
this.boxbot = new Boxbot()
this.editor = new BoxbotEditor('.boxbot-commander')
this.imageReader = new ImageReader()
this.$image = document.querySelector('#image')
this.$random = document.querySelector('#random')
this.$run = document.querySelector('#run')
this.$reset = document.querySelector('#reset')
this.$duration = document.querySelector('#duration')
this.$resolution = document.querySelector('#resolution')
this.init()
this.reset()
}
Application.prototype.init = function () {
document.addEventListener('keydown', this.hotkey.bind(this))
this.$run.addEventListener('click', this.run.bind(this))
this.$reset.addEventListener('click', this.reset.bind(this))
this.$random.addEventListener('click', this.random.bind(this))
this.$duration.addEventListener('change', this.setDuration.bind(this))
this.$resolution.addEventListener('change', this.setResolution.bind(this))
this.$image.addEventListener('change', this.loadImage.bind(this))
}
/**
* 设置运行速度
*/
Application.prototype.setDuration = function () {
this.boxbot.setDuration(parseInt(this.$duration.value))
}
/**
* 设置地图尺寸
*/
Application.prototype.setResolution = function () {
this.boxbot.setResolution(parseInt(this.$resolution.value))
this.reset()
}
/**
* 读取图片并生成绘图命令
*/
Application.prototype.loadImage = function () {
if (this.$image.files.length > 0) {
this.imageReader
.read(this.$image.files[0], this.boxbot.map.columns, this.boxbot.map.rows)
.then((function (data) {
var commands = 'mov to 1,1\nmov bot\nmov top\n'
for (var y = 1; y <= data.length; y += 1) {
// 移动到下一个位置
if (y == data.length) {
commands += 'tun rig\ntra lef\n'
} else {
commands += 'tra bot\n'
}
var columns = data[y - 1].length
for (var x = 1; x <= columns; x += 1) {
// 最后一个方块无法修墙,结束绘图
if (y == data.length && x == columns) {
break
}
var _x = columns - x
var direction = 'lef'
if (y % 2) {
_x = [x - 1]
direction = 'rig'
}
if (x != 1) {
commands += 'tra ' + direction + '\n'
}
commands += 'biud\nbru ' + data[y - 1][_x] + '\n'
}
}
this.editor.setCodes(commands)
}).bind(this))
}
}
/**
* 键盘事件处理
*
* @param {Event} event
*/
Application.prototype.hotkey = function (event) {
if (event.target.tagName == 'BODY') {
var direction = {37: LEFT, 38: TOP, 39: RIGHT, 40: BOTTOM}[event.keyCode]
if (typeof direction != 'undefined') {
event.preventDefault()
if (direction == this.boxbot.bot.getCurrentDirection()) {
this.boxbot.run(this.boxbot.go).then(null, function (e) {
console.log(e)
})
} else {
this.boxbot.run(this.boxbot.turn, [direction])
}
} else if (event.keyCode == 32) {
event.preventDefault()
this.boxbot.run(this.boxbot.build)
}
}
}
Application.prototype.run = function () {
this.editor.clearFlag()
var parseError = false
var codes = this.editor.getCodes()
// 检查命令是否有误
for (var i = 0; i < codes.length; i += 1) {
if (codes[i] && this.boxbot.parse(codes[i]) === false) {
parseError = true
this.editor.setFlag(i, 'error')
}
}
// 依次运行命令
if (!parseError) {
var prev = 0
codes.forEach((function (code, i) {
if (code) {
this.boxbot.exec(code).then(
(function () {
if (i % 37 == 0) {
this.editor.scrollTo(i)
}
this.editor.clearFlag(prev)
this.editor.setFlag(i, 'success')
prev = i
}).bind(this),
(function (e) {
console.log(e)
this.editor.clearFlag(prev)
this.editor.setFlag(i, 'warning')
}).bind(this))
}
}).bind(this))
}
}
Application.prototype.random = function () {
// 先找出所有可修墙的位置
var positions = []
for (var y = 1; y <= this.boxbot.map.rows; y += 1) {
for (var x = 1; x <= this.boxbot.map.columns; x += 1) {
if (this.boxbot.map.getType([x, y]) == 'null') {
positions.push([x, y])
}
}
}
if (positions.length) {
this.boxbot.build(positions[Math.floor(Math.random() * positions.length)])
}
}
Application.prototype.reset = function () {
this.boxbot.queue = []
this.boxbot.map.clear()
this.boxbot.bot.init()
this.editor.clearFlag()
this.$image.value = ''
}
new Application()