123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /*
- * Copyright (c) 2015 Razeware LLC
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- using UnityEngine;
- using System.Collections;
- using System;
- public class Player : MonoBehaviour {
- //Player parameters
- [Range(1, 2)] //Enables a nifty slider in the editor
- public int playerNumber = 2; //Indicates what player this is: P1 or P2
- public float moveSpeed = 5f;
- public bool canDropBombs = true; //Can the player drop bombs?
- public bool canMove = true; //Can the player move?
- 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
- //Prefabs
- public GameObject bombPrefab;
- //Cached components
- private Rigidbody rigidBody;
- private Transform myTransform;
- private Animator animator;
- // Use this for initialization
- void Start() {
- //Cache the attached components for better performance and less typing
- rigidBody = GetComponent<Rigidbody>();
- myTransform = transform;
- animator = myTransform.FindChild("PlayerModel").GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update() {
- UpdateMovement();
- }
- private void UpdateMovement() {
- animator.SetBool("Walking", false);
- if (!canMove) { //Return if player can't move
- return;
- }
- //Depending on the player number, use different input for moving
- if (playerNumber == 1) {
- UpdatePlayer1Movement();
- }
- else {
- UpdatePlayer2Movement();
- }
- }
- /// <summary>
- /// Updates Player 1's movement and facing rotation using the WASD keys and drops bombs using Space
- /// </summary>
- private void UpdatePlayer1Movement() {
- if (Input.GetKey(KeyCode.W)) { //Up movement
- rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, moveSpeed);
- myTransform.rotation = Quaternion.Euler(0, 0, 0);
- animator.SetBool("Walking",true);
- }
- if (Input.GetKey(KeyCode.A)) { //Left movement
- rigidBody.velocity = new Vector3(-moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
- myTransform.rotation = Quaternion.Euler(0, 270, 0);
- animator.SetBool("Walking", true);
- }
- if (Input.GetKey(KeyCode.S)) { //Down movement
- rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, -moveSpeed);
- myTransform.rotation = Quaternion.Euler(0, 180, 0);
- animator.SetBool("Walking", true);
- }
- if (Input.GetKey(KeyCode.D)) { //Right movement
- rigidBody.velocity = new Vector3(moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
- myTransform.rotation = Quaternion.Euler(0, 90, 0);
- animator.SetBool("Walking", true);
- }
- if (canDropBombs && Input.GetKeyDown(KeyCode.Space)) { //Drop bomb
- DropBomb();
- }
- }
- /// <summary>
- /// Updates Player 2's movement and facing rotation using the arrow keys and drops bombs using Enter or Return
- /// </summary>
- private void UpdatePlayer2Movement() {
- if (Input.GetKey(KeyCode.UpArrow)) { //Up movement
- rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, moveSpeed);
- myTransform.rotation = Quaternion.Euler(0, 0, 0);
- animator.SetBool("Walking", true);
- }
- if (Input.GetKey(KeyCode.LeftArrow)) { //Left movement
- rigidBody.velocity = new Vector3(-moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
- myTransform.rotation = Quaternion.Euler(0, 270, 0);
- animator.SetBool("Walking", true);
- }
- if (Input.GetKey(KeyCode.DownArrow)) { //Down movement
- rigidBody.velocity = new Vector3(rigidBody.velocity.x, rigidBody.velocity.y, -moveSpeed);
- myTransform.rotation = Quaternion.Euler(0, 180, 0);
- animator.SetBool("Walking", true);
- }
- if (Input.GetKey(KeyCode.RightArrow)) { //Right movement
- rigidBody.velocity = new Vector3(moveSpeed, rigidBody.velocity.y, rigidBody.velocity.z);
- myTransform.rotation = Quaternion.Euler(0, 90, 0);
- animator.SetBool("Walking", true);
- }
- 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
- DropBomb();
- }
- }
- /// <summary>
- /// Drops a bomb beneath the player
- /// </summary>
- private void DropBomb() {
- if (bombPrefab) { //Check if bomb prefab is assigned first
- Vector3 BombPos = new Vector3 (Mathf.RoundToInt (myTransform.position.x),
- Mathf.RoundToInt (myTransform.position.y), Mathf.RoundToInt (myTransform.position.z));
- Instantiate(bombPrefab, BombPos, bombPrefab.transform.rotation);
- }
- }
- public void OnTriggerEnter(Collider other) {
- if (other.CompareTag("Explosion")) {
- Debug.Log("P" + playerNumber + " hit by explosion!");
- }
- }
- }
|