BloodAndGore/Assets/Scripts/Guns/Gun.cs

55 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon {
public class Gun : MonoBehaviour {
public Bullet Bullet;
public SpriteRenderer Sprite;
public Transform BulletHole;
public float Cooldown = 0.5f;
private bool IsHeld = false;
private float CurrCooldown = 0;
void Update() {
if (CurrCooldown > 0) {
CurrCooldown -= Time.deltaTime;
}
}
void OnTriggerEnter2D(Collider2D collider) {
if (IsHeld) {
return;
}
Player Player = collider.GetComponent<Player>();
if (Player != null) {
IsHeld = true;
Player.SetGun(this);
Destroy(GetComponent<BoxCollider2D>());
}
}
public bool Shoot(Vector2 direction, float rotation) {
if (Bullet == null) {
Debug.LogError("THERE IS NO BULLET");
} else {
if (CurrCooldown <= 0) {
CurrCooldown = Cooldown;
Bullet ShotBullet = Instantiate(Bullet, BulletHole.position, new Quaternion());
ShotBullet.Direction = direction.normalized;
ShotBullet.InitialRotation = rotation;
return true;
}
}
return false;
}
public void FlipGun(bool flipped) {
Sprite.flipY = flipped;
}
}
}