diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs index 84b56b833d998102e4dd3933b78312758833c4cf..1ec0c444c1b556b1ab82bbfab777f3f7f46b6d91 100644 --- a/MrBigsock/Assets/Code/PlayerController.cs +++ b/MrBigsock/Assets/Code/PlayerController.cs @@ -81,174 +81,213 @@ namespace BigSock { } - - private void FixedUpdate() { - if(canMove) { - // If movement input is not 0, try to move - if(movementInput != Vector2.zero){ - - bool success = TryMove(movementInput); - - if(!success) { - success = TryMove(new Vector2(movementInput.x, 0)); - } - - if(!success) { - success = TryMove(new Vector2(0, movementInput.y)); - } - - animator.SetBool("isMoving", success); - } else { - animator.SetBool("isMoving", false); - } + + private void FixedUpdate() { + if(canMove) { + // If movement input is not 0, try to move + if(movementInput != Vector2.zero){ + + bool success = TryMove(movementInput); + + if(!success) { + success = TryMove(new Vector2(movementInput.x, 0)); + } - // Set direction of sprite to movement direction - var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition); - var pos = transform.position; - var temp = (mouse - pos); - //temp.z = 0; - //direction = temp.normalized; - - if(temp.x < 0) { - spriteRenderer.flipX = true; - } else if (temp.x > 0) { - spriteRenderer.flipX = false; - } - } - + if(!success) { + success = TryMove(new Vector2(0, movementInput.y)); + } + + animator.SetBool("isMoving", success); + } else { + animator.SetBool("isMoving", false); + } + // Set direction of sprite to movement direction + var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition); + var pos = transform.position; + var temp = (mouse - pos); + //temp.z = 0; + //direction = temp.normalized; + + if(temp.x < 0) { + spriteRenderer.flipX = true; + } else if (temp.x > 0) { + spriteRenderer.flipX = false; + } } + + } + + // Dictionary that holds start times for charging abilities. + Dictionary<KeyCode, float> chargeStarts = new Dictionary<KeyCode, float>(); - private void Update() { - // Regenerate mana & stamina. - Regenerate(); + /* + Triggers an ability if it should be. + */ + private void CheckAbilityInput(KeyCode key, IAbility ability) { + var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition)); + + switch(ability.FireType) { + // Standard: Press to fire. + case FireType.Standard: + 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); + break; + // Charge: Fire when let go. + case FireType.Charge: + // If pressed down: Store start time. + if(Input.GetKeyDown(key)) chargeStarts[key] = Time.time; + // If let go: Activate + else if(Input.GetKeyUp(key)) { + par.ChargeTime = Time.time - chargeStarts[key]; + ability.Use(par); + } + break; + default: + break; + } - // Object w/ parameters for abilities. - var par = GetAbilityParam(Camera.main.ScreenToWorldPoint(Input.mousePosition)); - //var par = new AbilityParam{ - // Actor = this, - // TargetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition), - // MovementDir = moveDir, - //}; + } + private void Update() { + // Regenerate mana & stamina. + Regenerate(); - // If pressed Soace or LMB: Regular attack. - if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0)) { - _testAttack.Use(par); - } + // Object w/ parameters for abilities. + //var par = new AbilityParam{ + // Actor = this, + // TargetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition), + // MovementDir = moveDir, + //}; - // If pressed Z: Big attack. - if(Input.GetKey(KeyCode.Z)) { - _testAttack2.Use(par); - } - // If pressed X: dodge. - if(Input.GetKey(KeyCode.X)) { - _dodge.Use(par); - } + //// If pressed Soace or LMB: Regular attack. + //if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0)) { + // _testAttack.Use(par); + //} + // Check ability 1. + CheckAbilityInput(KeyCode.Space, _testAttack); + // Check ability 2. + CheckAbilityInput(KeyCode.Z, _testAttack2); + // Check ability 3. + CheckAbilityInput(KeyCode.X, _dodge); + //// If pressed Z: Big attack. + //if(Input.GetKey(KeyCode.Z)) { + // _testAttack2.Use(par); + //} - //!! Code for testing the new item stuff. - if(Input.GetKeyDown(KeyCode.M)) { - var item = ItemService.SINGLETON.GetRandom(); // new ItemRunningShoes(); - TryPickUpItem(item); - } + //// If pressed X: dodge. + //if(Input.GetKey(KeyCode.X)) { + // _dodge.Use(par); + //} - // Code for opening the menu. - if (Input.GetKeyDown(KeyCode.I)) { - GameObject canvas = GameObject.Find("Canvas"); - if(canvas != null) { - var playerMenu = PrefabService.SINGLETON.Instance("UI/PlayerMenu", canvas.transform); - var invMen = playerMenu.transform.GetChild(1).transform.GetChild(0).GetComponent<InventoryPanel>(); - var statMen = playerMenu.transform.GetChild(1).transform.GetChild(1).GetComponent<StatPanel>(); - invMen.player = this; - statMen.player = this; - } - } - } - /* - Updates the modifiers to the character's stats. - */ - public override void UpdateModifiers(ICharacterStats modifiers = null) { - base.UpdateModifiers(modifiers); - utilBar?.WithMana(maxValue: (int)Stats.MaxMana) - ?.WithStamina(maxValue: (int)Stats.MaxStamina); + //!! Code for testing the new item stuff. + if(Input.GetKeyDown(KeyCode.M)) { + var item = ItemService.SINGLETON.GetRandom(); // new ItemRunningShoes(); + TryPickUpItem(item); } - - /* - Regenerates mana and stamina. - */ - protected override void Regenerate() { - base.Regenerate(); - utilBar?.WithMana(value: (int)Mana) - ?.WithStamina(value: (int)Stamina); + // Code for opening the menu. + if (Input.GetKeyDown(KeyCode.I)) { + GameObject canvas = GameObject.Find("Canvas"); + if(canvas != null) { + var playerMenu = PrefabService.SINGLETON.Instance("UI/PlayerMenu", canvas.transform); + var invMen = playerMenu.transform.GetChild(1).transform.GetChild(0).GetComponent<InventoryPanel>(); + var statMen = playerMenu.transform.GetChild(1).transform.GetChild(1).GetComponent<StatPanel>(); + invMen.player = this; + statMen.player = this; + } } + } + + /* + Updates the modifiers to the character's stats. + */ + public override void UpdateModifiers(ICharacterStats modifiers = null) { + base.UpdateModifiers(modifiers); + + utilBar?.WithMana(maxValue: (int)Stats.MaxMana) + ?.WithStamina(maxValue: (int)Stats.MaxStamina); + } - private bool TryMove_OLD(Vector2 direction) { - if(direction != Vector2.zero) { - // Check for potential collisions - int count = rb.Cast( - direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions - movementFilter, // The settings that determine where a collision can occur on such as layers to collide with - castCollisions, // List of collisions to store the found collisions into after the Cast is finished - (float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset - - if(count == 0){ - rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime); - return true; - } else { - return false; - } - } else { - // Can't move if there's no direction to move in - return false; - } - // - } + /* + Regenerates mana and stamina. + */ + protected override void Regenerate() { + base.Regenerate(); + utilBar?.WithMana(value: (int)Mana) + ?.WithStamina(value: (int)Stamina); + } - void OnMove(InputValue movementValue) { - movementInput = movementValue.Get<Vector2>(); - } + private bool TryMove_OLD(Vector2 direction) { + if(direction != Vector2.zero) { + // Check for potential collisions + int count = rb.Cast( + direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions + movementFilter, // The settings that determine where a collision can occur on such as layers to collide with + castCollisions, // List of collisions to store the found collisions into after the Cast is finished + (float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset + + if(count == 0){ + rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime); + return true; + } else { + return false; + } + } else { + // Can't move if there's no direction to move in + return false; + } + + // + } + void OnMove(InputValue movementValue) { + movementInput = movementValue.Get<Vector2>(); + } - public void LockMovement() { - canMove = false; - } - public void UnlockMovement() { - canMove = true; - } + public void LockMovement() { + canMove = false; + } - /* - Method for what to do when the character takes damage. - */ - protected override void AfterDamage(IAttackStats attack) { - base.AfterDamage(attack); - utilBar?.WithHealth(Convert.ToInt32(HP)); - } + public void UnlockMovement() { + canMove = true; + } - public void GainXp(float xp){ - GiveXp(xp); - CheckXp(); - utilBar?.WithXP((int)this.xp); - } + /* + Method for what to do when the character takes damage. + */ + protected override void AfterDamage(IAttackStats attack) { + base.AfterDamage(attack); + utilBar?.WithHealth(Convert.ToInt32(HP)); + } + + public void GainXp(float xp){ + GiveXp(xp); + CheckXp(); + utilBar?.WithXP((int)this.xp); + } - private void CheckXp(){ - if(xp > maxXp){ - level += 1; - xp -= maxXp; - maxXp = (level + 1) * 100; - utilBar?.WithXP(maxValue: (int)maxXp); + private void CheckXp(){ + if(xp > maxXp){ + level += 1; + xp -= maxXp; + maxXp = (level + 1) * 100; + utilBar?.WithXP(maxValue: (int)maxXp); - - } + } + } }