-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontrol.js
204 lines (173 loc) · 7.57 KB
/
control.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
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
// TODO: Would be nice to make the structure nicer - the way how you add different types of control like keyboard input, mouse-click, npc-follow
// TODO: Add goto a given position
// TODO: Maybe fuse into Component.js
/**
* attach it to a Component to add control: Following, Mouse Control -> goto click
* @param obj
*/
function control(obj) {
this.obj = obj;
// Follow Properties
this.doFollow = false;
this.route;
this.routeIndex;
// Mouse/Touch move
this.swipMove = false;
// Goto Click
this.goto = false;
this.finished = true;
this.setTarget = function(target) {
this.target = target;
return this;
}
this.update = function() {
if (this.target != undefined && this.doFollow) {
this.destX = Math.floor((this.target.x+this.target.offset_x)/16);
this.destY = Math.floor((this.target.y+this.target.offset_y)/16);
this.follow();
}
if (this.swipMove && myGameArea.mousedown) {
this.gestureMove();
}
if (this.goto) {
if (myGameArea.clicked) {
// Update new click only if finished
if (this.finished) {
this.destX = Math.floor((myGameArea.clickdownX+myGameArea.gameCamera.x)/16);
this.destY = Math.floor((myGameArea.clickdownY+myGameArea.gameCamera.y)/16);
this.finished = false;
}
// Disable Key Control while going to obj
obj.disableControls = true;
this.gotoClick();
}
}
}
this.drawRoute = function() {
if (this.rects)
for (i = this.routeIndex; i < this.rects.length; i++) this.rects[i].draw(myGameArea.context);
}
// Follow a given object
this.follow = function() {
// No collision: Take direct route
if (!this.obj.collidable) {
this.directPath();
}
// Map collision: Take astar route
else {
if (!this.obj.ComponentCollision(this.target)) {
// Create / Update route
this.createRoute(this.destX, this.destY);
// Follow the route
this.followRoute();
}
}
}
// Go to a clicked position
this.gotoClick = function() {
// Create routet
if (this.route == undefined) {
this.createRoute(this.destX, this.destY);
}
// Follow the route
this.followRoute();
if (this.finished) {
myGameArea.clicked = false;
}
}
this.directPath = function() {
if (Math.floor((this.obj.x+this.obj.offset_x)/16) < Math.floor((this.target.x+this.target.offset_x)/16))
this.obj.speedX = this.obj.speed;
else if (Math.floor((this.obj.x+this.obj.offset_x)/16) > Math.floor((this.target.x+this.target.offset_x)/16))
this.obj.speedX = -this.obj.speed;
else if (Math.floor((this.obj.y+this.obj.offset_y)/16) < Math.floor((this.target.y+this.target.offset_y)/16))
this.obj.speedY = this.obj.speed;
else if (Math.floor((this.obj.y+this.obj.offset_y)/16) > Math.floor((this.target.y+this.target.offset_y)/16))
this.obj.speedY = -this.obj.speed;
}
this.createRoute = function(x, y) {
this.route = astarPath(Math.floor((this.obj.x+this.obj.offset_x)/16), Math.floor((this.obj.y+this.obj.offset_y)/16), x, y);
this.routeIndex = 0;
}
this.followRoute = function() {
if (this.route != undefined) {
if (this.route.length != 0) {
// else-if to prevent diagonal movement
if (Math.floor((this.obj.x+this.obj.offset_x)/16) < this.route[this.routeIndex].x)
this.obj.speedX = this.obj.speed;
else if (Math.floor((this.obj.x+this.obj.offset_x)/16) > this.route[this.routeIndex].x)
this.obj.speedX -= this.obj.speed;
else if (Math.floor((this.obj.y+this.obj.offset_y)/16) < this.route[this.routeIndex].y)
this.obj.speedY = this.obj.speed;
else if (Math.floor((this.obj.y+this.obj.offset_y)/16) > this.route[this.routeIndex].y)
this.obj.speedY = -this.obj.speed;
// To show the Path
if (myGameArea.debug) {
this.rects = [];
for (i = 0; i < this.route.length; i++) this.rects[i] = new Component(this.route[i].x * 16, this.route[i].y * 16).rectangle(16, 16, "black", false, "yellow", true);
}
// Increase routeIndex to follow the given path until the destination is reached
if ((Math.abs(this.route[this.routeIndex].x-((this.obj.x+this.obj.offset_x)/16)) < 1.0) && (Math.abs(this.route[this.routeIndex].y-((this.obj.y+this.obj.offset_y)/16)) < 1.0)) {
// Target not reached
if (this.route.length - 1 > this.routeIndex) {
this.routeIndex++;
}
// Target reached
else {
//console.log("Reached");
this.route = undefined;
this.rects = undefined;
this.obj.disableControls = false;
this.finished = true;
}
}
}
// Not reachable
else {
//console.log("Not reachable!")
this.route = undefined;
this.rects = undefined;
this.obj.disableControls = false;
this.finished = true;
}
if (this.obj.collided) {
//console.log("Collided");
this.route = undefined;
this.obj.disableControls = false;
this.rects = undefined;
this.finished = true;
}
}
}
// ###############################################
// ### Doesn't works so well with tablet touch ###
// ###############################################
this.gestureMove = function() {
if (Math.abs(myGameArea.x - myGameArea.clickdownX) > Math.abs(myGameArea.y - myGameArea.clickdownY)) {
if (myGameArea.x < myGameArea.clickdownX - 4)
obj.speedX -= obj.speed;
else if (myGameArea.x > myGameArea.clickdownX + 4)
obj.speedX += obj.speed;
}
else {
if (myGameArea.y < myGameArea.clickdownY - 4)
obj.speedY -= obj.speed;
else if (myGameArea.y > myGameArea.clickdownY + 4)
obj.speedY += obj.speed;
}
}
this.drawSwip = function() {
if (myGameArea.mousedown) {
ctx = myGameArea.context;
ctx.beginPath();
ctx.arc(myGameArea.clickdownX, myGameArea.clickdownY, 5, 0, 2 * Math.PI, true);
ctx.arc(myGameArea.x, myGameArea.y, 5, 0, 2 * Math.PI, true);
// Fill
ctx.fillStyle = "black";
ctx.fill();
// Outline
ctx.strokeStyle = "black";
ctx.stroke();
}
}
}