Respawn, fixed pool size, fix enemy spawn position

This commit is contained in:
Norbi Peti 2020-11-07 14:10:48 +01:00
parent eef0a307d0
commit 247e630c46
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 51 additions and 33 deletions

View file

@ -647,6 +647,9 @@
<Reference Include="Unity.Rider.Editor"> <Reference Include="Unity.Rider.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Rider.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Rider.Editor.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unity.Mathematics">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.dll</HintPath>
</Reference>
<Reference Include="Unity.2D.Psdimporter.Editor"> <Reference Include="Unity.2D.Psdimporter.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Psdimporter.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Psdimporter.Editor.dll</HintPath>
</Reference> </Reference>
@ -671,6 +674,9 @@
<Reference Include="Unity.2D.Animation.Editor"> <Reference Include="Unity.2D.Animation.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Animation.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Animation.Editor.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unity.Mathematics.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.Editor.dll</HintPath>
</Reference>
<Reference Include="Unity.2D.Common.Editor"> <Reference Include="Unity.2D.Common.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Common.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Common.Editor.dll</HintPath>
</Reference> </Reference>
@ -679,12 +685,6 @@
</Reference> </Reference>
<Reference Include="UnityEditor.UI"> <Reference Include="UnityEditor.UI">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/UnityEditor.UI.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/UnityEditor.UI.dll</HintPath>
</Reference>
<Reference Include="Unity.Mathematics">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.dll</HintPath>
</Reference>
<Reference Include="Unity.Mathematics.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.Editor.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -634,6 +634,9 @@
<Reference Include="Unity.Rider.Editor"> <Reference Include="Unity.Rider.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Rider.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Rider.Editor.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unity.Mathematics">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.dll</HintPath>
</Reference>
<Reference Include="Unity.2D.Psdimporter.Editor"> <Reference Include="Unity.2D.Psdimporter.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Psdimporter.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Psdimporter.Editor.dll</HintPath>
</Reference> </Reference>
@ -658,6 +661,9 @@
<Reference Include="Unity.2D.Animation.Editor"> <Reference Include="Unity.2D.Animation.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Animation.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Animation.Editor.dll</HintPath>
</Reference> </Reference>
<Reference Include="Unity.Mathematics.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.Editor.dll</HintPath>
</Reference>
<Reference Include="Unity.2D.Common.Editor"> <Reference Include="Unity.2D.Common.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Common.Editor.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.2D.Common.Editor.dll</HintPath>
</Reference> </Reference>
@ -666,12 +672,6 @@
</Reference> </Reference>
<Reference Include="UnityEditor.UI"> <Reference Include="UnityEditor.UI">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/UnityEditor.UI.dll</HintPath> <HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/UnityEditor.UI.dll</HintPath>
</Reference>
<Reference Include="Unity.Mathematics">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.dll</HintPath>
</Reference>
<Reference Include="Unity.Mathematics.Editor">
<HintPath>/D/Unity/Projects/Projekt/Library/ScriptAssemblies/Unity.Mathematics.Editor.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -720,7 +720,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1692511763} m_GameObject: {fileID: 1692511763}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.67, y: 2.4, z: -2.796909} m_LocalPosition: {x: 2.27, y: 2.74, z: -2.796909}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
@ -743,7 +743,7 @@ GameObject:
- component: {fileID: 2053847427} - component: {fileID: 2053847427}
m_Layer: 0 m_Layer: 0
m_Name: Character m_Name: Character
m_TagString: Untagged m_TagString: Player
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0

View file

@ -79,4 +79,13 @@ public class EnemyController : MonoBehaviour
else else
HitWhileFlying(); HitWhileFlying();
} }
private void OnCollisionEnter2D(Collision2D other)
{
var go = other.gameObject;
if(!go.CompareTag("Player"))
return;
if (IsAlive())
go.GetComponent<OwnCharacterController>().Hit();
}
} }

View file

@ -28,7 +28,7 @@ public class EnemySpawner : MonoBehaviour
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var pos = transform.position + _diff; var pos = transform.position + _diff;
var enemy = _pool.GetObject(); var enemy = _pool.GetObject(true);
if (enemy is null) break; if (enemy is null) break;
enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z); enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z);
var rb = enemy.GetComponent<Rigidbody2D>(); var rb = enemy.GetComponent<Rigidbody2D>();

View file

@ -18,10 +18,9 @@ public class ObjectPool
/// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó. /// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó.
/// </summary> /// </summary>
/// <returns>Egy objektum a poolból.</returns> /// <returns>Egy objektum a poolból.</returns>
public GameObject GetObject(short maxCount = 0) public GameObject GetObject(bool fixedPool = false)
{ {
GameObject theRocket = null; GameObject theRocket = null;
int c = 0;
foreach (var rocket in _objects) foreach (var rocket in _objects)
{ {
if (!rocket.activeSelf) if (!rocket.activeSelf)
@ -29,14 +28,13 @@ public class ObjectPool
theRocket = rocket; theRocket = rocket;
break; break;
} }
c++;
if (c == maxCount)
return null;
} }
if (theRocket is null) if (theRocket is null)
_objects.Add(theRocket = Object.Instantiate(_prefab)); if (fixedPool)
return null;
else
_objects.Add(theRocket = Object.Instantiate(_prefab));
return theRocket; return theRocket;
} }
} }

View file

@ -1,28 +1,29 @@
using System.Collections; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
public class OwnCharacterController : MonoBehaviour public class OwnCharacterController : MonoBehaviour
{ {
private Rigidbody2D rb; private Rigidbody2D _rb;
private Collider2D collider; private Vector3 _spawnPos;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
rb = GetComponent<Rigidbody2D>(); _rb = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>(); _spawnPos = transform.position;
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
if (Mathf.Abs(rb.velocity.x) > 3) if (Mathf.Abs(_rb.velocity.x) > 3)
return; return;
float input = Input.GetAxis("Horizontal"); float input = Input.GetAxis("Horizontal");
if (input < 0 && rb.transform.localScale.x > 0 if (input < 0 && _rb.transform.localScale.x > 0
|| input > 0 && rb.transform.localScale.x < 0) || input > 0 && _rb.transform.localScale.x < 0)
{ {
var tr = transform; var tr = transform;
var scale = tr.localScale; var scale = tr.localScale;
@ -32,16 +33,26 @@ public class OwnCharacterController : MonoBehaviour
if (Input.GetButton("Fire3")) if (Input.GetButton("Fire3"))
input *= 10; input *= 10;
rb.AddForce(new Vector2(input * 5, 0)); _rb.AddForce(new Vector2(input * 5, 0));
if (Input.GetButtonDown("Jump") && IsOnGround()) if (Input.GetButtonDown("Jump") && IsOnGround())
rb.AddForce(new Vector2(0, 4), ForceMode2D.Impulse); _rb.AddForce(new Vector2(0, 4), ForceMode2D.Impulse);
}
public void Hit()
{
Respawn();
}
private void Respawn()
{
transform.position = _spawnPos;
} }
private bool IsOnGround() private bool IsOnGround()
{ {
var res = new List<Collider2D>(); var res = new List<Collider2D>();
rb.OverlapCollider(new ContactFilter2D(), res); _rb.OverlapCollider(new ContactFilter2D(), res);
return res.Any(col => col.CompareTag("Ground")); return res.Any(col => col.CompareTag("Ground"));
} }
} }