/// <summary> /// Moves to the specified movingDirection and calls moveComplete method when the gameObject arrives /// at the destination. /// </summary> /// <param name="movingDirection">Moving direction (relative to the gameobject's rotation).</param> /// <param name="moveComplete">Method to be called when moving is finished.</param> public void Move(FourDirections movingDirection, MovementCallback moveComplete) { if (!moving) { this.moveComplete = moveComplete; startPosition = transform.position; Vector3 globalMovingDirection = GetGlobalMovingDirection(movingDirection); // Check for obstacles int obstacles = LayerMask.GetMask(new string[] { "Obstacle" }); if (Physics.Raycast(startPosition, globalMovingDirection, GridMovement.tileSize, obstacles)) { // If there's an obstacle in front of the player bump forward and then back a little bit. endPosition = startPosition + globalMovingDirection * GridMovement.tileSize * 0.2f; BlockedMoveForward(); SoundEffectManager.playSoundEffectOnce("WalkAgainstObstacle"); } else { // Otherwis just move to the next tile. endPosition = startPosition + globalMovingDirection * GridMovement.tileSize; MoveToNextTile(); } moving = true; } }
public void DeactivateCorrespondingCollider(FourDirections dir) { switch (dir) { case FourDirections.North: //Debug.Log ("North"); //Activate RightUp collider and animation colliderUp.DisableCollider(); break; case FourDirections.East: //Debug.Log ("East"); colliderRight.DisableCollider(); break; case FourDirections.South: //Debug.Log ("South"); colliderDown.DisableCollider(); break; case FourDirections.West: //Debug.Log ("West"); colliderLeft.DisableCollider(); break; } }
public void SetDirection(FourDirections direction) { if (this.currentDirection != direction) { this.currentDirection = direction; } }
private Vector2 GetVectorFromDirection(FourDirections direction) { switch (direction) { case FourDirections.Up: return(GeneralExtensions.UnitAngleVector(0f)); case FourDirections.Left: return(GeneralExtensions.UnitAngleVector(270f)); case FourDirections.Right: return(GeneralExtensions.UnitAngleVector(90f)); case FourDirections.Down: return(GeneralExtensions.UnitAngleVector(180f)); case FourDirections.Stopped: return(CurrentDirectionVector); case FourDirections.Unknown: throw new ArgumentException("Where are you going?s"); break; default: return(GeneralExtensions.UnitAngleVector(123f)); } }
private void DeactivateCorrespondingCollider(FourDirections direction) { switch (direction) { case FourDirections.North: //Debug.Log ("North"); swordColliderUp.DisableCollider(); break; case FourDirections.East: //Debug.Log ("East"); swordColliderRight.DisableCollider(); break; case FourDirections.South: //Debug.Log ("South"); swordCollider.DisableCollider(); break; case FourDirections.West: //Debug.Log ("West"); swordColliderLeft.DisableCollider(); break; } }
private void DeactivateCorrespondingCollider(FourDirections dir) { switch (dir) { case FourDirections.North: Debug.Log("RIGHT UP"); swordColliderUp.DisableCollider(); break; case FourDirections.East: Debug.Log("UP LEFT"); swordColliderRight.DisableCollider(); break; case FourDirections.South: Debug.Log("LEFT DOWN"); swordCollider.DisableCollider(); break; case FourDirections.West: Debug.Log("DOWN RIGHT"); swordColliderLeft.DisableCollider(); break; } }
//@dir: normalized vector in direction of attack public void WarpStrike(ref PlayerState playerState, Vector2 dir) { switch (this.warpState) { case WarpState.Setup: this.warpState = WarpState.InitialStrike; this.warpDirVector = dir; this.warpDirection = this.DirectionHandler.GetDirectionFromVector(dir); this.chargedAttack.ActivateCorrespondingCollider(warpDirection); //Let the attack info container know what just happened this.playerAttackInfo.UpdateAttackInfo(AttackID.WarpStrike, baseAttackForce, dir, damageAmount); this.timer = this.warpDuration; //Play Animation this.playerAnim.Play("Initial Warp Strike"); break; case WarpState.InitialStrike: //Animation Event transitions out of this state break; case WarpState.Warping: this.timer -= Time.deltaTime; if (this.timer <= 0f) { EndWarp(); } break; case WarpState.EndStrike: //Animation Event transitions out of this state break; case WarpState.Cooldown: this.timer -= Time.deltaTime; if (this.timer <= 0f) { this.timer = this.warpDuration; this.warpState = WarpState.Done; } break; case WarpState.Done: this.warpState = WarpState.Setup; playerState = PlayerState.Default; break; } }
public FourWayDirection(FourDirections initialDirection, float initialVelocity) { currentDirection = initialDirection; if (currentDirection != FourDirections.Unknown) { _previousDirection = FourDirections.Unknown; } this.CurrentDirectionVector = GetVectorFromDirection(currentDirection); velocity = initialVelocity; referenceVelocity = initialVelocity; }
public void move(int direction) { //0 up, 1 down, 2 left, 3 right FourDirections fourDirections = (FourDirections)direction; if (movable) { actionByKey(getDirectionPoint(fourDirections)); StartCoroutine(DelayProcess(3.0f)); } }
/// <summary> /// The character's movement direction is calculated based on localDirection and /// it's rotation: for example if the character is facing right (or mostly right /// from the 4 main directions), and it's localDirection is right, it's global /// movement direction should be backward. /// </summary> /// <returns>The global moving direction.</returns> /// <param name="localDirection">The local moving direction enum.</param> Vector3 GetGlobalMovingDirection(FourDirections localDirection) { Vector3 localMovementDirectionVector = Vector3.zero; switch (localDirection) { case FourDirections.Back: localMovementDirectionVector = Vector3.back; break; case FourDirections.Forward: localMovementDirectionVector = Vector3.forward; break; case FourDirections.Left: localMovementDirectionVector = Vector3.left; break; case FourDirections.Right: localMovementDirectionVector = Vector3.right; break; } float yRotation = transform.rotation.eulerAngles.y; Vector3 facingDirection = Vector3.zero; if (yRotation < 45 || 360 - 45 < yRotation) { facingDirection = Vector3.forward; } else if (45 < yRotation && yRotation < 135) { facingDirection = Vector3.right; } else if (135 < yRotation && yRotation < 225) { facingDirection = Vector3.back; } else if (225 < yRotation && yRotation < 360 - 45) { facingDirection = Vector3.left; } Vector3 globalDirection = Quaternion.LookRotation(localMovementDirectionVector, Vector3.up) * facingDirection; return(globalDirection); }
public void Update(float mlSinceupdate) { if ((currentDirection != FourDirections.Unknown) && currentDirection != _previousDirection) { if (currentDirection != FourDirections.Stopped) { this.velocity = referenceVelocity; CurrentDirectionVector = GetVectorFromDirection(currentDirection); } else { this.velocity = 0f; } _previousDirection = currentDirection; } }
private Vector2 getDirectionPoint(FourDirections fourDirections) { switch (fourDirections) { case FourDirections.Up: return(new Vector2(transform.position.x, transform.position.y + 0.3f)); case FourDirections.Down: return(new Vector2(transform.position.x, transform.position.y - 0.3f)); case FourDirections.Left: return(new Vector2(transform.position.x - 0.7f, transform.position.y)); case FourDirections.Right: return(new Vector2(transform.position.x + 0.7f, transform.position.y)); } Debug.LogError("not in Enum"); return(new Vector2()); }
public static Vector2 DirectionToVector(FourDirections Direction) { switch (Direction) { case FourDirections.Left: return(Vector2.left); case FourDirections.Up: return(Vector2.up); case FourDirections.Right: return(Vector2.right); case FourDirections.Down: return(Vector2.down); default: return(Vector2.zero); } }
public Vector2 GetVectorFromDirection(FourDirections dir) { if (dir == FourDirections.North) { return(Vector2.up); } else if (dir == FourDirections.East) { return(Vector2.right); } else if (dir == FourDirections.South) { return(Vector2.down); } else if (dir == FourDirections.West) { return(Vector2.left); } return(Vector2.up); }
private void DisableAttackCollider() { //See which way we're facing FourDirections dir = dirHandler.GetDirectionFromVector(lastMove); switch (dir) { case FourDirections.North: this.attackUp.DisableAttackCollider(); break; case FourDirections.East: this.attackRight.DisableAttackCollider(); break; case FourDirections.South: this.attackDown.DisableAttackCollider(); break; case FourDirections.West: this.attackLeft.DisableAttackCollider(); break; } }
public void Interact(ref PlayerState playerState) { switch (state) { case InteractState.Check: //Only interact if the prompt UI is active if (GameObject.Find("PromptUI") == null) { playerState = PlayerState.Default; return; } //Determine direction to raycast FourDirections dir = this.playerMovementSystem.GetFaceDirectionIn4DirSystem(); //Raycast again to get information about interactable object RaycastHit2D hit = Physics2D.Raycast(transform.position, this.DirectionSystem.GetVectorFromDirection(dir), 1, this.whatToHit); //If mistake, go back to default //NOTE: Raycast will only hit objects on layer "InteractableObjects", which are the only ones with this tag if (hit.collider == null || hit.collider.tag != "InteractableStaticObject" || hit.collider.gameObject.GetComponent <InteractableObject>().IsActive() == false) { Debug.Log("No interactable object in range"); playerState = PlayerState.Default; return; } //Determine which type of interactable we hit InteractableObject interactable = hit.collider.gameObject.GetComponent <InteractableObject>(); if (interactable == null) { playerState = PlayerState.Default; return; } switch (interactable.objectType) { case InteractableObject.InteractableType.Chest: HandleChestInteractable(interactable, ref playerState); break; case InteractableObject.InteractableType.Door: HandleDoorInteractable(interactable, ref playerState); break; case InteractableObject.InteractableType.Lever: break; } break; case InteractState.Done: HideHoldItem(); this.state = InteractState.Check; playerState = PlayerState.Default; break; } }
public void ChargeAttack(ref PlayerState playerState, Vector2 attackDirection) { switch (chargeAttackState) { case ChargeAttackState.Setup: timer = maxChargeTime; finalAttackForce = baseAttackForce; chargeAttackState = ChargeAttackState.Charging; playerBody.velocity = Vector2.zero; spriteRenderer.material.SetFloat("_FlashAmount", 0.60f); //Activate corresponding collider //playerFaceDirection = orientationSystem.GetDirection(attackDirection); playerFaceDirection = DirectionHandler.GetDirectionFromVector(attackDirection); break; case ChargeAttackState.Charging: timer -= Time.deltaTime; //Play Charging Animation animator.Play("Charging Attack"); //Make player flash while charging spriteRenderer.material.SetFloat("_FlashAmount", Mathf.PingPong(timer * 4.7f, 0.60f)); //If timer is up or player releases button, then perform attack if (timer <= 0f || (Input.GetButtonUp("AttackPS4")) || Input.GetButtonUp("Attack")) { spriteRenderer.material.SetFloat("_FlashAmount", 0f); chargeAttackState = ChargeAttackState.Attacking; ActivateCorrespondingCollider(playerFaceDirection); playerAttackInfo.UpdateAttackInfo(AttackID.ChargedAttack, finalAttackForce, movementInfo.GetLastMove().normalized, damageAmount); } //If button is still being held down if (Input.GetButton("AttackPS4") || Input.GetButton("Attack")) { //Increase damage that will be dealt here finalAttackForce += (Time.deltaTime * chargeMultiplier); break; } break; case ChargeAttackState.Attacking: //Play animation animator.Play("Charged Attack"); break; case ChargeAttackState.Cooldown: timer -= Time.deltaTime; if (timer <= 0f) { //Reset stuff timer = 0f; chargeAttackState = ChargeAttackState.Setup; playerState = PlayerState.Default; } break; } }
public void SprintAttack(ref PlayerState playerState, Vector3 attackDirection) { switch (attackState) { case AttackState.Setup: //modify sprint bool so that we can change the animation //playerSprinting = false; //playerSprAttacking = true; attackState = AttackState.SprintAttacking; attackTimer = sprintAttackTime; startPosition = transform.position; currentLerpTime = 0f; Debug.Log("SPRINT ATTACK!"); playerFaceDirection = DirectionHandler.GetDirectionFromVector(movementInfo.GetLastMove()); ActivateCorrespondingCollider(playerFaceDirection); playerAttackInfo.UpdateAttackInfo(AttackID.SprintAttack, baseAttackForce, movementInfo.GetLastMove().normalized, damageAmount); playerAnimator.Play("Sprint Attack"); break; case AttackState.SprintAttacking: //This state is when the player was sprinting and attacked attackTimer -= Time.deltaTime; //move towards attack direction playerBody.velocity = Vector2.zero; //Stop player from moving while attacking currentLerpTime += Time.deltaTime; if (currentLerpTime > sprintAttackTime) { currentLerpTime = sprintAttackTime; } float t = currentLerpTime / sprintAttackTime; t = Mathf.Sin(t * Mathf.PI * 0.5f); transform.position = Vector2.Lerp(startPosition, attackDirection, t); if (attackTimer <= 0f) { attackState = AttackState.SprintAttackCooldown; //Disable colliders DeactivateCorrespondingCollider(playerFaceDirection); //Cooldown time for dash attack attackTimer = sprintAttackCooldownTime; } break; case AttackState.SprintAttackCooldown: attackTimer -= Time.deltaTime; if (attackTimer <= 0f) { //Reset stuff attackTimer = 0f; attackState = AttackState.Setup; playerState = PlayerState.Default; //playerSprAttacking = false; } break; case AttackState.Done: attackState = AttackState.Setup; playerState = PlayerState.Default; break; } }
//Performs the attack ability // @playerState: the state of the player is needed so we can change it to Attacking state // @playerAttacking: the bool is needed so that we can let the animator know when an attack is happening // @attackDirection: the vector that contains which direction the attack is happening at so we can set the // appropriate animation and collider public void Attack(ref PlayerState playerState, Vector3 attackDirection) { switch (attackState) { case AttackState.Ready: //Setup attack attackState = AttackState.Attacking; timer = inputBufferTime; //Increase combo counter comboCount++; //Use combo count to let animator know which attack is being performed playerAnimator.SetInteger("ComboCount", comboCount); if (comboCount == 1) { lastAttackDirection = attackDirection; } //Decide which animation to play switch (comboCount) { case 1: playerAnimator.Play("Attack 1"); break; case 2: playerAnimator.Play("Attack 2"); break; case 3: playerAnimator.Play("Attack 1"); break; case 4: playerAnimator.Play("Attack 2"); break; } //Determine which which collider to enable //playerFaceDirection = orientationSystem.GetDirection(CreateAttackVector()); Debug.Log(CreateAttackVector()); playerFaceDirection = DirectionHandler.GetDirectionFromVector(CreateAttackVector()); ActivateCorrespondingCollider(playerFaceDirection); playerAttacking = true; //Set up to check if charged attack will be performed //Charge attack happens if attack button is held down while performing attack if (Input.GetButtonUp("AttackPS4") || Input.GetButtonUp("Attack")) { this.chargeAttack = false; } else { this.chargeAttack = true; } //Let the attack info container know what just happened playerAttackInfo.UpdateAttackInfo(AttackID.NormalAttack, baseAttackForce, movementInfo.GetLastMove().normalized, damageAmount); //Calculate where the attack will move the player targetPosition = new Vector3(transform.position.x + lastAttackDirection.x * 10f, transform.position.y + lastAttackDirection.y * 10f); break; case AttackState.Attacking: //This state is when the sword is swinging in the animation timer -= Time.deltaTime; if (Input.GetButtonUp("AttackPS4") || Input.GetButtonUp("Attack")) { this.chargeAttack = false; } //Check if user inputted attack while an attack is still happening (in buffer time window) if (!(timer <= 0f) && (Input.GetButtonDown("AttackPS4") || Input.GetButtonDown("Attack")) && (comboCount != comboMax)) { bufferInput = true; } //move towards click position playerBody.velocity = Vector2.zero; //Stop player from moving while attacking transform.position = Vector3.MoveTowards(transform.position, targetPosition, attackDistance * Time.deltaTime); //Animation Event will trigger exit out of this state break; case AttackState.Done: //NOTE: Will need to have the animation continue to play during this state. During this state, the sword is // no longer swinging, but this state can be interrupted by another attack -> combo timer -= Time.deltaTime; //Check for charged attack if (Input.GetButtonUp("AttackPS4") || Input.GetButtonUp("Attack") || comboCount == comboMax) { this.chargeAttack = false; } //Inputs that can interrupt attack: // Moving // Attack -> Combo //If input is received within cooldown period (except when combo max reached), change states. if (((Input.GetButtonDown("AttackPS4") || Input.GetButtonDown("Attack")) && (comboCount != comboMax)) || bufferInput) { //Go to next attack -> Maybe create switch case here to assign different animations or attack times Debug.Log("Buffered Input"); attackState = AttackState.Ready; bufferInput = false; } //If input not received, finish playing the cooldown animation and then transition into default state if (timer <= 0f) { //Reset stuff comboCount = 0; timer = 0f; playerAttacking = false; attackState = AttackState.Ready; if (this.chargeAttack) { playerState = PlayerState.ChargedAttacking; } else { playerState = PlayerState.Default; } this.bufferInput = false; this.chargeAttack = true; } //need some sort of bool/message that will let the animator know to keep playing cooldown animation/sprite break; }//switch }