using System.Collections; using System.Collections.Generic; using UnityEngine; using wildlogicgames; using Com.LuisPedroFonseca.ProCamera2D; namespace DoomBreakers { // //We'll be using ItemBase for Sword, Shield, Armor as base for in game world pickup items as they //have all the same behaviours, bar, animations and what they represent. //This will also be used for Health items that are also picked up. Such as Apples, Chicken & Fish. // public enum EquipmentMaterialType { None = -1, Bronze = 0, Iron = 1, Steel = 2, Ebony = 3 }; public enum EquipmentWeaponType { None = -1, Broadsword = 0, Longsword = 1, MorningstarMace = 2 }; public enum EquipmentArmorType { None = -1, Breastplate = 0, Shield = 1 } public enum HealingItemType { None = -1, Apple = 0, Chicken = 1, Fish = 2 }; public enum CurrencyItemType { None = -1, Goldcoin = 0, Ruby = 1, Emerald = 2, Sapphire = 3, Diamond = 4 }; [RequireComponent(typeof(Animator))] [RequireComponent(typeof(SpriteRenderer))] [RequireComponent(typeof(Collider2D))] [RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(CharacterController2D))] public class ItemBase : MonoBehaviour//, IItem { [Header("Item ID")] [Tooltip("ID ranges from 0 to ?")] //Max ? items. public int _itemID; //Set in editor per item or else where. protected ItemBehaviour _itemBehaviour; protected ItemAnimator _itemAnimator; private SpriteRenderer _spriteRenderer; public bool _optimizeFlag, _destroyFlag, _spawnJumpFlag; private GameObject timerObj; private ITimer _timer; public ItemBase(){} // //Initialize for items that are Equipment, such as weapons, armor. // public virtual void Initialize(SpriteRenderer spriteRenderer, Animator animator, PlayerAnimatorController animController, ItemAnimationState animationState, PlayerItem itemType, EquipmentMaterialType equipMaterialType) { _itemBehaviour = this.gameObject.AddComponent(); _itemBehaviour.Setup(this.transform, this.GetComponent(), this.GetComponent()); //Resources/ItemAnimControllers/Equipment/Weapon.controller _itemAnimator = new ItemAnimator(animator, "ItemAnimControllers", "Equipment", "Weapon", animationState); //SetupOptimization(); } // //Initialize for items that are Health, such as apples, fish. // public virtual void Initialize(SpriteRenderer spriteRenderer, Animator animator, HealingItemType healingItemType) { _itemBehaviour = this.gameObject.AddComponent(); _itemBehaviour.Setup(this.transform, this.GetComponent(), this.GetComponent()); //Resources/ItemAnimControllers/Equipment/Weapon.controller _itemAnimator = new ItemAnimator(animator, "ItemAnimControllers", "HealthItems", "Health", healingItemType); //SetupOptimization(); } // //Initialize for items that are Currency, such as goldcoins, ruby. // public virtual void Initialize(SpriteRenderer spriteRenderer, Animator animator, CurrencyItemType currencyItemType) { _itemBehaviour = this.gameObject.AddComponent(); _itemBehaviour.Setup(this.transform, this.GetComponent(), this.GetComponent()); //Resources/ItemAnimControllers/Equipment/Weapon.controller _itemAnimator = new ItemAnimator(animator, "ItemAnimControllers", "Currency", "Currency", currencyItemType); //SetupOptimization(); } public virtual void Awake() { //Initialize(); } public virtual void Start() { _destroyFlag = false; timerObj = new GameObject(); timerObj.AddComponent(); _timer = timerObj.GetComponent(); //_timer = new Timer(); _spriteRenderer = this.GetComponent(); } private void OnEnable() { if (_itemAnimator != null) _itemAnimator.GetAnimator().enabled = true; if (_spriteRenderer != null) _spriteRenderer.enabled = true; } private void OnDisable() { if (_itemAnimator != null) _itemAnimator.GetAnimator().enabled = false; if (_spriteRenderer != null) _spriteRenderer.enabled = false; } public virtual void Update() { SpawnJumpUpdate(); if (_optimizeFlag) return; if (_itemAnimator != null) _itemAnimator.UpdateAnimator(); if (_itemBehaviour != null) _itemBehaviour.UpdateMovement(); if (_destroyFlag) this.gameObject.SetActive(false); } public void Destroy() { _optimizeFlag = false; _destroyFlag = true; if (_itemBehaviour != null) _itemBehaviour.DisableCollisions(); //Especially important for player Equip Indicator usage. if (_itemAnimator != null) _itemAnimator.UpdateAnimator(); if (_itemBehaviour != null) _itemBehaviour.UpdateMovement(); _itemAnimator = null; _itemBehaviour = null; if (this.gameObject.GetComponent() != null) this.gameObject.GetComponent().Sleep(); if (this.gameObject.GetComponent() != null) this.gameObject.GetComponent().enabled = false; if (this.gameObject.GetComponent() != null) this.gameObject.GetComponent().enabled = false; if (this.gameObject != null) this.gameObject.SetActive(false); } public void InitiateSpawnJumpBehaviour() //=> _spawnJumpFlag = true; { _timer.StartTimer(4.0f); _spawnJumpFlag = true; } private void SpawnJumpUpdate() { if (!_spawnJumpFlag) return; if (_timer.HasTimerFinished(true)) return; //_timeStamp = false; No timer has initiated. if (_timer.HasTimerFinished()) _spawnJumpFlag = false; } public bool IsGravityDisabled() => _itemBehaviour.GravityFlag(); public void EnableGravity() //=> _itemBehaviour.EnableGravity(); { _itemBehaviour.EnableGravity(); _optimizeFlag = false; } public void DisableGravity() => _itemBehaviour.DisableGravity(); } }