-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefactoring a code.txt
459 lines (396 loc) · 32.1 KB
/
Refactoring a code.txt
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//_________initialise_processes_and_global_vars__________
//frames problems
PAUSE = false;
FPS_stable = 120; time_slow = 1; timer = 0; sec; var updates:Number = 0; var timeElapsed:Number = 0; //stable fps, time_slow as a koefficent of time capacity
tim = 0; upd = 0; tE = 0; G = .15;
total_enemyes = 0; total_objects = 0; total_effects = 0; total_bullets = 0; total_spreads = 0;
//global enterframe
onEnterFrame = function (){
//__________________FPS_ISSUES__________________
current_fps = _root.menu.fps.fps; //get an fps from fps counter
sec = FPS_stable/current_fps; timer+=sec*time_slow*(!PAUSE); //editing sec and adding its to timer
timeElapsed = sec*time_slow*(!PAUSE); //how much time was spend from previous sec
updates = 0; while (timer>1){ timer--; updates++; } //now we have enougth updates to do with each component of this world
/*special one for thos who can not be paused (console, fps menues etc)*/
tim+=sec*time_slow; tE = sec*time_slow; upd = 0; while (tim>1){ tim--; upd++; }
//if (current_fps < 20)FPS_stable = 60; else FPS_stable = 120; //slow down for low fps
//_____________________INFOTXT___________________
if (hero_bullets.info._visible)hero_bullets.info.text = 'Enemyes: '+total_enemyes+'\nObjects: '+total_objects+'\nEffects: '+total_effects+'\nBullets: '+total_bullets+'\nSpreads: '+total_spreads+'\nGround defines: '+defined; defined = 0;
//_______________SOUND_LIB_LOADING_______________
max = 0; now = 0; ld.text = ""; // ïðîõîä ïî âñåì çâóêàì
for (var i=0; i<sounds.length; i++) // åñëè çâóê íå çàáóïàí, òî èõ òåêóùèé\ìàê ðàçìåð äîáàâëÿþòñÿ â ñóììó
{ if (sounds[i].isBoop != true){max += sounds[i].getBytesTotal(); now += sounds[i].getBytesLoaded(); } // ñ÷èòàåòñÿ è âûâîäèòñÿ â îáùèé òåêñòáîêñ ïðîöåíò çàãðóçêè íåçàáóïàíûõ çâóêîâ
pc = Math.round(100*sounds[i].getBytesLoaded()/sounds[i].getBytesTotal()); ld.text += " "+pc+"%" // â ñëó÷àå, åñëè ïîêàçàòåëü çàãðóçêè NaN - çâóê íå ìîæåò áûòü çàãðóæåí
if (isNaN(pc) && !sounds[i].isBoop) {trace(sounds[i].name +' corrupted!'); temp = sounds[i].name; // ïåðåõâàòûâàåì åãî è ïîä òåì æå èìåíåì ñóåì â íåãî ïðåäâàðèòåëüíî îòýêñïîð÷åííûé áóï
sounds[i] = new Sound(); sounds[i].name = temp; sounds[i].attachSound("boop"); sounds[i].isBoop = true;} // áóï, åñëè ÷òî, ìîæíî çàìåíèòü íà òèøèíó, èëè ïðîñòî óáðàòü ñîîòâåòñòâóþùèå ñòðîêè
}
if (now == max && !all_sounds_loaded){all_sounds_loaded = true; console_trace('> Sound library loaded ('+get_sounds+'/'+await_sounds+')');}
}
//menu_problems
//cameram which foolows menu_handler
_root.attachMovie("cam","cam",_root.getNextHighestDepth()); cam._width = 600; cam._height = 400; cam._x = menu._x; cam._y = menu._y;
//menu handler
_root.attachMovie("menu_handler","menu",_root.getNextHighestDepth());
_root.menu._width = 600; _root.menu._height = 400; _root.menu._x = 300; _root.menu._y = 200; _root.menu._visible = true; _root.menu._alpha = 100;
menu.amp = menu.dir = 0; menu.kX = cam._xscale / menu._xscale; menu.kY = cam._yscale / menu._yscale;
menu.onEnterFrame = function (){
cam._xscale = this._xscale * this.kX; cam._yscale = this._yscale * this.kY;
cam._x = this._x+Math.cos(this.dir)*this.amp; cam._y = this._y+Math.sin(this.dir)*this.amp; if (this.amp>0){this.amp-=.25; this.dir+=Math.PI/3*2;}}
//menu _console
_root.menu.attachMovie("console","console",_root.menu.getNextHighestDepth()); _root.menu.console._y = -200; menu.console.need_trace = new Array("> Message stack test;","> Message stack is ready;");
function console_trace (messag:String){menu.console.need_trace.push( messag ); }
more_info_show = false;
//menu fps (in itself)
_root.menu.attachMovie("fps_counter_wide","fps_wide",_root.menu.getNextHighestDepth()); _root.menu.fps_wide._x = -200; _root.menu.fps_wide._y = -200; _root.menu.fps_wide._visible = false;
_root.menu.attachMovie("fps_counter","fps",_root.menu.getNextHighestDepth()); _root.menu.fps._x = -300; _root.menu.fps._y = -200; _root.menu.fps.blendMode = 10;
//sound handler for menu_clicks
_root.menu.attachMovie("sound_handler","sounder",_root.menu.getNextHighestDepth()); _root.menu.sounder._x = -300; _root.menu.sounder._y = -180; _root.menu.sounder._visible = false;
//health n ammo bars
_root.menu.attachMovie("hero_GUI","hero_GUI",_root.menu.getNextHighestDepth()); _root.menu.hero_GUI._x = -300; _root.menu.hero_GUI._y = 182;
console_trace( "> Console is ready; Camera is ready; Fps counter is ready;" );
//menu._xscale /= 2; menu._yscale /=2; menu._y = mouse._y - 50; menu._x = mouse._x;
//frame solving
function animate (who:MovieClip, startFrame, finishFrame, speed:Number, direct){//animate an object according to improoved timeline
//direct -1 or 1 :: a side where do you wish to animate
if (who == null) return; if (who.anim>300)who.anim%=300;
if (isNaN(who.anim)) {console_trace("# Warning! Object ("+who+") can not be animate cause do not have an 'anim' property!"); return; } //error
if (isNaN(direct))direct = 1; //default direction of animating is from left to right
if (who.anim%speed == 0 || isNaN(speed)){
fr = who._currentframe; fr += direct; if (fr<startFrame)fr = finishFrame; if (fr>finishFrame)fr = startFrame; //increasing or descresing a frame counter
who.gotoAndStop(fr); //applying changes
}}
//finding a frame in movieclip by name
function getByName (who:MovieClip, nam:String):Number{
temp = who._currentframe; who.gotoAndStop(nam); res = who._currentframe; who.gotoAndStop(temp);
return res;
}
console_trace( "> Animation function block is ready;");
//levle design problems
//sounder
_root.attachMovie("sounder_footsteps","fts",_root.getNextHighestDepth());_root.fts._visible = false;
function footstep_sound (typ:MovieClip){ pochva = 0; _root.sound_start(ground_types[pochva]+""+(1+random(ground_nums[pochva]))); }
function other_sound (typ:String){ _root.fts.gotoAndPlay(typ); }
//character human movement problems
function set_body(body:MovieClip, legs:MovieClip){
body.legs = legs; body.xs = body._xscale; body.stat = "idle"; body.anim = 0; body.stop();
}
function being_body(body:MovieClip){
body._y = body.legs._y + body.legs.taz._y; body._rotation = body.legs.taz._rotation * (body.legs.taz._xscale / Math.abs(body.legs.taz._xscale));
body._xscale = body._parent.sp_x / Math.abs(body._parent.sp_x ) * body.xs; //_xscale as sp_x of parent
if (!isNaN(body._parent.sp_x / Math.abs(body._parent.sp_x )))body._parent.wasScale = body._parent.sp_x / Math.abs(body._parent.sp_x );
body.anim++;
animate(body,getByName(body,body.stat+"_start"),getByName(body,body.stat+"_stop"),body.anim_spd);//animating
}
function autoAnimate( who:MovieClip ){
if (who.stat == undefined) {console_trace("# ("+who + ") can not be auto-animated. Cause it do not have 'stat'"); return;} //have no state
if (who.anim == undefined) {console_trace("# ("+who + ") can not be auto-animated. Cause it do not have 'anim'"); return;} //have no anim
who.anim++; if (who.anim_spd == undefined) who.anim_spd = 2; //if have no speed
animate(who,getByName(who,who.stat+"_start"),getByName(who,who.stat+"_stop"),who.anim_spd);//animating
}
//character control problems
function set_controlable (who:MovieClip,keys:Array, maxXspeed:Number, jumpHeigth:Number){
who.keyBinds = keys; // left | right | up | down | reload (R) | interract (E) | drop (G) | swap (Q)
who.keypresses = new Array(); for (var i=0; i<keys.length; i++) keypresses.push(0);
who.sp_x_max = maxXspeed; who.jump_heigth = jumpHeigth;
who.controlable = true; who.wantReload = false; who.wantDrop = false;
}
//moveble character
function set_moveble (who:MovieClip, acseleration:Number, desacseleration_k:Number, jumpBack:Number, mass:Number){
who.sp_x = 0; who.sp_x0 = 0; who.sp_y0 = 0; who.acs = acseleration; who.tormoz = desacseleration_k;
who.sp_y = 0; who.ground = false;
if (mass == undefined) who.mass = 1; else who.mass = mass; if (jumpBack == undefined) who.jumpBack = 0; else who.jumpBack = jumpBack;
}
//being a moveble thing (äàæå òóìáî÷êà è çîëîäèëüíèê äîëæíû ýòî èñïîëüçûâàòü)
function being_moveble (who:MovieClip, ignore_ground:Boolean){
//default values
if (ignore_ground == undefined) ignore_ground = false;
for (var tick = 0; tick<_root.updates; tick++){
if (who.ground){ who.ground = defineGround(who); if (Math.abs(who.sp_x0) > 0.1) who.sp_x0 /= Math.pow(who.tormoz,.25); else who.sp_x0 = 0; } //F òðåíèÿ
if (!who.ground){ who.sp_y += G*who.mass; if (!ignore_ground){if (defineGround(who)){if (who.jumpBack == 0){ who.ground = true; who.sp_y = 0; if (who.sp_y0>0) who.sp_y0 = 0; }
/*otskok ili finish*/ else{ if (Math.abs(who.sp_y)>=1){ if (Math.abs(who.sp_y)>=4)footstep_sound(); who.sp_y0 = -who.jumpBack*who.sp_y; who.sp_x0 /= who.tormoz; who.sp_y = 0;} else {who.ground = true; who.sp_y = 0; who.sp_y0 = 0;}}}}}
//landing
//movement applyes
who._x += who.sp_x + who.sp_x0;
who._y += who.sp_y + who.sp_y0;
}}
ground_blocks = new Array( ); defined = 0;
//define is object on ground oe not
function defineGround (who:MovieClip):Boolean{
//maximum
if (who.sp_x + who.sp_x0 == 0 && who.sp_y+who.sp_y0 == 0){return who.ground;}
//defined ++;
for (var i=0; i<ground_blocks.length; i++){
if ((++defined || true) and who.sp_y + who.sp_y0 >= 0 && who.hitTest(ground_blocks[i].nad) &&
(ground_blocks[i].gr.hitTest(who._x, who._y + 1 + who.sp_y + who.sp_y0, true) || ground_blocks[i].gr.hitTest(who._x, who._y + 1, true)))
{ who._y = ground_blocks[i]._y; return true; } }
if (who._y + who.sp_y + who.sp_y0 >= 360){ who._y = 360; return true; }
return false;
}
//on every frame (non update depending)
function being_controlable (who:MovieClip){
for (var tick = 0; tick<_root.updates; tick++){
//rejoice with a keys
for (var i=0; i<who.keyBinds.length; i++) if (Key.isDown(who.keyBinds[i])) who.keypresses[i]++; else who.keypresses[i]=0;
//move accept from keys
who.wantDrop = (who.keypresses[6] >= 1 && who.keypresses[6] <= 60); //want to drop weapon
who.wantReload = (who.keypresses[4] >= 1 && who.keypresses[4] <= 60); //want to reload a weapon
//if left | right pressed increase speed
if (who.keypresses[1]>0 && who.keypresses[0]==0) who.sp_x += who.acs * (who.sp_x < who.sp_x_max);
if (who.keypresses[0]>0 && who.keypresses[1]==0) who.sp_x -= who.acs * (who.sp_x > -who.sp_x_max);
//descresing speed when nothing is presseds
if (who.ground && !((who.keypresses[0]>0 && who.sp_x<0) || (who.keypresses[1]>0 && who.sp_x>0)))if (Math.abs(who.sp_x)>.1)who.sp_x /= who.tormoz; else who.sp_x = 0;
//watching a jumping
if (who.keypresses[2] == 1 && who.ground){ who.sp_y = -who.jump_heigth; who._y -= 5; who.ground = false; character_sound_start(who,'jump');}
//not working if is dead
if (who.dead)who.keypresses = 0;
}
}
//hitable shits
all_hitable = new Array();
function set_health ( who:MovieClip, healthMax:Number, regen:Number ){
who.hpwas = who.hp = who.hpmax = healthMax; who.regenedHealth = 0; who.regenSpd = regen;
who.dead = false; who.hited = 0; who.stop(); //times hited
}
function set_hitable (who:MovieClip, team:Number){ // 0 - nothing // 1 - friend // 2+ - enemy
if (team == undefined)who.team = 0; else {who.team = team; if (team > 1)total_enemyes++;}
who.hitboxes = new Array();
all_hitable.push(who);
}
function being_hitable( who:MovieClip ){
//check color of hitboxes
if (!(who.hitboxes.length > 0))console_trace("# "+who+" do not have any hitboxes!");
for (var h = 0; h<who.hitboxes.length; h++)
{who.hitboxes[h].colors.gotoAndStop(2+4*(who.hp<=0)+who.team);}
if (who.hited>0)who.hited = 0;
for (var tick = 0; tick < updates; tick++){
//is barely alife problems
if (who.hp <=0 || who.dead){ if (!who.dead || who.hp>0){character_sound_start(who,'dead'); who.gotoAndStop('dead'); if( who.team > 1)total_enemyes--;} who.dead = true; who.hp = 0; return;}
//regeneration problems
if (who.hp < who.hpmax){ who.regenedHealth += who.regenSpd; while (who.regenedHealth>1){ who.regenedHealth--; who.hp = Math.min(who.hpmax, who.hp+1); } }
//accepting a hit
if (who.hp <= who.hpwas-1){who.hited++; character_sound_start(who,'hited');} who.hpwas = who.hp;
}
}
console_trace( "> Object's interfaces block is ready;");
//HEALTH
health_bars = 0; lastHB = null;
function set_health_bar (where:MovieClip, x0, y0){
health_bars++; where.attachMovie("health_bar","heba_"+health_bars, where.getNextHighestDepth()); hb = where["heba_"+health_bars];
hb._x = x0; hb._y = y0; lastHB = hb; trace(hb);
}
//AMMO_________________________________________________
ammo_adders = new Array(20, 8, 10, 5);
function set_ammo_holder (who:MovieClip, ammo:Array){ // this, new Array(50, 0, 0, Number.POSTIVE_INFINITY);
who.Ammo = ammo; who.Ammo_max = new Array(200, 60, 100, 40); who.ammo_holder = true;
}
// typ == { 1, 2, 3, 4 } { 'bullets', 'shells', 'energys', 'bombs' } size = {0 - minimal , 1 - medium, 2 - large}
function spawn_pickup (x0:Number, y0:Number, typ, size, fadetime){
if (typ == 0)typ = -1; if (size == undefined)size = 0; if (fadetime == undefined)fadetime = 120 * 10;what_to_export = 'none';
if (typ >= 1 && typ <= 4){ /*ýòî ïàòðîíû*/ if (size == 2) what_to_export = "pickup_large_ammo"; if (size == 1) for (var n=0;n<3; n++)spawn_pickup (x0, y0, typ, 0, fadetime); if (size == 0) what_to_export = "pickup_ammo"; }
if (typ == -1){ /*çäîðîâüå*/ what_to_export = 'pickup_healing';}
_root.export_object (_root, what_to_export, fadetime, x0 , y0, (random(200/100)-.5)*3,-random(300)/100-2, .2,1.8,1, false,.3);
_root.last_exported.isPickUp = true; _root.last_exported.pickUpType = typ; _root.last_exported.siz = size;
}
console_trace( "> Ammo interfaces block is ready;" );
//flying things
//_root.export_object (where, path, timer, x0, y0, sp_x0 (0), sp_y0 (0), acs (0), tormoz (1.05), mass(1), ignore_ground (false), jumpBack (0));
obs_fly = -1; last_exported = null;
function export_object (where, path:String, time:Number, x0, y0, sp_x0, sp_y0, acs, tormoz, mass , ignore_ground , jumpBack ){
//default values
if (where == undefined){ console_trace('# No place '+where); return; } if (path == undefined){ console_trace('# No path: '+where); return; }
if (time == undefined) time = FPS_stable * 10; /*if time == -1 then this object will not be remooved*/
if (x0 == undefined || y0 == undefined){ console_trace('# Place is incorrect'); return;}
if (sp_x0 == undefined) sp_x0 = 0; if (sp_y0 == undefined) sp_y0 = 0; if (acs == undefined) acs = 0; if (tormoz == undefined) tormoz = 1.05;
if (mass == undefined) mass = 1; if (jumpBack == undefined) jumpBack = 0; if (ignore_ground == undefined) ignore_ground = false;
//______________________________
//exporting from library
obs_fly ++; total_objects++; where.attachMovie(path, "ob_fl_"+obs_fly, where.getNextHighestDepth()); s = where["ob_fl_"+obs_fly];
//set vairables
set_moveble(s,acs, tormoz, jumpBack, mass); s._x = x0; s._y = y0; s.sp_x0 = sp_x0; s.sp_y = sp_y0; s.stop(); s.ignore_ground = ignore_ground; s.timer = time;
//move as you want
s.onEnterFrame = function (){ _root.being_moveble(this, ignore_ground); if (timer>0){this.timer -= _root.timeElapsed; if (this.timer<=0 || (! this.hitTest(_root.cam) && !this.isPickUp))this.removeMovieClip();} }
s.onUnload = function (){ _root.total_objects--; }
//add to linkage
last_exported = s;
}
//effect exporting
ef_sc = -1; lastEffect = null;
function export_effect (where, path:String, x0, y0, angle){
ef_sc ++; total_effects++; where.attachMovie(path+"_effect", "ef_sc_"+ef_sc, where.getNextHighestDepth()); e = where["ef_sc_"+ef_sc];
e._x = x0; e._y= y0; e._rotation =( angle )/Math.PI*180; e.onUnload = function (){ _root.total_effects --; }
lastEffect = e;
}
console_trace( "> Object's export block is ready;");
//____________SHOOOTING___THINGS_________________
//DEFAULT BULLET SPEEDS
bullet_types_array = new Array("pistol_bullet","enemy_rifle_bullet");
bullet_speed_array = new Array( 7, new Array(1,10));
bullet_spread_array= new Array( 6, 1);
function rnd_spd ( min, max ){ return min + random(Math.round(100*(max-min)))/100 ; }
_root.attachMovie("bullets_layer","hero_bullets",_root.getNextHighestDepth()); hero_bullets.swapDepths( menu ); hero_bullets.nam.text = 'hero bullets layer';
_root.attachMovie("bullets_layer","enemy_bullets",_root.getNextHighestDepth()); enemy_bullets.swapDepths( menu ); enemy_bullets.nam._y += 30; enemy_bullets.nam.text = 'enemy bullets layer';
//shoot a bullet
hero_bul = -1; last_bullet = null;
function spawn_a_bullet( where:MovieClip, bullet_path:String, x0, y0, spd, ang, spread, spread_stats){
//FPS ïðåäîõðàíèòåëü
if (menu.fps.fps < 10)return;
//calculate default speed
ok = false; if (spd == 'default' || spread == 'default'){for (var i=0; i<bullet_types_array.length; i++) if ((bullet_types_array[i]+"").indexOf(bullet_path+"")>=0)
{ if (spd == 'default')spd = bullet_speed_array[i]; if (spread == 'default')spread = bullet_spread_array[i]; ok = true; break; }
if (!ok){console_trace("# No default speed for bullet '"+bullet_path+"'"); return;}}
if (spd[1]!=undefined){ if (spd.length == 2)spd = rnd_spd (spd[0], spd[1]); }
//spawn_a_bulelt
hero_bul++; total_bullets ++;
where.attachMovie(bullet_path,"hero_bul_"+hero_bul, where.getNextHighestDepth()); b = where["hero_bul_"+hero_bul];
b._x = x0; b._y = y0; b.spd = spd; b.ang = ang; b.damage = 1; b.damage_done = false;
b.onUnload = function (){ _root.total_bullets--; }
//spawn spread
for (var i= 0; i<spread; i++){spread_stat = spread_stats[random(spread_stats.length)]; spawn_a_spread (hero_bullets, x0, y0, ang, spd, spread_stat, .001*(random(6)+5)+1);}
//links
last_bullet = b;
}
sprd = -1;
function spawn_a_spread (where:MovieClip, x0, y0, ang, spd_bul, stat, degrad){// return;
sprd++; total_spreads ++;
where.attachMovie("bullet_fly_out","bl_sprd"+sprd, where.getNextHighestDepth()); s = where["bl_sprd"+sprd];
s._x = x0; s._y = y0; s.ang = ang; s.stat = stat; s.timer = timer; s.spd = Math.max(1,Math.min(3,spd_bul/3))*(random(80)/100+.3)*(1+.2*random(6)*(random(50)==0));
s.degrade_speed = degrad; s.onUnload = function (){ _root.total_spreads--; }
}
//hitboxes problems
function set_hitbox (hitbox:MovieClip, host:MovieClip){ //íàçíà÷àåò õèòáîñ äëÿ êîíêðåòíîãî õîñòà
if (host == undefined){ console_trace("# unknown hitbox host!"); return; } //åñëè õîñò íå îïðåäåëåí, òî âîçâðàò ñ îøèáêîé
if (host.hitboxes.length <= 0)host.hitboxes = new Array(); //åñëè íåò òàêîãî ìàññèâà, òî ñîçäàòü åãî
host.hitboxes.push(hitbox); hitbox.hostID = host; //äîáàâëåíèå â ìàññèâ õèòáîêñîâ, åñëè âñå óñïåøíî
hitbox.onUnload = function (){num = -1; for (var i=0; i<this.hostID.hitboxes.length; i++)if (this.hostID.hitboxes[i] == this){num = i; break;} if (num==-1)return; this.hostID.hitboxes.splice(num,1);} //ïðè îòãðóçêå óáðàòü õèòáîêñ, ÷òîáû íå çàñîðÿòü ìàññèâ
}
//bullet collision
function test_collision (bullet:MovieClip, side:Number):Boolean{
if (!bullet.damage_done)
for (var tt = 0; tt<all_hitable.length; tt++){ target = all_hitable[tt];
if (((target.team != 1 && bullet._parent == hero_bullets) || (target.team != 2 && bullet._parent == enemy_bullets)) //team check
&& target.hitTest(bullet._x, bullet._y, true)) for (var i=0; i<target.hitboxes.length; i++)if ( bullet.hitTest(target.hitboxes[i]) ) //collision operator
{ bullet.gotoAndStop('dead'); bullet.damage_done = true; target.hitboxes[i].colors.hurted._alpha = 100; target.hp-= bullet.damage; target.sp_x0 += .1*bullet.spd*Math.cos(bullet.ang); return true; } //collision action
} return false; }
//WARNING - NOW WORKING ONLY FILTERING UNITS WHO A AMMO HOLDERS
function hittest_collision (thing:MovieClip):MovieClip{
for (var tt = 0; tt<all_hitable.length; tt++){ target = all_hitable[tt];
if (target.hitTest(thing) && target.ammo_holder)for (var i=0; i<target.hitboxes.length; i++)if ( thing.hitTest(target.hitboxes[i]) ){ return target; }
} return null;
}
console_trace( "> Bullet physics block is ready;")
//__________________
shadow_level = 75; //íà ñêîëüêî çàòåìíÿòü ïóõó, êîòîðàÿ âèñèò çà ñïèíîé
// ñóùåñòâî, êîòîðîå ìîæåò äåðæàòü ïóøêó (ïðåäïîëîæèòåëüíî ëþáóþ)
function set_a_gun_holder (gunner:MovieClip){ gunner.weapons = new Array(); gunner.weaponActive = -1; gunner.pQ = 0; gunner.wA = -1; gunner.wL = 0; }
function being_a_gun_holder (gunner:MovieClip){
if (gunner.weaponActive != gunner.wA || gunner.weapons.length != gunner.wL){ gunner.wA = gunner.weaponActive; gunner.wL = gunner.weapons.length; //çàïðîñ íà ïåðåðàñïðåäåëåíèå ãëóáèí òîëüêî ïðè ñìåíå àêòèâíîãî èëè äîáàâëåíèè íîâîãî
for (var i=0; i<gunner.weapons.length; i++){ if (i == gunner.weaponActive){gunner.weapons[i].swapDepths( gunner.getDepth()+1 ); gunner.weapons[i].clr.setTransform({rb:0, gb:0, bb:0});} else {gunner.weapons[i].swapDepths( gunner.getDepth()-1 - i ); gunner.weapons[i].clr.setTransform({ rb:-shadow_level, gb:-shadow_level, bb:-shadow_level })}}} //êàæäîå îðóæèå íà ñâîåì ñëîå ãëóáèíû (îáåñïå÷åíèå îðóæèÿ çà ñïèíîé)
if ((gunner.keypresses[7]>0 && gunner.keypresses[7]<60) && gunner.weapons.length>=2)
{ gunner.keypresses[7] = 360; gunner.weapons[ gunner.weaponActive ]._rotation = random(40)-20; //ïðåäûäóùåå îðóæèå íåàêòèâíîå ñëó÷àéíî ïîâåðíóòî è çà ñïèíîé
gunner.weaponActive = (gunner.weaponActive+1)%gunner.weapons.length; gunner.weapons[ gunner.weaponActive ].missValue = (random(40)/100+.2)*Math.PI*(random(2)*2-1); //íåáîëüøàÿ òðÿñêà ïðè ñìåíå
sound_start(gunner.weapons[ gunner.weaponActive ].sound_equip);} /*hold.text = gunner.weaponActive +' '+ gunner.weapons;*/} //ìåíÿåì àêòèâíîå îðóæèå, ñòàâèì åãî àêòèâíûì, ñóåì â ðóêè, ïðîèãðûâàåì çâóê
// äåëàåò ïóøêó ïóøêîé
// ammo_type == {0 - bullets, 1 - shells, 2 - energys, 3 - bombs, -1 - none}
// function set_a_gun(GUN, ammo_type (0), bullet_type (pistol_bullet), bullet_spread(0), realoadPartly(18), ammo(6), reloadFull(120), automatic(false), host_dist(20), ammo_per_shot(1), bullet_per_shot(1), otadat(10))
function set_a_gun (who:MovieClip, ammo_type, bullet_type:String, bullet_spread:Number, realoadPartly:Number, ammo:Number, reloadFull:Number, automatic:Boolean,
host_dist:Number, ammo_per_shot:Number, bullet_per_shot:Number, otdat:Number, effect_path:String, spread_stats:Array,
bullet_speed){
if (who == null) return; who.stop();
if (ammo_type == undefined)who.ammo_type = -1; else who.ammo_type = ammo_type; // òèï ïîòðåáëÿåìûõ ïàòðîíîâ (ïî äåôîëòó èõ íå ïîòðåáëÿåò)
if (bullet_type == undefined)who.bullet_type = "pistol_bullet"; else who.bullet_type = bullet_type; // òèï ïóëü
if (bullet_spread == undefined)who.bullet_spread = 0; else who.bullet_spread = bullet_spread; // ðàçáðîñ óãîë
if (realoadPartly == undefined)who.realoadPartly = 18; else who.realoadPartly = realoadPartly; // ïåðåðûâ ìåæäó ñîñåäíèìè âûñòðåëàìè
if (reloadFull == undefined)who.reloadFull = 120; else who.reloadFull = reloadFull; // ïî èñòå÷åíèþ îáîéìû ïåðåçàðÿäêà
if (ammo == undefined)who.ammo = 6; else who.ammo = ammo; // ìàãàçèí
if (automatic == undefined)who.automatic = false; else who.automatic = automatic; // àâòîìàòè÷åñêàÿ ëè ñòðåëüáà?
if (host_dist == undefined)who.host_dist = 20; else who.host_dist = host_dist; // ðàññòîÿíèå, íà êîòîðîì âëàäåëåö äåðæèò ïóøêó
if (otdat == undefined)who.otdat = 10; else who.otdat = otdat; // îòäà÷à ïðè âûëåòå 1(!!!!!) ïóëè
if (ammo_per_shot == undefined)who.ammo_per_shot = 1; else who.ammo_per_shot = ammo_per_shot; // çàòðà÷åíûå íà 1 âûñòðåë ïàòðîíû
if (bullet_per_shot == undefined)who.bullet_per_shot = 1; else who.bullet_per_shot = bullet_per_shot; // êîëè÷åñòâî ïóëü, âûëåòàþùèõ ïðè âûñòðåëå
if (effect_path == undefined)who.effect_path = 'pistol_shoot'; else who.effect_path = effect_path; // ýôôåêò âûñòðåëà (ó ñòâîëà)
if (spread_stats == undefined)who.spread_stats = new Array(1,2,3); else who.spread_stats = spread_stats; // ìàññèâ âîçìîæíûõ ñîñòîÿíèé âûëåòàþùèõ ñïðåàäèêîâ
if (bullet_speed == undefined)who.bullet_speed = 'default'; else who.bullet_speed = bullet_speed; // default - ïî äåôîëòó, ìîæåò áûòü ìàññèâîì, ìîæåò áûòü 1ì ÷èñëîì
who.clr = new Color(who); who.missValue = 0; who.host = null; who.watchR = 0; who.ys = who._yscale; who.reload_timer = 0;
who.current_ammo = who.ammo; who.host = null; who.watch1 = 0; who.ot_dist = 0; who.never_hosted = true;
}
//íàçíà÷àåò ïóøêå âëàäåëüöà
function set_a_gun_host (gun:MovieClip, host:MovieClip){
gun.host = host; sound_start ( gun.sound_equip+"" );
host.weapons.push(gun); host.weaponActive = host.weapons.length-1; //select new weapon
if (gun.never_hosted){ gun.never_hosted = false; if (gun.ammo_type >= 0)gun.host.Ammo[gun.ammo_type]+= gun.current_ammo; }
}
//being a GUN
function being_a_gun (gun:MovieClip){
//õîçÿèí âïðèíöèïå íå îïðåäåëåí (null, åñëè åãî òàêè íåò)
if (gun.host!= null && gun.host == undefined){ console_trace('# '+gun+' have no host!');return;} //no host check
//åñëè íåò õîçÿèíà, òî ýòî ïðîñòî âàëÿþùèéñÿ êóñîê æåëåçà
if (gun.host == null){
if (gun.mass == undefined){ set_moveble(gun,0, 1.2, 0.1, 1); gun.ground = false; }else{ being_moveble(gun); } //falling and mooving
for (var man = 0; man < all_hitable.length; man++)
if (all_hitable[man].controlable == true && all_hitable[man].hitTest(gun) && (all_hitable[man].keypresses[5] > 1)){ if ( Math.abs( gun._x - all_hitable[man]._x )+Math.abs(gun._y - all_hitable[man]._y)<40 ){set_a_gun_host (gun, all_hitable[man]); return;}}
return; }
//åñëè åñòü õîçÿèí, íî ïóøêà ëåæèò çà ïàçóõîé (íå àêòèâíà), òî ïðîñòî ïåðåìåùàòü å¸ çà èãðîêîì
if (gun.host != null && gun!=gun.host.weapons[gun.host.weaponActive]) //ÿâëÿåòñÿ íààêòèâíîé (ïðîâåðêà òàêàÿ ÷òîáû èçáåæàòü ïåðåìåííîé àêòèâ)
{if (Math.abs(gun._rotation) >20)gun._rotation = random(40)-20; gun._x = gun.host._x - gun._width/5; gun._y = gun.host._y + gun.host.gunYoffset; return; }
//åñëè õîçÿèí åñòü, íî îí ýòó ïóøêó ðåøèë âûêèíóòü ê ÷åðòÿì
if (gun.host.wantDrop){
gun.host.keypresses[6] = 360; gun.host.wantDrop = false; gun.host.weapons.splice(gun.host.weaponActive,1); gun.host.weaponActive--; if (gun.host.weapons.length>0 && gun.host.weaponActive < 0){gun.host.weaponActive = 0;}//âû÷åðêíóòü èç ñïèñêà îðóæèÿ âëàäåëüöà è ñäåëàòü ñëåäóþùåå îðóæèå àêòèâíûì
if (gun.mass == undefined){ set_moveble(gun,0, 1.2, 0.1, 1); } //åñëè îíà íå áûëà îáúåêòîì äî ýòîãî
gun.ground = false; if (gun.host.keypresses[3]==0 || gun.host.dead){ if (gun.host.dead) ang = -Math.PI*( .25+random(50)/100 );pow = (5+random(20)/10)/gun.mass; gun.sp_y = pow*Math.sin(ang); gun.sp_x0 = .4*pow*Math.cos(ang);} gun.host = null; gun._rotation += random(121)-60; return; } //ïóøêà âûêèíóòà. ðåòóðí.
//êàæäûé êàäð ñìîòðåòü òóäà, êóäà öåëèòñÿ õîçÿèí. Âíå âðåìåíè.
if (gun.host.followX == undefined || gun.host.followY == undefined){console_trace("# "+gun+"'s has no 'follow' variable!"); return;} //ó õîçÿèíà íåò ïàðàìåòðà öåëè. íå÷åãî òóò ëîâèòü
if ( gun.host.dead )return;
if (gun.host.gunYoffset == undefined)gun.host.gunYoffset = 0;
if(timeElapsed>0)ang = Math.atan2( gun.host._y + gun.host.gunYoffset - gun.host.followY, gun.host._x - gun.host.followX ) + Math.PI + gun.missValue; //angle_calculate
gun._rotation = ang/Math.PI*180; gun._yscale = (2*(gun._rotation > -90 && gun._rotation < 90)-1)*gun.ys;
if (Math.abs(gun.ot_dist)<.1)gun.ot_dist = 0; for (var i=0;i<_root.updates;i++){gun.ot_dist/=1.1; gun.missValue /= 1.1; }
gun._x = gun.host._x + Math.cos(ang)* (gun.host_dist - gun.ot_dist); gun._y = gun.host._y + gun.host.gunYoffset + Math.sin(ang) * (gun.host_dist - gun.ot_dist);
//ïðèñòðåëêè è âñå â òàêîì äóõå
shot_this_frame = false;
for (var tick = 0; tick<_root.updates; tick++){
if (Key.isDown(1))gun.watch1++; else gun.watch1 = 0; //when mouse is clicked || R is pressed
gun.reload_timer -= (gun.reload_timer>0)*1;
//no ammo case
if (gun.reload_timer == 0 && gun.watch1 == 1 && !(gun.current_ammo >= gun.ammo_per_shot && gun.ammo_type>=0 && gun.host.Ammo[gun.ammo_type] >= gun.ammo_per_shot))sound_start('bullets/no_ammo');
//reload
if (gun.reload_timer<=0 && ((gun.current_ammo > 0 && gun.watch1 != 1 && gun.host.wantReload) || (gun.current_ammo == 0 && gun.watch1 + gun.host.wantReload*1==1)))
{ gun.reload_timer += gun.reloadFull; gun.ost = gun.current_ammo; gun.current_ammo = gun.ammo; gun.gotoAndStop('hand_reload'); gun.reload_base.gotoAndStop(1); }
//shoot
if (!shot_this_frame && gun.reload_timer<=0 && ((!gun.automatic && gun.watch1 == 1) || (gun.automatic && gun.watch1 > 0))){ /*SHOT*/
//attach a bullet
shot_this_frame = true; // â ýòîò êàäð óæå ñòðåëÿëè
if (gun.current_ammo >= gun.ammo_per_shot && gun.ammo_type>=0 && gun.host.Ammo[gun.ammo_type] >= gun.ammo_per_shot){ gun.current_ammo -= gun.ammo_per_shot; gun.host.Ammo[gun.ammo_type] -= gun.ammo_per_shot;
if (gun.current_ammo > 0) {gun.reload_timer += gun.realoadPartly; gun.gotoAndStop('fire');} // îáîéìà åçå íå êîí÷èëàñü \ ñïàâí çâóê âûñòðåëà
else {gun.reload_timer += gun.realoadPartly+gun.reloadFull; gun.current_ammo = gun.ammo; gun.gotoAndStop('reload'); gun.reload_base.gotoAndStop(1); gun.ost = 0;} // îáîéìà êîí÷èëàñü \ ñïàâí çâóê ïåðåçàðÿäêè
dulo_x = gun._x + Math.cos(gun._rotation/180*Math.PI) * (gun.dulo._x) + Math.cos(gun._rotation/180*Math.PI+Math.PI/2) * gun.dulo._y *gun._yscale/gun.ys; // ïðîñ÷åò òî÷êè âûëåòà ïóëè èç ñòâîëà
dulo_y = gun._y + Math.sin(gun._rotation/180*Math.PI) * (gun.dulo._x) + Math.sin(gun._rotation/180*Math.PI+Math.PI/2) * gun.dulo._y*gun._yscale/gun.ys;
export_effect (hero_bullets, gun.effect_path+"", dulo_x, dulo_y, gun._rotation/180*Math.PI);
for (var shot=0; shot<gun.bullet_per_shot; shot++){ // äëÿ êàæäîé ïóëè ñïàâíèòü åå è óâåëè÷èâàòü îòäà÷ó
gun.ot_dist += gun.otdat;
gun.host.sp_x0 += - 0.02*gun.otdat * Math.cos(gun._rotation / 180 * Math.PI);
where = enemy_bullets; if (gun.host.team == 1)where = hero_bullets;
spawn_a_bullet (where, gun.bullet_type, dulo_x, dulo_y, gun.bullet_speed, gun._rotation/180*Math.PI + random(Math.round(gun.bullet_spread*1000))/1000 - gun.bullet_spread/2, 'default', gun.spread_stats);}}// bullet_spawn
}
}
tst.text = gun.current_ammo + '/' + gun.ammo +'/'+ gun.reload_timer;
}
//gun reloading help functions
function set_reloader (gun:MovieClip, maxFrame:Number){ gun.anim = 0; gun.stop(); gun.spd = (Math.round(gun._parent.reload_timer/maxFrame+.4)); }
function being_reloader (gun:MovieClip){++gun.anim; if (gun._parent.host!=null && gun._parent.getDepth() > gun._parent.host.getDepth())_root.animate(gun,1,100,gun.spd);}
console_trace( "> Gun logick block is ready;")
for (var i=3; i<5; i++){
_root.attachMovie("single_pistol", "sp"+i, _root.getNextHighestDepth());
set_a_gun (_root["sp"+i], 0); _root["sp"+i]._x = random(100); //_root["sp"+i].current_ammo = 0;
_root["sp"+i].onEnterFrame = function (){ _root.being_a_gun(this); }}
_root.attachMovie('simple_shotgun',"sp1",_root.getNextHighestDepth());
set_a_gun(sp1, 1, "pistol_bullet", Math.PI/6,5, 1, 95, false, 10, 1, 5, 5, "pistol_shoot", new Array(1,2,3), new Array(4,10));
sp1._x = 200; sp1.current_ammo = 0;
sp1.onEnterFrame = function (){ _root.being_a_gun(this); }
_root.attachMovie('simple_sawed_off',"sp2",_root.getNextHighestDepth());
set_a_gun(sp2, 1, "pistol_bullet", Math.PI/3, 50, 2, 120, false, 18, 2, 12, 4, "pistol_shoot", new Array(1,2,3), new Array(4,10));
sp2._x = 400; sp1.current_ammo = 0;
sp2.onEnterFrame = function (){ _root.being_a_gun(this); }
/*function set_a_gun (who:MovieClip, bullet_type:String, bullet_spread:Number, realoadPartly:Number, ammo:Number, reloadFull:Number, automatic:Boolean,
host_dist:Number, ammo_per_shot:Number, bullet_per_shot:Number, otdat:Number, effect_path:String, spread_stats:Array,
bullet_speed){*/
//set_a_gun(sp0, "enemy_rifle_bullet", Math.PI/30, 3, 60, 180, true, 40, 2, 3, 1, 'enemy_rifle', new Array(4,5)); set_a_gun_host(sp0, mouse);