/// <summary> /// Throw the given item. /// </summary> /// <param name="carriable">The item to throw.</param> public void Throw(Carriable carriable) { var guide = player.GetComponentInChildren <ThrowingGuide>(true); if (guide.ThrowingStrength < 0.33f) { Drop(carriable); return; } carriable.OnThrow(); Vector3 playerHead = new Vector3(player.transform.position.x, player.transform.position.y + player.Collider.bounds.size.y); Vector3 direction = (player.GetMouseWorldPosition() - playerHead); direction.z = 0; direction = direction.normalized; carriable.Physics.Velocity = direction * settings.ThrowingForce * guide.ThrowingStrength; // Check if object will collide w/ player. If it does, transport it through // character. if (direction.y < 0) { NudgeThrow(carriable, direction); } }
//------------------------------------------------------------------------- // Weak Spot Interface //------------------------------------------------------------------------- /// <summary> /// Take damage if the object that hit it is a Carriable item. /// </summary> /// <param name="col">the collider of the object that hit the eye.</param> /// <returns>True if the Eye is open and the object that hit the eye is a /// <see cref="Carriable" /></returns> protected override bool DamageCondition(Collider2D col) { Carriable carriable = col.transform.root.GetComponent <Carriable>(); return(carriable != null && col == carriable.Collider && carriable.Physics.Velocity.magnitude > 0 && Exposed); }
private void OnCollisionEnter2D(Collision2D col) { if (IsSolved(col)) { Transform root = col.collider.transform.root; Carriable carriable = root.GetComponentInChildren <Carriable>(); carriable.Physics.Velocity = Vector2.zero; Solve(); } }
//------------------------------------------------------------------------- // Puzzle API //------------------------------------------------------------------------- protected override bool IsSolved(object info) { if (info is Collision2D col) { Transform root = col.collider.transform.root; Carriable carriable = root.GetComponentInChildren <Carriable>(); return(carriable != null && carriable != GameManager.Player.CarriedItem); } return(false); }
/// <summary> /// Try to nudge a downward pointing throw out of the player's way. /// </summary> /// <param name="carriable">The carriable being thrown.</param> /// <param name="direction">The normalized direction of the throw.</param> private void NudgeThrow(Carriable carriable, Vector3 direction) { // Raycast to check if the carriable would collide with the player. In // this case, we need to nudge the box out of the way to prevent it from // effecting the player's physics. Vector2 pos = carriable.Collider.transform.position; RaycastHit2D[] hits = Physics2D.RaycastAll(pos, direction, carriable.Physics.Velocity.magnitude); foreach (RaycastHit2D hit in hits) { if (hit.collider.CompareTag("Player") && player.IsHitBy(carriable.Collider)) { CalculateBackCast(carriable, direction); } } }
/// <summary> /// Drop the given item. /// </summary> /// <param name="carriable">The item to drop.</param> public void Drop(Carriable carriable) { carriable.OnPutDown(); Vector3 playerHead = new Vector3(player.transform.position.x, player.transform.position.y + player.Collider.bounds.size.y); Vector3 direction = (player.GetMouseWorldPosition() - playerHead); carriable.Physics.Vy = settings.DropForce.y; if (direction.x > 0) { carriable.Physics.Vx = settings.DropForce.x; } else { carriable.Physics.Vx = -settings.DropForce.x; } }
/// <summary> /// Fires when code outside the state machine is trying to send information. /// </summary> /// <param name="signal">The signal sent.</param> public override void OnSignal(GameObject obj) { if (CanCarry(obj)) { Carriable item = obj.GetComponent <Carriable>(); item.OnPickup(); ChangeToState <CarryJumpFall>(); } else if (IsAimableFlingFlower(obj)) { ChangeToState <FlingFlowerAim>(); } else if (IsDirectionalFlingFlower(obj)) { ChangeToState <FlingFlowerDirectedLaunch>(); } }
/// <summary> /// Close the eye. /// </summary> /// <param name="col">The collider of the object that hit the eye.</param> protected override void OnHit(Collider2D col) { // Stop the object that hit the eye. Carriable carriable = col.transform.root.GetComponent <Carriable>(); if (carriable != null) { carriable.Physics.Velocity = Vector2.zero; } // Have the eye shake and flash red. shaking.Shake(); animator.SetTrigger("damage"); // Close the eye. Close(); }
private GuidComponent GetObjectReference(Collider2D collider) { if (collider.CompareTag("Player")) { PlayerCharacter player = collider.GetComponent <PlayerCharacter>(); Carriable carriable = player.CarriedItem; if (carriable != null) { return(carriable.GetComponentInChildren <GuidComponent>()); } } else { return(collider.transform.root.GetComponentInChildren <GuidComponent>()); } return(null); }
/// <summary> /// Project the carriable object forward, then calculate where the object would /// come out from the player's hitbox and apply it to the carriable's position. /// </summary> /// <param name="carriable">The carriable to check.</param> /// <param name="direction">The normalized direction of the throw.</param> private void CalculateBackCast(Carriable carriable, Vector3 direction) { Vector2 nextPos = (Vector2)carriable.Collider.transform.position + carriable.Physics.Velocity; bool moved = false; while (!moved) { // project raycast backwards to find where the carriable would // exit the player's hitbox. RaycastHit2D[] backHits = Physics2D.RaycastAll(nextPos, -direction, carriable.Physics.Velocity.magnitude); foreach (RaycastHit2D backHit in backHits) { if (backHit.collider.CompareTag("Player") && player.IsHitBy(carriable.Collider)) { carriable.transform.position = (Vector3)backHit.point; moved = true; } } nextPos = nextPos + carriable.Physics.Velocity; } }
/// <summary> /// Drop the given item. /// </summary> /// <param name="carriable">The item to drop.</param> public void Drop(Carriable carriable) => ThrowingComponent.Drop(carriable);
/// <summary> /// Throw the given item /// </summary> /// <param name="carriable">The item to throw.</param> public void Throw(Carriable carriable) => ThrowingComponent.Throw(carriable);
/// <summary> /// Fires whenever the state is entered into, after the previous state exits. /// </summary> public override void OnStateEnter() { Carriable carriable = player.CarriedItem; carriable.OnPickup(); }