BloodAndGore/Assets/Scripts/Guns/Gun.cs
2019-08-22 20:19:07 +03:00

111 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Saltosion.OneWeapon.Player;
using Saltosion.OneWeapon.Effects;
namespace Saltosion.OneWeapon.Guns {
public class Gun : MonoBehaviour {
[Header("Essentials")]
public Bullet Bullet;
public SpriteRenderer Sprite;
public float MaxCooldown = 0.5f;
public float MinCooldown = 0.2f;
public bool IsAutomatic = false;
[Header("Effects")]
public Transform BulletHole;
public Bobbing Bobbing;
public ParticleSystem LaunchExplosion;
public CustomLight LaunchLight;
public float LaunchLightIntensity = 2;
public float LaunchLightIntensityDegrade = 2;
private bool IsHeld = false;
private float CurrCooldown = 0;
private float CurrLaunchLight = 0;
private Bullet CurrentBullet;
private bool HasShotBullet = false;
private float BulletHoleOriginalY;
void Start() {
BulletHoleOriginalY = BulletHole.localPosition.y;
if (LaunchExplosion != null) {
LaunchExplosion.Stop();
}
}
void Update() {
if (CurrCooldown > 0) {
CurrCooldown -= Time.deltaTime;
}
if (HasShotBullet && (CurrentBullet.HasExploded || CurrentBullet == null)) {
CurrCooldown = Mathf.Min(CurrCooldown, MinCooldown);
CurrentBullet = null;
HasShotBullet = false;
}
if (CurrLaunchLight > 0) {
CurrLaunchLight -= Time.deltaTime * LaunchLightIntensityDegrade;
LaunchLight.LightIntensity = CurrLaunchLight;
}
}
void OnCollisionEnter2D(Collision2D collision) {
if (IsHeld) {
return;
}
PlayerController Player = collision.collider.GetComponent<PlayerController>();
if (Player != null) {
IsHeld = true;
Bobbing.BobbingMultiplier = 0;
Destroy(GetComponent<BoxCollider2D>());
Destroy(GetComponent<Rigidbody2D>());
Player.SetGun(this);
}
}
public bool Shoot(Vector2 direction, float rotation, bool automatic) {
if (automatic && !IsAutomatic) {
return false;
}
if (Bullet == null) {
Debug.LogError("THERE IS NO BULLET");
} else {
if (CurrCooldown <= 0) {
CurrCooldown = MaxCooldown;
if (LaunchExplosion != null) {
LaunchExplosion.Play();
}
if (LaunchLight != null) {
CurrLaunchLight = LaunchLightIntensity;
LaunchLight.LightIntensity = CurrLaunchLight;
}
Bullet ShotBullet = Instantiate(Bullet, BulletHole.position, new Quaternion());
ShotBullet.Direction = direction.normalized;
ShotBullet.InitialRotation = rotation;
CurrentBullet = ShotBullet;
HasShotBullet = true;
return true;
}
}
return false;
}
public void FlipGun(bool flipped) {
Sprite.flipY = flipped;
float y = BulletHoleOriginalY;
if (flipped) {
y = -y;
}
BulletHole.localPosition = new Vector2(BulletHole.localPosition.x, y);
}
}
}