added character spawner script, spawn and destroy characters
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Character : MonoBehaviour
|
||||
@@ -11,13 +12,11 @@ public abstract class Character : MonoBehaviour
|
||||
protected GameObject _bonesSide;
|
||||
[SerializeField]
|
||||
protected GameObject _bonesBack;
|
||||
[SerializeField]
|
||||
protected GameObject _spawnPoint;
|
||||
|
||||
|
||||
[SerializeField]
|
||||
private LayerMask _mapLayer;
|
||||
|
||||
|
||||
protected SpriteRenderer _spriteRenderer;
|
||||
|
||||
|
||||
@@ -36,20 +35,16 @@ public abstract class Character : MonoBehaviour
|
||||
protected bool isAllowLeft = true;
|
||||
private Vector2 _cellSize;
|
||||
|
||||
public event EventHandler OnCharacterDeath;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_body = GetComponent<Rigidbody2D>();
|
||||
_capsuleCollider = GetComponent<CapsuleCollider2D>();
|
||||
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
_cellSize = new Vector2(0.6f, 1f);
|
||||
Spawn();
|
||||
_cellSize = new Vector2(0.6f, 1f);
|
||||
}
|
||||
|
||||
protected void Spawn()
|
||||
{
|
||||
transform.position = _spawnPoint.transform.position;
|
||||
}
|
||||
|
||||
|
||||
protected void MoveTo(float inputHorizontal, float inputVertical)
|
||||
{
|
||||
var block = GetMapElement();
|
||||
@@ -132,9 +127,9 @@ public abstract class Character : MonoBehaviour
|
||||
|
||||
}
|
||||
|
||||
private void Death()
|
||||
protected void Death()
|
||||
{
|
||||
OnDeath();
|
||||
OnCharacterDeath?.Invoke(this,EventArgs.Empty);
|
||||
}
|
||||
|
||||
private bool CanClimbUp()
|
||||
@@ -174,7 +169,6 @@ public abstract class Character : MonoBehaviour
|
||||
return mapElement;
|
||||
}
|
||||
|
||||
protected abstract void OnDeath();
|
||||
protected abstract void SetWalkingAnimation(bool isWalking);
|
||||
protected abstract void SetClimbingAnimation(bool isClimbing);
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CharacterSpawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _prefab;
|
||||
|
||||
[SerializeField]
|
||||
private int _maxCharacters=1;
|
||||
|
||||
[SerializeField]
|
||||
private Transform _spawnPoint;
|
||||
|
||||
private int _respawnTimeout = 4;
|
||||
private float _respawnElementTimer;
|
||||
|
||||
private List<Character> _characters;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
if(_spawnPoint==null)
|
||||
{
|
||||
_spawnPoint=gameObject.transform;
|
||||
}
|
||||
_characters=new List<Character>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
if(_characters.Count<_maxCharacters)
|
||||
{
|
||||
_respawnElementTimer -= Time.deltaTime;
|
||||
if (_respawnElementTimer <= 0)
|
||||
{
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
var prefab= Instantiate(_prefab, _spawnPoint.position, _spawnPoint.rotation);
|
||||
var character=prefab.GetComponent<Character>();
|
||||
character.OnCharacterDeath+=OnCharacterDeath;
|
||||
|
||||
_characters.Add(character);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCharacterDeath(object sender, EventArgs e)
|
||||
{
|
||||
var character=(sender as Character);
|
||||
character.OnCharacterDeath-=OnCharacterDeath;
|
||||
_characters.Remove(character);
|
||||
Destroy(character.gameObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4eeeff2f5d376d04793a6175a82f037c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,16 +2,6 @@
|
||||
|
||||
public class EnemyAI : Character
|
||||
{
|
||||
private bool _needRespawn = false;
|
||||
private int _respawnTimeout = 4;
|
||||
private float _respawnElementTimer;
|
||||
|
||||
protected override void OnDeath()
|
||||
{
|
||||
_needRespawn = true;
|
||||
_spriteRenderer.enabled =false;
|
||||
}
|
||||
|
||||
protected override void SetClimbingAnimation(bool isClimbing)
|
||||
{
|
||||
}
|
||||
@@ -22,19 +12,6 @@ public class EnemyAI : Character
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(_needRespawn)
|
||||
{
|
||||
_respawnElementTimer -= Time.deltaTime;
|
||||
if (_respawnElementTimer <= 0)
|
||||
{
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
_needRespawn = false;
|
||||
_spriteRenderer.enabled =true;
|
||||
Spawn();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
float horizontal = 0;
|
||||
float vertical = 0;
|
||||
|
||||
@@ -56,7 +33,6 @@ public class EnemyAI : Character
|
||||
{
|
||||
if (Mathf.Abs(Player.Instance.transform.position.x - transform.position.x) < 0.1f)
|
||||
{
|
||||
print($"horizontal block");
|
||||
horizontal = 0;
|
||||
}
|
||||
else if (directionToPlayer.x < 0)
|
||||
@@ -65,7 +41,6 @@ public class EnemyAI : Character
|
||||
{ horizontal = 1; }
|
||||
|
||||
}
|
||||
print($"horizontal {horizontal}");
|
||||
|
||||
if (Input.GetKey(KeyCode.T))
|
||||
{ vertical = 1; }
|
||||
|
||||
@@ -118,17 +118,14 @@ public class Player : Character
|
||||
_animator.SetBool("Climb", isClimbing);
|
||||
}
|
||||
|
||||
protected override void OnDeath()
|
||||
protected void OnDeath()
|
||||
{
|
||||
Lives--;
|
||||
Spawn();
|
||||
|
||||
if (Lives==0)
|
||||
{
|
||||
print("game over");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user