Created
May 1, 2017 23:42
-
-
Save lycoris102/97ea3f7d84979a51b48b4f9e2e0b32b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UniRx; | |
using UnityEngine; | |
using System.Linq; | |
using UniRx.Triggers; | |
public class BoxSpawner : MonoBehaviour | |
{ | |
public TextAsset CsvFile; | |
public AudioSource SE; | |
private List<Box> boxList; | |
private List<int> timingList = new List<int>(); | |
void Awake() | |
{ | |
this.LoadCsv(); | |
boxList = this.GetComponentsInChildren<Box>().ToList(); | |
boxList.ForEach(box => | |
{ | |
// 予め全てのブロックを非アクティブに | |
box.gameObject.SetActive(false); | |
}); | |
} | |
void OnEnable() | |
{ | |
// 拍の切り替わりのタイミング | |
var timingObservable = this.UpdateAsObservable() | |
.Where(_ => Music.IsPlaying) | |
.Where(_ => Music.IsJustChanged) | |
.Share(); | |
// 曲の節目であれば出現しているブロックを全て非アクティブにする | |
timingObservable | |
.Where(_ => Music.Just.Bar % 16 == 15 && Music.Just.Beat == 3 && Music.Just.Unit == 3) | |
.Select(_ => this.boxList.Where(box => box.gameObject.activeSelf).ToList()) | |
.Where(activieList => activieList.Any()) | |
.Subscribe(activieList => activieList.ForEach(box => box.Remove())) | |
.AddTo(this); | |
// CSVに指定された個数分ブロックを表示する | |
timingObservable | |
.Select(_ => this.boxList.Where(box => !box.gameObject.activeSelf)) | |
.Where(disableBoxEnumerable => disableBoxEnumerable.Any()) | |
.Subscribe(disableBoxEnumerable => | |
{ | |
// ブロックの出現モーション時間を考慮して、再生タイミングから1つ先のブロックを先読みする | |
int timingIndex = Music.Just.Bar * 16 + Music.Just.Beat * 4 + Music.Just.Unit + 1; | |
if (timingIndex < this.timingList.Count && this.timingList[timingIndex] > 0) | |
{ | |
// 曲に合わせてSEを鳴らす | |
Music.QuantizePlay(this.SE); | |
for (int i = 0; i < this.timingList[timingIndex]; i++) | |
{ | |
var index = UnityEngine.Random.Range(0, disableBoxEnumerable.Count()); | |
var targetBox = disableBoxEnumerable.ElementAt(index); | |
targetBox.gameObject.SetActive(true); | |
} | |
} | |
}) | |
.AddTo(this); | |
} | |
void LoadCsv() | |
{ | |
string[] lines = this.CsvFile.text.Replace("\r\n", "\n").Split("\n"[0]); | |
foreach (var line in lines) | |
{ | |
if (line == "") | |
{ | |
continue; | |
} | |
this.timingList.AddRange(line.Split(',').Select(n => int.Parse(n)).ToList()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment