BloodAndGore/Assets/Scripts/Guns/Gun.cs

72 lines
2.2 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 {
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;
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<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) {
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;
}
}
}