2019-08-04 00:59:18 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2019-08-14 16:17:48 +02:00
|
|
|
|
using Saltosion.OneWeapon.Player;
|
2019-08-14 20:35:06 +02:00
|
|
|
|
using Saltosion.OneWeapon.Effects;
|
2019-08-04 00:59:18 +02:00
|
|
|
|
|
2019-08-14 16:17:48 +02:00
|
|
|
|
namespace Saltosion.OneWeapon.Guns {
|
2019-08-04 00:59:18 +02:00
|
|
|
|
public class Gun : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
public Bullet Bullet;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
public SpriteRenderer Sprite;
|
|
|
|
|
public Transform BulletHole;
|
2019-08-14 23:58:24 +02:00
|
|
|
|
public float MaxCooldown = 0.5f;
|
|
|
|
|
public float MinCooldown = 0.2f;
|
2019-08-14 20:35:06 +02:00
|
|
|
|
public Bobbing Bobbing;
|
2019-08-04 00:59:18 +02:00
|
|
|
|
|
|
|
|
|
private bool IsHeld = false;
|
2019-08-04 20:01:28 +02:00
|
|
|
|
private float CurrCooldown = 0;
|
|
|
|
|
|
2019-08-14 23:58:24 +02:00
|
|
|
|
private Bullet CurrentBullet;
|
|
|
|
|
private bool HasShotBullet = false;
|
|
|
|
|
|
2019-08-04 20:01:28 +02:00
|
|
|
|
void Update() {
|
|
|
|
|
if (CurrCooldown > 0) {
|
|
|
|
|
CurrCooldown -= Time.deltaTime;
|
|
|
|
|
}
|
2019-08-14 23:58:24 +02:00
|
|
|
|
|
|
|
|
|
if (HasShotBullet && (CurrentBullet.HasExploded || CurrentBullet == null)) {
|
|
|
|
|
CurrCooldown = Mathf.Min(CurrCooldown, MinCooldown);
|
|
|
|
|
CurrentBullet = null;
|
|
|
|
|
HasShotBullet = false;
|
|
|
|
|
}
|
2019-08-04 20:01:28 +02:00
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
|
2019-08-14 18:21:27 +02:00
|
|
|
|
void OnCollisionEnter2D(Collision2D collision) {
|
2019-08-04 00:59:18 +02:00
|
|
|
|
if (IsHeld) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-14 18:21:27 +02:00
|
|
|
|
PlayerController Player = collision.collider.GetComponent<PlayerController>();
|
2019-08-04 00:59:18 +02:00
|
|
|
|
if (Player != null) {
|
|
|
|
|
IsHeld = true;
|
2019-08-14 20:35:06 +02:00
|
|
|
|
Bobbing.BobbingMultiplier = 0;
|
2019-08-04 00:59:18 +02:00
|
|
|
|
Destroy(GetComponent<BoxCollider2D>());
|
2019-08-14 18:21:27 +02:00
|
|
|
|
Destroy(GetComponent<Rigidbody2D>());
|
2019-08-14 20:35:06 +02:00
|
|
|
|
Player.SetGun(this);
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 20:01:28 +02:00
|
|
|
|
public bool Shoot(Vector2 direction, float rotation) {
|
2019-08-04 00:59:18 +02:00
|
|
|
|
if (Bullet == null) {
|
|
|
|
|
Debug.LogError("THERE IS NO BULLET");
|
|
|
|
|
} else {
|
2019-08-04 20:01:28 +02:00
|
|
|
|
if (CurrCooldown <= 0) {
|
2019-08-14 23:58:24 +02:00
|
|
|
|
CurrCooldown = MaxCooldown;
|
2019-08-04 20:01:28 +02:00
|
|
|
|
Bullet ShotBullet = Instantiate(Bullet, BulletHole.position, new Quaternion());
|
|
|
|
|
ShotBullet.Direction = direction.normalized;
|
|
|
|
|
ShotBullet.InitialRotation = rotation;
|
2019-08-14 23:58:24 +02:00
|
|
|
|
|
|
|
|
|
CurrentBullet = ShotBullet;
|
|
|
|
|
HasShotBullet = true;
|
2019-08-04 20:01:28 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
2019-08-04 20:01:28 +02:00
|
|
|
|
return false;
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
2019-08-04 15:43:22 +02:00
|
|
|
|
|
|
|
|
|
public void FlipGun(bool flipped) {
|
|
|
|
|
Sprite.flipY = flipped;
|
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|