-
Notifications
You must be signed in to change notification settings - Fork 14
/
Agent.js
132 lines (102 loc) · 3.26 KB
/
Agent.js
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
class Agent {
constructor(start, goal, name, color, path = null) {
this.start = start;
this.goal = goal;
this.name = name;
this.color = color;
this.path = path; //[t, x, y]
this.pathLength = 0; //每个小车补全路径之前的路径长度
this.agentImg = loadImage('assets/agent.png');
this.isReached = false; //标记是否到达,到达后,就不会再参与冲突计算
this.turnCount = 0; //转弯次数
this.waitCount = 0; //等待次数
}
// 把路径补足到最大时间点
fullFillPath(maxT) {
var n = this.path.length;
var lastItem = this.path[n - 1];
// var lastItem =
for (var i = n; i < maxT; i++) {
this.path.push(lastItem);
}
}
// 计算转弯次数
calcTurnInPath() {
this.turnCount = 0;
for (var i = 2; i < this.path.length; i++) {
if (abs(this.path[i - 2]['x'] - this.path[i]['x']) == 1 &&
abs(this.path[i - 2]['y'] - this.path[i]['y']) == 1) {
this.turnCount += 1;
}
}
}
// 计算等待次数
calcWaitInPath() {
this.waitCount = 0;
for (var i = 1; i < this.path.length; i++) {
if (abs(this.path[i - 1]['x'] - this.path[i]['x']) == 0 &&
abs(this.path[i - 1]['y'] - this.path[i]['y']) == 0) {
this.waitCount += 1;
}
}
}
// 画Agent,执行一步
stepShow(t) {
let curx = this.path[t]['x'];
let cury = this.path[t]['y'];
translate(left_pos, top_pos);
var imgSize = 3 * cellw / 4;
imageMode(CENTER);
tint(this.color[0], this.color[1], this.color[2]);
image(this.agentImg, curx * cellw + cellw / 2, cury * cellh + cellh / 2, imgSize, imgSize);
translate(-left_pos, -top_pos);
// console.log(x, y);
// console.log(this.agentImg);
}
// 消除上一步的图像
stepOff(t) {
let curx = this.path[t]['x'];
let cury = this.path[t]['y'];
translate(left_pos, top_pos);
if (t < this.pathLength) {
strokeWeight(2);
stroke(51);
fill(255);
rect(curx * cellw, cury * cellh, cellw, cellh);
}
else {
var imgSize = 3 * cellw / 4;
imageMode(CENTER);
tint(255);
image(this.agentImg, curx * cellw + cellw / 2, cury * cellh + cellh / 2, imgSize, imgSize);
}
translate(-left_pos, -top_pos);
}
// 添加一个边框
addBorder(pos) {
let x = pos[0];
let y = pos[1];
translate(left_pos, top_pos);
strokeWeight(2);
stroke('#ff5252');
fill(255);
rect(x * cellw, y * cellh, cellw, cellh)
translate(-left_pos, -top_pos);
}
// 删除边框
delBorder(pos) {
let x = pos[0];
let y = pos[1];
translate(left_pos, top_pos);
strokeWeight(2);
stroke(51);
fill(255);
rect(x * cellw, y * cellh, cellw, cellh)
translate(-left_pos, -top_pos);
}
// 清除全部边框
closeBorder() {
this.delBorder(this.start);
this.delBorder(this.goal);
}
}