-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoworker.js
227 lines (221 loc) · 8.71 KB
/
coworker.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
225
226
227
/* jshint esversion: 6 */
// EVERYTHING NEEDED FOR COWORKERS:
Coworker = (function(){
var waittime = 3 * 1000;
var walkspeed = 0.1;
var coworkers = [];
function Walker(appear, spawn) {
this.overhead = newText("", null, 50, 0xFF0000);
this.overhead.style.dropShadow = true;
this.overhead.style.dropShadowBlur = 5;
this.overhead.style.dropShadowDistance = 1;
this.overhead.style.dropShadowColor = 0;
guiStage.addChild(this.overhead);
this.appear = appear;
this.route = spawn;
this.target = spawn;
this.position = pointlist[spawn];
this.targetPoint = this.position.clone();
this.deltav = POINTS.new(0,0);
this.getSpriteOf = function(face, flip=false) {
let t = getTexture(this.appear + face);
t.anchor.set(0.5, 1.0);
if (this.appear == "boss") {
t.scale.set(GAME.scale.x, GAME.scale.y);
} else {
t.scale.set(GAME.scale.x * 0.7, GAME.scale.y * 0.7);
}
if (flip) {
t.scale.x = -1 * t.scale.x;
}
return t;
};
this.sprites = [
[this.getSpriteOf("B1"), this.getSpriteOf("B2")],
[this.getSpriteOf("S1", true), this.getSpriteOf("S2", true)],
[this.getSpriteOf("F1"), this.getSpriteOf("F2")],
[this.getSpriteOf("S1"), this.getSpriteOf("S2")]
];
this.sprite = this.sprites[2][0];
this.updatePosition = function() {
for ( var i = 0; i < this.sprites.length; i++ ) {
for ( var j = 0; j < this.sprites[i].length; j++ ) {
this.sprites[i][j].position.set( this.position.x, this.position.y );
}
}
this.overhead.position.x = this.position.x;
this.overhead.position.y = this.position.y - this.sprite.height*1.1;
};
this.newTarget = function() {
if ( this.target == "P" ) {
let mini = null;
let mina = null;
for ( var i = 0; i < pointlist.length; i++ ) {
let dist = this.position.dist(pointlist[i]);
if (!mina) {
mini = i;
mina = dist;
continue;
}
if ( dist < mina ) {
mini = i;
mina = dist;
}
}
this.route = mini;
this.target = mini;
this.targetPoint = pointlist[this.target].clone();
let diff = this.position.diff(this.targetPoint);
this.deltav = POINTS.new(
diff.x / diff.length() * deltaT * walkspeed,
diff.y / diff.length() * deltaT * walkspeed
);
return;
}
this.route = this.target;
do {
this.target = getPossibleNext(this.route);
} while ( this.appear == "boss" && this.route == this.target );
let split = 10 * GAME.scale.x;
this.targetPoint = pointlist[this.target].add(
POINTS.new(
(Math.random()-0.5)*split,
(Math.random()-0.5)*split
)
);
let diff = this.position.diff(this.targetPoint);
this.deltav = POINTS.new(
diff.x / diff.length() * deltaT * walkspeed,
diff.y / diff.length() * deltaT * walkspeed
);
};
this.waiting = -1;
this.update = function() {
this.updatePosition();
if ( this.position.diff(this.targetPoint).length() < 10 ) {
if ( this.waiting < 0 ) {
this.newTarget();
this.waiting = waittime;
}
} else {
this.position = this.position.add(this.deltav);
}
this.waiting -= deltaT;
this.see();
this.switchSprite();
};
this.detectedPlayer = function() {
this.overhead.text = "!!!";
GAME.switch_to(GAME.end, '\nYOU WERE CAUGHT BY '+ (this.appear=="boss"?'THE BOSS':'A COWORKER') + '!');
};
this.see = function() {
let pd = this.position.diff(GAME.player.pos);
let pdl = pd.length();
let d = this.deltav.dot(pd);
this.overhead.text = "";
let f = 1;
if ( this.appear == "boss" ) {
f = 1.5;
}
if ( d > pdl ) {
if ( GAME.player.isHiding ) {
if ( pdl < rtax(0.05 * f) ) {
if ( this.target == "P" ) {
// was just(!) following the player
// and is close now
this.detectedPlayer();
} else {
// has no clue the player is there,
// but is close
this.overhead.text = "??";
}
} else {
// to far away to see hidden player
this.overhead.text = "";
}
return;
}
if ( pdl < rtax(0.05 * f) ) {
// player went up to coworker
this.detectedPlayer();
} else if ( pdl < rtax(0.1 * f) ) {
let diff = this.position.diff(GAME.player.pos);
let unitc = diff.unit();
let diffl = diff.length();
for ( var i = 20; i < diffl; i+=20 ) {
let p = {pos: this.position.add(unitc.clone().mult(i))};
for ( var j = 0; j < GAME.gameobjects.length; j++ ) {
if ( person_is_colliding_bb(p, GAME.gameobjects[j]) ) {
return;
}
}
}
// coworker saw the player
this.overhead.text = "!";
if (this.target != "P") {
DATA.play_sound("notice");
}
// follow the player
this.targetPoint = GAME.player.pos.clone();
this.target = "P";
this.deltav = POINTS.new(
diff.x / diff.length() * deltaT * walkspeed,
diff.y / diff.length() * deltaT * walkspeed
);
} else if ( pdl < rtax(0.15 * f) ) {
// hears the player
this.overhead.text = "?";
} else {
// no clue
this.overhead.text = "";
}
}
};
this.switchSprite = function() {
let unit = this.deltav.unit();
let xp = unit.x;
let yp = unit.y;
GAME.getCurrentStage().removeChild(this.sprite);
if ( Math.abs(yp) < 0.4 ) {
// use left or right facing sprite
if ( xp > 0 ) {
this.sprite = this.sprites[1][Math.max(Math.floor(this.waiting/waittime*2),0)];
} else {
this.sprite = this.sprites[3][Math.max(Math.floor(this.waiting/waittime*2),0)];
}
} else {
// use up or down facing sprite
if ( yp > 0 ) {
this.sprite = this.sprites[2][Math.max(Math.floor(this.waiting/waittime*2),0)];
} else {
this.sprite = this.sprites[0][Math.max(Math.floor(this.waiting/waittime*2),0)];
}
}
GAME.pushGameObj(this.sprite);
};
}
return {
coworkers: coworkers,
num_coworkers: 6,
second_between: 3,
generate_coworkers: function(wt) {
let current_game_version = GAME.gameVersion;
addCoworker = function(){
if ( GAME.gameVersion == current_game_version ) {
let nc = Coworker.new(13);
GAME.getCurrentStage().addChild(nc.sprite);
Coworker.coworkers.push(nc);
}
};
for ( var i = 0; i < this.num_coworkers; i++ ) {
setTimeout(addCoworker,i*this.second_between*1000);
}
},
new: function(spawn) {
return new Walker("coworker", spawn);
},
newBoss: function(spawn) {
return new Walker("boss", spawn);
}
};
})();