Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerControl : CharacterControl {
- public Joystick joystickPrefab;
- Joystick joystick;
- Joystick throwButton;
- CharacterMotor motor;
- Inventory inventory;
- public int lives { get; private set; }
- Vector3 startPosition;
- Quaternion startRotation;
- void Start()
- {
- startPosition = transform.position;
- startRotation = transform.rotation;
- lives = 3;
- alive = true;
- Game.SetPlayer(transform);
- motor = GetComponent<CharacterMotor>();
- inventory = GetComponent<Inventory>();
- #if UNITY_ANDROID || UNITY_IPHONE
- joystick = (Joystick)Instantiate(joystickPrefab);
- throwButton = (Joystick)Instantiate(joystickPrefab);
- throwButton.touchPad = true;
- // Move to right side of screen
- var g = throwButton.GetComponent<GUITexture>();
- var gpi = g.pixelInset;
- gpi.x = Screen.width - gpi.x - gpi.width;
- g.pixelInset = gpi;
- #endif
- }
- // Update is called once per frame
- void Update () {
- if (alive)
- {
- #if UNITY_ANDROID || UNITY_IPHONE
- float h = joystick.position.x;
- float v = joystick.position.y;
- bool button = throwButton.touched;
- #else
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- bool button = Input.GetKeyDown(KeyCode.Space);
- #endif
- motor.MovementInput(h,v);
- if (button)
- {
- inventory.ThrowItem();
- }
- } else {
- motor.MovementInput(0,0);
- }
- }
- void OnTriggerEnter( Collider c )
- {
- if (c.tag == "ItemDropZone")
- {
- GetComponent<Inventory>().DropItems();
- }
- }
- public override void HitObject (GameObject obj)
- {
- if (alive)
- {
- if (obj.tag == "Button")
- {
- obj.GetComponent<Button>().Press ();
- }
- if (obj.tag == "Item")
- {
- GetComponent<Inventory>().CollectItem(obj);
- }
- if (obj.tag == "Enemy")
- {
- if (obj.GetComponent<CharacterControl>().alive)
- {
- StartCoroutine( EnemyHit() );
- }
- }
- }
- }
- IEnumerator EnemyHit()
- {
- alive = false;
- // -- Play a sound here!
- yield return new WaitForSeconds(4);
- lives--;
- if (lives > 0)
- {
- transform.position = startPosition;
- transform.rotation = startRotation;
- alive = true;
- } else {
- // game over
- Game.GameOver();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment