/// <summary> /// If grounded on a surface, assign its surfaceSpeeds to the player movement speeds /// </summary> private void InitializeSurfaceSpeeds() { SurfaceMaterial groundSurface; if (currentState.surfGround != null) { groundSurface = currentState.surfGround.GetComponent <SurfaceMaterial>(); surfaceSpeeds = groundSurface.surfaceSpeeds; if (groundSurface.type == GameController.materialType.BOUNCE) { maintainVelocity = true; } else if (groundSurface.type == GameController.materialType.NONE) { maintainVelocity = false; } currentState.defaultSpeed = surfaceSpeeds.defaultSpeed; currentState.pushSpeed = surfaceSpeeds.pushSpeed; currentState.pullSpeed = surfaceSpeeds.pullSpeed; currentState.slopeSpeed = surfaceSpeeds.upSlopeSpeed; } }
// Update is called once per frame void Update() { SetCurrentSurroundings(); currentState.direction = GetDirection(body.velocity.x) ?? previousState.direction; currentState.oppDirection = GetDirection(-1 * body.velocity.x) ?? previousState.oppDirection; touchingLeftWall = ObjectExists(currentState.objLeft); touchingRightWall = ObjectExists(currentState.objRight); GameController.materialType?groundType = GetMaterial(currentState.objGround); GameController.materialType?prevGroundType = GetMaterial(previousState.objGround); GameController.materialType?wallType = null; if (currentState.direction == Direction.LEFT) { if (currentState.objLeft) { wallType = GetMaterial(currentState.objLeft); } } else { wallType = GetMaterial(currentState.objRight); } float slideSpeed = body.velocity.x; if (groundType != null && groundType != GameController.materialType.BOUNCE) { initialBounce = true; } if (wallType == GameController.materialType.BOUNCE && Mathf.Abs(previousState.velocity.x) >= 0) { gameController.audioController.PlaySoundEffect(AudioController.SoundType.BOUNCE); body.velocity = new Vector2(-maxSlideSpeed * Mathf.Sign(previousState.velocity.x), previousState.velocity.y); } else if (groundType == GameController.materialType.BOUNCE && !(prevGroundType == GameController.materialType.BOUNCE) && Mathf.Abs(body.velocity.y) > 2f) { float initialBounceBonus = 0; if (initialBounce) { initialBounceBonus = 1f; initialBounce = false; } // .79 because guestimation said so gameController.audioController.PlaySoundEffect(AudioController.SoundType.BOUNCE); body.velocity = new Vector2(previousState.velocity.x, Mathf.Abs(previousState.velocity.y) + .79f + initialBounceBonus); } else if (groundType == GameController.materialType.SLIP && currentState.playerLeft == null && currentState.playerRight == null) { GameController.SurfaceSpeeds surfaceSpeeds = currentState.objGround.GetComponent <SurfaceMaterial>().surfaceSpeeds; if (Mathf.Abs(body.velocity.x) > 2f) { if (Mathf.Abs(body.velocity.x) < minSlideSpeed) { if (body.velocity.x > 0) { slideSpeed = minSlideSpeed; } else { slideSpeed = -minSlideSpeed; } } else if (Mathf.Abs(body.velocity.x) > maxSlideSpeed) { if (body.velocity.x > 0) { slideSpeed = maxSlideSpeed; } else { slideSpeed = -maxSlideSpeed; } } body.velocity = new Vector2(slideSpeed, body.velocity.y); } } previousState = currentState; previousState.velocity = body.velocity; }
/// <summary> /// Initializes surface with associated move speeds from surfaceSpeeds (called from derived class) /// </summary> protected void InitializeSurfaceSpeeds(GameController.material materialType) { // Initialize surface speeds surfaceSpeeds = GameObject.FindWithTag("GameController").GetComponent <GameController>().speedMapping[materialType]; }