Refactored Player and deleted BombSpawner

This commit is contained in:
Denis-Cosmin Nutiu 2019-08-24 19:11:14 +03:00
parent 2aa1e9c054
commit d6ffb9b095
14 changed files with 143 additions and 253 deletions

View file

@ -1,47 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1896072655702505677
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1896072655702505676}
- component: {fileID: 5279176810039195611}
m_Layer: 0
m_Name: BombSpawner
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1896072655702505676
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1896072655702505677}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 7.8192964, y: -2.0062397, z: 8.0234375}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &5279176810039195611
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1896072655702505677}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cd3bf3610d18aa14e805185b5e3f2d4b, type: 3}
m_Name:
m_EditorClassIdentifier:
bombPrefab: {fileID: 5909392949477884267, guid: 28467e0d65b5d3844bf1862ac0c49ad9,
type: 3}

View file

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: c9b7903151183144dbf30c9b2ab371b2
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -11,6 +11,7 @@ GameObject:
- component: {fileID: 2219719956326328737}
- component: {fileID: 2976769454423190410}
- component: {fileID: 5317321277803550577}
- component: {fileID: 5518668270798060090}
m_Layer: 0
m_Name: Explosion
m_TagString: Explosion
@ -71,6 +72,18 @@ BoxCollider2D:
serializedVersion: 2
m_Size: {x: 0.5, y: 0.5}
m_EdgeRadius: 0
--- !u!114 &5518668270798060090
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2086996798441195857}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 00b7acc459217644283ce8202cadcded, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &5120645164323341697
GameObject:
m_ObjectHideFlags: 0

View file

@ -145,6 +145,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e3cc05deb3d6e44128ad7bc8610cedac, type: 3}
m_Name:
m_EditorClassIdentifier:
godMode: 0
movementSpeed: 4
--- !u!50 &6843575828445418653
Rigidbody2D:

View file

@ -136,75 +136,6 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 6843575829779162303}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &2013742123
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 1896072655702505677, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_Name
value: BombSpawner
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_RootOrder
value: 7
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1896072655702505676, guid: c9b7903151183144dbf30c9b2ab371b2,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c9b7903151183144dbf30c9b2ab371b2, type: 3}
--- !u!1001 &158905819792232859
PrefabInstance:
m_ObjectHideFlags: 0
@ -212,6 +143,14 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 0}
propertyPath: boardHolder
value:
objectReference: {fileID: 585332173}
- target: {fileID: 0}
propertyPath: startPosition
value:
objectReference: {fileID: 185746015}
- target: {fileID: 158905820630581597, guid: fdaa7451ab31b477b930076bbc888bab,
type: 3}
propertyPath: m_Name
@ -272,14 +211,6 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 0}
propertyPath: boardHolder
value:
objectReference: {fileID: 585332173}
- target: {fileID: 0}
propertyPath: startPosition
value:
objectReference: {fileID: 185746015}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: fdaa7451ab31b477b930076bbc888bab, type: 3}
--- !u!1001 &485484359436847253
@ -401,7 +332,7 @@ PrefabInstance:
- target: {fileID: 2219516564235934920, guid: 4ca131020414f4b1597dac2e1d292d7f,
type: 3}
propertyPath: m_RootOrder
value: 8
value: 7
objectReference: {fileID: 0}
- target: {fileID: 2219516564235934920, guid: 4ca131020414f4b1597dac2e1d292d7f,
type: 3}

View file

@ -30,7 +30,7 @@ namespace src.Ammo
private IEnumerator _startCameraShake()
{
SetCameraNoise(amplitudeGain, frequencyGain);
yield return new WaitForSeconds(_bombsUtilManager.ExplosionDuration);
yield return new WaitForSeconds(_bombsUtilManager.explosionDuration);
SetCameraNoise(0, 0);
}

View file

@ -18,7 +18,7 @@ namespace src.Ammo
void Start()
{
_cameraShake = GameObject.Find("VCAM1").GetComponent<BombCameraShake>();
Invoke(nameof(Explode), _bombsUtil.Timer);
Invoke(nameof(Explode), _bombsUtil.timer);
}
void Explode()
@ -42,7 +42,7 @@ namespace src.Ammo
private IEnumerator CreateExplosions(Vector3 direction)
{
var currentPosition = transform.position;
for (var i = 1; i < _bombsUtil.Power; i++)
for (var i = 1; i < _bombsUtil.power; i++)
{
var hit = Physics2D.Raycast(new Vector2(currentPosition.x + 0.5f,
currentPosition.y + 0.5f), direction, i, 1 << 8);
@ -54,7 +54,6 @@ namespace src.Ammo
}
else
{
Debug.Log("Hit something");
var key = hit.collider.GetComponent<IExplosable>();
key?.OnExplosion();
break;
@ -79,8 +78,9 @@ namespace src.Ammo
}
public void OnDestroy()
{
_bombsUtil.RemoveBomb(transform.position);
{
DebugHelper.LogError("UNREGISTERED BOMB");
_bombsUtil.UnregisterBomb(transform.position);
}
}
}

