diff --git a/Scripts/GameSystem.cs b/Scripts/GameSystem.cs index 737378c..d7626bb 100644 --- a/Scripts/GameSystem.cs +++ b/Scripts/GameSystem.cs @@ -1,26 +1,23 @@ +using ADX2LEDemo.Scripts.Player; +using ADX2LEDemo.Scripts.UI; +using ADX2LEDemo.Scripts.Score; +using ADX2LEDemo.Scripts.Target; +using ADX2LEDemo.Scripts.UtilClass; using UnityEngine; -namespace ADX2LEDemo { - public class GameSystem : MonoBehaviour { +namespace ADX2LEDemo.Scripts { + public class GameSystem : MonoBehaviour + { public static GameSystem Instance { get; private set; } - - public int score { get; private set; } = 0; - - public int destroyTargetNum { get; private set; } = 0; - - private int maxTargetNum = 4; - - private int nowTargetNum = 0; - - private int weekTarget = 0; - - private GUIStyle style; - - bool gameOver = false; - - float guiX = 10; - float guiY = 10; - + + [SerializeField] UIController uiController; + + private ScoreManager scoreManager; + private TargetManager targetManager; + + private bool gameOver = false; + public bool GetGameOver => gameOver; + private void Awake() { if (GameSystem.Instance == null) { GameSystem.Instance = this; @@ -28,98 +25,66 @@ private void Awake() { Destroy(this); return; } - PlayerGenerate(); - } - void Start() { - style = new GUIStyle(); - style.fontSize = 32; + ManagerInit(); + } + private void ManagerInit() + { + scoreManager=new ScoreManager(); + + uiController = GetComponent(); + UIController.Delegate loadSampleScene=LoadSampleScene; + uiController.Init(loadSampleScene); + + new PlayerGenerator().PlayerGenerate(); + + targetManager = new TargetManager(); + TargetController.Delegate defeatedTarget=DefeatTarget; + targetManager.Init(defeatedTarget); + } + void Update() { - if (nowTargetNum < maxTargetNum && !gameOver) { - if (weekTarget < 3) { - GenerateTarget(1); - weekTarget++; - } else { - GenerateTarget(3); - weekTarget = 0; - } - nowTargetNum++; + + if (gameOver) + { + return; } - if (PlayerStatus.Instance.GetHP() <= 0) { + + UpdateManager(); + if (PlayerStatus.Instance.GetHP <= 0) { GameOver(); } } - public void PlayerGenerate() { - GameObject player = GameObject.CreatePrimitive(PrimitiveType.Capsule); - player.tag = "Player"; - player.name = "Player"; - player.transform.position = new Vector3(70, 1, 70); - player.GetComponent().isTrigger = true; - Rigidbody rb = player.AddComponent(); - rb.useGravity = false; - rb.constraints = RigidbodyConstraints.FreezeAll; - Camera.main.transform.SetParent(player.transform); - Camera.main.transform.localPosition = new Vector3(0f, 1.3f, 0f); - Camera.main.transform.localRotation = Quaternion.Euler(new Vector3(0, 90, 0)); - GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); - cube.transform.SetParent(player.transform); - cube.transform.localPosition = new Vector3(0.7f, 0.8f, -0.54f); - cube.transform.localRotation = Quaternion.Euler(new Vector3(0f, -8.31f, 0f)); - cube.transform.localScale = new Vector3(1.6f, 0.4f, 0.4f); - GameObject shotPos = new GameObject("ShotPos"); - shotPos.transform.SetParent(cube.transform); - shotPos.transform.localPosition = new Vector3(0.5f, 0f, 0f); - player.AddComponent(); - player.AddComponent(); - player.AddComponent().SetParams(shotPos.transform); - } - - public void AddScore(int addScore) => score += addScore; - - public void AddDestroyTargetNum() { - nowTargetNum--; - destroyTargetNum++; + private void UpdateManager() + { + targetManager.TryGenerateTarget(); + } - public void GenerateTarget(int hp) { - GameObject target = GameObject.CreatePrimitive(PrimitiveType.Cylinder); - target.name = "Target"; - target.transform.localScale = new Vector3(2f, 0.1f, 2f); - target.transform.position = RandomSpownPos(); - target.GetComponent().isTrigger = true; - target.AddComponent().SetParams(hp); - } - - public Vector3 RandomSpownPos() { - float[] xPos = { Random.Range(30f, 50f), Random.Range(90f, 110f) }; - float[] zPos = { Random.Range(30f, 50f), Random.Range(90f, 110f) }; - return new Vector3(xPos[Random.Range(0, 2)], 1f, zPos[Random.Range(0, 2)]); - } - - private void GameOver() { - style.fontSize = 72; - style.fontStyle = FontStyle.Bold; - style.alignment = TextAnchor.MiddleCenter; + /// + /// 倒されたターゲットから呼び出し + /// + /// + private void DefeatTarget(int defeatedTargetPoint) + { + scoreManager.AddScore(defeatedTargetPoint); + targetManager.AddDestroyTargetNum(); + uiController.UpdateScore(scoreManager.score); + } + + private void GameOver() + { + GetComponent().UpdateUiByGameOver(scoreManager.score); Cursor.lockState = CursorLockMode.None; gameOver = true; } - public bool GetGameOver() => gameOver; - - private void OnGUI() { - Rect rect = new Rect(0, 540, 1920, 400); - if (!gameOver) { - GUI.Label(rect, string.Format("Score:{0}\nHP:{1}", score, PlayerStatus.Instance.GetHP()), style); - } else { - GUI.Label(rect, "GameOver\n" + score.ToString(), style); - if (GUI.Button(new Rect(guiX, guiY, 400, 300), "Reset")) - UnityEngine.SceneManagement.SceneManager.LoadScene("SampleScene"); - - } - + private void LoadSampleScene() + { + UnityEngine.SceneManagement.SceneManager.LoadScene(SceneStrings.SampleScene); } } } \ No newline at end of file diff --git a/Scripts/Player/PlayerGenerator.cs b/Scripts/Player/PlayerGenerator.cs new file mode 100644 index 0000000..9f48a52 --- /dev/null +++ b/Scripts/Player/PlayerGenerator.cs @@ -0,0 +1,12 @@ +using UnityEngine; + +namespace ADX2LEDemo.Scripts.Player +{ + public class PlayerGenerator + { + public void PlayerGenerate() { + var player = GameObject.CreatePrimitive(PrimitiveType.Capsule); + new PlayerInitializer(player).InitPlayerObj(); + } + } +} \ No newline at end of file diff --git a/Scripts/Player/PlayerInitializer.cs b/Scripts/Player/PlayerInitializer.cs new file mode 100644 index 0000000..0d1b199 --- /dev/null +++ b/Scripts/Player/PlayerInitializer.cs @@ -0,0 +1,68 @@ +using UnityEngine; + +namespace ADX2LEDemo.Scripts.Player +{ + public class PlayerInitializer + { + private readonly GameObject player; + private GameObject cube; + private GameObject shotPos; + + public PlayerInitializer(GameObject player) + { + this.player = player; + } + + public void InitPlayerObj() + { + player.tag = "Player"; + player.name = "Player"; + + cube = GameObject.CreatePrimitive(PrimitiveType.Cube); + InitCube(); + + shotPos = new GameObject("ShotPos"); + InitShotPosObj(); + + InitCameraObj(); + + InitPlayerComponent(); + } + + private void InitPlayerComponent() + { + player.transform.position = new Vector3(70, 1, 70); + + player.GetComponent().isTrigger = true; + Rigidbody rb = player.AddComponent(); + rb.useGravity = false; + rb.constraints = RigidbodyConstraints.FreezeAll; + + player.AddComponent(); + player.AddComponent(); + player.AddComponent().SetParams(shotPos.transform); + } + + //@NOTE :Cubeには具体的な名前がほしい + private void InitCube() + { + cube.transform.SetParent(player.transform); + cube.transform.localPosition = new Vector3(0.7f, 0.8f, -0.54f); + cube.transform.localRotation = Quaternion.Euler(new Vector3(0f, -8.31f, 0f)); + cube.transform.localScale = new Vector3(1.6f, 0.4f, 0.4f); + } + + private void InitShotPosObj() + { + shotPos.transform.SetParent(cube.transform); + shotPos.transform.localPosition = new Vector3(0.5f, 0f, 0f); + } + + private void InitCameraObj() + { + Camera.main.transform.SetParent(player.transform); + Camera.main.transform.localPosition = new Vector3(0f, 1.3f, 0f); + Camera.main.transform.localRotation = Quaternion.Euler(new Vector3(0, 90, 0)); + } + } +} \ No newline at end of file diff --git a/Scripts/PlayerRotation.cs b/Scripts/Player/PlayerRotation.cs similarity index 85% rename from Scripts/PlayerRotation.cs rename to Scripts/Player/PlayerRotation.cs index e2f55ea..a7368ca 100644 --- a/Scripts/PlayerRotation.cs +++ b/Scripts/Player/PlayerRotation.cs @@ -1,37 +1,37 @@ -using UnityEngine; - -namespace ADX2LEDemo { - public class PlayerRotation : MonoBehaviour { - - [SerializeField] float speed = 1.0f; - - bool cursorLock = true; - - void Update() { - if (GameSystem.Instance.GetGameOver()) - return; - - float y = Input.GetAxis("Mouse X") * speed + transform.rotation.eulerAngles.y; - float z = Input.GetAxis("Mouse Y") * speed + transform.rotation.eulerAngles.z; - - transform.rotation = Quaternion.Euler(0, y, z); - - UpdateCursorLock(); - } - - public void UpdateCursorLock() { - if (Input.GetKeyDown(KeyCode.Escape)) { - cursorLock = false; - } else if (Input.GetMouseButton(0)) { - cursorLock = true; - } - - - if (cursorLock) { - Cursor.lockState = CursorLockMode.Locked; - } else if (!cursorLock) { - Cursor.lockState = CursorLockMode.None; - } - } - } +using UnityEngine; + +namespace ADX2LEDemo.Scripts.Player { + public class PlayerRotation : MonoBehaviour { + + [SerializeField] float speed = 1.0f; + + bool cursorLock = true; + + void Update() { + if (GameSystem.Instance.GetGameOver) + return; + + float y = Input.GetAxis("Mouse X") * speed + transform.rotation.eulerAngles.y; + float z = Input.GetAxis("Mouse Y") * speed + transform.rotation.eulerAngles.z; + + transform.rotation = Quaternion.Euler(0, y, z); + + UpdateCursorLock(); + } + + private void UpdateCursorLock() { + if (Input.GetKeyDown(KeyCode.Escape)) { + cursorLock = false; + } else if (Input.GetMouseButton(0)) { + cursorLock = true; + } + + + if (cursorLock) { + Cursor.lockState = CursorLockMode.Locked; + } else if (!cursorLock) { + Cursor.lockState = CursorLockMode.None; + } + } + } } \ No newline at end of file diff --git a/Scripts/PlayerStatus.cs b/Scripts/Player/PlayerStatus.cs similarity index 78% rename from Scripts/PlayerStatus.cs rename to Scripts/Player/PlayerStatus.cs index 5226c10..b06ee55 100644 --- a/Scripts/PlayerStatus.cs +++ b/Scripts/Player/PlayerStatus.cs @@ -1,22 +1,24 @@ -using UnityEngine; - -namespace ADX2LEDemo { - public class PlayerStatus : MonoBehaviour { - public static PlayerStatus Instance { get; private set; } - - [SerializeField] int hp = 5; - - private void Awake() { - if (PlayerStatus.Instance == null) { - PlayerStatus.Instance = this; - } else { - Destroy(this); - return; - } - } - - public int GetHP() => hp; - public int Damage() => hp--; - - } +using System; +using ADX2LEDemo.Scripts.UtilClass; +using UnityEngine; + +namespace ADX2LEDemo.Scripts.Player { + public class PlayerStatus : MonoBehaviour { + public static PlayerStatus Instance { get; private set; } + + [SerializeField] int hp = 5; + + private void Awake() { + if (PlayerStatus.Instance == null) { + PlayerStatus.Instance = this; + } else { + Destroy(this); + return; + } + } + + public int GetHP => hp; + public int Damage() => hp--; + + } } \ No newline at end of file diff --git a/Scripts/Score/ScoreManager.cs b/Scripts/Score/ScoreManager.cs new file mode 100644 index 0000000..756f0fa --- /dev/null +++ b/Scripts/Score/ScoreManager.cs @@ -0,0 +1,8 @@ +namespace ADX2LEDemo.Scripts.Score +{ + public class ScoreManager + { + public int score { get; private set; } = 0; + public void AddScore(int addScore) => score += addScore; + } +} \ No newline at end of file diff --git a/Scripts/Shot.cs b/Scripts/Shot.cs index 037c0d7..bd1f2c1 100644 --- a/Scripts/Shot.cs +++ b/Scripts/Shot.cs @@ -1,6 +1,7 @@ +using ADX2LEDemoAssets.Scripts.UtilClass; using UnityEngine; -namespace ADX2LEDemo { +namespace ADX2LEDemo.Scripts { public class Shot : MonoBehaviour { [SerializeField] float span = 0.5f; [SerializeField] Transform shotPos; @@ -28,7 +29,7 @@ public void SetParams(Transform shotPos, float span = 0.5f, float speed = 10f) { private void BulletGenerate() { GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere); bullet.name = "Bullet"; - bullet.tag = "Bullet"; + bullet.tag = TagStrings.Bullet; bullet.transform.position = shotPos.transform.position; bullet.transform.rotation = this.transform.rotation; bullet.transform.localScale = Vector3.one * 0.5f; @@ -48,7 +49,7 @@ private void Start() { } private void Update() { - if (GameSystem.Instance.GetGameOver()) + if (GameSystem.Instance.GetGameOver) return; rb.velocity = transform.right * speed; @@ -58,7 +59,7 @@ private void Update() { } private void OnTriggerEnter(Collider other) { - if (other.CompareTag("Target")) { + if (other.CompareTag(TagStrings.Target)) { //Destroy(other.gameObject); Destroy(gameObject); } diff --git a/Scripts/Target/TargetGenerator.cs b/Scripts/Target/TargetGenerator.cs new file mode 100644 index 0000000..a0142f6 --- /dev/null +++ b/Scripts/Target/TargetGenerator.cs @@ -0,0 +1,27 @@ +using UnityEngine; + +namespace ADX2LEDemo.Scripts.Target +{ + public class TargetGenerator + { + private TargetController targetController; + private readonly TargetController.Delegate defeatedTarget; + public TargetGenerator(TargetController.Delegate defeatedTarget) + { + this.defeatedTarget = defeatedTarget; + } + + public void GenerateTarget(int hp) { + GameObject target = GameObject.CreatePrimitive(PrimitiveType.Cylinder); + targetController=target.GetComponent(); + var instancePos = RandomSpownPos(); + targetController.Init(hp,instancePos,defeatedTarget); + } + + private Vector3 RandomSpownPos() { + float[] xPos = { Random.Range(30f, 50f), Random.Range(90f, 110f) }; + float[] zPos = { Random.Range(30f, 50f), Random.Range(90f, 110f) }; + return new Vector3(xPos[Random.Range(0, 2)], 1f, zPos[Random.Range(0, 2)]); + } + } +} \ No newline at end of file diff --git a/Scripts/Target/TargetManager.cs b/Scripts/Target/TargetManager.cs new file mode 100644 index 0000000..dc6f8c8 --- /dev/null +++ b/Scripts/Target/TargetManager.cs @@ -0,0 +1,47 @@ +namespace ADX2LEDemo.Scripts.Target +{ + public class TargetManager + { + //private int diedTargetNum = 0; + + private TargetGenerator targetGenerator; + + private const int MaxTargetNum = 4; + private int nowTargetNum = 0; + private int weakTarget = 0; + + public void Init(TargetController.Delegate defeatedTarget) + { + targetGenerator=new TargetGenerator(defeatedTarget); + } + + private bool CanGenerateTarget => nowTargetNum < MaxTargetNum; + + public void AddDestroyTargetNum() { + nowTargetNum--; + //diedTargetNum++; + } + + public void TryGenerateTarget() + { + if (CanGenerateTarget) + return; + + int nextGenerateTargetHp=0; + if (weakTarget < 3) + { + nextGenerateTargetHp = 1; + weakTarget++; + } + else + { + nextGenerateTargetHp = 3; + weakTarget = 0; + } + + targetGenerator.GenerateTarget(nextGenerateTargetHp); + nowTargetNum++; + } + + } +} \ No newline at end of file diff --git a/Scripts/TargetCon.cs b/Scripts/TargetCon.cs deleted file mode 100644 index b8587b2..0000000 --- a/Scripts/TargetCon.cs +++ /dev/null @@ -1,59 +0,0 @@ -using UnityEngine; - -namespace ADX2LEDemo { - public class TargetCon : MonoBehaviour { - int score = 100; - float speed = 0.01f; - int hp = 1; - bool alreadyDamege = false; - - void Start() { - gameObject.tag = "Target"; - transform.LookAt(PlayerStatus.Instance.transform); - transform.Rotate(new Vector3(90, 0, 0)); - } - - void Update() { - if (GameSystem.Instance.GetGameOver()) - return; - transform.position = Vector3.MoveTowards(transform.position, PlayerStatus.Instance.transform.position, speed); - } - - public void SetParams(int hp) { - Color[] colors = { Color.blue, Color.green, Color.black }; - gameObject.GetComponent().materials[0].color = colors[hp - 1]; - this.hp = hp; - this.score = hp * 150; - this.speed = hp * 0.01f; - } - - private void OnTriggerEnter(Collider other) { - if (!alreadyDamege) { - if (other.CompareTag("Bullet")) { - hp--; - if(hp <= 0) - DefeatTarget(); - } else if (other.CompareTag("Player")) { - PlayerDamage(); - } - } else { - Debug.LogWarning("Alredy Damage."); - } - - } - - private void PlayerDamage() { - alreadyDamege = true; - PlayerStatus.Instance.Damage(); - GameSystem.Instance.AddDestroyTargetNum(); - Destroy(gameObject); - } - - private void DefeatTarget() { - GameSystem.Instance.AddScore(score); - GameSystem.Instance.AddDestroyTargetNum(); - Destroy(gameObject); - } - - } -} diff --git a/Scripts/TargetController.cs b/Scripts/TargetController.cs new file mode 100644 index 0000000..7c9f890 --- /dev/null +++ b/Scripts/TargetController.cs @@ -0,0 +1,77 @@ +using ADX2LEDemo.Scripts.Player; +using ADX2LEDemo.Scripts.UtilClass; +using UnityEngine; + +namespace ADX2LEDemo.Scripts { + public class TargetController : MonoBehaviour { + private int score = 100; + private float speed = 0.01f; + private int hp = 1; + private bool hadDamage = false; + private bool initializedThis=false; + + public delegate void Delegate(int score); + + private Delegate defeatedTarget; + + public void Init(int hp,Vector3 instancePos,Delegate defeatedTarget) + { + gameObject.name = "Target"; + gameObject.tag = TagStrings.Bullet; + + transform.position = instancePos; + transform.localScale = new Vector3(2f, 0.1f, 2f); + transform.LookAt(PlayerStatus.Instance.transform); + transform.Rotate(new Vector3(90, 0, 0)); + + GetComponent().isTrigger = true; + SetParams(hp); + + this.defeatedTarget = defeatedTarget; + initializedThis = true; + } + + private void Update() + { + if (!initializedThis||GameSystem.Instance.GetGameOver) + return; + + transform.position = Vector3.MoveTowards(transform.position, PlayerStatus.Instance.transform.position, speed); + } + + private void SetParams(int hp) { + Color[] colors = { Color.blue, Color.green, Color.black }; + gameObject.GetComponent().materials[0].color = colors[hp - 1]; + this.hp = hp; + this.score = hp * 150; + this.speed = hp * 0.01f; + } + + private void OnTriggerEnter(Collider other) { + if (!hadDamage) { + if (other.CompareTag(TagStrings.Bullet)) { + hp--; + if(hp <= 0) + DefeatTarget(); + } else if (other.CompareTag(TagStrings.Player)) { + PlayerDamage(); + } + } else { + Debug.LogWarning("Already Damaged."); + } + + } + + private void PlayerDamage() { + hadDamage = true; + PlayerStatus.Instance.Damage(); + Destroy(gameObject); + } + + private void DefeatTarget() { + defeatedTarget(score); + Destroy(gameObject); + } + + } +} \ No newline at end of file diff --git a/Scripts/UI/UIController.cs b/Scripts/UI/UIController.cs new file mode 100644 index 0000000..0503ad5 --- /dev/null +++ b/Scripts/UI/UIController.cs @@ -0,0 +1,43 @@ +using ADX2LEDemo.Scripts.Player; +using UnityEngine; + +namespace ADX2LEDemo.Scripts.UI +{ + public class UIController:MonoBehaviour + { + public delegate void Delegate(); + + private GUIStyle style; + private string uiText; + private Delegate loadSampleScene; + private const float GuiX = 10; + private const float GuiY = 10; + + private readonly Rect labelRect =new Rect(0, 540, 1920, 400); + public void Init(Delegate loadSampleScene) { + style = new GUIStyle(); + style.fontSize = 32; + + this.loadSampleScene = loadSampleScene; + } + + private void OnGUI() { + GUI.Label(labelRect, uiText, style); + + if (GUI.Button(new Rect(GuiX, GuiY, 400, 300), "Reset")) + loadSampleScene(); + } + + public void UpdateScore(int score) + { + uiText = $"Score:{score}\nHP:{PlayerStatus.Instance.GetHP}"; + } + + public void UpdateUiByGameOver(int score) { + style.fontSize = 72; + style.fontStyle = FontStyle.Bold; + style.alignment = TextAnchor.MiddleCenter; + uiText = "GameOver\n" + score; + } + } +} \ No newline at end of file diff --git a/Scripts/UtilClass/SceneStrings.cs b/Scripts/UtilClass/SceneStrings.cs new file mode 100644 index 0000000..eb3e0f4 --- /dev/null +++ b/Scripts/UtilClass/SceneStrings.cs @@ -0,0 +1,7 @@ +namespace ADX2LEDemo.Scripts.UtilClass +{ + public static class SceneStrings + { + public static string SampleScene = "SampleScene"; + } +} \ No newline at end of file diff --git a/Scripts/UtilClass/TagStrings.cs b/Scripts/UtilClass/TagStrings.cs new file mode 100644 index 0000000..5acd541 --- /dev/null +++ b/Scripts/UtilClass/TagStrings.cs @@ -0,0 +1,9 @@ +namespace ADX2LEDemo.Scripts.UtilClass +{ + public static class TagStrings + { + public static string Bullet = "Bullet"; + public static string Player = "Player"; + public static string Target = "Target"; + } +} \ No newline at end of file