BloodAndGore/Assets/Scripts/Guns/Gun.cs

55 lines
1.6 KiB
C#
Raw Normal View History

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