-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcars.js
224 lines (207 loc) · 6.02 KB
/
cars.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function Car(sprite, lights, doors, def, stopSignLines, carId) {
// lights are a hash with array keys (with [left,right]) of rearLights and headLights
this.sprite = sprite;
this.lights = lights;
this.doors = doors;
this.coordPath = copyCoordPath(def.coordPath);
this.startingCoords = this.coordPath[0];
this.coordPathIndex = 0;
this.type = def.type;
this.carId = carId;
this.state = 'new';
this.angleState = 'new';
this.lookaheadState = 'moving';
this.extraState = 'normal';
this.lastState = 'new';
this.def = def;
this.hit = false;
this.sequentialHitCounter = 0;
this.lastInScene = null;
this.sceneChangeCount = -1; //because we start null
this.restartTimer = 20;
this.stopSignLines = copyStopsignLines(stopSignLines);
this.stopSignLinesCopy = copyStopsignLines(stopSignLines);
this.movementAI = new MovementAI(this, stopSignLines);
}
Car.prototype.isInScene = function() {
if (this.sprite.position.y > 0 && this.sprite.position.y < (globalOptions.level.levelSize[1] || 600) && this.sprite.position.x > 0 && this.sprite.position.x < (globalOptions.level.levelSize[0] || 800)) {
return true
}
return false
};
Car.prototype.animateSprites = function(movement, reset) {
if (movement.state == 'hit') {
return;
}
if (!reset) {
this.sprite.position.x += movement.changeX;
this.sprite.position.y += movement.changeY;
}
else {
this.sprite.position.x = movement.changeX;
this.sprite.position.y = movement.changeY;
}
this.sprite.rotation = movement.rotation;
// DO THE LIGHTS
_.each(this.lights.rearLights, function(lightSprite) {
if (movement.lookaheadState == 'slowing') {
lightSprite.alpha = 1;
}
else {
lightSprite.alpha = .4;
}
});
if (this.angleState === 'turning') {
if (movement.leftTurn) {
this.lights.blinkers[0].alpha=1;
}
else {
this.lights.blinkers[1].alpha=1;
}
}
else {
if (this.lights.blinkers) {
this.lights.blinkers[0].alpha=0;
this.lights.blinkers[1].alpha=0;
}
}
};
Car.prototype.changeDoor = function(doorDef, openBool) {
doorSide = doorDef.split(' ')[0];
this.doors[doorSide].open = openBool;
};
Car.prototype.run = function(sharedCarState) {
if (this.sequentialHitCounter > 10) {
console.log("car " + this.carId + " got stuck; restarting");
this.restartTimer = -5;
}
if (this.movementAI.curSpeed > 1) {
this.sequentialHitCounter = 0;
}
var movementResult = this.movementAI.calcMovement(sharedCarState);
this.animateSprites(movementResult);
// decide if car should be restarted
var curInScene = this.isInScene();
if (curInScene != this.lastInScene) {
this.sceneChangeCount += 1;
}
this.lastInScene = curInScene;
if (this.sceneChangeCount == (this.def.sceneIncrementRestart || 2)) {
this.restartTimer -= 1;
}
};
Car.prototype.drawHonk = function() {
if (this.startedHonk) {
return
}
this.startedHonk = true;
var g = new PIXI.Graphics()
var that = this;
g.lineStyle(3, 0xFF0000, 1);
g.beginFill(0xFF0000, 0);
g.drawCircle(0,0,20);
g.endFill();
var honkOn = false;
setTimeout(function() {
run(10);
}, 300);
var run = function(counter) {
var interval = setInterval(function() {
if (!honkOn) {
that.sprite.addChild(g);
}
else {
that.sprite.removeChild(g);
}
honkOn = !honkOn;
counter -= 1;
if (!counter) {
setTimeout(function() {
that.startedHonk = false;
}, 500);
clearInterval(interval);
}
}, 100);
}
};
Car.prototype.getNewCar = function() {
var car = this
return new Car(car.sprite, car.lights, car.doors, car.def, car.stopSignLinesCopy, car.carId);
}
function runCars(cars, sharedCarState) {
var found = false
_.each(cars, function(car, idx) {
car.run(sharedCarState);
res = doCarRestart(car, sharedCarState)
if (res[0]) {
found = [idx, res[1]]
return
}
});
return found
}
function doCarRestart(car, sharedCarState) {
if (car.restartTimer > 0) {
return [false, false]
};
car.animateSprites({changeX:car.startingCoords[0], changeY: car.startingCoords[1], rotation:0}, true)
car.bbPoly = BBFromSprite(car.sprite);
var collisionResult = false;
_.each(sharedCarState.cars, function(carObj, carId) {
if (collisionResult || (carObj.carId === car.carId)) {
return
}
if (checkCollision2(car.movementAI.lookaheadAI.lookaheadBBPoly, carObj.bbPoly)) {
car.animateSprites({changeX:getRandomInt(-500000,-500), changeY: getRandomInt(-500000,-500), rotation:0}, true)
car.bbPoly = BBFromSprite(car.sprite);
car.movementAI.lookaheadAI.bbPoly = car.bbPoly;
collisionResult = [false, false]
}
});
if (collisionResult) { // dont add car back until next tick, then check bb again
return collisionResult;
}
var newcar = car.getNewCar();
return [true, newcar]
}
function setupSharedCarState(setupResult, cars, staticCollisionObjects, bike, intersectionDefs) {
allStopsignIntersections = _.uniq(_.map(setupResult.stopSignLines, function(v,k) { return k}));
var ss = {stopSignLines: setupResult.stopSignLines,
stopSignQueues: {},
cars: {},
allStopsignIntersections: allStopsignIntersections,
carsInIntersection: {},
carsAtStopsign: {},
staticCollisionObjects: staticCollisionObjects,
theBike: bike,
trafficLightLines: setupResult.trafficLightLines,
intersections: setupIntersectionDefs(intersectionDefs),
doors: {},
};
_.each(allStopsignIntersections, function(intersectionId) {
ss.stopSignQueues[intersectionId] = [];
});
_.each(cars, function(car) {
ss.cars[car.carId] = car;
});
return ss
}
function copyStopsignLines(d) {
var n = {};
_.each(d, function(v, k) {
var newv = [];
_.each(v, function(el) {
newv.push($.extend(true, {}, el));
});
n[k] = newv;
});
return n
}
function copyCoordPath(p) {
var n = [];
_.each(p, function(el) {
var newel = el.slice(0);
n.push(newel);
});
return n
}