private void Reset() { _state = new WalkerState() { RandomIndex = -1, //Random index to pick a direction from the STEP constant array from last lesson Offset = Vector2.Zero, //Current position on the grid Delta = Vector2.Zero, //Direction to increment the offset key above DownCounter = 0, //The number of times the walker moved down without interruption Path = new List <WalkerPath>(), //The level's unobstructed path EmptyCells = new Dictionary <Vector2, int>() //Coordinates of the cells we haven't populated yet }; for (int x = 0; x < GridSize.x; x++) { for (int y = 0; y < GridSize.y; y++) { _state.EmptyCells[new Vector2(x, y)] = 0; } } }
void CheckRoof() { if(Physics2D.Raycast((MyTransform.position + Vector3.up * MyCollider.size.y) + Vector3.left * (MyCollider.size.x / 2f), Vector2.up, minDistanceFromGround, groundMask.value)) { targetHeightVel = 0f; state = WalkerState.FALLING; return; } if(Physics2D.Raycast((MyTransform.position + Vector3.up * MyCollider.size.y) + Vector3.down * (MyCollider.size.x / 2f), Vector2.up, minDistanceFromGround, groundMask.value)) { state = WalkerState.FALLING; targetHeightVel = 0f; return; } }
void CheckGround(Transform groundTransform) { if(state == WalkerState.RISING) { transform.parent = null; CheckRoof(); return; } if(Physics2D.Raycast(MyTransform.position + Vector3.left * (MyCollider.size.x / 2f) + Vector3.down * (MyCollider.size.y / 2f), -Vector2.up, minDistanceFromGround, groundMask.value)) { state = WalkerState.GROUNDED; transform.parent = groundTransform; return; } if(Physics2D.Raycast(MyTransform.position + Vector3.right * (MyCollider.size.x / 2f) + Vector3.down * (MyCollider.size.y / 2f), -Vector2.up, minDistanceFromGround, groundMask.value)) { state = WalkerState.GROUNDED; transform.parent = groundTransform; return; } state = WalkerState.FALLING; transform.parent = null; }
protected override void EnhancedUpdate () { base.EnhancedUpdate (); if(MyTransform.position.y < -10f) { Application.LoadLevel("game"); } minTimeToJump.UpdateTime(Time.deltaTime); if(Input.GetButtonDown("Jump")) { minTimeToJump.Reset(); } if(state == WalkerState.GROUNDED) { targetHeightVel = gravity; Crouched = Input.GetButton("Duck"); if(!minTimeToJump.IsOver(0.05f)) { targetHeightVel = baseJumpForce; state = WalkerState.RISING; } } else if(state == WalkerState.RISING) { targetHeightVel = Mathf.MoveTowards(targetHeightVel, 0f, riseAcceleration); if(!Input.GetKey(KeyCode.Space)) { targetHeightVel = 0f; } if(targetHeightVel <= 0f) { state = WalkerState.FALLING; } } else { targetHeightVel = Mathf.MoveTowards(targetHeightVel, gravity, fallAcceleration); } Debug.DrawRay(MyTransform.position + Vector3.left * (MyCollider.size.x / 2f) + Vector3.up * MyCollider.size.y / 2f, -Vector2.up * minDistanceFromGround, Color.yellow); Debug.DrawRay(MyTransform.position + Vector3.right * (MyCollider.size.x / 2f) + Vector3.up * MyCollider.size.y / 2f, -Vector2.up * minDistanceFromGround, Color.red); Debug.DrawRay((MyTransform.position + Vector3.up * MyCollider.size.y / 2f) + Vector3.left * (MyCollider.size.x / 2f), Vector2.up * minDistanceFromGround, Color.red); Debug.DrawRay((MyTransform.position + Vector3.down * MyCollider.size.y / 2f) + Vector3.right * (MyCollider.size.x / 2f), Vector2.up * minDistanceFromGround, Color.red); }
public void ChangeWalkerState(WalkerState state) { walkerState = state; ChangeWalkerSpriteTexture(); }