public bool ActionWasReleased(ushort field, UserCmd lastCmd = null) { if (lastCmd != null) { return(lastCmd.ActionIsPressed(field) && !this.ActionIsPressed(field)); } return(false); }
public PlayerState StateUpdate(UserCmd cmd, PlayerState predictedState, float dt) { PlayerState newState = new PlayerState(Vector3.zero, Vector3.zero); Vector3 startingOrigin = predictedState.Origin; Vector3 startingVelocity = predictedState.Velocity; if (m_OverrideVelocity == true) { m_OverrideVelocity = false; startingVelocity = m_CurrentVelocity; } bool moveLeft = cmd.ActionIsPressed(PlayerInputSynchronization.IN_LEFT), moveRight = cmd.ActionIsPressed(PlayerInputSynchronization.IN_RIGHT); bool accel = cmd.ActionIsPressed(PlayerInputSynchronization.IN_ACCELERATE), deccel = cmd.ActionIsPressed(PlayerInputSynchronization.IN_DECCELERATE); Vector3 startingPosition = predictedState.Origin; if (moveLeft ^ moveRight) { m_CurrentAcceleration.x = ((moveLeft) ? -1 : 1) * m_BaseAcceleration; var moveDirection = Mathf.Sign(m_CurrentAcceleration.x); if (moveDirection != m_LastMoveDirection && (m_LastMoveDirection < 0f || m_LastMoveDirection > 0f)) { m_CurrentAcceleration.x *= 1.5f; } m_LastMoveDirection = Mathf.Sign(m_CurrentAcceleration.x); } else { m_CurrentAcceleration.x = 0; } if (m_CurrentAcceleration.x == 0) { startingVelocity = ApplyFriction(dt, startingVelocity); } if (m_ControlThresholdHit) { if (accel ^ deccel) { if (accel) { m_AcceleratorAcceleration = Mathf.Clamp(m_AcceleratorAcceleration + (3 * dt), 0f, m_AcceleratorMaxAcceleration); m_CurrentAcceleration.y = m_AcceleratorAcceleration; } else { m_AcceleratorAcceleration = Mathf.Clamp(m_AcceleratorAcceleration - (3 * dt), 0f, m_AcceleratorMaxAcceleration); } if (deccel) { m_AcceleratorAcceleration = 0f; if (m_CurrentVelocity.y > m_MinimumVerticalVelocity) { m_CurrentAcceleration.y = -m_BrakingDecceleration; } } } } else { if (startingVelocity.y > m_MinimumVerticalVelocity) { m_ControlThresholdHit = true; } } if (!m_RaceBegun) { m_CurrentAcceleration = Vector3.zero; } newState.Velocity = startingVelocity + m_CurrentAcceleration * dt; newState.Velocity.x = Mathf.Clamp(newState.Velocity.x, -m_MaxHorizontalVelocity, m_MaxHorizontalVelocity); newState.Velocity.y = Mathf.Clamp(newState.Velocity.y, (m_ControlThresholdHit) ? m_MinimumVerticalVelocity : 0f, m_MaxVerticalVelocity); newState.Origin = startingOrigin + newState.Velocity * dt; if (isServer) { //Is there a collider where we're trying to move var overlappingColliders = Physics2D.OverlapBoxAll( new Vector2(newState.Origin.x, newState.Origin.y), m_BoxCollider.size, m_CollisionLayer ); foreach (var collider in overlappingColliders) { if (collider.gameObject != this.gameObject) { var colliderOrigin = new Vector2(startingOrigin.x, startingOrigin.y); var colliderDisplacement = (new Vector2(newState.Origin.x, newState.Origin.y) - colliderOrigin); var colliderHit = Physics2D.Raycast( colliderOrigin, colliderDisplacement.normalized, colliderDisplacement.magnitude, m_CollisionLayer ); if (collider.tag == "Ship") { //Do our bounce var otherShip = collider.GetComponent <ShipController>(); Vector3 p1 = startingPosition; Vector3 p2 = otherShip.Position; Vector3 v1 = startingVelocity; Vector3 v2 = otherShip.Velocity; Vector3 collisionVelocity1 = v1 - (Vector3.Dot(v1 - v2, p1 - p2) / ((p1 - p2).magnitude * (p1 - p2).magnitude)) * (p1 - p2); Vector3 collisionVelocity2 = v2 - (Vector3.Dot(v2 - v1, p2 - p1) / ((p2 - p1).magnitude * (p2 - p1).magnitude)) * (p2 - p1); collisionVelocity1.y = v1.y; collisionVelocity2.y = v2.y; otherShip.OverrideNextVelocity(collisionVelocity2); newState = predictedState; newState.Velocity = collisionVelocity1; } if (!m_CollisionImmunity && (collider.tag == "Obstacle") || (collider.tag == "Enemy")) { audiosource.PlayOneShot(sound_hit, 0.7f); TakeDamage(1); newState = predictedState; newState.Velocity.y *= 0.3f; //Slow down StartCoroutine(CollisionImmunity()); } if (!m_CollisionImmunity && collider.tag == "Speed") { audiosource.PlayOneShot(sound_boost, 0.7f); newState = predictedState; newState.Velocity.y *= 2f; //Speed up StartCoroutine(DecaySpeedBoost(m_MaxVerticalVelocity, 0.5f)); m_MaxVerticalVelocity = m_MaxVerticalBoostVelocity; StartCoroutine(CollisionImmunity()); } if (collider.tag == "Border") { newState = predictedState; newState.Velocity.x = 0f; m_CurrentAcceleration.x = 0; } if (collider.tag == "FinishLine") { RaceManager.Instance.FinishRace(); } } } } newState.Velocity.x = Mathf.Clamp(newState.Velocity.x, -m_MaxHorizontalVelocity, m_MaxHorizontalVelocity); newState.Velocity.y = Mathf.Clamp(newState.Velocity.y, (m_ControlThresholdHit) ? m_MinimumVerticalVelocity : 0f, m_MaxVerticalVelocity); m_CurrentVelocity = newState.Velocity; m_CurrentPosition = newState.Origin; return(newState); }