Commit 25a3d60c authored by 16이상민's avatar 16이상민

Write tests for "JudgeManager"

parent b7d28703
This diff is collapsed.
fileFormatVersion: 2
guid: 3e4992bf17dae8e44b8880d8867b91ea
timeCreated: 1518892677
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: b5f132dad262fcc43a19502f6a0ee9e3
timeCreated: 1518923184
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -36,18 +36,16 @@ namespace JudgeModule
public Color Color { get; private set; }
public static readonly Judge BAD = JudgeList[2];
public static readonly Judge MISS = JudgeList.Last();
public static readonly Judge MISS = JudgeList[3];
public static readonly Judge Perfect = JudgeList[0];
public static readonly
float MaxButtonTimingRange = BadTime;
public static Judge TestJudge(Note note, float elapsedTime, bool end = false, bool test = false)
public static Judge TestJudge(Note note, float elapsedTime, bool end = false)
{
float timing = end ? note.EndTiming : note.StartTiming;
float difference = elapsedTime - timing;
if (test)
Debug.Log(difference);
var result = JudgeList.Where(x => Mathf.Abs(difference) < x.ButtonTimingRange).ToList();
return result.Count == 0 ? JudgeList.Last() : result[0];
......@@ -67,9 +65,18 @@ namespace JudgeModule
public static bool IsNoteEnd(Note note, float elapsedTime)
{
var timing = elapsedTime - note.EndTiming;
return timing <= BadTime &&
timing >= 0;
var difference = elapsedTime - note.EndTiming;
return difference <= BadTime &&
difference >= 0;
}
public static bool IsNoteProgress(Note note, float elapsedTime, float interval)
{
var timing = note.StartTiming + (interval * (note.JudgeCount + 1));
return Mathf.Abs(elapsedTime - timing) < PerfectTime &&
Mathf.Abs(elapsedTime - timing) <
Mathf.Abs(elapsedTime - (timing - interval));
}
}
}
\ No newline at end of file
......@@ -39,16 +39,13 @@ namespace JudgeModule
DeactivateNote(note);
}
public void ContinuingNoteProc(Note note, float timing)
public void ContinuingNoteProc(Note note, float timing, float interval)
{
var isEnd = Judge.IsNoteEnd(note, timing);
var judge = Judge.TestJudge(note, timing, isEnd);
SetJudge(judge);
if (!isEnd &&
judge.IsBreak)
DeactivateNote(note);
if (Judge.IsNoteProgress(note, timing, interval))
{
SetJudge(Judge.Perfect);
++note.JudgeCount;
}
}
public void CorrectlyStoppedNoteProc(Note note, float timing)
......
......@@ -7,9 +7,6 @@ namespace JudgeModule
{
public class NoteManager
{
private float baseTime;
private bool IsInitTime;
private GameObject offset;
private GameObject noteobj;
private GameObject deactives;
......@@ -49,16 +46,8 @@ namespace JudgeModule
GetLastNote = getlastnote;
}
public void Controll(float current)
{
if (!IsInitTime)
public void Controll(float timing)
{
baseTime = current;
IsInitTime = true;
}
float timing = current - baseTime;
offset.transform.position = Vector3.left * timing * ScrollSpeed;
RelocateControllers(timing);
......
......@@ -10,8 +10,6 @@ public class Controller : MonoBehaviour
private readonly float minAlpha = 0.3f;
private readonly float maxAlpha = 0.7f;
public int id;
// Update is called once per frame
void Update()
{
......
......@@ -168,7 +168,7 @@ public class InGameManager : MonoBehaviour {
if (condition.IsLongNoteStartCorrectly(note))
judger.EnteredNoteProc(note, timing);
else if (condition.IsLongNoteHoldCorrectly(note))
judger.ContinuingNoteProc(note, timing);
judger.ContinuingNoteProc(note, timing, 0);
else if (condition.IsLongNoteFinishCorrectly(note, timing))
judger.CorrectlyStoppedNoteProc(note, timing);
else if (condition.IsLongNoteFinishIncorrectly(note, timing))
......
using JudgeModule;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class JudgeManager : MonoBehaviour
namespace JudgeModule
{
float latency = 225f;
float scrollMultiplier = 1.0f;
float startOffset = -5f;
float baseTime;
bool IsInitTime;
bool IsGameEnd;
private GameObject offset;
private GameObject noteobj;
private GameObject deactives;
private GameObject appear;
private GameObject disappear;
private MotionGageManager motionGageManager;
private GameObject judgeText;
private GameObject motionSampleDisplayPrefab;
private Vector3 initialPos;
private float MsPerBeat
{
get
{
return 60 * 1000f / GameManager.Instance.CurrentTrack.BPM;
}
}
private float ScrollSpeed
{
get
public class JudgeManager
{
return scrollMultiplier * MsPerBeat / 1000f;
}
}
private InputManager input;
private NoteCondition condition;
private List<Note> notes;
private NoteJudger judger;
void SetObjectRef()
{
offset = GameObject.Find("Offset");
noteobj = GameObject.Find("Noteobj");
deactives = GameObject.Find("Deactives");
appear = GameObject.Find("NoteAppear");
disappear = GameObject.Find("NoteDisappear");
judgeText = GameObject.Find("Judge");
input = GameObject.Find("InGameManagers").GetComponent<InputManager>();
condition = new NoteCondition(input);
judger = new NoteJudger(new Dictionary<string, GameObject> { }, ShowResult);
motionGageManager = new MotionGageManager
{ motionGuage = GameObject.Find("Motion Guage") };
}
void JudgeInit()
{
initialPos = offset.transform.position;
judgeText.SetActive(false);
motionGageManager.ResetGuage();
IsGameEnd = false;
baseTime = 0f;
IsInitTime = false;
}
void LoadGameObjects()
{
motionSampleDisplayPrefab = Resources.Load("Motion Sample Display") as GameObject;
notes = GameManager.Instance.CurrentTrack.Notes.ToList();
notes.ForEach(LoadNote);
var sounds = GameObject.Find("Sounds");
sounds.transform.Cast<Transform>()
.ToList()
.ForEach(x => x.gameObject.SetActive(false));
if (GameManager.Instance.CurrentTrack.BGM != null)
sounds.transform.Find(GameManager.Instance.CurrentTrack.BGM)
.gameObject.SetActive(true);
}
// Use this for initialization
void Start()
{
SetObjectRef();
JudgeInit();
LoadGameObjects();
}
private float interval;
// Update is called once per frame
void Update()
public JudgeManager(InputManager input,
Action endgame,
Dictionary<string, GameObject> objects,
float bpm)
{
float current = motionGageManager.UpdateTime();
if (!IsInitTime)
{
baseTime = current;
IsInitTime = true;
}
float timing = current - baseTime;
offset.transform.position = Vector3.left * timing * ScrollSpeed;
motionGageManager.UpdateGuage();
if (noteobj.transform.childCount == 0 ||
IsGameEnd)
Invoke("ShowResult", 2f);
RelocateControllers(timing);
JudgeNote(timing);
}
void RelocateControllers(float timing)
{
DeactiveUndecided();
SwitchStandBy();
AssignNotes(timing);
}
void DeactiveUndecided()
{
noteobj.transform
.Cast<Transform>()
.Where(c => initialPos.x - c.position.x > Judge.BAD.ButtonTimingRange)
.ToList()
.ForEach(x => x.SetParent(deactives.transform));
}
void SwitchStandBy()
{
deactives.transform
.Cast<Transform>()
.Select(x => x.GetComponent<Controller>())
.Where(c => c.transform.position.x < disappear.transform.position.x)
.ToList()
.ForEach(x =>
{
x.transform.position = appear.transform.position;
x.transform.SetParent(appear.transform);
x.Instance = null;
x.gameObject.SetActive(false);
});
}
void AssignNotes(float timing)
{
appear.transform
.Cast<Transform>()
.Select(x => x.GetComponent<Controller>())
.ToList()
.ForEach(c =>
{
c.Instance = GetLastNote(timing);
if (c.Instance != null)
{
c.Instance.Component = c;
c.transform.position = initialPos + Vector3.right * ((c.Instance.StartTiming - timing) * ScrollSpeed);
c.transform.SetParent(noteobj.transform);
c.gameObject.SetActive(true);
}
});
condition = new NoteCondition(input);
judger = new NoteJudger(objects, endgame);
interval = 60 * 1000f / bpm;
}
void JudgeNote(float timing)
public void JudgeNote(Note note, float timing)
{
Note note = GetLastNote(timing);
if (note == null)
return;
if (condition.IsWrongInput(note))
{
judger.WrongNoteProc(note);
return;
}
if (Judge.IsNoteStart(note, timing))
PlaySampleMotion(note as MotionNote);
if (note.IsLong)
JudgeLongNote(note, timing);
else
JudgeShortNote(note, timing);
}
Note GetLastNote(float timing)
{
while (noteobj.transform.childCount != 0)
{
var note = noteobj.transform.GetChild(0).gameObject
.GetComponent<Controller>().Instance;
if (!Judge.IsPastNote(note, timing))
return note;
judger.SetJudge(Judge.MISS);
judger.DeactivateNote(note);
}
return null;
}
void PlaySampleMotion(MotionNote note)
{
GameObject motionSample = Instantiate(motionSampleDisplayPrefab);
MotionSampleDisplay msd = motionSample.GetComponent<MotionSampleDisplay>();
msd.sprite = note.Image;
msd.timeout = MsPerBeat;
note.Activated = true;
note.MotionSampleDisplay = msd;
}
void JudgeShortNote(Note note, float timing)
{
if (condition.IsShortNoteEntered(note))
......@@ -231,58 +51,7 @@ public class JudgeManager : MonoBehaviour
else if (condition.IsLongNoteFinishIncorrectly(note, timing))
judger.IncorrectlyStoppedNoteProc(note, timing);
else if (condition.IsLongNoteHoldCorrectly(note))
judger.ContinuingNoteProc(note, timing);
}
void LoadNote(Note note)
{
GameObject obj = Instantiate(
Resources.Load(note.Type.ToString(), typeof(GameObject)),
noteobj.transform)
as GameObject;
var controller = obj.AddComponent<Controller>();
controller.Instance = note;
note.Component = controller;
if (note.Type == NoteType.MeasureLine || note.Type == NoteType.BeatLine)
obj.transform.SetParent(offset.transform);
if (note.IsLong)
StretchLongNote(note, obj);
obj.transform.position += initialPos +
(Vector3.right * (note.StartTiming * ScrollSpeed));
}
void StretchLongNote(Note note, GameObject obj)
{
float length = note.Length * ScrollSpeed;
var holdTransform = obj.transform.GetChild(1)
.GetComponent<RectTransform>();
var endTransform = obj.transform.GetChild(2)
.GetComponent<RectTransform>();
holdTransform.SetSizeWithCurrentAnchors(
RectTransform.Axis.Horizontal, length);
holdTransform.position += (Vector3.right * length / 2);
endTransform.position += (Vector3.right * length);
}
private bool onResult = false;
private void ShowResult()
{
if (!onResult)
{
Destroy(GetComponent<InputManager>());
Destroy(GetComponent<MotionView>());
Destroy(GetComponent<CoordinateMapperManager>());
Destroy(this);
GameManager.Instance.SceneTransition("Result");
onResult = true;
judger.ContinuingNoteProc(note, timing, interval);
}
}
}
......@@ -2,7 +2,7 @@
{
public Controller Component = null;
public int id;
public int JudgeCount;
public float StartTiming { get; private set; }
public float EndTiming { get; private set; }
......
using UnityEngine.TestTools;
using System.Collections;
using UnityEngine;
using JudgeModule;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using TestSupport;
using UnityEngine.UI;
using MotionAnalysis;
public class JudgeManagerTests
{
[UnityTest]
public IEnumerator Judge_Miss_And_Note_Deactive_When_Wrong_Input_Button()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SMO", 0);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = (objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name &&
controller.transform.parent.name == "Deactives");
Assert.AreEqual(expected, actual, "Judge is miss and note is deactive when wrong input with button");
}
[UnityTest]
public IEnumerator Judge_Miss_And_Note_Deactive_When_Wrong_Input_Motion()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SBT", 0);
controller.Instance.Component = controller;
yield return null;
input.CurrentMotionState = MotionState.CLAP_PREPARE;
yield return null;
input.CurrentMotionState = MotionState.CLAP_DONE;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = (objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name &&
controller.transform.parent.name == "Deactives");
Assert.AreEqual(expected, actual, "Judge is miss and note is deactive when wrong input with motion");
}
[UnityTest]
public IEnumerator Judge_Valid_When_Short()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
bool isCorrect = true;
new Dictionary<float, Judge>
{
{ 0, Judge.JudgeList[0] },
{ Judge.JudgeList[0].ButtonTimingRange - 1, Judge.JudgeList[0] },
{ Judge.JudgeList[0].ButtonTimingRange, Judge.JudgeList[1] },
{ Judge.JudgeList[1].ButtonTimingRange - 1, Judge.JudgeList[1] },
{ Judge.JudgeList[1].ButtonTimingRange, Judge.JudgeList[2] },
{ Judge.JudgeList[2].ButtonTimingRange - 1, Judge.JudgeList[2] },
{ Judge.JudgeList[2].ButtonTimingRange, Judge.JudgeList[3] }
}
.ToList()
.ForEach(x =>
{
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SBT", 0);
controller.Instance.Component = controller;
manager.JudgeNote(controller.Instance, x.Key);
if (objects["judgetext"].GetComponent<Text>().text != x.Value.Name)
isCorrect = false;
})
;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is valid when short note");
}
[UnityTest]
public IEnumerator Judge_Valid_When_Long()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
bool isCorrect = true;
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("LBT", 0, 2000);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
if (controller.Instance.Activated == false)
isCorrect = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 1000);
if (controller.Instance.JudgeCount == 0)
isCorrect = false;
yield return null;
input.IsButtonDown = false;
manager.JudgeNote(controller.Instance, 2000);
if (controller.Instance.JudgeCount != 1 ||
objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name)
isCorrect = false;
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is valid when long note");
}
[UnityTest]
public IEnumerator Judge_Invalid_When_Long()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
bool isCorrect = true;
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("LBT", 0, 2000);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
if (controller.Instance.Activated == false)
isCorrect = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 1000);
if (controller.Instance.JudgeCount == 0)
isCorrect = false;
yield return null;
input.IsButtonDown = false;
manager.JudgeNote(controller.Instance, 1800);
if (controller.Instance.JudgeCount != 1 ||
objects["judgetext"].GetComponent<Text>().text != Judge.MISS.Name)
isCorrect = false;
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is invalid when long note");
}
private Dictionary<string, GameObject> MakeObjects()
{
var offset = new GameObject("Offset");
var noteobj = new GameObject("NoteObj");
var deactives = new GameObject("Deactives");
var appear = new GameObject("AppearNote");
var disappear = new GameObject("DisappearNote");
var judgetext = new GameObject("Judge");
offset .transform.position = Vector3.zero;
noteobj .transform.position = Vector3.zero;
deactives.transform.position = Vector3.zero;
appear .transform.position = Vector3.right * 1000;
disappear.transform.position = Vector3.left * 1000;
noteobj .transform.SetParent(offset.transform);
deactives.transform.SetParent(offset.transform);
Enumerable.Range(0, 20).ToList().ForEach(x =>
{
var obj = new GameObject();
obj.transform.SetParent(appear.transform);
var controller = obj.AddComponent<Controller>();
controller.Instance = new Note(0, 0);
});
judgetext.AddComponent<Text>();
return new Dictionary<string, GameObject>
{
{ "offset", offset },
{ "noteobj", noteobj },
{ "deactives", deactives },
{ "appear", appear },
{ "disappear", disappear },
{ "judgetext", judgetext }
};
}
private InputManager MakeInput()
{
var obj = new GameObject();
return obj.AddComponent<InputManager>();
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 141ae0ccc58281c4a8177fb1b037e44f
timeCreated: 1518879354
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -166,9 +166,9 @@ public class NoteJudgerTests
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Deactive_When_Not_End_And_Break()
public IEnumerator ContinuingNoteProc_Should_Judge_Perfect_When_Next_Timeing_In_Interval()
{
var settings = new TestSettings("LBT", 0, Judge.BAD.ButtonTimingRange + 1);
var settings = new TestSettings("LBT", 0, 1000);
var note = settings.note;
......@@ -181,60 +181,18 @@ public class NoteJudgerTests
yield return null;
judger.ContinuingNoteProc(note, -(Judge.BAD.ButtonTimingRange + 1));
note.Activated = true;
var expected = "Deactives";
var actual = note.Component.transform.parent.name;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should deactive when not end and break");
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Not_Deactive_When_End()
{
var settings = new TestSettings("LBT", 0, 1);
var note = settings.note;
var judger = new NoteJudger(new Dictionary<string, GameObject>
{
{ "deactives", settings.deactives },
{ "judgetext", settings.judge }
},
() => { });
yield return null;
judger.ContinuingNoteProc(note, note.EndTiming);
float interval = 60f;
float frame = 10f;
var expected = "NoteObj";
var actual = note.Component.transform.parent.name;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should not deactive when end");
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Not_Deactive_When_Not_Break()
{
var settings = new TestSettings("LBT", 0, Judge.BAD.ButtonTimingRange + 1);
var note = settings.note;
for (float t = note.StartTiming + frame; t < note.EndTiming; t += frame)
judger.ContinuingNoteProc(note, t, interval);
var judger = new NoteJudger(new Dictionary<string, GameObject>
{
{ "deactives", settings.deactives },
{ "judgetext", settings.judge }
},
() => { });
yield return null;
judger.ContinuingNoteProc(note, note.StartTiming);
var expected = "NoteObj";
var actual = note.Component.transform.parent.name;
var expected = (int)(note.EndTiming / interval);
var actual = note.JudgeCount;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should not deactive when not break");
Assert.AreEqual(expected, actual, "ContinuingNoteProc should judge perfect when next timing is included in interval");
}
class TestSettings
......
......@@ -113,7 +113,7 @@ public class NoteManagerTests
return Mathf.Abs(a - b) >= errorLimit;
}
public Dictionary<string, GameObject> MakeObjects()
private Dictionary<string, GameObject> MakeObjects()
{
var offset = new GameObject();
var noteobj = new GameObject();
......@@ -134,6 +134,7 @@ public class NoteManagerTests
{
var obj = new GameObject();
obj.transform.SetParent(appear.transform);
var controller = obj.AddComponent<Controller>();
controller.Instance = new Note(0, 0);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment