diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs index 8a5b37fd07d91a2d9d23c1d70307761af7bb36b8..40982143828cbf784f0249fd0ed420f0ddf2cee9 100644 --- a/MrBigsock/Assets/Code/PlayerController.cs +++ b/MrBigsock/Assets/Code/PlayerController.cs @@ -38,12 +38,21 @@ namespace BigSock { bool canMove = true; + // The abilities of the player. + private List<AbilityEntity> _abilities = new List<AbilityEntity>(); + + // A list of the keycodes that correspond to each of the player's abilities. + private List<List<KeyCode>> _keyMapping = new List<List<KeyCode>> { + new List<KeyCode>{KeyCode.Space, KeyCode.Mouse0}, + new List<KeyCode>{KeyCode.Z}, + new List<KeyCode>{KeyCode.C}, + new List<KeyCode>{KeyCode.LeftShift}, + }; - - protected IAttack _testAttack; - protected IAttack _testAttack2; - protected IAttack _testAttack3; - protected IAbility _dodge; + //protected IAttack _testAttack; + //protected IAttack _testAttack2; + //protected IAttack _testAttack3; + //protected IAbility _dodge; public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now; @@ -56,6 +65,22 @@ namespace BigSock { TryPickUpItem(ItemService.SINGLETON.Get(101)); } + /* + Updates the list of the user's abilities. + */ + protected void SetAbilities(List<IAbility> neo) { + var res = new List<AbilityEntity>(); + for(int i = 0; i < neo.Count; ++i) + res.Add(new AbilityEntity(neo[i], i, _keyMapping[i])); + + _abilities = res; + + //!! Put in updating he ui here. + foreach(var ability in _abilities) { + // AbilityUI.SetAbility(ability.Index, ability); ??? + } + } + // Start is called before the first frame update protected override void Start() { @@ -81,10 +106,21 @@ namespace BigSock { //var tmp = PrefabService.SINGLETON; //var tmp = SpriteService.SINGLETON; - _testAttack = (IAttack)AbilityService.SINGLETON.Get(104); - _testAttack2 = (IAttack)AbilityService.SINGLETON.Get(102); - _testAttack3 = (IAttack)AbilityService.SINGLETON.Get(101); - _dodge = AbilityService.SINGLETON.Get(201); + + // Get the abilities and set them up. + var aService = AbilityService.SINGLETON; + var abilities = new List<IAbility> { + aService.Get(104), + aService.Get(102), + aService.Get(101), + aService.Get(201), + }; + SetAbilities(abilities); + + //_testAttack = (IAttack)AbilityService.SINGLETON.Get(104); + //_testAttack2 = (IAttack)AbilityService.SINGLETON.Get(102); + //_testAttack3 = (IAttack)AbilityService.SINGLETON.Get(101); + //_dodge = AbilityService.SINGLETON.Get(201); _ = AudioService.SINGLETON; _ = SpriteService.SINGLETON; @@ -131,34 +167,90 @@ namespace BigSock { } // Dictionary that holds start times for charging abilities. - Dictionary<KeyCode, float> chargeStarts = new Dictionary<KeyCode, float>(); + //Dictionary<KeyCode, float> chargeStarts = new Dictionary<KeyCode, float>(); /* Triggers an ability if it should be. Need to add support for mouse buttons n shiet */ - private void CheckAbilityInput(KeyCode key, IAbility ability) { + private void CheckAbilityInput(AbilityEntity ability) { + var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition)); + + // Check input on each key, stop if one was a success. + foreach(var key in ability.Keys) + if(CheckAbilityInput(key, ability)) break; + + // Update the UI. + //> AbilityUI?.UpdateCharge(ability.Index, ability.ChargePercent); + //> AbilityUI?.UpdateCooldown(ability.Index, ability.CooldownPercent); + + } + + private bool CheckAbilityInput(KeyCode key, AbilityEntity ability) { + var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition)); + + switch (ability.Ability.FireType) { + // Standard: Press to fire. + case FireType.Standard: + if (Input.GetKeyDown(key)) { + ability.Ability.Use(par); + return true; + } + break; + // FullAuto: Keep firing while key is down. + case FireType.FullAuto: + if (Input.GetKey(key)) { + ability.Ability.Use(par); + return true; + } + break; + // Charge: Fire when let go. + case FireType.Charge: + // If pressed down: Store start time. + if (Input.GetKeyDown(key)) { + ability.ChargeStarted = Time.time; + return true; + } + // If let go: Activate + else if (Input.GetKeyUp(key)) { + par.ChargeTime = ability.ChargeTime; + var t = ability.Ability.Use(par); + if (!t) { + if (par.ChargeTime < ability.Ability.MinCharge) + Debug.Log($"[PlayerController.CheckAbilityInput({key})] {ability.Ability.Name} not fired ({par.ChargeTime:N3} < {ability.Ability.MinCharge:N3})"); + } + return true; + } + break; + default: + break; + } + return false; + } + + + private void CheckAbilityInput2(KeyCode key, IAbility ability) { var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition)); - switch(ability.FireType) { + switch (ability.FireType) { // Standard: Press to fire. case FireType.Standard: - if(Input.GetKeyDown(key)) ability.Use(par); + if (Input.GetKeyDown(key)) ability.Use(par); break; // FullAuto: Keep firing while key is down. case FireType.FullAuto: - if(Input.GetKey(key)) ability.Use(par); + if (Input.GetKey(key)) ability.Use(par); break; // Charge: Fire when let go. case FireType.Charge: // If pressed down: Store start time. - if(Input.GetKeyDown(key)) chargeStarts[key] = Time.time; + if (Input.GetKeyDown(key)) chargeStarts[key] = Time.time; // If let go: Activate - else if(Input.GetKeyUp(key)) { + else if (Input.GetKeyUp(key)) { par.ChargeTime = Time.time - chargeStarts[key]; var t = ability.Use(par); - if(!t) { - if(par.ChargeTime < ability.MinCharge) + if (!t) { + if (par.ChargeTime < ability.MinCharge) Debug.Log($"[PlayerController.CheckAbilityInput({key})] {ability.Name} not fired ({par.ChargeTime:N3} < {ability.MinCharge:N3})"); } } @@ -187,14 +279,18 @@ namespace BigSock { // _testAttack.Use(par); //} + // Check and update all abilities. + foreach(var ability in _abilities) { + CheckAbilityInput(ability); + } // Check ability 1. - CheckAbilityInput(KeyCode.Space, _testAttack); - CheckAbilityInput(KeyCode.Mouse0, _testAttack); + //CheckAbilityInput(KeyCode.Space, _testAttack); + //CheckAbilityInput(KeyCode.Mouse0, _testAttack); // Check ability 2. - CheckAbilityInput(KeyCode.Z, _testAttack2); + //CheckAbilityInput(KeyCode.Z, _testAttack2); // Check ability 3. - CheckAbilityInput(KeyCode.LeftShift, _dodge); - CheckAbilityInput(KeyCode.C, _testAttack3); + //CheckAbilityInput(KeyCode.LeftShift, _dodge); + //CheckAbilityInput(KeyCode.C, _testAttack3); //if(Input.GetKeyDown(KeyCode.Z)) Debug.Log($"[PlayerController.Update()] Z was pressed.");