-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.js
122 lines (98 loc) · 3 KB
/
Robot.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
function Robot(world, x, y, genome) {
this.x = x;
this.maxX = x;
this.y = y;
this.world = world;
this.genome = genome;
this.chassisLength = genome.chassisLength;
this.chassisWidth = genome.chassisWidth;
this.frontGenome = genome.frontGenome;
this.rearGenome = genome.rearGenome;
this.lastMovement = Date.now();
this.alive = true;
this.deathTime = 8000;
// this.color = ColorUtils.fromHue(this.genome.hue);
this.color = ColorUtils.randomPastel();
this.add();
}
/**
* Add the robot to the world
*/
Robot.prototype.add = function() {
var chassisShape = new p2.Rectangle(this.chassisLength, this.chassisWidth);
chassisShape.collisionGroup = CONTACT_ROBOT;
chassisShape.collisionMask = GROUND;
this.chassis = new p2.Body({
mass: chassisShape.width * chassisShape.height / 3000,
position: [this.x, this.y]
});
this.chassis.color = this.color;
this.chassis.addShape(chassisShape);
this.world.addBody(this.chassis);
this.frontLinkage = new KlannLinkage(this.world,
this.x + this.chassisLength / 2,
this.y,
this.color,
this.frontGenome,
false);
this.frontLinkage.add();
this.rearLinkage = new KlannLinkage(this.world,
this.x - this.chassisLength / 2,
this.y,
this.color,
this.rearGenome,
true);
this.rearLinkage.add();
this.frontConstraint = new p2.LockConstraint(
this.chassis,
this.frontLinkage.baseBody,
{localAngleB: this.frontGenome.baseAngle}
);
this.rearConstraint = new p2.LockConstraint(
this.chassis,
this.rearLinkage.baseBody,
{localAngleB: -this.rearGenome.baseAngle}
);
this.driverConstraint = new p2.GearConstraint(
this.frontLinkage.driverBody,
this.rearLinkage.driverBody
);
this.world.addConstraint(this.frontConstraint);
this.world.addConstraint(this.rearConstraint);
this.world.addConstraint(this.driverConstraint);
};
/**
* Remove the Robot from the world
*/
Robot.prototype.remove = function() {
this.rearLinkage.remove();
this.frontLinkage.remove();
this.world.removeConstraint(this.frontConstraint);
this.world.removeConstraint(this.rearConstraint);
this.world.removeConstraint(this.driverConstraint);
this.world.removeBody(this.chassis);
};
/**
* Update the Robot's simulation
*/
Robot.prototype.update = function() {
if (!this.alive) {
return;
}
this.x = this.chassis.position[0];
this.y = this.chassis.position[1];
if (this.maxX + 0.2 < this.x) {
this.maxX = this.x;
this.lastMovement = Date.now();
}
if (this.lastMovement + this.deathTime < Date.now()) {
this.die();
}
};
/**
* Murder the robot
*/
Robot.prototype.die = function() {
this.remove();
this.alive = false;
};