void TryAttack() { bool isAttack = RBInput.GetButtonDownForPlayer(InputStrings.FIRE, PlayerIndex, playerDevice); if (isAttack) { fighter.SwingWeapon(Fighter.AttackType.Weak); } else { bool isHeavyAttack = Input.GetKeyDown(KeyCode.Mouse1); if (isHeavyAttack) { fighter.SwingWeapon(Fighter.AttackType.Strong); } } bool test = Input.GetKeyDown(KeyCode.Backspace); if (test) { int i = 0; while (i < 1000) { Debug.Log("Yes or no:" + RBRandom.PercentageChance(24.5f).ToString()); i++; } } }
void TryAttack() { bool isLightAttack = RBInput.GetButtonDownForPlayer(InputStrings.FIRE, PlayerIndex); if (isLightAttack) { fighter.AttackWithWeapon(Fighter.AttackType.Weak); } bool isLightReleased = RBInput.GetButtonUpForPlayer(InputStrings.FIRE, PlayerIndex); if (isLightReleased) { fighter.ReleaseWeapon(); } bool isHeavyAttack = RBInput.GetButtonDownForPlayer(InputStrings.FIRE2, PlayerIndex); if (isHeavyAttack) { fighter.AttackWithWeapon(Fighter.AttackType.Strong); } bool isHeavyReleased = RBInput.GetButtonUpForPlayer(InputStrings.FIRE2, PlayerIndex); if (isHeavyReleased) { fighter.ReleaseWeapon(); } }
/* * Checks all input sources for a player pressing "Start" and binds them when they join. */ void PollForNewPlayer() { int nextPlayerIndex = NumPlayers; foreach (InputDevice device in InputDevices.GetAllInputDevices()) { if (!boundDevices.ContainsKey(device) || (IsCheatingEnabled && cheats.BIND_MANY_TO_ONE_DEVICE)) { if (RBInput.GetButtonDownForPlayer(InputStrings.PAUSE, nextPlayerIndex, device)) { BindNextPlayer(device); // Deactivate the splash screen once a player is bound. This is NOT ideal, but // neither is putting a splash screen into every scene. It should be it's own scene. WorldTime worldTime = (WorldTime)GetComponent <WorldTime> (); Transform startPoint = worldTime.startPointP2; if (NumPlayers == 1) { HideSplashScreen(); worldTime.Reset(); } else { players [nextPlayerIndex].GetComponent <PlayerController> ().SnapToPoint(startPoint); } } } } }
/* * Read input for Switch Weapon button and ask Fighter to switch between Sword and Spear. */ void TrySwitchWeapon() { bool isSwitchWeapon = RBInput.GetButtonDownForPlayer(InputStrings.SWITCHWEAPON, PlayerIndex); if (isSwitchWeapon) { fighter.SwitchWeapon(); } }
/* * Reads input and handles action for cycling through the players items in his inventory */ void TryCycleItems() { bool isCycleItems = RBInput.GetButtonDownForPlayer(InputStrings.SWAPITEM, PlayerIndex, playerDevice); if (isCycleItems) { CycleItems(); } }
/* * Set the fighter to bandaging state and start bandaging. */ void TryBandage() { if (RBInput.GetButtonForPlayer(InputStrings.BANDAGE, PlayerIndex, playerDevice)) { fighter.Bandage(); } else { fighter.InterruptBandage(); } }
void TryDodge() { // Get input values float horizontal = 0.0f, vertical = 0.0f; horizontal = RBInput.GetAxisRawForPlayer(InputStrings.HORIZONTAL, PlayerIndex, playerDevice); vertical = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, PlayerIndex, playerDevice); // If player isn't standing still and hits dodge button, let's dodge! if (RBInput.GetButtonDownForPlayer(InputStrings.DODGE, PlayerIndex, playerDevice) && (horizontal != 0 || vertical != 0)) { fighter.Dodge(new Vector3(horizontal, 0.0f, vertical)); } }
/* * Apply movement in the Player's desired directions according to the various speed * and movement variables. */ void TryMove() { // Get input values float horizontal = 0.0f, vertical = 0.0f; horizontal = RBInput.GetAxisRawForPlayer(InputStrings.HORIZONTAL, PlayerIndex); vertical = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, PlayerIndex); // Convert to camera world space Vector3 direction = ConvertInputToCamera(horizontal, vertical); if (direction != Vector3.zero) { fighter.Run(direction); } }
/* * Set fighter to blocking or unblocking depending on button up or down. */ void TryBlock() { if (RBInput.GetAxisForPlayer(InputStrings.BLOCK, PlayerIndex) == 1 || RBInput.GetButtonForPlayer(InputStrings.BLOCK, PlayerIndex)) { fighter.Block(); } else if (RBInput.GetAxisForPlayer(InputStrings.BLOCK, PlayerIndex) < 1 || RBInput.GetButtonUpForPlayer(InputStrings.BLOCK, PlayerIndex)) { if (fighter.isBlocking) { fighter.UnBlock(false); } } }
/* * Set fighter to blocking or unblocking depending on button up or down. */ void TryBlock() { if (RBInput.GetAxisForPlayer(InputStrings.BLOCK, PlayerIndex, playerDevice) == 1 || RBInput.GetButtonForPlayer(InputStrings.BLOCK, PlayerIndex, playerDevice)) { fighter.Block(); } else if (RBInput.GetAxisForPlayer(InputStrings.BLOCK, PlayerIndex, playerDevice) == 0 || RBInput.GetButtonUpForPlayer(InputStrings.BLOCK, PlayerIndex, playerDevice)) { if (fighter.IsBlocking) { fighter.UnBlock(); } } }
/* * Attempts to hoe the action tile */ void TryHoe() { bool isUsingWeapon = RBInput.GetButtonDownForPlayer(InputStrings.WEAPON1, PlayerIndex, playerDevice); if (isUsingWeapon) { if (actionTile != null) { GroundTile tile = (GroundTile)actionTile.GetComponent <GroundTile> (); tile.Hoe(); AudioSource.PlayClipAtPoint(digSound, transform.position); } else { AudioSource.PlayClipAtPoint(digSoundFail, transform.position); } } }
/* * If tile has a plant and player isn't out of water, water it. */ void TryWatering() { bool isWatering = RBInput.GetButtonDownForPlayer(InputStrings.WEAPON2, PlayerIndex, playerDevice); if (isWatering) { if (actionTile != null) { GroundTile tile = (GroundTile)actionTile.GetComponent <GroundTile> (); Plant plant = tile.getPlant(); if (plant != null) { plant.Water(); } SpawnWaterFX(); } } }
void TryDodge() { // Get input values float horizontal = 0.0f, vertical = 0.0f; horizontal = RBInput.GetAxisRawForPlayer(InputStrings.HORIZONTAL, PlayerIndex); vertical = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, PlayerIndex); // Convert to camera world space Vector3 direction = ConvertInputToCamera(horizontal, vertical); // If player isn't standing still and hits dodge button, let's dodge! if (RBInput.GetButtonDownForPlayer(InputStrings.DODGE, PlayerIndex) && direction != Vector3.zero) { fighter.Dodge(direction); } }
/* * Apply movement in the Player's desired directions according to the various speed * and movement variables. */ void Move() { // Get input values float horizontal = 0.0f, vertical = 0.0f; horizontal = RBInput.GetAxisRawForPlayer(InputStrings.HORIZONTAL, PlayerIndex, playerDevice); vertical = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, PlayerIndex, playerDevice); // Determine move direction from target values float targetSpeed = 0.0f; Vector3 targetDirection = new Vector3(horizontal, 0.0f, vertical); if (targetDirection != Vector3.zero) { moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, Mathf.Infinity, 1000); moveDirection = moveDirection.normalized; if (RBInput.GetButtonForPlayer(InputStrings.SPRINT, PlayerIndex, playerDevice)) { targetSpeed = sprintspeed; } else { targetSpeed = movespeed; } } // Get movement vector Vector3 movement = (moveDirection * targetSpeed) + new Vector3(0.0f, verticalSpeed, 0.0f); movement *= Time.deltaTime; // Apply movement vector CharacterController biped = GetComponent <CharacterController> (); collisionFlags = biped.Move(movement); // Rotate to face the direction of movement immediately if (moveDirection != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveDirection); } }
/* * Determine which buttons are being pressed and set our global booleans * for later consumption by the UI. */ void DetectControllerButtons() { GameObject playerObj = FindActivePlayer(); InputDevice device = playerObj.GetComponent <PlayerController> ().playerDevice; // Bumper buttons if (RBInput.GetButtonDownForPlayer(InputStrings.SWAPITEM, activePlayerIndex, device)) { swapPressed = true; } else { swapPressed = false; } // Action button (A) if (RBInput.GetButtonDownForPlayer(InputStrings.ACTION, activePlayerIndex, device)) { actionPressed = true; } else { actionPressed = false; } // Weapon2 buttons (Y) if (RBInput.GetButtonDownForPlayer(InputStrings.WEAPON2, activePlayerIndex, device)) { weaponPressed = true; } else { weaponPressed = false; } // Exit button (B) if (RBInput.GetButtonDownForPlayer(InputStrings.ITEM, activePlayerIndex, device)) { exitPressed = true; } else { exitPressed = false; } }
/* * If no target is selected, pick the first using the ENEMY tag. If one is selected * already, choose the next until the end is reached at which point, select none. This * will need to be refactored to be smarter later, i.e. choose the closest first, then * switch further away. Also this should be a hold down button to keep lock, release to * unlock behavior as well but right now it just tabs through. */ void TrySwitchTarget() { if (RBInput.GetButtonDownForPlayer(InputStrings.TARGET, PlayerIndex, playerDevice)) { GameObject [] enemies = GameObject.FindGameObjectsWithTag(Tags.ENEMY); // Toggle to nothing when user has tabbed through the targets if (curTarget >= enemies.Length) { ResetTargetIndex(); fighter.LoseTarget(); return; } // Select the next target HighlightArrow(true); fighter.LockOnTarget(enemies [curTarget].transform); curTarget++; } }
/* * Check if the tile is a valid tile to be picked and if so, pick it * and handle inventory changes. */ void TryPicking() { bool isAction = RBInput.GetButtonDownForPlayer(InputStrings.ACTION, PlayerIndex, playerDevice); if (isAction) { //.TODO This violates MVC, fix it if (actionTile != null) { GroundTile tile = (GroundTile)actionTile.GetComponent <GroundTile> (); Plant plant = tile.getPlant(); if (plant != null && plant.isRipe()) { Inventory inventory = (Inventory)GetComponent <Inventory> (); int pickedItemID = tile.Pick(); inventory.AddItem(pickedItemID, 1); AudioSource.PlayClipAtPoint(backpackSound, transform.position); } } } }
/* * Apply movement in the Player's desired directions according to the various speed * and movement variables. */ void TryMove() { // Get input values float horizontal = 0.0f, vertical = 0.0f; horizontal = RBInput.GetAxisRawForPlayer(InputStrings.HORIZONTAL, PlayerIndex, playerDevice); vertical = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, PlayerIndex, playerDevice); Vector3 direction = new Vector3(horizontal, 0.0f, vertical); if (direction != Vector3.zero) { if (RBInput.GetButtonForPlayer(InputStrings.SPRINT, PlayerIndex, playerDevice)) { fighter.Sprint(direction); } else { fighter.Run(direction); } } }
/* * Check if the user tried planting and if so, check that location is * a valid place to plant. Then plant it and handle inventory changes. */ void TryPlanting() { bool isUsingItem = RBInput.GetButtonDownForPlayer(InputStrings.ITEM, PlayerIndex, playerDevice); if (isUsingItem) { if (actionTile != null) { GroundTile tile = (GroundTile)actionTile.GetComponent <GroundTile> (); //TODO This violates MVC, fix it if (tile.isSoil() && tile.getPlant() == null) { Inventory inventory = (Inventory)GetComponent <Inventory> (); if (inventory.GetEquippedItem() != null) { GameObject plant = inventory.GetEquippedItem().plantPrefab; tile.Plant(plant); inventory.RemoveItem(inventory.GetEquippedItem().id, 1); } } } } }
/* * Run through the vertical axis input to see if it has changed. If it has, * set the corresponding bools to let the GUI know later. */ void DetectVertAxis() { GameObject playerObj = FindActivePlayer(); InputDevice device = playerObj.GetComponent <PlayerController> ().playerDevice; float axis = RBInput.GetAxisRawForPlayer(InputStrings.VERTICAL, activePlayerIndex, device); bool axisChanged = (Mathf.Sign(axis) != Mathf.Sign(lastVertAxis)) || lastVertAxis == 0; if (axis > 0 && axisChanged) { upPressed = true; downPressed = false; } else if (axis < 0 && axisChanged) { upPressed = false; downPressed = true; } else { upPressed = false; downPressed = false; } lastVertAxis = axis; }
/* * Handle toggling of targetting as well as switching between targets. While holding down * target button, player can cycle through targets. */ void TryTargetting() { float deadStickThreshold = 0.5f; InputDevice xbox = InputDevices.GetAllInputDevices() [(int)InputDevices.ControllerTypes.XBox]; float rightStickPressedAxis = Input.GetAxisRaw(RBInput.ConcatPlayerIndex(InputStrings.TARGET, PlayerIndex, xbox)); bool rightStickPressed = rightStickPressedAxis >= 0.99 && rightStickAvailable; // Consolidate bool for PC and XBox bool isTargetPressed = RBInput.GetButtonDownForPlayer(InputStrings.TARGET, PlayerIndex) || rightStickPressed; // Toggle Targeting on and off if (isTargetPressed) { rightStickAvailable = false; if (!fighter.isLockedOn) { TargetNearest(); } else { fighter.LoseTarget(); } } // Switch between targets if (fighter.isLockedOn) { // PC and Controller controls likely should diverge here due to right stick use InputDevice pc = InputDevices.GetAllInputDevices() [(int)InputDevices.ControllerTypes.Keyboard]; float horizontalTargetAxis = Input.GetAxisRaw(RBInput.ConcatPlayerIndex(InputStrings.TARGETHORIZONTAL, PlayerIndex, xbox)); float verticalTargetAxis = -Input.GetAxisRaw(RBInput.ConcatPlayerIndex(InputStrings.TARGETVERTCIAL, PlayerIndex, xbox)); // Move target in axis direction bool changingTarget = false; float horizontal = 0; float vertical = 0; // // Read in PC input // // Set Horizontal Input for PC if (Input.GetButtonDown(RBInput.ConcatPlayerIndex(InputStrings.TARGETLEFT, PlayerIndex, pc))) { //|| )) { //rightStickHorizontalAvailable = false; //TargetNext (true); horizontal = -1; changingTarget = true; } else if (Input.GetButtonDown(RBInput.ConcatPlayerIndex(InputStrings.TARGETRIGHT, PlayerIndex, pc))) { horizontal = 1; changingTarget = true; } // Set Vertical Input if (Input.GetButtonDown(RBInput.ConcatPlayerIndex(InputStrings.TARGETDOWN, PlayerIndex, pc))) { vertical = -1; changingTarget = true; } else if (Input.GetButtonDown(RBInput.ConcatPlayerIndex(InputStrings.TARGETUP, PlayerIndex, pc))) { vertical = 1; changingTarget = true; } // // Read in XBox input // // Set Horizontal and Vertical values for XBox bool horizontalAxisPressed = rightStickAxisAvailable && (horizontalTargetAxis <-deadStickThreshold || horizontalTargetAxis> deadStickThreshold); bool verticalAxisPressed = rightStickAxisAvailable && (verticalTargetAxis <-deadStickThreshold || verticalTargetAxis> deadStickThreshold); if (horizontalAxisPressed || verticalAxisPressed) { rightStickAxisAvailable = false; TargetNearDirection(horizontalTargetAxis, verticalTargetAxis); changingTarget = true; } // Make target change if (changingTarget) { TargetNearDirection(horizontal, vertical); } // Enforce stick behavior like a button rightStickAxisAvailable = IsAxisDead(horizontalTargetAxis, deadStickThreshold) && IsAxisDead(verticalTargetAxis, deadStickThreshold); } rightStickAvailable = IsAxisDead(rightStickPressedAxis, deadStickThreshold); }