void Update() { // input 처리 keyDowns = GetKeyDowns(); // 플레이어 이동 player.Move(keyDowns); // 게임오버 처리 if (IsGameOver()) { Gameover(); } // UI 설정 SetUIs(); }
public void Initialize(ControlMode controlMode) { List <Text> texts = new List <Text>(); GetComponentsInChildren <Text>(false, texts); foreach (Text text in texts) { string name = text.gameObject.name; textBoxes.Add(new NamedField(name, text)); } keyDowns = new KeyDowns(false, false, false, false, false, false); switch (controlMode) { case ControlMode.Gyro: { touchControl.SetActive(false); buttonControl.SetActive(false); break; } case ControlMode.Buttons: { touchControl.SetActive(false); buttonControl.SetActive(true); break; } case ControlMode.Touch: { touchControl.SetActive(true); buttonControl.SetActive(false); break; } default: break; } }
private KeyDowns GetKeyDowns() { bool up, down, right, left, shift, space; switch (controlMode) { case ControlMode.Gyro: { up = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) || Input.acceleration.y > sensitivity; down = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) || Input.acceleration.y < -sensitivity; right = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) || Input.acceleration.x > sensitivity; left = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) || Input.acceleration.x < -sensitivity; shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.Mouse0); space = Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Mouse0); break; } case ControlMode.Touch: { return(uiManager.GetButtonStates()); } case ControlMode.Buttons: { return(uiManager.GetButtonStates()); } default: { up = false; down = false; right = false; left = false; shift = false; space = false; break; } } KeyDowns keyDowns = new KeyDowns(up, down, right, left, shift, space); return(keyDowns); }
public void Move(KeyDowns keys) { // 기본상수 계산 float velocity = speed; float vFactor_X = (keys.right == keys.left) ? 0 : (keys.right ? 1 : -1); float vFactor_Y = (keys.up == keys.down) ? 0 : (keys.up ? 1 : -1); float deltaTime = Time.deltaTime; if (isJumpEnabled)// 점프 관련 계산 { bool space = keys.space; bool ground = GroundCheck(); bool timeout = Time.time > jumpStartTime + jumpDuration; int prevstate = jumpState; // JumpState 변경 switch (jumpState % 4) { case 0: // Ground { if (space) { jumpState = 1; jumpStartTime = Time.time; } break; } case 1: // Ascending { if (!space) { jumpState += 2; jumpStartTime = Time.time; } else if (timeout) { jumpState += 1; jumpStartTime = Time.time; } break; } case 2: // Timeout Decending { if (!space) { jumpState += 1; } else if (ground) { jumpState = 0; } break; } case 3: // Decending { if (space && (jumpState + 1) / 4 < MAXJUMP) { jumpState += 2; jumpStartTime = Time.time; } else if (ground) { jumpState = 0; } break; } default: break; } // JumpState 변경에 따른 상수 계산 switch (jumpState % 4) { case 0: // Ground { vFactor_Y = 0; break; } case 1: // Ascending { vFactor_Y = 1f - (Time.time - jumpStartTime) / jumpDuration; break; } case 2: // Timeout Decending { vFactor_Y = -1 * Mathf.Min((Time.time - jumpStartTime) / jumpDuration * 2, 1f); break; } case 3: // Decending { vFactor_Y = -1 * Mathf.Min((Time.time - jumpStartTime) / jumpDuration * 2, 1f); break; } default: break; } vFactor_Y *= jumpSpeed; } if (isDashEnabled)// 대쉬 관련 계산 { bool shift = keys.shift; if (shift) { if (isDashing) //대쉬계속 { dashTimeRemain -= deltaTime; if (dashTimeRemain < 0) // 지속시간 끝나면 대쉬종료 { isDashing = false; dashTimeRemain = 0; dashCooldownRemain = dashCooldown; } } else if (dashCooldownRemain == 0) // 대쉬 시작 { dashTimeRemain = dashTime; isDashing = true; } } else if (isDashing) //대쉬종료 { isDashing = false; dashTimeRemain = 0; dashCooldownRemain = dashCooldown; } // 쿨다운 if (dashCooldownRemain > 0) { dashCooldownRemain = Mathf.Max(0f, dashCooldownRemain - deltaTime); } if (isDashing) { vFactor_X *= 2; } } // 움직임 벡터 계산 Vector3 dsVector; dsVector = new Vector3(vFactor_X * deltaTime * velocity, vFactor_Y * deltaTime * velocity, 0); // 스프라이트 조정 SpriteRenderer sprite = GetComponent <SpriteRenderer>(); sprite.flipX = vFactor_X < 0; sprite.color = isDashing ? new Color(1, 1, 1, 0.5f) : Color.white; Animator animator = GetComponent <Animator>(); if (vFactor_Y == 0 && vFactor_X == 0) { animator.Play("Stop"); } else if (vFactor_Y == 0 && vFactor_X != 0) { animator.Play("Walk"); } else if (vFactor_Y > 0) { animator.Play("Jump"); } else { animator.Play("Fall"); } // 캐릭터 위치 조정 transform.localPosition = ClampPosition(transform.localPosition + dsVector); }