Fix platform spawning, enemy respawn on ground

This commit is contained in:
Norbi Peti 2020-11-17 16:14:13 +01:00
parent 3e57a06752
commit e0b194d7eb
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 77 additions and 49 deletions

View file

@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\Scripts\CameraController.cs" />
<Compile Include="Assets\Scripts\CharacterControllerBase.cs" />
<Compile Include="Assets\Scripts\EnemyController.cs" />
<Compile Include="Assets\Scripts\EnemySpawner.cs" />
<Compile Include="Assets\Scripts\ObjectPool.cs" />

View file

@ -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}

View file

@ -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<Collider2D>();
_rb.OverlapCollider(new ContactFilter2D(), res);
return res.Any(col => col.CompareTag("Ground") && (groundName.Length == 0 || col.name.StartsWith(groundName)));
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a64ce6ae64aa4dc79590539400aa7523
timeCreated: 1605624251

View file

@ -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<Rigidbody2D>();
_platformSpawner = GameObject.FindGameObjectWithTag("Game manager").GetComponent<PlatformSpawner>();
}
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

View file

@ -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<EnemyController>().target = target;
_pool = new ObjectPool(enemyPrefab.gameObject, 10);
_spawner = GetComponent<PlatformSpawner>();
}
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;

View file

@ -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<Vector3> _checkpointPosList = new List<Vector3>();
private readonly Random _random = new Random();
private readonly List<Vector3> _checkpointPosList = new List<Vector3>();
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<Collider2D>();
_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);

View file

@ -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");
}
}

View file

@ -6,6 +6,7 @@ TagManager:
tags:
- Ground
- Enemy
- Game manager
layers:
- Default
- TransparentFX