duck

Index - PlayerControl

Oct 4th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerControl : CharacterControl {
  5.    
  6.     public Joystick joystickPrefab;
  7.     Joystick joystick;
  8.     Joystick throwButton;
  9.    
  10.     CharacterMotor motor;
  11.     Inventory inventory;
  12.    
  13.     public int lives { get; private set; }
  14.    
  15.     Vector3 startPosition;
  16.     Quaternion startRotation;
  17.    
  18.     void Start()
  19.     {
  20.         startPosition = transform.position;
  21.         startRotation = transform.rotation;
  22.        
  23.         lives = 3;
  24.         alive = true;
  25.         Game.SetPlayer(transform);
  26.         motor = GetComponent<CharacterMotor>();
  27.         inventory = GetComponent<Inventory>(); 
  28.        
  29.         #if UNITY_ANDROID || UNITY_IPHONE
  30.             joystick = (Joystick)Instantiate(joystickPrefab);  
  31.                
  32.             throwButton = (Joystick)Instantiate(joystickPrefab);
  33.             throwButton.touchPad = true;
  34.        
  35.             // Move to right side of screen
  36.             var g = throwButton.GetComponent<GUITexture>();
  37.             var gpi = g.pixelInset;
  38.             gpi.x = Screen.width - gpi.x - gpi.width;
  39.             g.pixelInset = gpi;
  40.        
  41.         #endif
  42.     }
  43.    
  44.     // Update is called once per frame
  45.     void Update () {
  46.         if (alive)
  47.         {
  48.             #if UNITY_ANDROID || UNITY_IPHONE
  49.                    
  50.                     float h = joystick.position.x;
  51.                     float v = joystick.position.y;
  52.                     bool button = throwButton.touched;
  53.             #else
  54.                     float h = Input.GetAxis("Horizontal");
  55.                     float v = Input.GetAxis("Vertical");
  56.                     bool button = Input.GetKeyDown(KeyCode.Space);
  57.             #endif     
  58.            
  59.             motor.MovementInput(h,v);
  60.            
  61.             if (button)
  62.             {
  63.                 inventory.ThrowItem();
  64.             }
  65.         } else {
  66.             motor.MovementInput(0,0);
  67.         }
  68.        
  69.     }
  70.    
  71.     void OnTriggerEnter( Collider c )
  72.     {
  73.         if (c.tag == "ItemDropZone")
  74.         {
  75.             GetComponent<Inventory>().DropItems(); 
  76.         }
  77.     }
  78.    
  79.     public override void HitObject (GameObject obj)
  80.     {
  81.         if (alive)
  82.         {
  83.             if (obj.tag == "Button")
  84.             {
  85.                 obj.GetComponent<Button>().Press ();   
  86.             }
  87.            
  88.             if (obj.tag == "Item")
  89.             {
  90.                 GetComponent<Inventory>().CollectItem(obj);
  91.             }
  92.            
  93.             if (obj.tag == "Enemy")
  94.             {
  95.                 if (obj.GetComponent<CharacterControl>().alive)
  96.                 {
  97.                     StartCoroutine( EnemyHit() );  
  98.                 }
  99.             }
  100.         }
  101.     }
  102.    
  103.     IEnumerator EnemyHit()
  104.     {
  105.         alive = false;
  106.         // -- Play a sound here!
  107.        
  108.         yield return new WaitForSeconds(4);
  109.        
  110.         lives--;
  111.        
  112.         if (lives > 0)
  113.         {
  114.             transform.position = startPosition;
  115.             transform.rotation = startRotation;
  116.             alive = true;
  117.            
  118.         } else {
  119.             // game over   
  120.             Game.GameOver();
  121.         }
  122.        
  123.        
  124.     }
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment