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 { public Bullet Bullet; public SpriteRenderer Sprite; public Transform BulletHole; public float MaxCooldown = 0.5f; public float MinCooldown = 0.2f; public Bobbing Bobbing; private bool IsHeld = false; private float CurrCooldown = 0; private Bullet CurrentBullet; private bool HasShotBullet = false; private float BulletHoleOriginalY; void Start() { BulletHoleOriginalY = BulletHole.localPosition.y; } void Update() { if (CurrCooldown > 0) { CurrCooldown -= Time.deltaTime; } if (HasShotBullet && (CurrentBullet.HasExploded || CurrentBullet == null)) { CurrCooldown = Mathf.Min(CurrCooldown, MinCooldown); CurrentBullet = null; HasShotBullet = false; } } void OnCollisionEnter2D(Collision2D collision) { if (IsHeld) { return; } PlayerController Player = collision.collider.GetComponent(); if (Player != null) { IsHeld = true; Bobbing.BobbingMultiplier = 0; Destroy(GetComponent()); Destroy(GetComponent()); Player.SetGun(this); } } public bool Shoot(Vector2 direction, float rotation) { if (Bullet == null) { Debug.LogError("THERE IS NO BULLET"); } else { if (CurrCooldown <= 0) { CurrCooldown = MaxCooldown; 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); } } }