// ==================================================================================================================== // Initializes the game state with the given beetle count (creating data arrays / assigning controls.) public static void Initialize(int beetleCount) { BEETLE_COUNT = beetleCount; BEETLE_CONTROLS = new BeetleControls[beetleCount]; BEETLE_MODEL_CHOICE = new int[beetleCount]; BEETLE_PALETTE_CHOICE = new int[beetleCount]; for (int i = 0; i < beetleCount; i++) { BEETLE_PALETTE_CHOICE[i] = i; } // First player BEETLE_CONTROLS[0] = new BeetleControls(KeyCode.A, KeyCode.D, KeyCode.W, KeyCode.S); // Second player bool gamepad = Input.GetJoystickNames().Length > 0; if (beetleCount >= 2) { if (gamepad) { BEETLE_CONTROLS[1] = new BeetleControls("Gamepad1Horizontal", "Gamepad1Vertical"); } else { BEETLE_CONTROLS[1] = new BeetleControls(KeyCode.J, KeyCode.L, KeyCode.I, KeyCode.K); } } // Third player bool secondGamepad = Input.GetJoystickNames().Length > 1; if (beetleCount >= 3) { if (secondGamepad) { BEETLE_CONTROLS[2] = new BeetleControls("Gamepad2Horizontal", "Gamepad2Vertical"); } else if (gamepad) { BEETLE_CONTROLS[2] = new BeetleControls(KeyCode.J, KeyCode.L, KeyCode.I, KeyCode.K); } else { BEETLE_CONTROLS[2] = new BeetleControls(KeyCode.Keypad4, KeyCode.Keypad6, KeyCode.Keypad8, KeyCode.Keypad5); } } }
private void Update() { // Read input bool left = false; bool right = false; bool up = false; bool down = false; if (acceptInput) { BeetleControls controls = Game.BEETLE_CONTROLS[index]; if (controls.useGamepad) { float h = Input.GetAxisRaw(controls.hor); float v = Input.GetAxisRaw(controls.ver); left = (h < -0.5f && h < gamepadHorPrev); right = (h > +0.5f && h > gamepadHorPrev); up = (v < -0.5f && v < gamepadVerPrev); down = (v > +0.5f && v > gamepadVerPrev); gamepadHorPrev = h; gamepadVerPrev = v; } else { left = Input.GetKeyDown(controls.left); right = Input.GetKeyDown(controls.right); up = Input.GetKeyDown(controls.up); down = Input.GetKeyDown(controls.down); } } // ==================================================================================== // Allow customization switch (state) { case State.ModelSelect: messageText.text = "CHOOSE A MODEL"; if (down) { state = State.PaletteSelect; } else { int cur = Game.BEETLE_MODEL_CHOICE[index]; int add = (right ? 1 : 0) - (left ? 1 : 0); int len = data.models.Length; Game.BEETLE_MODEL_CHOICE[index] = (cur + add + len) % len; if (add != 0) { visuals.UpdateAppearance(); visuals.Jiggle(Mathf.Abs(add) * -0.05f); MusicManager.Play(Sound.Tick); messageX.velocity += add * 10f; } } break; case State.PaletteSelect: messageText.text = "CHOOSE A PALETTE"; if (down) { state = State.Ready; } else if (up) { state = State.ModelSelect; } else { int cur = Game.BEETLE_PALETTE_CHOICE[index]; int add = (right ? 1 : 0) - (left ? 1 : 0); int len = data.palettes.Length; Game.BEETLE_PALETTE_CHOICE[index] = (cur + add + len) % len; if (add != 0) { visuals.UpdateAppearance(); visuals.Jiggle(Mathf.Abs(add) * -0.05f); MusicManager.Play(Sound.Tick); messageX.velocity += add * 10f; } } break; case State.Ready: messageText.text = "READY!"; if (up) { state = State.PaletteSelect; } break; } if (up != down) { MusicManager.Play(Sound.Select); messageY.velocity += (up ? 5f : 0f) - (down ? 5f : 0f); } // ==================================================================================== visuals.transform.rotation = Quaternion.Euler(0, index * 120f + Time.time * 20f, 0); messageText.rectTransform.anchoredPosition = new Vector2(messageX, messageY - 25); }