/// <summary> /// Shows the cornors of the platform /// </summary> public void OnDrawGizmos() { Gizmos.color = Color.blue; Gizmos2D.DrawCircle(new Vector2(Physics2DExtra.Left(gCollider), Physics2DExtra.Top(gCollider)), 0.1f); Gizmos.color = Color.red; Gizmos2D.DrawCircle(new Vector2(Physics2DExtra.Right(gCollider), Physics2DExtra.Top(gCollider)), 0.1f); Gizmos.color = Color.green; Gizmos2D.DrawCircle(new Vector2(Physics2DExtra.Left(gCollider), Physics2DExtra.Bottom(gCollider)), 0.1f); Gizmos.color = Color.yellow; Gizmos2D.DrawCircle(new Vector2(Physics2DExtra.Right(gCollider), Physics2DExtra.Bottom(gCollider)), 0.1f); }
/// <summary> /// Causes the player to jump and set some helper bools /// </summary> public void Jump() { entity.Velocity = new Vector2(entity.Velocity.x, JumpSpeed); coyoteTimer = 0; jumpBufferTimer = 0; isJumping = true; // audioSource.PlayOneShot(jumpSound); int amount = Random.Range(1, 3); for (int i = 0; i < amount; i++) { //Set positions and velocity. ParticleSystem.EmitParams ep = new ParticleSystem.EmitParams(); ep.position = new Vector3(transform.position.x + (Random.Range(-4, 4) * Physics2DExtra.PIXEL_UNIT), Physics2DExtra.Bottom(bCollider), transform.position.z); //Set velcoyty to be fairly random ep.velocity = (entity.Velocity * -Random.Range(0.25f, 0.5f)); //Particle emit dustSystem.Emit(ep, 1); } }
/// <summary> /// Moves the platform, pushes entities /// </summary> /// <param name="xAmount"></param> /// <param name="yAmount"></param> private void Move(float xAmount, float yAmount) { subPixelVelocity.x += xAmount; subPixelVelocity.y += yAmount; float moveX = Mathf.Round(subPixelVelocity.x * Physics2DExtra.PIXEL_SIZE) / Physics2DExtra.PIXEL_SIZE; // In Units float moveY = Mathf.Round(subPixelVelocity.y * Physics2DExtra.PIXEL_SIZE) / Physics2DExtra.PIXEL_SIZE; // In Units if (moveX != 0 || moveY != 0) { List <Entity2D> riding = GetAllRidingEntities(); Entity2D[] AllEntities = FindObjectsOfType <Entity2D>(); //May need to change gCollider.isTrigger = true; //X if (moveX != 0) { //Moveplatform subPixelVelocity.x -= moveX; transform.position += new Vector3(moveX, 0, 0); Physics2D.SyncTransforms(); if (moveX > 0) { foreach (Entity2D ent in AllEntities) { if (Physics2DExtra.PlaceMeeting(gCollider, Vector2.zero, entitiyLayer, ent.bCollider, true)) { ent.MoveX(Physics2DExtra.Right(gCollider) - Physics2DExtra.Left(ent.bCollider)); } else if (riding.Contains(ent)) { ent.MoveX(moveX); } } } else if (moveX < 0) { foreach (Entity2D ent in AllEntities) { if (Physics2DExtra.PlaceMeeting(gCollider, Vector2.zero, entitiyLayer, ent.bCollider, true)) { ent.MoveX(Physics2DExtra.Left(gCollider) - Physics2DExtra.Right(ent.bCollider)); } else if (riding.Contains(ent)) { ent.MoveX(moveX); } } } } //Y if (moveY != 0) { //Moveplatform subPixelVelocity.y -= moveY; transform.position += new Vector3(0, moveY, 0); Physics2D.SyncTransforms(); if (moveY > 0) { foreach (Entity2D ent in AllEntities) { if (Physics2DExtra.PlaceMeeting(gCollider, Vector2.zero, entitiyLayer, ent.bCollider, true)) { ent.MoveY(Physics2DExtra.Top(gCollider) - Physics2DExtra.Bottom(ent.bCollider)); } else if (riding.Contains(ent)) { ent.MoveY(moveY); } } } else { foreach (Entity2D ent in AllEntities) { if (Physics2DExtra.PlaceMeeting(gCollider, Vector2.zero, entitiyLayer, ent.bCollider, true)) { ent.MoveY(Physics2DExtra.Bottom(gCollider) - Physics2DExtra.Top(ent.bCollider)); } else if (riding.Contains(ent)) { ent.MoveY(moveY); } } } } //May need to change gCollider.isTrigger = false; } }
/// <summary> /// Movement code, jumping, gravity, /// </summary> void GameplayState() { float fricc = groundFriction; if (!entity.onGround) { fricc = airFirction; } float grav = 0.2f; //Lock it to this frame if (doFireJump) { entity.Velocity = new Vector2(entity.Velocity.x, fireJumpAmount); coyoteTimer = 0; jumpBufferTimer = 0; isJumping = false; doFireJump = false; if (gameObject.transform.childCount > 0) { Destroy(gameObject.transform.GetChild(0).gameObject); } GameObject obj = GameObject.Instantiate(burnJumpParticle, transform); } //Mvoement if (KeyRight) { entity.Velocity = new Vector2(Numbers.Approach(entity.Velocity.x, MoveSpeed, fricc), entity.Velocity.y); } else if (KeyLeft) { entity.Velocity = new Vector2(Numbers.Approach(entity.Velocity.x, -MoveSpeed, fricc), entity.Velocity.y); } else { entity.Velocity = new Vector2(Numbers.Approach(entity.Velocity.x, 0.0f, fricc), entity.Velocity.y); } //Landed Dust if (entity.landed) { int amount = Random.Range(1, 3); for (int i = 0; i < amount; i++) { //Set positions and velocity. ParticleSystem.EmitParams ep = new ParticleSystem.EmitParams(); ep.position = new Vector3(transform.position.x + (Random.Range(-4, 4) * Physics2DExtra.PIXEL_UNIT), Physics2DExtra.Bottom(bCollider), transform.position.z); //Set velcoyty to be fairly random ep.velocity = new Vector3(Random.Range(-1, 1), Random.Range(0.5f, 1f), ep.velocity.z); //Particle emit dustSystem.Emit(ep, 1); } } if (entity.onGround) { //Set this so it can count down when not on ground coyoteTimer = coyoteTimeThreshhold; isJumping = false; if (KeyJump || jumpBufferTimer > 0) { Jump(); } //create dust if moving if (entity.Velocity.x != 0) { if (Random.Range(0, 20) >= 19) { //Set positions and velocity. ParticleSystem.EmitParams ep = new ParticleSystem.EmitParams(); ep.position = new Vector3(transform.position.x + (Random.Range(-4, 4) * Physics2DExtra.PIXEL_UNIT), Physics2DExtra.Bottom(bCollider), transform.position.z); //Set velcoyty to be fairly random ep.velocity = (entity.Velocity * -Random.Range(0.5f, 1)); ep.velocity = new Vector3(ep.velocity.x, Random.Range(0.5f, 1f), ep.velocity.z); //Particle emit dustSystem.Emit(ep, 1); } } } else // Not on ground { if (KeyJump) { if (coyoteTimer > 0) { Jump(); } else // Set jump buffer { jumpBufferTimer = jumpBufferThreshhold; } } if (KeyJumpHeld && Mathf.Abs(entity.Velocity.y) <= halfGravBuffer) { grav *= 0.5f; } //Variable jump height if (KeyJumpRel && isJumping) { if (entity.Velocity.y > 0) { if (entity.Velocity.y >= JumpSpeed / 4.0f) { entity.Velocity = new Vector2(entity.Velocity.x, JumpSpeed / 4.0f); } } isJumping = false; } coyoteTimer = Numbers.Approach(coyoteTimer, 0, 1); } jumpBufferTimer = Numbers.Approach(jumpBufferTimer, 0, 1); //Gravity if (entity.Velocity.y > -10) { entity.Velocity = new Vector2(entity.Velocity.x, entity.Velocity.y - grav); } //Debug.Log(entity.Velocity.ToString() + " : " + overFlowVelocity.ToString()); ; if (recentlyHurt) { hurtTimer -= 1; if (hurtTimer % 5 == 0) { sRenderer.enabled = true; } else { sRenderer.enabled = false; } if (hurtTimer <= 0) { sRenderer.enabled = true; recentlyHurt = false; } } Animation(); //These should only be active one frame KeyJump = false; KeyJumpRel = false; }