Player.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2015 Razeware LLC
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. using UnityEngine;
  23. using System.Collections;
  24. using System;
  25. public class Player : MonoBehaviour {
  26. //Player parameters
  27. [Range(1, 2)] //Enables a nifty slider in the editor
  28. public int playerNumber = 2; //Indicates what player this is: P1 or P2
  29. public float moveSpeed = 5f;
  30. public bool canDropBombs = true; //Can the player drop bombs?
  31. public bool canMove = true; //Can the player move?
  32. private int bombs = 2; //Amount of bombs the player has left to drop, gets decreased as the player drops a bomb, increases as an owned bomb explodes
  33. //Prefabs
  34. public GameObject bombPrefab;
  35. //Cached components
  36. private Rigidbody rigidBody;
  37. private Transform myTransform;
  38. private Animator animator;
  39. // Use this for initialization
  40. void Start() {
  41. //Cache the attached components for better performance and less typing
  42. rigidBody = GetComponent<Rigidbody>();
  43. myTransform = transform;
  44. animator = myTransform.FindChild("PlayerModel").GetComponent<Animator>();
  45. }
  46. // Update is called once per frame
  47. void Update() {
  48. UpdateMovement();
  49. }
  50. private void UpdateMovement() {
  51. animator.SetBool("Walking", false);
  52. if (!canMove) { //Return if player can't move
  53. return;
  54. }
  55. //Depending on the player number, use different input for moving
  56. if (playerNumber == 1) {
  57. UpdatePlayer1Movement();
  58. }
  59. else {
  60. UpdatePlayer2Movement();
  61. }
  62. }
  63. /// <summary>
  64. /// Updates Player 1's movement and facing rotation using the WASD keys and drops bombs using Space
  65. /// </summary>
  66. private void UpdatePlayer1Movement() {
  67. if (Input.GetKey(KeyCode.W)) { //Up movement
  68. rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, moveSpeed);
  69. myTransform.rotation = Quaternion.Euler(0, 0, 0);
  70. animator.SetBool("Walking",true);
  71. }
  72. if (Input.GetKey(KeyCode.A)) { //Left movement
  73. rigidBody.velocity = new Vector3(-moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
  74. myTransform.rotation = Quaternion.Euler(0, 270, 0);
  75. animator.SetBool("Walking", true);
  76. }
  77. if (Input.GetKey(KeyCode.S)) { //Down movement
  78. rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, -moveSpeed);
  79. myTransform.rotation = Quaternion.Euler(0, 180, 0);
  80. animator.SetBool("Walking", true);
  81. }
  82. if (Input.GetKey(KeyCode.D)) { //Right movement
  83. rigidBody.velocity = new Vector3(moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
  84. myTransform.rotation = Quaternion.Euler(0, 90, 0);
  85. animator.SetBool("Walking", true);
  86. }
  87. if (canDropBombs && Input.GetKeyDown(KeyCode.Space)) { //Drop bomb
  88. DropBomb();
  89. }
  90. }
  91. /// <summary>
  92. /// Updates Player 2's movement and facing rotation using the arrow keys and drops bombs using Enter or Return
  93. /// </summary>
  94. private void UpdatePlayer2Movement() {
  95. if (Input.GetKey(KeyCode.UpArrow)) { //Up movement
  96. rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, moveSpeed);
  97. myTransform.rotation = Quaternion.Euler(0, 0, 0);
  98. animator.SetBool("Walking", true);
  99. }
  100. if (Input.GetKey(KeyCode.LeftArrow)) { //Left movement
  101. rigidBody.velocity = new Vector3(-moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
  102. myTransform.rotation = Quaternion.Euler(0, 270, 0);
  103. animator.SetBool("Walking", true);
  104. }
  105. if (Input.GetKey(KeyCode.DownArrow)) { //Down movement
  106. rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, -moveSpeed);
  107. myTransform.rotation = Quaternion.Euler(0, 180, 0);
  108. animator.SetBool("Walking", true);
  109. }
  110. if (Input.GetKey(KeyCode.RightArrow)) { //Right movement
  111. rigidBody.velocity = new Vector3(moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
  112. myTransform.rotation = Quaternion.Euler(0, 90, 0);
  113. animator.SetBool("Walking", true);
  114. }
  115. if (canDropBombs && (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))) { //Drop Bomb. For Player 2's bombs, allow both the numeric enter as the return key or players without a numpad will be unable to drop bombs
  116. DropBomb();
  117. }
  118. }
  119. /// <summary>
  120. /// Drops a bomb beneath the player
  121. /// </summary>
  122. private void DropBomb() {
  123. if (bombPrefab) { //Check if bomb prefab is assigned first
  124. Vector3 BombPos = new Vector3 (Mathf.RoundToInt (myTransform.position.x),
  125. Mathf.RoundToInt (myTransform.position.y), Mathf.RoundToInt (myTransform.position.z));
  126. Instantiate(bombPrefab, BombPos, bombPrefab.transform.rotation);
  127. }
  128. }
  129. public void OnTriggerEnter(Collider other) {
  130. if (other.CompareTag("Explosion")) {
  131. Debug.Log("P" + playerNumber + " hit by explosion!");
  132. }
  133. }
  134. }