public void InsertMag ( GameObject mag ){ if(gun_type != GunType.AUTOMATIC){ return; } if(magazine_instance_in_gun){ return; } magazine_instance_in_gun = mag; mag.transform.parent = transform; mag_stage = MagStage.INSERTING; PlaySoundFromGroup(sound_mag_insertion, kGunMechanicVolume); mag_seated = 0.0f; }
void Update (){ if(gun_type == GunType.AUTOMATIC){ if(magazine_instance_in_gun){ var mag_pos= transform.FindChild("point_mag_inserted").position; var mag_rot= transform.rotation; var mag_seated_display= mag_seated; if(disable_springs){ mag_seated_display = Mathf.Floor(mag_seated_display + 0.5f); } mag_pos += (transform.FindChild("point_mag_to_insert").position - transform.FindChild("point_mag_inserted").position) * (1.0f - mag_seated_display); magazine_instance_in_gun.transform.position = mag_pos; magazine_instance_in_gun.transform.rotation = mag_rot; } if(mag_stage == MagStage.INSERTING){ mag_seated += Time.deltaTime * 5.0f; if(mag_seated >= 1.0f){ mag_seated = 1.0f; mag_stage = MagStage.IN; if(slide_amount > 0.7f){ ChamberRoundFromMag(); } recoil_transfer_y += Random.Range(-40.0f,40.0f); recoil_transfer_x += Random.Range(50.0f,300.0f); rotation_transfer_x += Random.Range(-0.4f,0.4f); rotation_transfer_y += Random.Range(0.0f,1.0f); } } if(mag_stage == MagStage.REMOVING){ mag_seated -= Time.deltaTime * 5.0f; if(mag_seated <= 0.0f){ mag_seated = 0.0f; ready_to_remove_mag = true; mag_stage = MagStage.OUT; } } } if(has_safety){ if(safety == Safety.OFF){ safety_off = Mathf.Min(1.0f, safety_off + Time.deltaTime * 10.0f); } else if(safety == Safety.ON){ safety_off = Mathf.Max(0.0f, safety_off - Time.deltaTime * 10.0f); } } if(has_auto_mod){ if(auto_mod_stage == AutoModStage.ENABLED){ auto_mod_amount = Mathf.Min(1.0f, auto_mod_amount + Time.deltaTime * 10.0f); } else if(auto_mod_stage == AutoModStage.DISABLED){ auto_mod_amount = Mathf.Max(0.0f, auto_mod_amount - Time.deltaTime * 10.0f); } } if(thumb_on_hammer == Thumb.SLOW_LOWERING){ hammer_cocked -= Time.deltaTime * 10.0f; if(hammer_cocked <= 0.0f){ hammer_cocked = 0.0f; thumb_on_hammer = Thumb.OFF_HAMMER; PlaySoundFromGroup(sound_hammer_decock, kGunMechanicVolume); //PlaySoundFromGroup(sound_mag_eject_button, kGunMechanicVolume); } } if(has_slide){ if(slide_stage == SlideStage.PULLBACK || slide_stage == SlideStage.HOLD){ if(slide_stage == SlideStage.PULLBACK){ slide_amount += Time.deltaTime * 10.0f; if(slide_amount >= kSlideLockPosition && slide_lock){ slide_amount = kSlideLockPosition; slide_stage = SlideStage.HOLD; PlaySoundFromGroup(sound_slide_back, kGunMechanicVolume); } if(slide_amount >= kPressCheckPosition && slide_lock){ slide_amount = kPressCheckPosition; slide_stage = SlideStage.HOLD; slide_lock = false; PlaySoundFromGroup(sound_slide_back, kGunMechanicVolume); } if(slide_amount >= 1.0f){ PullSlideBack(); slide_amount = 1.0f; slide_stage = SlideStage.HOLD; PlaySoundFromGroup(sound_slide_back, kGunMechanicVolume); } } } var slide_amount_display= slide_amount; if(disable_springs){ slide_amount_display = Mathf.Floor(slide_amount_display + 0.5f); if(slide_amount == kPressCheckPosition){ slide_amount_display = kPressCheckPosition; } } transform.FindChild("slide").localPosition = slide_rel_pos + (transform.FindChild("point_slide_end").localPosition - transform.FindChild("point_slide_start").localPosition) * slide_amount_display; } if(has_hammer){ var hammer= GetHammer(); var point_hammer_cocked= transform.FindChild("point_hammer_cocked"); var hammer_cocked_display= hammer_cocked; if(disable_springs){ hammer_cocked_display = Mathf.Floor(hammer_cocked_display + 0.5f); } hammer.localPosition = Vector3.Lerp(hammer_rel_pos, point_hammer_cocked.localPosition, hammer_cocked_display); hammer.localRotation = Quaternion.Slerp(hammer_rel_rot, point_hammer_cocked.localRotation, hammer_cocked_display); } if(has_safety){ var safety_off_display= safety_off; if(disable_springs){ safety_off_display = Mathf.Floor(safety_off_display + 0.5f); } transform.FindChild("safety").localPosition = Vector3.Lerp(safety_rel_pos, transform.FindChild("point_safety_off").localPosition, safety_off_display); transform.FindChild("safety").localRotation = Quaternion.Slerp(safety_rel_rot, transform.FindChild("point_safety_off").localRotation, safety_off_display); } if(has_auto_mod){ var auto_mod_amount_display= auto_mod_amount; if(disable_springs){ auto_mod_amount_display = Mathf.Floor(auto_mod_amount_display + 0.5f); } var slide= transform.FindChild("slide"); slide.FindChild("auto mod toggle").localPosition = Vector3.Lerp(auto_mod_rel_pos, slide.FindChild("point_auto_mod_enabled").localPosition, auto_mod_amount_display); } if(gun_type == GunType.AUTOMATIC){ hammer_cocked = Mathf.Max(hammer_cocked, slide_amount); if(hammer_cocked != 1.0f && thumb_on_hammer == Thumb.OFF_HAMMER && (pressure_on_trigger == PressureState.NONE || action_type == ActionType.SINGLE)){ hammer_cocked = Mathf.Min(hammer_cocked, slide_amount); } } else { if(hammer_cocked != 1.0f && thumb_on_hammer == Thumb.OFF_HAMMER && (pressure_on_trigger == PressureState.NONE || action_type == ActionType.SINGLE)){ hammer_cocked = 0.0f; } } if(has_slide){ if(slide_stage == SlideStage.NOTHING){ var old_slide_amount= slide_amount; slide_amount = Mathf.Max(0.0f, slide_amount - Time.deltaTime * kSlideLockSpeed); if(!slide_lock && slide_amount == 0.0f && old_slide_amount != 0.0f){ PlaySoundFromGroup(sound_slide_front, kGunMechanicVolume*1.5f); if(round_in_chamber){ round_in_chamber.transform.position = transform.FindChild("point_chambered_round").position; round_in_chamber.transform.rotation = transform.FindChild("point_chambered_round").rotation; } } if(slide_amount == 0.0f && round_in_chamber_state == RoundState.LOADING){ round_in_chamber_state = RoundState.READY; } if(slide_lock && old_slide_amount >= kSlideLockPosition){ slide_amount = Mathf.Max(kSlideLockPosition, slide_amount); if(old_slide_amount != kSlideLockPosition && slide_amount == kSlideLockPosition){ PlaySoundFromGroup(sound_slide_front, kGunMechanicVolume); } } } } if(gun_type == GunType.REVOLVER){ if(yolk_stage == YolkStage.CLOSED && hammer_cocked == 1.0f){ target_cylinder_offset = 0; } if(target_cylinder_offset != 0){ var target_cylinder_rotation= ((active_cylinder + target_cylinder_offset) * 360.0f / cylinder_capacity); cylinder_rotation = Mathf.Lerp(target_cylinder_rotation, cylinder_rotation, Mathf.Pow(0.2f,Time.deltaTime)); if(cylinder_rotation > (active_cylinder + 0.5f) * 360.0f / cylinder_capacity){ ++active_cylinder; --target_cylinder_offset; if(yolk_stage == YolkStage.CLOSED){ PlaySoundFromGroup(sound_cylinder_rotate, kGunMechanicVolume); } } if(cylinder_rotation < (active_cylinder - 0.5f) * 360.0f / cylinder_capacity){ --active_cylinder; ++target_cylinder_offset; if(yolk_stage == YolkStage.CLOSED){ PlaySoundFromGroup(sound_cylinder_rotate, kGunMechanicVolume); } } } if(yolk_stage == YolkStage.CLOSING){ yolk_open -= Time.deltaTime * 5.0f; if(yolk_open <= 0.0f){ yolk_open = 0.0f; yolk_stage = YolkStage.CLOSED; PlaySoundFromGroup(sound_cylinder_close, kGunMechanicVolume * 2.0f); target_cylinder_offset = 0; } } if(yolk_stage == YolkStage.OPENING){ yolk_open += Time.deltaTime * 5.0f; if(yolk_open >= 1.0f){ yolk_open = 1.0f; yolk_stage = YolkStage.OPEN; PlaySoundFromGroup(sound_cylinder_open, kGunMechanicVolume * 2.0f); } } if(extractor_rod_stage == ExtractorRodStage.CLOSING){ extractor_rod_amount -= Time.deltaTime * 10.0f; if(extractor_rod_amount <= 0.0f){ extractor_rod_amount = 0.0f; extractor_rod_stage = ExtractorRodStage.CLOSED; PlaySoundFromGroup(sound_extractor_rod_close, kGunMechanicVolume); } for(var i=0; i<cylinder_capacity; ++i){ if(cylinders[i]._object){ cylinders[i].falling = false; } } } if(extractor_rod_stage == ExtractorRodStage.OPENING){ var old_extractor_rod_amount= extractor_rod_amount; extractor_rod_amount += Time.deltaTime * 10.0f; if(extractor_rod_amount >= 1.0f){ if(!extracted){ for(var i=0; i<cylinder_capacity; ++i){ if(cylinders[i]._object){ if(Random.Range(0.0f,3.0f) > cylinders[i].seated){ cylinders[i].falling = true; cylinders[i].seated -= Random.Range(0.0f,0.5f); } else { cylinders[i].falling = false; } } } extracted = true; } for(var i=0; i<cylinder_capacity; ++i){ if(cylinders[i]._object && cylinders[i].falling){ cylinders[i].seated -= Time.deltaTime * 5.0f; if(cylinders[i].seated <= 0.0f){ var bullet= cylinders[i]._object; var rbody = bullet.AddComponent<Rigidbody>(); bullet.transform.parent = null; rbody.interpolation = RigidbodyInterpolation.Interpolate; rbody.velocity = velocity; rbody.angularVelocity = new Vector3(Random.Range(-40.0f,40.0f),Random.Range(-40.0f,40.0f),Random.Range(-40.0f,40.0f)); cylinders[i]._object = null; cylinders[i].can_fire = false; } } } extractor_rod_amount = 1.0f; extractor_rod_stage = ExtractorRodStage.OPEN; if(old_extractor_rod_amount < 1.0f){ PlaySoundFromGroup(sound_extractor_rod_open, kGunMechanicVolume); } } } if(extractor_rod_stage == ExtractorRodStage.OPENING || extractor_rod_stage == ExtractorRodStage.OPEN){ extractor_rod_stage = ExtractorRodStage.CLOSING; } var yolk_open_display= yolk_open; var extractor_rod_amount_display= extractor_rod_amount; if(disable_springs){ yolk_open_display = Mathf.Floor(yolk_open_display + 0.5f); extractor_rod_amount_display = Mathf.Floor(extractor_rod_amount_display + 0.5f); } var yolk_pivot= transform.FindChild("yolk_pivot"); yolk_pivot.localRotation = Quaternion.Slerp(yolk_pivot_rel_rot, transform.FindChild("point_yolk_pivot_open").localRotation, yolk_open_display); var cylinder_assembly= yolk_pivot.FindChild("yolk").FindChild("cylinder_assembly"); var eulerAngles = cylinder_assembly.localEulerAngles; eulerAngles.z = cylinder_rotation; cylinder_assembly.localEulerAngles = eulerAngles; var extractor_rod= cylinder_assembly.FindChild("extractor_rod"); extractor_rod.localPosition = Vector3.Lerp( extractor_rod_rel_pos, cylinder_assembly.FindChild("point_extractor_rod_extended").localPosition, extractor_rod_amount_display); for(var i=0; i<cylinder_capacity; ++i){ if(cylinders[i]._object){ var name= "point_chamber_"+(i+1); var bullet_chamber= extractor_rod.FindChild(name); cylinders[i]._object.transform.position = bullet_chamber.position; cylinders[i]._object.transform.rotation = bullet_chamber.rotation; cylinders[i]._object.transform.localScale = transform.localScale; } } } }
public bool MagEject (){ if(gun_type != GunType.AUTOMATIC){ return false; } PlaySoundFromGroup(sound_mag_eject_button, kGunMechanicVolume); if(mag_stage != MagStage.OUT){ mag_stage = MagStage.REMOVING; PlaySoundFromGroup(sound_mag_ejection, kGunMechanicVolume); return true; } return false; }