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

View file

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

View file

@ -720,7 +720,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1692511763}
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_Children: []
m_Father: {fileID: 0}
@ -743,7 +743,7 @@ GameObject:
- component: {fileID: 2053847427}
m_Layer: 0
m_Name: Character
m_TagString: Untagged
m_TagString: Player
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0

View file

@ -79,4 +79,13 @@ public class EnemyController : MonoBehaviour
else
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++)
{
var pos = transform.position + _diff;
var enemy = _pool.GetObject();
var enemy = _pool.GetObject(true);
if (enemy is null) break;
enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z);
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ó.
/// </summary>
/// <returns>Egy objektum a poolból.</returns>
public GameObject GetObject(short maxCount = 0)
public GameObject GetObject(bool fixedPool = false)
{
GameObject theRocket = null;
int c = 0;
foreach (var rocket in _objects)
{
if (!rocket.activeSelf)
@ -29,13 +28,12 @@ public class ObjectPool
theRocket = rocket;
break;
}
c++;
if (c == maxCount)
return null;
}
if (theRocket is null)
if (fixedPool)
return null;
else
_objects.Add(theRocket = Object.Instantiate(_prefab));
return theRocket;
}

View file

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