HeavenStudioPlus/Assets/Scripts/Games/KarateMan/BarrelDestroyEffect.cs

115 lines
3.7 KiB
C#
Raw Normal View History

2022-01-03 15:15:48 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
namespace RhythmHeavenMania.Games.KarateMan
{
2022-01-19 05:40:49 +00:00
// Physics in Rhythm Heaven Mania? nah im just fuckin lazy
2022-01-03 15:15:48 +00:00
public class BarrelDestroyEffect : MonoBehaviour
{
2022-01-19 05:40:49 +00:00
public SpriteRenderer SpriteRenderer;
2022-01-03 15:15:48 +00:00
private Rigidbody2D rb2d;
2022-01-19 05:40:49 +00:00
private BoxCollider2D col;
2022-01-03 15:15:48 +00:00
public int spriteIndex;
2022-01-19 05:40:49 +00:00
public int index;
2022-01-20 07:26:53 +00:00
public bool combo;
2022-01-19 05:40:49 +00:00
public GameObject shadow;
2022-01-03 15:15:48 +00:00
private void Start()
{
2022-01-19 05:40:49 +00:00
SpriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
2022-01-03 15:15:48 +00:00
SpriteRenderer.sprite = KarateMan.instance.BarrelSprites[spriteIndex];
rb2d = this.gameObject.AddComponent<Rigidbody2D>();
2022-01-19 05:40:49 +00:00
rb2d.gravityScale = 11;
rb2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
2022-01-03 15:15:48 +00:00
// rb2d.interpolation = RigidbodyInterpolation2D.Interpolate;
2022-01-19 05:40:49 +00:00
float yRange = 0;
2022-01-21 01:24:30 +00:00
float xRange = Random.Range(500, 800);
2022-01-19 05:40:49 +00:00
switch (index)
{
case 0:
yRange = Random.Range(400, 1500);
break;
case 1:
yRange = Random.Range(200, 700);
break;
case 2:
yRange = Random.Range(300, 1200);
break;
case 3:
yRange = Random.Range(300, 1200);
break;
case 4:
yRange = Random.Range(300, 1200);
break;
case 5:
yRange = Random.Range(300, 1200);
break;
case 6:
yRange = Random.Range(300, 1200);
break;
case 7:
yRange = Random.Range(500, 1600);
break;
}
2022-01-21 01:24:30 +00:00
if (combo)
{
yRange = Random.Range(800, 1600);
xRange = Random.Range(200, 500);
}
2022-01-03 15:15:48 +00:00
2022-01-19 05:40:49 +00:00
rb2d.AddForce(Vector3.up * yRange);
2022-01-21 01:24:30 +00:00
rb2d.AddForce(Vector3.right * xRange);
2022-01-03 15:15:48 +00:00
// this.gameObject.AddComponent<Rotate>().rotateSpeed = Random.Range(60, 450);
2022-01-19 05:40:49 +00:00
col = this.gameObject.AddComponent<BoxCollider2D>();
PhysicsMaterial2D mat = new PhysicsMaterial2D();
mat.bounciness = 0;
col.sharedMaterial = mat;
2022-01-03 15:15:48 +00:00
col.offset = new Vector2(-0.0574677f, -0.07480353f);
col.size = new Vector2(0.5694333f, 1.912059f);
StartCoroutine(FadeOut());
2022-01-19 05:40:49 +00:00
gameObject.name = "barrel_p";
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name != "barrel_p")
{
Destroy(rb2d);
Destroy(col);
}
else
{
Physics2D.IgnoreCollision(collision.collider, col);
}
2022-01-03 15:15:48 +00:00
}
private void Update()
{
2022-01-19 05:40:49 +00:00
if (rb2d != null)
2022-01-03 15:15:48 +00:00
this.transform.eulerAngles = new Vector3(0, 0, rb2d.velocity.magnitude * 4);
2022-01-19 05:40:49 +00:00
shadow.transform.localPosition = new Vector3(this.transform.localPosition.x, shadow.transform.localPosition.y);
2022-01-03 15:15:48 +00:00
}
private IEnumerator FadeOut()
{
yield return new WaitForSeconds(Conductor.instance.secPerBeat * 3);
SpriteRenderer.DOColor(new Color(1, 1, 1, 0), Conductor.instance.secPerBeat * 3).OnComplete(delegate { Destroy(this.gameObject); });
2022-01-19 05:40:49 +00:00
shadow.GetComponent<SpriteRenderer>().DOColor(new Color(1, 1, 1, 0), Conductor.instance.secPerBeat * 3).OnComplete(delegate { Destroy(shadow); });
2022-01-03 15:15:48 +00:00
}
}
}