-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
209 lines (187 loc) · 5.96 KB
/
bot.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
205
206
207
208
209
ig.module('plugins.bot')
.requires(
'impact.entity',
'impact.timer'
)
.defines(function() {
ig.bot = ig.Entity.extend({
bot_default: {
// Movements loop properties
movements: {},
movement_start: 0
},
timer: null,
step: 0,
pause: true,
distance: 0,
// Movements (Can be refefined)
bot_walk: function(direction) {
if(direction == 'left') {
this.accel.x = -(this.standing ? this.accelDef.ground : this.accelDef.air);
} else {
this.accel.x = (this.standing ? this.accelDef.ground : this.accelDef.air);
}
},
bot_jump: function(vel) {
this.vel.y = vel;
},
bot_stop: function() {
this.vel = { x: 0, y: 0 };
this.accel = { x: 0, y: 0 };
},
// Impact events
init: function(x, y, settings) {
this.parent(x, y, settings);
// Exist bot object?
if(this.bot == undefined) {
console.error('BOT Error: object bot is undefined');
return;
}
// Bot config
if(this.bot.config == undefined) this.bot.config = {};
if(this.bot.config.loop != false) this.bot.config.loop = true;
if(this.bot.config.hole_jump != false) this.bot.config.hole_jump = true;
if(this.bot.config.death_fall != false) this.bot.config.death_fall = true;
// Exist bot.movements object?
if(this.bot.movements == undefined || this.bot.movements.length == 0) {
console.error('BOT Error: object bot.movements is empty or undefined');
return;
}
// Movement start
this.bot.movement_start = typeof this.bot.movement_start !== 'undefined' ? this.bot.movement_start : this.bot_default.movement_start;
this.step = this.bot.movement_start;
},
update: function() {
var tx = Math.round(this.pos.x / ig.game.collisionMap.tilesize),
ty = Math.round(this.pos.y / ig.game.collisionMap.tilesize),
mx = this.vel.x * ig.system.tick,
my = this.vel.y * ig.system.tick,
step = this.bot.movements[this.step];
if(this.step == null) return;
// Function
if(typeof step.action == 'function') {
step.action.call(this);
this.step += 1;
// Start function
if(typeof step.start == 'function') step.start.call(this);
// Complete function
if(typeof step.complete == 'function') step.complete.call(this);
}
// Wait
if(step.action == 'wait') {
// Wait for entity on screen
var entity = ig.game.getEntityByName(step.entity);
if(step.entity != null && entity != undefined && this.distanceTo(entity) > ig.system.width) {
this.pause = true;
} else if(step.duration > 0) {
this.pause = false;
// Timer undefined
if(this.timer == null) {
this.timer = new ig.Timer(step.duration);
// Start function
if(typeof step.start == 'function') step.start.call(this);
} else if(this.timer.delta() >= 0) {
this.timer = null;
this.step += 1;
// Complete function
if(typeof step.complete == 'function') step.complete.call(this);
}
} else {
console.error('BOT Error: wait duration is undefined');
}
} else {
this.pause = false;
}
// Jump
if(step.action == 'jump') {
if(this.standing && this.distance == 0) {
this.distance = 0;
this.bot_jump(-step.vel);
// Start function
if(typeof step.start == 'function') step.start.call(this);
} else if(!this.standing && this.vel.y != 0) {
if(step.direction != undefined) this.bot_walk(step.direction);
// Calculate distance
var distance_y = my;
if(distance_y < 0) distance_y = distance_y * -1;
this.distance += distance_y;
} else {
this.bot_stop();
this.step += 1;
this.distance = 0;
// Complete function
if(typeof step.complete == 'function') step.complete.call(this);
}
}
// Walk
if(step.action == 'walk') {
// Setup walk
if(this.vel.x == 0 && this.distance == 0) {
this.distance = 0;
// Start function
if(typeof step.start == 'function') step.start.call(this);
}
// Change direction (collision x)
if(this.pos.x == this.last.x && this.distance != 0) {
if(step.direction == 'left') {
step.direction = 'right';
} else if(step.direction == 'right') {
step.direction = 'left';
}
}
// Move
if(step.direction != undefined) this.bot_walk(step.direction);
// Calculate distance
var distance_x = mx;
if(distance_x < 0) distance_x = distance_x * -1;
if(this.vel.y == 0) this.distance += distance_x;
if(this.distance >= step.distance) {
this.bot_stop();
this.step += 1;
this.distance = 0;
// Complete function
if(typeof step.complete == 'function') step.complete.call(this);
}
}
// Hole jump
if(this.bot.config.hole_jump && this.standing) {
if(this.vel.x < 0 && (tx > 0 && ty < ig.game.collisionMap.height - 1)) {
if(ig.game.collisionMap.data[ty+1][tx-1] == 0) {
this.bot_jump(-this.jump);
}
}
if(this.vel.x > 0 && (tx < ig.game.collisionMap.height - 1 && ty > 0)) {
if(ig.game.collisionMap.data[ty+1][tx+1] == 0) {
this.bot_jump(-this.jump);
}
}
}
// Hole wall
if(this.bot.config.hole_wall && step.action != 'jump') {
if(this.vel.x < 0 && (tx > 0 && ty < ig.game.collisionMap.height - 1)) {
if(ig.game.collisionMap.data[ty+1][tx-1] == 0) {
step.direction = 'right';
}
}
if(this.vel.x > 0 && (ty < ig.game.collisionMap.height - 1 && ty > 0)) {
if(ig.game.collisionMap.data[ty+1][tx+1] == 0) {
step.direction = 'left';
}
}
}
// Routine complete
if(this.step > this.bot.movements.length - 1) {
this.step = 0;
if(!this.bot.config.loop) this.step = null;
}
// During function
if(typeof step.during == 'function') step.during.call(this);
// Death fall
if(this.bot.config.death_fall) {
if(ty > ig.game.collisionMap.height) this.kill();
}
// Refresh
this.parent();
}
});
});