View file

@ -1,15 +1,16 @@
using src.Base;
using src.Helpers;
using src.Managers;
namespace src.Ammo
{
public class Explosion : GameplayComponent
public class BombExplosion : GameplayComponent
{
private readonly BombsUtilManager _bombUtil = BombsUtilManager.instance;
public void Start()
{
Destroy(gameObject, _bombUtil.ExplosionDuration);
Destroy(gameObject, _bombUtil.explosionDuration);
}
}
}

View file

@ -1,32 +0,0 @@
using System;
using src.Base;
using src.Managers;
using UnityEngine;
namespace src.Ammo
{
public class BombsSpawner : GameplayComponent
{
public GameObject bombPrefab;
private BombsUtilManager _bombsUtil;
public void Start()
{
_bombsUtil = BombsUtilManager.instance;
}
public void PlaceBomb(Transform location)
{
var position1 = location.position;
var absX = Mathf.RoundToInt(position1.x);
var absY = Mathf.RoundToInt(position1.y);
var position = new Vector2(absX, absY);
if (_bombsUtil.CanPlaceBomb(position))
{
Instantiate(bombPrefab, position, Quaternion.identity);
_bombsUtil.PlaceBomb(position);
}
}
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cd3bf3610d18aa14e805185b5e3f2d4b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -25,6 +25,10 @@ namespace src.Helpers
Resources.Load<GameObject>("Ammo/FlameUpgrade");
public static readonly GameObject GoldenBombUpgrade =
Resources.Load<GameObject>("GoldenBombUpgrade/FlameUpgrade");
/* Items */
public static readonly GameObject PlayerBomb =
Resources.Load<GameObject>("Ammo/Bomb");
/* Enemies */
public static readonly GameObject GreenEnemy = Resources.Load<GameObject>("Enemies/SnowEnemyRandom");

View file

@ -6,22 +6,18 @@ namespace src.Managers
{
public sealed class BombsUtilManager : GameplayComponent
{
private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
public static BombsUtilManager instance;
public int power = 3;
public int allowedBombs = 2;
public int placedBombs = 0;
public float timer = 3.0f;
public float explosionDuration = 0.55f;
private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
private const int MaxPower = 7;
private const int MaxAllowedBombs = 10;
public int Power { get; private set; } = 3;
public int AllowedBombs { get; private set; } = 2;
public int PlacedBombs { get; private set; } = 0;
public float Timer { get; } = 3.0f;
public float ExplosionDuration { get; } = 0.55f;
public static BombsUtilManager instance;
public void Awake()
{
@ -37,41 +33,41 @@ namespace src.Managers
public void IncreasePower()
{
if (Power <= MaxPower)
if (power <= MaxPower)
{
Power++;
power++;
}
}
public void IncreaseAllowedBombs()
{
if (AllowedBombs <= MaxAllowedBombs)
if (allowedBombs <= MaxAllowedBombs)
{
AllowedBombs++;
allowedBombs++;
}
}
public void PlaceBomb(Vector3 position)
public void RegisterBomb(Vector3 position)
{
if (CanPlaceBomb(position))
{
PlacedBombs++;
placedBombs++;
_usedPosition.Add(position);
}
}
public void RemoveBomb(Vector3 position)
public void UnregisterBomb(Vector3 position)
{
if (_usedPosition.Contains(position))
{
PlacedBombs--;
placedBombs--;
_usedPosition.Remove(position);
}
}
public bool CanPlaceBomb(Vector3 position)
{
return (!_usedPosition.Contains(position) && (PlacedBombs < AllowedBombs));
return (!_usedPosition.Contains(position) && (placedBombs < allowedBombs));
}
}
}

View file

@ -1,39 +1,47 @@
using src.Ammo;
using System;
using System.ComponentModel;
using src.Ammo;
using src.Base;
using src.Helpers;
using src.Managers;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace src.Player
{
public class PlayerController : GameplayComponent, IExplosable
{
public bool godMode;
public float movementSpeed = 4f;
/* Components */
private GameStateManager _gameStateManager;
private Rigidbody2D _rigidbody2d;
private Collider2D _collider2D;
private Transform _respawnPosition;
private BombsSpawner _bombsSpawner;
private BombsUtilManager _bombsUtil;
private Animator _animator;
private PlayerUpgrade _playerUpgrade;
/* Variables */
private bool _isDead;
/* Animator Variables*/
private static readonly int AnimHorizontal = Animator.StringToHash("AnimHorizontal");
private static readonly int AnimVertical = Animator.StringToHash("AnimVertical");
private static readonly int AnimDeath = Animator.StringToHash("AnimDeath");
public bool godMode;
public float movementSpeed = 4f;
private bool _isDead;
protected void Start()
{
_gameStateManager = GameStateManager.instance;
_playerUpgrade = PlayerUpgrade.instance;
_bombsUtil = BombsUtilManager.instance;
_rigidbody2d = GetComponent<Rigidbody2D>();
_collider2D = GetComponent<Collider2D>();
_animator = GetComponentInChildren<Animator>();
_respawnPosition = GameObject.Find("RespawnPosition").transform;
_bombsSpawner = GameObject.Find("BombSpawner").GetComponent<BombsSpawner>();
movementSpeed = _playerUpgrade.GetMovementSpeed();
_playerUpgrade.PlayerSpeed += OnSpeedUpgrade;
@ -53,15 +61,50 @@ namespace src.Player
private void Update()
{
if (_gameStateManager.IsGamePaused || _gameStateManager.IsPlayerMovementForbidden || _isDead) {return;}
HandleBombInput();
HandleItemsInput();
}
private void HandleMovementInput()
{
#if UNITY_STANDALONE || UNITY_WEBGL || UNITY_EDITOR
HandleKeyboardMovement();
#elif UNITY_IOS || UNITY_ANDROID
HandleTouchMovement();
#elif UNITY_PS4 || UNITY_XBOXONE
HandlerControllerMovement();
#endif
}
private void HandleItemsInput()
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
if (Input.GetKeyDown(KeyCode.Space))
{
PlaceBomb();
}
#elif UNITY_IOS || UNITY_ANDROID
if (CrossPlatformInputManager.GetButton("PlaceBomb"))
{
PlaceBomb();
}
#elif UNITY_PS4 || UNITY_XBOXONE
// Console bomb placement is not supported yet.
#endif
}
#if UNITY_STANDALONE || UNITY_WEBGL || UNITY_EDITOR
private void HandleKeyboardMovement()
{
var horizontal = Input.GetAxisRaw("Horizontal");
var vertical = Input.GetAxisRaw("Vertical");
#elif UNITY_IOS || UNITY_ANDROID
var movementVector = new Vector2(horizontal, vertical);
MovePosition(movementVector);
}
#endif
#if UNITY_IOS || UNITY_ANDROID
private void HandleTouchMovement()
{
var horizontal = 0;
var vertical = 0;
if (CrossPlatformInputManager.GetButton("MoveUp"))
@ -80,50 +123,60 @@ namespace src.Player
{
horizontal = -1;
}
#elif UNITY_PS4 || UNITY_XBOXONE
// // Console movement is not supported yet.
var movementVector = new Vector2(horizontal, vertical);
MovePosition(movementVector);
}
#endif
var movementVector = new Vector2(horizontal, vertical).NormalizeToCross();
#if UNITY_PS4 || UNITY_XBOXONE
private void HandlerControllerMovement()
{
throw new NotImplementedException();
}
#endif
private void MovePosition(Vector2 movementVector)
{
movementVector = movementVector.NormalizeToCross();
_animator.SetFloat(AnimHorizontal, movementVector.x);
_animator.SetFloat(AnimVertical, movementVector.y);
_rigidbody2d.MovePosition(_rigidbody2d.position + movementSpeed * Time.deltaTime * movementVector);
}
private void PlaceBomb()
{
_bombsSpawner.PlaceBomb(transform);
var position = transform.position;
var absX = Mathf.RoundToInt(position.x);
var absY = Mathf.RoundToInt(position.y);
var newPosition = new Vector2(absX, absY);
if (!_bombsUtil.CanPlaceBomb(newPosition)) return;
Instantiate(PrefabAtlas.PlayerBomb, newPosition, Quaternion.identity);
_bombsUtil.RegisterBomb(newPosition);
}
private void HandleBombInput()
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
if (Input.GetKeyDown(KeyCode.Space))
{
PlaceBomb();
}
#elif UNITY_IOS || UNITY_ANDROID
if (CrossPlatformInputManager.GetButton("PlaceBomb"))
{
PlaceBomb();
}
#elif UNITY_PS4 || UNITY_XBOXONE
// Console bomb placement is not supported yet.
#endif
}
public void Respawn()
{
DebugHelper.LogInfo("Player is respawning!");
DebugHelper.LogInfo("Player is re-spawning!");
transform.SetPositionAndRotation(_respawnPosition.position, Quaternion.identity);
_animator.Play("IdleDown");
}
private void Die()
{
if (godMode)
{
return;
}
_isDead = true;
_collider2D.enabled = false;
_animator.SetBool(AnimDeath, true);
Destroy(gameObject, 0.7f);
}
public void OnTriggerExit2D(Collider2D other)
{
/* Turn off bomb trigger making it so you can't pass through. */
if (other.CompareTag("Bomb"))
{
other.isTrigger = false;
@ -142,18 +195,6 @@ namespace src.Player
}
}
private void Die()
{
if (godMode)
{
return;
}
_isDead = true;
_collider2D.enabled = false;
_animator.SetBool(AnimDeath, true);
Destroy(gameObject, 0.7f);
}
public void OnExplosion()
{
DebugHelper.LogInfo("Player hit by explosion");