using System; using UnityEngine; using Com.LuisPedroFonseca.ProCamera2D; namespace DoomBreakers { public enum CompareTags //int id must align with SetupCompareTags() contents. { Player = 0, Player2 = 1, Player3 = 2, Player4 = 3, Enemy = 4, Item = 5, Container = 6, Health = 7, Currency = 8, Arrow = 9, Platform = 10, Rest = 11 }; public enum C2D { PlayerCollider2D = 0, EquipmentCollider2D = 1, HealthCollider2D = 2, CurrencyCollider2D = 3 };//_colliderIdentities2D[]; //_collider2d, _equipCollider2d, _healthCollider2d; public enum CurrencyCollision { GoldCoin = 0, Ruby = 1, Sapphire = 2, Emerald = 3, Diamond = 4 }; public enum HealthCollision { Apple = 0, Chicken = 1, Fish = 2, }; public class PlayerCollision : MonoBehaviour//, IPlayerCollision { private Transform _transform; private int _playerID; private CompareTags _compareTags; private Collider2D[] _colliderIdentities2D;// = new Collider2D[4]; private Collider2D[] _enemyTargetsHit; private Transform[] _attackPoints; //1=quickATK, 2=powerATK, 3=upwardATK private float[] _attackRadius;// = new float[3]; //1=quickATK, 2=powerATK, 3=upwardATK private Vector3 _vector; //We simply use this cached vector to flip attack points when needed. See FlipAttackPoints(int dir) //private Vector3 _velocity; private LayerMask[] _enemyLayerMasks;// = new LayerMask[2]; private string _playerLayerMaskStr;// = "Player"; private string _enemyLayerMaskStr;// = "Enemy"; //private const string _portalLayerMaskStr = "Portal"; //private const string _propsLayerMaskStr = "Items"; private string _healthTagStrCached;// = "Health"; private string _currencyTagStrCached;// = "Currency"; private string _fallenTagStrCached;// = "FallenFlag"; private string _campsiteTagStrCached;// = "CampsiteFlag"; private string _portalTagStrCached;// = "PortalFlag"; private string _propTagStrCached;// = "Prop"; private string _endActTagStrCached;// = "EndActFlag"; private string _npcTagStrCached;// = "NPC"; private string _waterTagStrCached;// = "Water"; private string[] _compareTagStrings;// = new string[10]; //private GameObject[] timerObjs = new GameObject[2]; private GameObject _timerObjA, _timerObjB; private ITimer _ladderPartCollisionCheckTimer; private const float _ladderCollisionWaitTime = 0.5f; private ITimer _attackCollisionCheckExceptionTimer; private const float _attackCollisionCheckExceptionWaitTime = 0.1f; private bool _attackCollisionEnabled; private bool _equipCollisionEnabled; private bool _equipPickupEnabled; private bool _equipPickupPossible; private bool _healthPickupEnabled; private bool _currencyPickupEnabled; private bool _ladderGrabPossible; private bool _ladderCollidedEnabled; private bool _npcTalkPossible; private bool _npcTalkCollidedEnabled; private bool _attackCollisionCheckExceptionFlag; private bool _waterCollisionEnabled; private CurrencyCollision _currencyPickupFlag; private HealthCollision _healthPickupFlag; private PlayerStateMachine _stateMachine; private IPlayerSprite _playerSprite; private IPlayerEquipment _playerEquipment; private PlayerStats _playerStats; //public PlayerCollision(Collider2D collider2D, ref Transform[] arrayAtkPoints) { } public void Setup(Collider2D collider2D, ref Transform[] arrayAtkPoints, int playerId, ref Transform t,ref PlayerStats playerStat) { _colliderIdentities2D = new Collider2D[4]; _playerLayerMaskStr = "Player"; _enemyLayerMaskStr = "Enemy"; _healthTagStrCached = "Health"; _currencyTagStrCached = "Currency"; _fallenTagStrCached = "FallenFlag"; _campsiteTagStrCached = "CampsiteFlag"; _portalTagStrCached = "PortalFlag"; _propTagStrCached = "Prop"; _endActTagStrCached = "EndActFlag"; _npcTagStrCached = "NPC"; _waterTagStrCached = "Water"; _transform = t; _playerID = playerId; _playerStats = playerStat; SetupCollider2D(collider2D); _attackPoints = arrayAtkPoints; SetupLayerMasks(); SetupAttackRadius(); SetupCompareTags(); //_collider2d.enabled = true; _attackCollisionEnabled = false; _equipCollisionEnabled = false; _equipPickupEnabled = false; _equipPickupPossible = false; _healthPickupEnabled = false; _currencyPickupEnabled = false; _ladderGrabPossible = false; _ladderCollidedEnabled = false; _npcTalkPossible = false; _ladderCollidedEnabled = false; _attackCollisionCheckExceptionFlag = false; _waterCollisionEnabled = false; _timerObjA = new GameObject(); _timerObjA.AddComponent(); _ladderPartCollisionCheckTimer = _timerObjA.GetComponent(); //_ladderPartCollisionCheckTimer = new Timer(); _timerObjB = new GameObject(); _timerObjB.AddComponent(); _attackCollisionCheckExceptionTimer = _timerObjB.GetComponent(); //_attackCollisionCheckExceptionTimer = new Timer(); } private void SetupCollider2D(Collider2D collider2D) { SetCollider2DIdentity(collider2D, C2D.PlayerCollider2D); //_collider2d = collider2D; SetCollider2DIdentity(collider2D, C2D.EquipmentCollider2D);//_colliderIdentities2D[1] = null; //C2D.EquipmentCollider2D SetCollider2DIdentity(collider2D, C2D.HealthCollider2D);//_colliderIdentities2D[2] = null; //C2D.HealthCollider2D SetCollider2DIdentity(collider2D, C2D.CurrencyCollider2D);//_colliderIdentities2D[3] = null; //C2D.CurrencyCollider2D; } public void SetupLayerMasks() { _enemyLayerMasks = new LayerMask[3]; _enemyLayerMasks[0] = LayerMask.NameToLayer(_playerLayerMaskStr); _enemyLayerMasks[1] = LayerMask.NameToLayer(_enemyLayerMaskStr); //_enemyLayerMasks[2] = LayerMask.NameToLayer(_enemyLayerMaskStr); //_enemyLayerMasks[2] = LayerMask.NameToLayer(_propsLayerMaskStr); //_enemyLayerMasks[0] = LayerMask.GetMask(_playerLayerMaskStr); //_enemyLayerMasks[1] = LayerMask.GetMask(_enemyLayerMaskStr); } public void SetupAttackRadius() { _attackRadius = new float[4]; _attackRadius[0] = 1.2f; //quick attack radius _attackRadius[1] = 1.6f; //power attack radius _attackRadius[2] = 1.3f; //upward attack radius _attackRadius[3] = 1.0f; //downward attack radius } public void SetupCompareTags() { _compareTagStrings = new string[12]; _compareTagStrings[0] = "Player"; _compareTagStrings[1] = "Player2"; _compareTagStrings[2] = "Player3"; _compareTagStrings[3] = "Player4"; _compareTagStrings[4] = "Enemy"; _compareTagStrings[5] = "Item"; _compareTagStrings[6] = "Container"; _compareTagStrings[7] = "Health"; _compareTagStrings[8] = "Currency";//oops _compareTagStrings[9] = "Arrow"; _compareTagStrings[10] = "Portal"; _compareTagStrings[11] = "Ladder"; } public string GetCompareTag(CompareTags compareTagId) => _compareTagStrings[(int)compareTagId]; private Collider2D GetCollider2DIdentity(C2D colliderIdentity) => _colliderIdentities2D[(int)colliderIdentity]; private void SetCollider2DIdentity(Collider2D collider2D, C2D colliderIdentity) => _colliderIdentities2D[(int)colliderIdentity] = collider2D; void Start() { } void Update() { } public void UpdateCollision(ref BaseState playerState, ref PlayerStateMachine stateMachine, ref Vector3 velocity, int playerId, ref PlayerAnimator playerAnimator ,ref IPlayerEquipment playerEquipment, ref IPlayerSprite playerSprite, ref PlayerStats playerStat, bool storyTell) { //if (_currencyPickupEnabled) ProcessCollisionWithGoldCoin(_colliderIdentities2D[3], ref _playerStats); //_velocity = velocity; _stateMachine = stateMachine; _playerSprite = playerSprite; UpdateAttackCollisionCheckExceptions(ref playerState); UpdateEquipmentGained(ref stateMachine, ref velocity, ref playerAnimator, ref playerSprite, ref playerEquipment, storyTell); UpdateDetectEnemyTargets(ref playerState, playerId, ref playerSprite); ProcessEquipmentCollisions(); ProcessShopEquipCollisions(playerId); UpdateEquipmentTargets(ref playerEquipment); ProcessHealthCollisions(ref playerStat, ref playerSprite); //if (_currencyPickupEnabled) ProcessCollisionWithGoldCoin(_colliderIdentities2D[3], ref playerStat); ProcessCurrencyCollisions(ref playerStat); } private void UpdateEquipmentGained(ref PlayerStateMachine stateMachine, ref Vector3 velocity, ref PlayerAnimator playerAnimator, ref IPlayerSprite playerSprite, ref IPlayerEquipment playerEquipment, bool storyTell) { if (playerEquipment.NewEquipmentGained()) { ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_ArmorObtainedFX, _transform, _playerID, playerSprite.GetSpriteDirection()); AudioEventManager.PlayPlayerSFX(PlayerSFXID.PlayerEquippedSFX); stateMachine.SetState(PlayerStatePool._instance.GetPlayerGainedEquipmentObjState(stateMachine, velocity, _transform));//new PlayerGainedEquipment(stateMachine, velocity, _transform)); playerAnimator.SetAnimatorController(ref _playerEquipment); _playerEquipment.NewEquipmentGained(false); } if (SignalItemPickupCollision()) { playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Pickup); return; } if (SignalLadderGrabCollision()) { playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Pickup); return; } if (SignalNPCTalkCollision()) { playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Talk); return; } if (!SignalItemPickupCollision()) { if (!storyTell) playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Idle); if (storyTell) playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Inactive); return; } if (!SignalLadderGrabCollision()) { playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Idle); return; } if (!SignalNPCTalkCollision()) { playerAnimator.PlayIndicatorAnimation(IndicatorAnimID.Idle); return; } } private void UpdateAttackCollisionCheckExceptions(ref BaseState playerState) { //This ensures enemies take damage while in PlayerDownwardAttack state but at a more reasonable damage rate, using timer checks. if (playerState.GetType() == typeof(PlayerDownwardAttack)) { if (_attackCollisionCheckExceptionFlag) { if (_attackCollisionCheckExceptionTimer.HasTimerFinished()) { _attackCollisionEnabled = true; _attackCollisionCheckExceptionFlag = false; } return; } if (!_attackCollisionCheckExceptionFlag) //if (!_attackCollisionEnabled) { _attackCollisionEnabled = true; _attackCollisionCheckExceptionFlag = true;//_attackCollisionEnabled = true; _attackCollisionCheckExceptionTimer.Reset(); _attackCollisionCheckExceptionTimer.StartTimer(_attackCollisionCheckExceptionWaitTime); return; } } return; } public void UpdateDetectEnemyTargets(ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (!_attackCollisionEnabled) return; for (int i = 0; i < _enemyLayerMasks.Length; i++) { DetermineCollisionPurpose(ref playerState, i); if (_enemyTargetsHit == null) break; foreach (Collider2D enemy in _enemyTargetsHit) { if (enemy.CompareTag(GetCompareTag(CompareTags.Player))) { } if (enemy.CompareTag(GetCompareTag(CompareTags.Player2))) { } if (enemy.CompareTag(GetCompareTag(CompareTags.Player3))) { } if (enemy.CompareTag(GetCompareTag(CompareTags.Player4))) { } if (enemy.CompareTag(GetCompareTag(CompareTags.Enemy))) { ProcessAttackCollisionSkeleton(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionSkeletonWarlock(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionBatSkully(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionBandit(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionBanditArcher(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionGoblin(enemy, ref playerState, playerId, ref playerSprite); ProcessAttackCollisionTroll(enemy, ref playerState, playerId, ref playerSprite); } if (enemy.CompareTag(GetCompareTag(CompareTags.Container))) { ProcessCollisionWithBarrel(enemy, ref playerSprite); ProcessCollisionWithPunchBag(enemy, ref playerSprite); ProcessCollisionWithAppleTree(enemy, ref playerSprite); ProcessCollisionWithSeveredHead(enemy, ref playerSprite, ref playerState); } if (enemy.CompareTag("Portal")) { ProcessAttackCollisionPortal(enemy, ref playerState); } if (enemy.CompareTag("Prop")) { ProcessCollisionWitLadder(enemy, ref playerSprite); } } } _attackCollisionEnabled = false; } private void DetermineCollisionPurpose(ref BaseState playerState, int i) { if (playerState.GetType() == typeof(PlayerQuickAttack)) { _enemyTargetsHit = Physics2D.OverlapCircleAll(_attackPoints[0].position, _attackRadius[0], LayerMask.GetMask(_enemyLayerMaskStr)); return; } if (playerState.GetType() == typeof(PlayerKnockAttack)) { _enemyTargetsHit = Physics2D.OverlapCircleAll(_attackPoints[0].position, _attackRadius[0], LayerMask.GetMask(_enemyLayerMaskStr)); return; } if (playerState.GetType() == typeof(PlayerReleaseAttack)) { _enemyTargetsHit = Physics2D.OverlapCircleAll(_attackPoints[1].position, _attackRadius[1], LayerMask.GetMask(_enemyLayerMaskStr)); return; } if (playerState.GetType() == typeof(PlayerUpwardAttack)) { _enemyTargetsHit = Physics2D.OverlapCircleAll(_attackPoints[2].position, _attackRadius[2], LayerMask.GetMask(_enemyLayerMaskStr));//_enemyLayerMasks[i]); return; } if (playerState.GetType() == typeof(PlayerDownwardAttack)) { _enemyTargetsHit = Physics2D.OverlapCircleAll(_attackPoints[3].position, _attackRadius[3], LayerMask.GetMask(_enemyLayerMaskStr));//_enemyLayerMasks[i]); return; } } private void ProcessAttackCollisionPortal(Collider2D enemy, ref BaseState playerState) { if (enemy.GetComponent() == null) //Guard clause. return; enemy.GetComponent().Hit(ref playerState, _transform.position, _playerID, ref _playerEquipment); } private void ProcessAttackCollisionSkeleton(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int skellyID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithSkeleton" + skellyID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionSkeletonWarlock(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int skellyID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithSkeletonWarlock" + skellyID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionBatSkully(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; if (enemy.GetComponent().IgnoreCollisionDamage()) return; int BatSkullyID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithPlayerForBatSkully" + BatSkullyID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionBandit(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int banditID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithBandit" + banditID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionGoblin(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int goblinID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithGoblin" + goblinID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionTroll(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int trollID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithTroll" + trollID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionBanditArcher(Collider2D enemy, ref BaseState playerState, int playerId, ref IPlayerSprite playerSprite) { if (enemy.GetComponent() == null) //Guard clause. return; int banditID = enemy.GetComponent()._enemyID; BattleColliderManager.AssignCollisionDetails("ReportCollisionWithBanditArcher" + banditID.ToString(), ref playerState, playerId, playerSprite, _playerEquipment.GetWeapon(), ref _playerStats); UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); } private void ProcessAttackCollisionArrow(Collider2D enemy) { if (enemy.GetComponent() == null) //Guard clause. return; //BattleColliderManager.TriggerEvent("ReportCollisionWithArrow"); } public void UpdateEquipmentTargets(ref IPlayerEquipment playerEquipment) { if (_playerEquipment == null) _playerEquipment = playerEquipment; //For ProcessCollisionFlags() Item use. if (!_equipCollisionEnabled) return; playerEquipment = _playerEquipment; //Any changes made apply to original parent class, Player.cs. playerEquipment.NewEquipmentGained(true); UIPlayerManager.SetPlayerEquipment(ref playerEquipment, _playerID); _equipCollisionEnabled = false; SignalItemPickupCollision(false); EnableItemPickupCollision(false); } private void ProcessEquipmentCollisions() { if (!_equipPickupPossible) return; if (!_equipPickupEnabled) return; ProcessItemCollisionFlags(GetCollider2DIdentity(C2D.EquipmentCollider2D));//_equipCollider2d } private void ProcessShopEquipCollisions(int playerId) { if (!UIShopkeepManager.IsShopkeepSelling()) return; if (!_equipPickupPossible) return; if (!_equipPickupEnabled) return; //ProcessItemCollisionFlags(GetCollider2DIdentity(C2D.EquipmentCollider2D));//_equipCollider2d ProcessCollisionWithShopSword(GetCollider2DIdentity(C2D.EquipmentCollider2D), playerId); ProcessCollisionWithShopMace(GetCollider2DIdentity(C2D.EquipmentCollider2D), playerId); ProcessCollisionWithShopShield(GetCollider2DIdentity(C2D.EquipmentCollider2D), playerId); ProcessCollisionWithShopArmor(GetCollider2DIdentity(C2D.EquipmentCollider2D), playerId); _equipCollisionEnabled = false; SignalItemPickupCollision(false); EnableItemPickupCollision(false); } public void ProcessItemCollisionFlags(Collider2D collision) { if (collision.CompareTag(GetCompareTag(CompareTags.Item))) { ProcessCollisionWithSword(collision); ProcessCollisionWithMace(collision); ProcessCollisionWithShield(collision); ProcessCollisionWithArmor(collision); } } private void ProcessCollisionWithSword(Collider2D collision) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a Sword. Get outta here! Sword tempSword = collision.GetComponent(); if (_playerEquipment.ApplyEquipment(tempSword)) { _playerStats.WeaponDamage = tempSword.Damage(); collision.GetComponent().Destroy(); _equipCollisionEnabled = true; //Flag so we update players equipment. } return; } private void ProcessCollisionWithMace(Collider2D collision) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a Mace. Get outta here! Mace tempMace = collision.GetComponent(); if (_playerEquipment.ApplyEquipment(tempMace)) { _playerStats.WeaponDamage = tempMace.Damage(); collision.GetComponent().Destroy(); _equipCollisionEnabled = true; //Flag so we update players equipment. } return; } private void ProcessCollisionWithShield(Collider2D collision) { if (collision.GetComponent() == null) return; //Then NOT a Shield. Get outta here! if(_playerEquipment.ApplyEquipment(collision.GetComponent())) { //UIPlayerManager.TriggerEvent("ReportUIPlayerEquipEvent", UIAnimationFlag.UILeftHandShield, _playerID); collision.GetComponent().Destroy(); _equipCollisionEnabled = true; } return; } private void ProcessCollisionWithArmor(Collider2D collision) { if (collision.GetComponent() == null) return; //Then NOT a Armor. Get outta here! if(_playerEquipment.ApplyEquipment(collision.GetComponent())) { //UIPlayerManager.TriggerEvent("ReportUIPlayerEquipEvent", UIAnimationFlag.Sword, _playerID); collision.GetComponent().Destroy(); _equipCollisionEnabled = true; } return; } private void ProcessCollisionWithShopSword(Collider2D collision, int playerId) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a ShopSword. Get outta here! if (!UIShopkeepManager.RunBuyerPlayerOption(playerId, collision.GetComponent().GetEquipID())) return; if (!collision.GetComponent().IsGravityDisabled()) { collision.GetComponent().EnableGravity(); //collision.GetComponent().SetBuyerPlayerID(playerId); return; } //EquipmentWeaponType equipWeapType = collision.GetComponent()._weaponType; //EquipmentMaterialType equipMatType = collision.GetComponent()._materialType; //Sword tempSword = new Sword(equipWeapType, equipMatType);//= collision.GetComponent(); //if (_playerEquipment.ApplyEquipment(tempSword)) //{ // _playerStats.WeaponDamage = tempSword.Damage(); // //collision.GetComponent().Destroy(); // _equipCollisionEnabled = true; //Flag so we update players equipment. //} return; } private void ProcessCollisionWithShopMace(Collider2D collision, int playerId) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a ShopMace. Get outta here! if (!UIShopkeepManager.RunBuyerPlayerOption(playerId, collision.GetComponent().GetEquipID())) return; if (!collision.GetComponent().IsGravityDisabled()) { collision.GetComponent().EnableGravity(); //collision.GetComponent().SetBuyerPlayerID(playerId); return; } return; } private void ProcessCollisionWithShopShield(Collider2D collision, int playerId) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a ShopShield. Get outta here! if (!UIShopkeepManager.RunBuyerPlayerOption(playerId, collision.GetComponent().GetEquipID())) return; if (!collision.GetComponent().IsGravityDisabled()) { collision.GetComponent().EnableGravity(); //collision.GetComponent().SetBuyerPlayerID(playerId); return; } return; } private void ProcessCollisionWithShopArmor(Collider2D collision, int playerId) { if (collision.GetComponent() == null)//Exists on Items Layer, Tag=Item return; //Then NOT a ShopBreastplate. Get outta here! //UIShopkeepManager.SetBuyerPlayerID(playerId); if (!UIShopkeepManager.RunBuyerPlayerOption(playerId, collision.GetComponent().GetEquipID())) return; if (!collision.GetComponent().IsGravityDisabled()) { collision.GetComponent().EnableGravity(); //collision.GetComponent().SetBuyerPlayerID(playerId); return; } return; } private void ProcessHealthCollisions(ref PlayerStats playerStat, ref IPlayerSprite playerSprite) { if (!_healthPickupEnabled) return; //if (GetCollider2DIdentity(C2D.HealthCollider2D) == null) return;//_healthCollider2d //if (GetCollider2DIdentity(C2D.HealthCollider2D).CompareTag(GetCompareTag(CompareTags.Health))) //{ if (_healthPickupFlag == HealthCollision.Apple) ProcessCollisionWithApple(_colliderIdentities2D[2], ref playerStat, ref playerSprite); if (_healthPickupFlag == HealthCollision.Chicken) ProcessCollisionWithChicken(_colliderIdentities2D[2], ref playerStat, ref playerSprite); if (_healthPickupFlag == HealthCollision.Fish) ProcessCollisionWithFish(_colliderIdentities2D[2], ref playerStat, ref playerSprite); //SetCollider2DIdentity(null, C2D.HealthCollider2D);//_healthCollider2d = null; //} } private void ProcessCollisionWithApple(Collider2D collision,ref PlayerStats playerStat, ref IPlayerSprite playerSprite) { if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Health return; //Then NOT a Apple. Get outta here! if (playerStat.Health != playerStat.GetMaxHealthLimit()) { double healPoints = collision.GetComponent().Health(); playerStat.Health += healPoints; playerStat.SetRecentHealItemType(HealingItemType.Apple); playerSprite.SetBehaviourTextureFlash(0.75f, Color.green); UIPlayerManager.TriggerEvent("ReportUIPlayerStatEvent", ref playerStat, _playerID); collision.GetComponent().DestroyThis(); AudioEventManager.PlayPropSFX(PropSFXID.PropHealUpSFX); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_HealingFX, _transform, _playerID, playerSprite.GetSpriteDirection()); _playerStats = playerStat; _colliderIdentities2D[2] = null; } _healthPickupEnabled = false; return; } private void ProcessCollisionWithChicken(Collider2D collision, ref PlayerStats playerStat, ref IPlayerSprite playerSprite) { //if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Health return; //Then NOT a Chicken. Get outta here! if (playerStat.Health != playerStat.GetMaxHealthLimit()) { double healPoints = collision.GetComponent().Health(); playerStat.Health += healPoints; playerStat.SetRecentHealItemType(HealingItemType.Chicken); playerSprite.SetBehaviourTextureFlash(0.75f, Color.green); UIPlayerManager.TriggerEvent("ReportUIPlayerStatEvent", ref playerStat, _playerID); collision.GetComponent().DestroyThis(); AudioEventManager.PlayPropSFX(PropSFXID.PropHealUpSFX); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_HealingFX, _transform, _playerID, playerSprite.GetSpriteDirection()); _playerStats = playerStat; _colliderIdentities2D[2] = null; } _healthPickupEnabled = false; return; } private void ProcessCollisionWithFish(Collider2D collision, ref PlayerStats playerStat, ref IPlayerSprite playerSprite) { //if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Health return; //Then NOT a Fish. Get outta here! if (playerStat.Health != playerStat.GetMaxHealthLimit()) { double healPoints = collision.GetComponent().Health(); playerStat.Health += healPoints; playerStat.SetRecentHealItemType(HealingItemType.Fish); playerSprite.SetBehaviourTextureFlash(0.75f, Color.green); UIPlayerManager.TriggerEvent("ReportUIPlayerStatEvent", ref playerStat, _playerID); collision.GetComponent().DestroyThis(); AudioEventManager.PlayPropSFX(PropSFXID.PropHealUpSFX); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_HealingFX, _transform, _playerID, playerSprite.GetSpriteDirection()); _playerStats = playerStat; _colliderIdentities2D[2] = null; } _healthPickupEnabled = false; return; } private void ProcessCurrencyCollisions(ref PlayerStats playerStat) { if (!_currencyPickupEnabled) return; //if (GetCollider2DIdentity(C2D.CurrencyCollider2D).CompareTag(GetCompareTag(CompareTags.Currency))) //if (_colliderIdentities2D[3].CompareTag("Currency")) //{ if (_currencyPickupFlag == CurrencyCollision.GoldCoin) ProcessCollisionWithGoldCoin(_colliderIdentities2D[3], ref playerStat); if (_currencyPickupFlag == CurrencyCollision.Ruby) ProcessCollisionWithRuby(_colliderIdentities2D[3], ref playerStat); if (_currencyPickupFlag == CurrencyCollision.Sapphire) ProcessCollisionWithSapphire(_colliderIdentities2D[3], ref playerStat); if (_currencyPickupFlag == CurrencyCollision.Emerald) ProcessCollisionWithEmerald(_colliderIdentities2D[3], ref playerStat); if (_currencyPickupFlag == CurrencyCollision.Diamond) ProcessCollisionWithDiamond(_colliderIdentities2D[3], ref playerStat); //SetCollider2DIdentity(null, C2D.CurrencyCollider2D); //} } private void ProcessCollisionWithGoldCoin(Collider2D collision, ref PlayerStats playerStat) { //if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Currency return; //Then NOT a GoldCoin. Get outta here! int currencyValue = 1;// collision.GetComponent().Amount(); playerStat.Currency += currencyValue; UIPlayerManager.TriggerEvent("ReportUIPlayerGoldscoreEvent", ref playerStat, _playerID); //collision.GetComponent().DestroyThis(); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_CurrencyFX, _transform, _playerID, 1); AudioEventManager.PlayPropSFX(PropSFXID.PropCoinPickSFX); _currencyPickupEnabled = false; _playerStats = playerStat; _colliderIdentities2D[3] = null; return; } private void ProcessCollisionWithRuby(Collider2D collision, ref PlayerStats playerStat) { if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Currency return; //Then NOT a Ruby. Get outta here! int currencyValue = 3;// collision.GetComponent().Amount(); playerStat.Currency += currencyValue; UIPlayerManager.TriggerEvent("ReportUIPlayerGoldscoreEvent", ref playerStat, _playerID); //collision.GetComponent().Destroy(); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_CurrencyFX, _transform, _playerID, 1); AudioEventManager.PlayPropSFX(PropSFXID.PropRubyPickSFX); _currencyPickupEnabled = false; _playerStats = playerStat; _colliderIdentities2D[3] = null; return; } private void ProcessCollisionWithSapphire(Collider2D collision, ref PlayerStats playerStat) { if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Currency return; //Then NOT a Sapphire. Get outta here! int currencyValue = 5;// collision.GetComponent().Amount(); playerStat.Currency += currencyValue; UIPlayerManager.TriggerEvent("ReportUIPlayerGoldscoreEvent", ref playerStat, _playerID); //collision.GetComponent().Destroy(); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_CurrencyFX, _transform, _playerID, 1); AudioEventManager.PlayPropSFX(PropSFXID.PropSapphirePickSFX); _currencyPickupEnabled = false; _playerStats = playerStat; _colliderIdentities2D[3] = null; return; } private void ProcessCollisionWithEmerald(Collider2D collision, ref PlayerStats playerStat) { if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Currency return; //Then NOT a Emerald. Get outta here! int currencyValue = 7;// collision.GetComponent().Amount(); playerStat.Currency += currencyValue; UIPlayerManager.TriggerEvent("ReportUIPlayerGoldscoreEvent", ref playerStat, _playerID); //collision.GetComponent().Destroy(); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_CurrencyFX, _transform, _playerID, 1); AudioEventManager.PlayPropSFX(PropSFXID.PropEmeraldPickSFX); _currencyPickupEnabled = false; _playerStats = playerStat; _colliderIdentities2D[3] = null; return; } private void ProcessCollisionWithDiamond(Collider2D collision, ref PlayerStats playerStat) { if (collision == null) return; if (collision.GetComponent() == null) //Exists on Items Layer, Tag=Currency return; //Then NOT a Diamond. Get outta here! int currencyValue = 10;// collision.GetComponent().Amount(); playerStat.Currency += currencyValue; UIPlayerManager.TriggerEvent("ReportUIPlayerGoldscoreEvent", ref playerStat, _playerID); //collision.GetComponent().Destroy(); ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_CurrencyFX, _transform, _playerID, 1); AudioEventManager.PlayPropSFX(PropSFXID.PropDiamondPickSFX); _currencyPickupEnabled = false; _playerStats = playerStat; _colliderIdentities2D[3] = null; return; } private void ProcessCollisionWithBarrel(Collider2D collision, ref IPlayerSprite playerSprite) { if (collision.GetComponent() == null) //Exists on Enemy Layer, Tag=Container return; //Then NOT a Barrel. Get outta here! //ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_DustHitFX, _transform, _playerID, playerSprite.GetSpriteDirection()); collision.GetComponent().IsHit(_playerID, -playerSprite.GetSpriteDirection()); //UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); return; } private void ProcessCollisionWithPunchBag(Collider2D collision, ref IPlayerSprite playerSprite) { if (collision.GetComponent() == null) //Exists on Enemy Layer, Tag=Container return; //Then NOT a PunchBag. Get outta here! //ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_DustHitFX, _transform, _playerID, playerSprite.GetSpriteDirection()); collision.GetComponent().IsHit(_playerID, -playerSprite.GetSpriteDirection()); //UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); return; } private void ProcessCollisionWithAppleTree(Collider2D collision, ref IPlayerSprite playerSprite) { if (collision.GetComponent() == null) //Exists on Enemy Layer, Tag=Container return; //Then NOT a AppleTree. Get outta here! //ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_DustHitFX, _transform, _playerID, playerSprite.GetSpriteDirection()); collision.GetComponent().IsHit(_playerID, -playerSprite.GetSpriteDirection()); //UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); return; } private void ProcessCollisionWithSeveredHead(Collider2D collision, ref IPlayerSprite playerSprite, ref BaseState playerState) { if (collision.GetComponent() == null) //Exists on Enemy Layer, Tag=Container return; //Then NOT a SeveredHead. Get outta here! //ObjectPooler._instance.InstantiateForPlayer(PrefabID.Prefab_DustHitFX, _transform, _playerID, playerSprite.GetSpriteDirection()); collision.GetComponent().IsHit(_playerID, -playerSprite.GetSpriteDirection(), ref playerState); //UIPlayerManager.TriggerEvent("ReportUIPlayerDamageEvent"); return; } private void ProcessCollisionWitLadder(Collider2D collision, ref IPlayerSprite playerSprite) { if (collision.GetComponent() == null) //Exists on Enemy Layer, Tag=Prop return; //Then NOT a Ladder. Get outta here! collision.GetComponent().IsHit(); return; } public void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag(GetCompareTag(CompareTags.Item))) { SetCollider2DIdentity(collision, C2D.EquipmentCollider2D);//_equipCollider2d = collision; SignalItemPickupCollision(true); } if (collision.CompareTag(_healthTagStrCached)) { if (_playerStats.Health == _playerStats.GetMaxHealthLimit()) return; _healthPickupEnabled = true; if (collision.GetComponent() != null) { _healthPickupFlag = HealthCollision.Apple; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _healthPickupFlag = HealthCollision.Chicken; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _healthPickupFlag = HealthCollision.Fish; collision.GetComponent().DestroyThis(); } _colliderIdentities2D[2] = collision; } if (collision.CompareTag(_currencyTagStrCached)) { _currencyPickupEnabled = true; if (collision.GetComponent() != null) { _currencyPickupFlag = CurrencyCollision.GoldCoin; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _currencyPickupFlag = CurrencyCollision.Ruby; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _currencyPickupFlag = CurrencyCollision.Sapphire; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _currencyPickupFlag = CurrencyCollision.Emerald; collision.GetComponent().DestroyThis(); } if (collision.GetComponent() != null) { _currencyPickupFlag = CurrencyCollision.Diamond; collision.GetComponent().DestroyThis(); } _colliderIdentities2D[3] = collision; //ProcessCollisionWithGoldCoin(collision, ref _playerStats); } if (collision.CompareTag(_fallenTagStrCached)) { _playerStats.Health -= _playerStats.Health; //LevelEventManager.ActivateRespawningLevel(_transform); } if (collision.CompareTag(_campsiteTagStrCached)) LevelEventManager.ActivateCampsite(collision.GetComponent()._campsiteID); if (collision.CompareTag(_portalTagStrCached)) //LevelEventManager.ActivateDoomPortal(collision.GetComponentInParent()._doomPortalID); { if (_transform == null) return; //print("\nPortalFlag activated"); var doomportal = collision.GetComponentInParent(); if (doomportal == null) return; LevelEventManager.ActivateDoomPortal(doomportal._doomPortalID, _transform.position); //collision.GetComponent } if (collision.CompareTag(_propTagStrCached)) { if (collision.GetComponent() != null)//Then a Ladder { if (!_ladderGrabPossible) _ladderGrabPossible = true; } } if (collision.CompareTag(_endActTagStrCached)) LevelEventManager.ActivateEndLevel(_transform); if (collision.CompareTag(_npcTagStrCached)) { if (collision.GetComponent() != null) { if (collision.GetComponent()._npcType == NPCType.StoryTeller_BridgeTroll) { if (UIDialogueManager.IsDialogue()) return; //Prevent repeatedly dialogue start. int enemyId = collision.GetComponent()._enemyID; AIEventNPCManager.TriggerEvent("ReportDetectionWithPlayerForTroll" + enemyId.ToString()); float timeHeldPerLine = collision.GetComponent()._timePerLine; float dialogueDuration = collision.GetComponent()._storyTellerLineCount * timeHeldPerLine;//0 skips dialogue . ProCamera2D.Instance.AddCameraTarget(collision.GetComponent().transform, 1.0f, 1.0f, dialogueDuration, Vector2.zero); LevelEventManager.ActivateDialogue(collision.GetComponent().transform, dialogueDuration/10, dialogueDuration, 1.33f); } } if (collision.GetComponent() == null) return; if (collision.GetComponent()._npcType == NPCType.StoryTeller_WiseSpirit || collision.GetComponent()._npcType == NPCType.StoryTeller_FireSpirit) { if (LevelEventManager.IsDialogue()) return; if (LevelEventManager.DialogueEnd()) return; int npcID = collision.GetComponent()._npcID; AIEventNPCManager.TriggerEvent("ReportStoryTellerDetected" + npcID.ToString()); float timeHeldPerLine = collision.GetComponent()._timePerLine; float dialogueDuration = collision.GetComponent()._storyTellerLineCount * timeHeldPerLine;//0 skips dialogue . LevelEventManager.ActivateDialogue(collision.GetComponent().transform, 3.0f, dialogueDuration/6, 1.5f); ProCamera2D.Instance.AddCameraTarget(_transform, 0.5f, 0.75f, dialogueDuration, Vector2.zero); } if (collision.GetComponent()._npcType == NPCType.ShopKeeper) { if (!_npcTalkPossible) _npcTalkPossible = true; if (UIShopkeepManager.IsShopkeepSelling()) return; if (_npcTalkCollidedEnabled) //Player hit talk button. { if (UIDialogueManager.IsDialogue()) return; //Prevent repeatedly dialogue start. if (UIShopkeepManager.IsShopkeepSelling()) return; int npcID = collision.GetComponent()._npcID; UIShopkeepManager.TriggerEvent("ReportShopkeepDetection" + npcID.ToString()); float timeHeldPerLine = collision.GetComponent()._timePerLine; float dialogueDuration = collision.GetComponent()._storyTellerLineCount * timeHeldPerLine;//0 skips dialogue . ProCamera2D.Instance.AddCameraTarget(_transform, 0.5f, 0.75f, dialogueDuration, Vector2.zero); } } } if (collision.CompareTag(_waterTagStrCached))//_waterTagStrCached { _waterCollisionEnabled = true; AudioEventManager.PlayPlayerSFX(PlayerSFXID.PlayerWaterImpactSFX); //_stateMachine.SetState(PlayerStatePool._instance.GetPlayerFallObjState(_stateMachine, _velocity, null, ref playerSprite, true)); } } //void OnTriggerStay2D(Collider2D collision) => ProcessCollisionFlags(collision); //unreliable. We use ProcessItemCollision() instead. void OnTriggerExit2D(Collider2D collision) { if (collision.CompareTag(GetCompareTag(CompareTags.Item))) { SetCollider2DIdentity(null, C2D.EquipmentCollider2D);//_equipCollider2d = null; SignalItemPickupCollision(false); } if (collision.CompareTag(GetCompareTag(CompareTags.Health))) SetCollider2DIdentity(null, C2D.HealthCollider2D); //_healthCollider2d = null; if (collision.CompareTag(GetCompareTag(CompareTags.Currency))) SetCollider2DIdentity(null, C2D.CurrencyCollider2D); if (collision.CompareTag(_propTagStrCached)) { if (collision.GetComponent() != null)//Then a Ladder { if (_ladderCollidedEnabled) _ladderCollidedEnabled = false; if (_ladderGrabPossible) _ladderGrabPossible = false; } } if (collision.CompareTag(_npcTagStrCached)) { if (collision.GetComponent() != null) { if (collision.GetComponent()._npcType == NPCType.ShopKeeper) { if (_npcTalkCollidedEnabled) _npcTalkCollidedEnabled = false; if (_npcTalkPossible) _npcTalkPossible = false; return; } } } if (collision.CompareTag(_waterTagStrCached))//_waterTagStrCached { _waterCollisionEnabled = false; AudioEventManager.PlayPlayerSFX(PlayerSFXID.PlayerWaterImpactSFX); _stateMachine.SetState(PlayerStatePool._instance.GetPlayerJumpObjState(_stateMachine, Vector3.zero, LevelEventManager.GetPlayerTransform(_playerID), ref _playerSprite, false)); } } public void OnTriggerStay2D(Collider2D collision) { if (collision.CompareTag(_propTagStrCached)) { if(_ladderPartCollisionCheckTimer.HasTimerFinished()) { if (collision.GetComponent() != null)//Then a Ladder { //print("\nLADDER OnTriggerStay2D Check Reached!"); if (!_ladderGrabPossible) _ladderGrabPossible = true; return; } } } if (collision.CompareTag(_npcTagStrCached)) { if (collision.GetComponent() != null)//Then a Ladder { if (collision.GetComponent()._npcType == NPCType.ShopKeeper) { if (!_npcTalkPossible) _npcTalkPossible = true; if (_npcTalkCollidedEnabled) //Player hit talk button. { if (UIDialogueManager.IsDialogue()) return; //Prevent repeatedly dialogue start. if (UIShopkeepManager.IsShopkeepSelling()) return; int npcID = collision.GetComponent()._npcID; UIShopkeepManager.TriggerEvent("ReportShopkeepDetection" + npcID.ToString()); } return; } } } } public void EnableAttackCollisions() => _attackCollisionEnabled = true; public void EnableItemPickupCollision() => _equipPickupEnabled = true; private void EnableItemPickupCollision(bool b) => _equipPickupEnabled = b; public bool SignalItemPickupCollision() => _equipPickupPossible; private bool SignalItemPickupCollision(bool b) => _equipPickupPossible = b; public bool SignalLadderGrabCollision() => _ladderGrabPossible; public bool IsLadderCollisionEnabled() => _ladderCollidedEnabled; public void EnableLadderCollision(bool b) => _ladderCollidedEnabled = b; public bool SignalNPCTalkCollision() => _npcTalkPossible; public bool IsNPCTalkCollisionEnabled() => _npcTalkCollidedEnabled; public void EnableNPCTalkCollision(bool b) => _npcTalkCollidedEnabled = b; public bool IsWaterCollisionEnabled() => _waterCollisionEnabled; public void FlipAttackPoints(int dir) { //Circles we draw(in editor) & detect enemies against. These must all be flipped //as this method will be called on player face direction change. if (dir == 1)//Facing Right { for(int i = 0; i < _attackPoints.Length; i++) { _vector = _attackPoints[i].localPosition; _vector.x = Mathf.Abs(_vector.x); _attackPoints[i].localPosition = _vector; } return; } if (dir == -1)//Facing Left { for (int i = 0; i < _attackPoints.Length; i++) { _vector = _attackPoints[i].localPosition; _vector.x = -Mathf.Abs(_vector.x); _attackPoints[i].localPosition = _vector; } return; } } } }