From e0b194d7eb0cf8e6c49e4ffb0b2b69ff5e677764 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 17 Nov 2020 16:14:13 +0100 Subject: [PATCH] Fix platform spawning, enemy respawn on ground --- Assembly-CSharp.csproj | 1 + Assets/Scenes/SampleScene.unity | 44 ++++++++++--------- Assets/Scripts/CharacterControllerBase.cs | 15 +++++++ .../Scripts/CharacterControllerBase.cs.meta | 3 ++ Assets/Scripts/EnemyController.cs | 9 ++-- Assets/Scripts/EnemySpawner.cs | 11 +++-- Assets/Scripts/OwnCharacterController.cs | 17 +++---- Assets/Scripts/PlatformSpawner.cs | 25 ++++++----- ProjectSettings/TagManager.asset | 1 + 9 files changed, 77 insertions(+), 49 deletions(-) create mode 100644 Assets/Scripts/CharacterControllerBase.cs create mode 100644 Assets/Scripts/CharacterControllerBase.cs.meta diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index 85c76a9..bbeb099 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -56,6 +56,7 @@ + diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index b3596f8..31e3834 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -131,9 +131,10 @@ GameObject: m_Component: - component: {fileID: 219260672} - component: {fileID: 219260673} + - component: {fileID: 219260674} m_Layer: 0 m_Name: GameManager - m_TagString: Untagged + m_TagString: Game manager m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -170,6 +171,25 @@ MonoBehaviour: player: {fileID: 2053847422} maxSize: 5 maxLevel: 5 + platformCount: 2 +--- !u!114 &219260674 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 219260671} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aa26aed626e74c85909419c3dde3ced8, type: 3} + m_Name: + m_EditorClassIdentifier: + spawnPos: {fileID: 1692511764} + timeBetweenSpawns: 2 + maxEnemyCount: 1 + enemyPrefab: {fileID: 7250562544566202694, guid: ef6e23d6fe1e28e8c809679081854c6a, + type: 3} + target: {fileID: 2053847422} --- !u!1 &407879043 GameObject: m_ObjectHideFlags: 0 @@ -1259,7 +1279,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1692511763} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 2.27, y: 3.93, z: -2.796909} + m_LocalPosition: {x: 2.27, y: 5.1, z: -2.796909} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -1361,7 +1381,6 @@ GameObject: - component: {fileID: 2053847423} - component: {fileID: 2053847425} - component: {fileID: 2053847426} - - component: {fileID: 2053847427} m_Layer: 0 m_Name: Character m_TagString: Player @@ -1497,6 +1516,7 @@ MonoBehaviour: jumpForce: 5 movementSpeed: 5 sprintSpeed: 15 + platformSpawner: {fileID: 219260673} --- !u!114 &2053847426 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1511,21 +1531,3 @@ MonoBehaviour: m_EditorClassIdentifier: prefab: {fileID: 6006275050469676560, guid: 7debf3c35b28387fdbec1c1c9942afae, type: 3} firePoint: {fileID: 1043406434} ---- !u!114 &2053847427 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2053847420} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: aa26aed626e74c85909419c3dde3ced8, type: 3} - m_Name: - m_EditorClassIdentifier: - spawnPos: {fileID: 1692511764} - timeBetweenSpawns: 2 - maxEnemyCount: 6 - enemyPrefab: {fileID: 7250562544566202694, guid: ef6e23d6fe1e28e8c809679081854c6a, - type: 3} - target: {fileID: 2053847422} diff --git a/Assets/Scripts/CharacterControllerBase.cs b/Assets/Scripts/CharacterControllerBase.cs new file mode 100644 index 0000000..40c796e --- /dev/null +++ b/Assets/Scripts/CharacterControllerBase.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public abstract class CharacterControllerBase : MonoBehaviour +{ + protected Rigidbody2D _rb; + + public bool IsOnGround(string groundName = "") + { + var res = new List(); + _rb.OverlapCollider(new ContactFilter2D(), res); + return res.Any(col => col.CompareTag("Ground") && (groundName.Length == 0 || col.name.StartsWith(groundName))); + } +} \ No newline at end of file diff --git a/Assets/Scripts/CharacterControllerBase.cs.meta b/Assets/Scripts/CharacterControllerBase.cs.meta new file mode 100644 index 0000000..00092db --- /dev/null +++ b/Assets/Scripts/CharacterControllerBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a64ce6ae64aa4dc79590539400aa7523 +timeCreated: 1605624251 \ No newline at end of file diff --git a/Assets/Scripts/EnemyController.cs b/Assets/Scripts/EnemyController.cs index cf44e0d..233bb80 100644 --- a/Assets/Scripts/EnemyController.cs +++ b/Assets/Scripts/EnemyController.cs @@ -4,20 +4,21 @@ using System.Collections.Generic; using UnityEngine; using Random = System.Random; -public class EnemyController : MonoBehaviour +public class EnemyController : CharacterControllerBase { public Transform target; public float speed; public float flyForce; public short finalHealth = 3; - private Rigidbody2D _rb; private short _hitsToRemove; - private Random _random = new Random(); + private readonly Random _random = new Random(); + private PlatformSpawner _platformSpawner; // Start is called before the first frame update void Start() { _rb = GetComponent(); + _platformSpawner = GameObject.FindGameObjectWithTag("Game manager").GetComponent(); } private void OnEnable() @@ -29,6 +30,8 @@ public class EnemyController : MonoBehaviour void FixedUpdate() { var tr = transform; + if (_platformSpawner.ShouldRespawn(tr, this)) + Remove(); var diff = target.position - tr.position; if (_rb.mass < 0.01f) { //Már lelőttük diff --git a/Assets/Scripts/EnemySpawner.cs b/Assets/Scripts/EnemySpawner.cs index aad60a8..767f9fc 100644 --- a/Assets/Scripts/EnemySpawner.cs +++ b/Assets/Scripts/EnemySpawner.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using UnityEngine; using Random = System.Random; @@ -13,21 +14,25 @@ public class EnemySpawner : MonoBehaviour private float _lastSpawn; private readonly Random _random = new Random(); private ObjectPool _pool; + private PlatformSpawner _spawner; private void Start() { - _diff = spawnPos.position - transform.position; + _diff = spawnPos.position - target.position; enemyPrefab.GetComponent().target = target; _pool = new ObjectPool(enemyPrefab.gameObject, 10); + _spawner = GetComponent(); } private void FixedUpdate() { + //int timeMultiplier = _spawner.maxLevel - _spawner.Level; + int countMultiplier = _spawner.Level; if (Time.fixedTime - _lastSpawn > timeBetweenSpawns && _random.Next(2) == 1) { - short count = (short) _random.Next(maxEnemyCount); + short count = (short) _random.Next(maxEnemyCount * countMultiplier); for (int i = 0; i < count; i++) { - var pos = transform.position + _diff; + var pos = target.position + _diff; var enemy = _pool.GetObject(true); if (enemy is null) break; enemy.transform.position = pos; diff --git a/Assets/Scripts/OwnCharacterController.cs b/Assets/Scripts/OwnCharacterController.cs index f4ec44c..0121070 100644 --- a/Assets/Scripts/OwnCharacterController.cs +++ b/Assets/Scripts/OwnCharacterController.cs @@ -5,17 +5,17 @@ using System.Linq; using UnityEngine; using Random = System.Random; -public class OwnCharacterController : MonoBehaviour +public class OwnCharacterController : CharacterControllerBase { public float jumpForce; public float movementSpeed; public float sprintSpeed; + public PlatformSpawner platformSpawner; - private Rigidbody2D _rb; private Vector3 _spawnPos; private float _health = 100f; - private Random _random = new Random(); - private List _checkpointPosList = new List(); + private readonly Random _random = new Random(); + private readonly List _checkpointPosList = new List(); private Vector3 _checkpointPos; // Start is called before the first frame update @@ -28,6 +28,8 @@ public class OwnCharacterController : MonoBehaviour // Update is called once per frame void Update() { + if (platformSpawner.ShouldRespawn(transform, this)) + Respawn(); if (Mathf.Abs(_rb.velocity.x) > 3) return; float input = Input.GetAxis("Horizontal"); @@ -69,13 +71,6 @@ public class OwnCharacterController : MonoBehaviour _rb.velocity = Vector2.zero; } - public bool IsOnGround(string groundName = "") - { - var res = new List(); - _rb.OverlapCollider(new ContactFilter2D(), res); - return res.Any(col => col.CompareTag("Ground") && (groundName.Length == 0 || col.name.StartsWith(groundName))); - } - public void SetCheckpoint(Vector3 pos) { _checkpointPosList.Add(pos); diff --git a/Assets/Scripts/PlatformSpawner.cs b/Assets/Scripts/PlatformSpawner.cs index 26e6ce2..b242ae7 100644 --- a/Assets/Scripts/PlatformSpawner.cs +++ b/Assets/Scripts/PlatformSpawner.cs @@ -14,7 +14,7 @@ public class PlatformSpawner : MonoBehaviour public int platformCount = 2; private Vector3 _spawnDiff; - private int _level; + internal int Level; private Random _random = new Random(); private Vector3 _lastPlatformPos; private OwnCharacterController _playerController; @@ -34,8 +34,6 @@ public class PlatformSpawner : MonoBehaviour // Update is called once per frame void FixedUpdate() { - if (_level > 0 && player.position.x > _lastLevel0Pos && _playerController.IsOnGround("Ground")) - _playerController.Respawn(); if (player.position.x + _spawnDiff.x <= _lastPlatformPos.x) return; int size = _random.Next(maxSize); @@ -45,21 +43,21 @@ public class PlatformSpawner : MonoBehaviour Instantiate(platformMiddle).position = pos += new Vector3(0.7f, 0, 0); Instantiate(platformRight).position = pos += new Vector3(0.7f, 0, 0); _lastPlatformPos = pos; - if (_level == 0) + if (Level == 0) _lastLevel0Pos = pos.x; if (--_remainingPlatforms != 0) return; int rand = _random.Next(2); switch (rand) { - case 0 when _level < maxLevel: - case 1 when _level == 0: - _level++; + case 0 when Level == 0: + case 1 when Level < maxLevel: + Level++; _lastPlatformPos.y++; _playerController.SetCheckpoint(_lastPlatformPos); break; - case 0 when _level > 1: - case 1 when _level == maxLevel: - _level--; + case 0 when Level > 1: + case 1 when Level == maxLevel: + Level--; _lastPlatformPos.y++; _playerController.SetCheckpoint(_lastPlatformPos); _lastPlatformPos.y -= 2; @@ -67,6 +65,11 @@ public class PlatformSpawner : MonoBehaviour } _totalLevel++; - _remainingPlatforms = (1 + _totalLevel) * platformCount; + _remainingPlatforms = _totalLevel * platformCount; + } + + public bool ShouldRespawn(Transform tr, CharacterControllerBase controller) + { + return Level > 0 && tr.position.x > _lastLevel0Pos && controller.IsOnGround("Ground"); } } \ No newline at end of file diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 10215bb..f1da479 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -6,6 +6,7 @@ TagManager: tags: - Ground - Enemy + - Game manager layers: - Default - TransparentFX