Advertisement
raphael76280

Player

Mar 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.     public bool Player2;
  7.  
  8.     public int Vie;
  9.     public int Munitions;
  10.  
  11.     public Rigidbody2D p_rb;
  12.     public float v_saut;
  13.     public float vitesse;
  14.     public float v_saut_max;
  15.     public float vitesse_max;
  16.  
  17.     public bool construction;
  18.     public GameObject constructionMenu;
  19.     public GameObject selecteur;
  20.  
  21.     public GameObject blocPrefab;
  22.  
  23.     public SpriteRenderer aspect_selecteur;
  24.     public Color redAlpha;
  25.     public Color grayAlpha;
  26.     public Color greenAlpha;
  27.     public Color yellowAlpha;
  28.  
  29.     public int BlockingLayer;
  30.  
  31.     public bool SnowPile;
  32.  
  33.     public GameObject snowballPrefab;
  34.     // Use this for initialization
  35.     void Start () {
  36.         Vie = 100;
  37.         Munitions = 5000;
  38.  
  39.         p_rb = this.gameObject.GetComponent<Rigidbody2D> ();
  40.  
  41.         constructionMenu.SetActive (false);
  42.     }
  43.    
  44.     // Update is called once per frame
  45.     void Update () {
  46.         Debug.DrawRay(this.transform.position, new Vector3(this.transform.position.x + Input.GetAxis ("Top360X") *10, Mathf.Abs(this.transform.position.y) + Input.GetAxis ("Top360Y") *10, this.transform.position.z), Color.red);
  47.         Debug.DrawRay(this.transform.position, new Vector3(this.transform.position.x + Input.GetAxis ("Bot360X") *10, Mathf.Abs(this.transform.position.y) + Input.GetAxis ("Bot360Y") *10, this.transform.position.z), Color.blue);
  48.         Debug.DrawRay(this.transform.position, new Vector3(this.transform.position.x + Input.GetAxis ("Cross360X") *10, Mathf.Abs(this.transform.position.y) + Input.GetAxis ("Cross360Y") *10, this.transform.position.z), Color.green);
  49.  
  50.         if (Player2 == false) {
  51.             if (construction == false) {
  52.                 if (Input.GetKey (KeyCode.Z) || Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.Space)) {
  53.                     if (p_rb.velocity.y == 0) {
  54.                         p_rb.AddForce (Vector2.up * v_saut);
  55.                     }
  56.                 }
  57.  
  58.                 if (Input.GetKey(KeyCode.Q) || Input.GetKey (KeyCode.LeftArrow)){
  59.                     p_rb.AddForce (Vector2.left * vitesse);
  60.                 }
  61.  
  62.                 if (Input.GetKey(KeyCode.D) || Input.GetKey (KeyCode.RightArrow)){
  63.                     p_rb.AddForce (Vector2.right * vitesse);
  64.                 }
  65.  
  66.                 if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
  67.                     p_rb.AddForce (Vector2.up * -v_saut);
  68.                     this.transform.localScale = new Vector3 (1, 1, 1);
  69.                 } else {
  70.                     this.transform.localScale = new Vector3 (1, 2, 1);
  71.                 }
  72.  
  73.                 if (Input.GetKeyDown(KeyCode.Mouse0)){
  74.                     tir_ramasser ();
  75.                 }
  76.  
  77.                 if (Input.GetKeyDown (KeyCode.Mouse1)) {
  78.                     ouvrir_construction ();
  79.                 }
  80.  
  81.             } else {
  82.                 aspect_selecteur = selecteur.GetComponent<SpriteRenderer> ();
  83.                 RaycastHit2D center;
  84.                 RaycastHit2D left;
  85.                 RaycastHit2D right;
  86.                 RaycastHit2D up;
  87.                 RaycastHit2D down;
  88.  
  89.                 center = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y - 0.5f), Vector2.zero, 0.4f);
  90.                 left = Physics2D.Raycast (new Vector2(selecteur.transform.position.x, selecteur.transform.position.y - 0.5f), Vector2.left, 0.4f);
  91.                 right = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 1f, selecteur.transform.position.y - 0.5f), Vector2.right, 0.4f);
  92.                 up = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y), Vector2.up, 0.4f);
  93.                 down = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y - 1f), Vector2.down, 0.4f);
  94.  
  95.                 int x = 0;
  96.                 int y = 0;
  97.  
  98.                 if (Input.GetKey (KeyCode.Z) || Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.Space)) {
  99.                     if (up.transform == null || up.transform.gameObject.layer != BlockingLayer || up.transform.gameObject.tag == "Bloc") {
  100.                         y = y+1;
  101.                     }
  102.                 }
  103.  
  104.                 if (Input.GetKey(KeyCode.Q) || Input.GetKey (KeyCode.LeftArrow)){
  105.                     if (left.transform == null || left.transform.gameObject.layer != BlockingLayer || left.transform.gameObject.tag == "Bloc") {
  106.                         x= x-1;
  107.                     }
  108.                 }
  109.  
  110.                 if (Input.GetKey(KeyCode.D) || Input.GetKey (KeyCode.RightArrow)){
  111.                     if (right.transform == null || right.transform.gameObject.layer != BlockingLayer || right.transform.gameObject.tag == "Bloc") {
  112.                         x=x+1;
  113.                     }
  114.                 }
  115.  
  116.                 if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
  117.                     if (down.transform == null || down.transform.gameObject.layer != BlockingLayer || down.transform.gameObject.tag == "Bloc") {
  118.                         y=y-1;
  119.                     }
  120.                 }
  121.  
  122.                 if (center.transform == null) {
  123.                     if (left.transform != null && left.transform.gameObject.layer == BlockingLayer || right.transform != null && right.transform.gameObject.layer == BlockingLayer || up.transform != null && up.transform.gameObject.layer == BlockingLayer || down.transform != null && down.transform.gameObject.layer == BlockingLayer) {
  124.                             aspect_selecteur.color = greenAlpha;
  125.                     } else {
  126.                         aspect_selecteur.color = redAlpha;
  127.                     }
  128.                 } else if (center.transform.gameObject.tag == "Bloc") {
  129.                     aspect_selecteur.color = yellowAlpha;
  130.                 }
  131.  
  132.                 if (Input.GetKeyDown(KeyCode.Mouse0)){
  133.                     if (center.transform == null) {
  134.                         if (left.transform != null && left.transform.gameObject.layer == BlockingLayer || right.transform != null && right.transform.gameObject.layer == BlockingLayer || up.transform != null && up.transform.gameObject.layer == BlockingLayer || down.transform != null && down.transform.gameObject.layer == BlockingLayer) {
  135.                             construire ();
  136.                         }
  137.                     } else if (center.transform.gameObject.tag == "Bloc") {
  138.                         upgrade (center.transform.gameObject);
  139.                     }
  140.                 }
  141.  
  142.                 if (Input.GetKeyDown (KeyCode.Mouse1)) {
  143.                     ouvrir_construction ();
  144.                 }
  145.                    
  146.                 selecteur.transform.position = new Vector2 (selecteur.transform.position.x + (x*Time.fixedDeltaTime*vitesse*0.6f), selecteur.transform.position.y + (y*Time.fixedDeltaTime*vitesse*0.6f));
  147.             }
  148.         } else {
  149.             if (construction == false) {
  150.                 if (Input.GetAxis ("Top360Y") > 0.5f || Input.GetAxis ("Cross360Y") < -0.5f || Input.GetKeyDown(KeyCode.JoystickButton0)) {
  151.                     if (p_rb.velocity.y == 0) {
  152.                         p_rb.AddForce (Vector2.up * v_saut);
  153.                     }
  154.                 }
  155.  
  156.                 if (Input.GetAxis ("Top360X") < 0 || Input.GetAxis ("Cross360X") < 0){
  157.                     p_rb.AddForce (Vector2.left * vitesse);
  158.                 }
  159.  
  160.                 if (Input.GetAxis ("Top360X") > 0 || Input.GetAxis ("Cross360X") > 0){
  161.                     p_rb.AddForce (Vector2.right * vitesse);
  162.                 }
  163.  
  164.                 if (Input.GetAxis ("Top360Y") < -0.5f || Input.GetAxis ("Cross360Y") > 0.5f) {
  165.                     p_rb.AddForce (Vector2.up * -v_saut);
  166.                     this.transform.localScale = new Vector3 (1, 1, 1);
  167.                 } else {
  168.                     this.transform.localScale = new Vector3 (1, 2, 1);
  169.                 }
  170.  
  171.  
  172.                 if (Input.GetAxis("Gachette360") > 0 || Input.GetKeyDown(KeyCode.JoystickButton2)){
  173.                     tir_ramasser ();
  174.                 }
  175.  
  176.                 if (Input.GetKeyDown(KeyCode.JoystickButton1)) {
  177.                     ouvrir_construction ();
  178.                 }
  179.  
  180.             } else {
  181.                 aspect_selecteur = selecteur.GetComponent<SpriteRenderer> ();
  182.                 RaycastHit2D center;
  183.                 RaycastHit2D left;
  184.                 RaycastHit2D right;
  185.                 RaycastHit2D up;
  186.                 RaycastHit2D down;
  187.  
  188.                 center = Physics2D.Linecast (selecteur.transform.position, new Vector2 (selecteur.transform.position.x, selecteur.transform.position.y));
  189.                 left = Physics2D.Raycast (new Vector2(this.transform.position.x - 0.1f, this.transform.position.y - 0.5f), Vector2.left, 1f);
  190.                 right = Physics2D.Raycast (new Vector2(this.transform.position.x + 1.1f, this.transform.position.y - 0.5f), Vector2.right, 1f);
  191.                 up = Physics2D.Raycast (new Vector2(this.transform.position.x + 0.5f, this.transform.position.y + 0.1f), Vector2.up, 1f);
  192.                 down = Physics2D.Raycast (new Vector2(this.transform.position.x + 0.5f, this.transform.position.y - 1.1f), Vector2.down, 1f);
  193.  
  194.                 float x = 0;
  195.                 float y = 0;
  196.  
  197.                 if (Input.GetAxis ("Top360Y") > 0 || Input.GetAxis ("Cross360Y") > 0.5f || Input.GetKeyDown(KeyCode.JoystickButton0)) {
  198.                     if (up.transform == null || up.transform.gameObject.layer != BlockingLayer) {
  199.                         y = Input.GetAxis ("Top360Y");
  200.                     }
  201.                 }
  202.  
  203.                 if (Input.GetAxis ("Top360X") < 0 || Input.GetAxis ("Cross360X") < 0){
  204.                     if (left.transform == null || left.transform.gameObject.layer != BlockingLayer) {
  205.                         x= Input.GetAxis ("Top360X");
  206.                     }
  207.                 }
  208.  
  209.                 if (Input.GetAxis ("Top360X") > 0 || Input.GetAxis ("Cross360X") > 0){
  210.                     if (right.transform == null || right.transform.gameObject.layer != BlockingLayer) {
  211.                         x=Input.GetAxis ("Top360X");
  212.                     }
  213.                 }
  214.  
  215.                 if (Input.GetAxis ("Top360Y") < 0 || Input.GetAxis ("Cross360Y") < -0.5f) {
  216.                     if (down.transform == null || down.transform.gameObject.layer != BlockingLayer) {
  217.                         y=Input.GetAxis ("Top360Y");
  218.                     }
  219.                 }
  220.  
  221.                 if (center.transform == null) {
  222.                     if (left.transform != null && left.transform.gameObject.layer == BlockingLayer || right.transform != null && right.transform.gameObject.layer == BlockingLayer || up.transform != null && up.transform.gameObject.layer == BlockingLayer || down.transform != null && down.transform.gameObject.layer == BlockingLayer) {
  223.                         aspect_selecteur.color = greenAlpha;
  224.                     } else {
  225.                         aspect_selecteur.color = redAlpha;
  226.                     }
  227.                 } else if (center.transform.gameObject.tag == "Bloc") {
  228.                     aspect_selecteur.color = yellowAlpha;
  229.                 }
  230.                 /*
  231.                     Construire !
  232.                 */
  233.  
  234.                 if (Input.GetKeyDown(KeyCode.JoystickButton1)) {
  235.                     ouvrir_construction ();
  236.                 }
  237.                 selecteur.transform.position = new Vector2 (selecteur.transform.position.x + (x*Time.fixedDeltaTime*vitesse*0.6f), selecteur.transform.position.y + (y*Time.fixedDeltaTime*vitesse*0.6f));
  238.  
  239.             }
  240.  
  241.         }
  242.        
  243.     }
  244.  
  245.     void LateUpdate() {
  246.         p_rb.velocity = new Vector2(Mathf.Clamp(p_rb.velocity.x, -vitesse_max, vitesse_max), Mathf.Clamp(p_rb.velocity.y, -100, v_saut_max));
  247.  
  248.         if (construction == true){
  249.             if (Player2 == false) {
  250.                 if (Input.GetKeyUp (KeyCode.Z) || Input.GetKeyUp (KeyCode.UpArrow) || Input.GetKeyUp (KeyCode.Space) || Input.GetKeyUp (KeyCode.Q) || Input.GetKeyUp (KeyCode.LeftArrow) || Input.GetKeyUp (KeyCode.D) || Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.S) || Input.GetKeyUp (KeyCode.DownArrow)) {
  251.                     selecteur.transform.position = new Vector2 (Mathf.RoundToInt (selecteur.transform.position.x), Mathf.RoundToInt (selecteur.transform.position.y));
  252.                 }
  253.             } else {
  254.                 if (Input.GetAxis ("Top360Y") == 0 && Input.GetAxis ("Top360X") == 0) {
  255.                     selecteur.transform.position = new Vector2 (Mathf.RoundToInt (selecteur.transform.position.x), Mathf.RoundToInt (selecteur.transform.position.y));
  256.                 }
  257.             }
  258.         }
  259.  
  260.     }
  261.  
  262.  
  263.  
  264.     void ouvrir_construction(){
  265.         if (construction == false) {
  266.             constructionMenu.SetActive (true);
  267.             construction = true;
  268.         }else{
  269.             constructionMenu.SetActive (false);
  270.             construction = false;
  271.         }
  272.  
  273.     }
  274.  
  275.     void construire(){
  276.         if (Munitions >= 100) {
  277.             Munitions = Munitions - 100;
  278.             GameObject obj = Instantiate (blocPrefab, selecteur.transform.position, Quaternion.identity) as GameObject;
  279.         }
  280.     }
  281.  
  282.     void upgrade(GameObject obj){
  283.         Bloc bloc = obj.GetComponent<Bloc> ();
  284.         if (bloc.bloc == "Neige") {
  285.             if (Munitions >= 150) {
  286.                 bloc.Upgrade ();
  287.                 Munitions = Munitions -150;
  288.             }
  289.         } else if (bloc.bloc == "Glace"){
  290.  
  291.         }
  292.     }
  293.  
  294.     public void OnTriggerEnter2D(Collider2D other){
  295.         if (other.tag == "SnowPile") {
  296.             SnowPile = true;
  297.         }
  298.  
  299.         if (other.tag == "SnowBall") {
  300.             Vie = Vie - 5;
  301.             Destroy (other);
  302.         }
  303.     }
  304.  
  305.     public void OnTriggerExit2D(Collider2D other){
  306.         if (other.tag == "SnowPile") {
  307.             SnowPile = false;
  308.         }
  309.     }
  310.  
  311.     void tir_ramasser(){
  312.         if (SnowPile == true) {
  313.             Munitions++;
  314.         } else {
  315.             GameObject obj = Instantiate (snowballPrefab, new Vector2(this.transform.position.x + 1, this.transform.position.y + 1), Quaternion.identity) as GameObject;
  316.             Rigidbody2D objRb = obj.GetComponent<Rigidbody2D> ();
  317.             Vector2 mousePos = Input.mousePosition;
  318.             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
  319.             float x = mousePos.x - this.transform.position.x;
  320.             float y = mousePos.y - this.transform.position.y;
  321.             objRb.AddForce (new Vector2 (x * vitesse * 10 , y * vitesse * 10));
  322.         }
  323.     }
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement