Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour {
- public bool Player2;
- public int Vie;
- public int Munitions;
- public Rigidbody2D p_rb;
- public float v_saut;
- public float vitesse;
- public float v_saut_max;
- public float vitesse_max;
- public bool construction;
- public GameObject constructionMenu;
- public GameObject selecteur;
- public GameObject blocPrefab;
- public SpriteRenderer aspect_selecteur;
- public Color redAlpha;
- public Color grayAlpha;
- public Color greenAlpha;
- public Color yellowAlpha;
- public int BlockingLayer;
- public bool SnowPile;
- public GameObject snowballPrefab;
- // Use this for initialization
- void Start () {
- Vie = 100;
- Munitions = 5000;
- p_rb = this.gameObject.GetComponent<Rigidbody2D> ();
- constructionMenu.SetActive (false);
- }
- // Update is called once per frame
- void Update () {
- 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);
- 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);
- 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);
- if (Player2 == false) {
- if (construction == false) {
- if (Input.GetKey (KeyCode.Z) || Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.Space)) {
- if (p_rb.velocity.y == 0) {
- p_rb.AddForce (Vector2.up * v_saut);
- }
- }
- if (Input.GetKey(KeyCode.Q) || Input.GetKey (KeyCode.LeftArrow)){
- p_rb.AddForce (Vector2.left * vitesse);
- }
- if (Input.GetKey(KeyCode.D) || Input.GetKey (KeyCode.RightArrow)){
- p_rb.AddForce (Vector2.right * vitesse);
- }
- if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
- p_rb.AddForce (Vector2.up * -v_saut);
- this.transform.localScale = new Vector3 (1, 1, 1);
- } else {
- this.transform.localScale = new Vector3 (1, 2, 1);
- }
- if (Input.GetKeyDown(KeyCode.Mouse0)){
- tir_ramasser ();
- }
- if (Input.GetKeyDown (KeyCode.Mouse1)) {
- ouvrir_construction ();
- }
- } else {
- aspect_selecteur = selecteur.GetComponent<SpriteRenderer> ();
- RaycastHit2D center;
- RaycastHit2D left;
- RaycastHit2D right;
- RaycastHit2D up;
- RaycastHit2D down;
- center = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y - 0.5f), Vector2.zero, 0.4f);
- left = Physics2D.Raycast (new Vector2(selecteur.transform.position.x, selecteur.transform.position.y - 0.5f), Vector2.left, 0.4f);
- right = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 1f, selecteur.transform.position.y - 0.5f), Vector2.right, 0.4f);
- up = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y), Vector2.up, 0.4f);
- down = Physics2D.Raycast (new Vector2(selecteur.transform.position.x + 0.5f, selecteur.transform.position.y - 1f), Vector2.down, 0.4f);
- int x = 0;
- int y = 0;
- if (Input.GetKey (KeyCode.Z) || Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.Space)) {
- if (up.transform == null || up.transform.gameObject.layer != BlockingLayer || up.transform.gameObject.tag == "Bloc") {
- y = y+1;
- }
- }
- if (Input.GetKey(KeyCode.Q) || Input.GetKey (KeyCode.LeftArrow)){
- if (left.transform == null || left.transform.gameObject.layer != BlockingLayer || left.transform.gameObject.tag == "Bloc") {
- x= x-1;
- }
- }
- if (Input.GetKey(KeyCode.D) || Input.GetKey (KeyCode.RightArrow)){
- if (right.transform == null || right.transform.gameObject.layer != BlockingLayer || right.transform.gameObject.tag == "Bloc") {
- x=x+1;
- }
- }
- if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
- if (down.transform == null || down.transform.gameObject.layer != BlockingLayer || down.transform.gameObject.tag == "Bloc") {
- y=y-1;
- }
- }
- if (center.transform == null) {
- 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) {
- aspect_selecteur.color = greenAlpha;
- } else {
- aspect_selecteur.color = redAlpha;
- }
- } else if (center.transform.gameObject.tag == "Bloc") {
- aspect_selecteur.color = yellowAlpha;
- }
- if (Input.GetKeyDown(KeyCode.Mouse0)){
- if (center.transform == null) {
- 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) {
- construire ();
- }
- } else if (center.transform.gameObject.tag == "Bloc") {
- upgrade (center.transform.gameObject);
- }
- }
- if (Input.GetKeyDown (KeyCode.Mouse1)) {
- ouvrir_construction ();
- }
- 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));
- }
- } else {
- if (construction == false) {
- if (Input.GetAxis ("Top360Y") > 0.5f || Input.GetAxis ("Cross360Y") < -0.5f || Input.GetKeyDown(KeyCode.JoystickButton0)) {
- if (p_rb.velocity.y == 0) {
- p_rb.AddForce (Vector2.up * v_saut);
- }
- }
- if (Input.GetAxis ("Top360X") < 0 || Input.GetAxis ("Cross360X") < 0){
- p_rb.AddForce (Vector2.left * vitesse);
- }
- if (Input.GetAxis ("Top360X") > 0 || Input.GetAxis ("Cross360X") > 0){
- p_rb.AddForce (Vector2.right * vitesse);
- }
- if (Input.GetAxis ("Top360Y") < -0.5f || Input.GetAxis ("Cross360Y") > 0.5f) {
- p_rb.AddForce (Vector2.up * -v_saut);
- this.transform.localScale = new Vector3 (1, 1, 1);
- } else {
- this.transform.localScale = new Vector3 (1, 2, 1);
- }
- if (Input.GetAxis("Gachette360") > 0 || Input.GetKeyDown(KeyCode.JoystickButton2)){
- tir_ramasser ();
- }
- if (Input.GetKeyDown(KeyCode.JoystickButton1)) {
- ouvrir_construction ();
- }
- } else {
- aspect_selecteur = selecteur.GetComponent<SpriteRenderer> ();
- RaycastHit2D center;
- RaycastHit2D left;
- RaycastHit2D right;
- RaycastHit2D up;
- RaycastHit2D down;
- center = Physics2D.Linecast (selecteur.transform.position, new Vector2 (selecteur.transform.position.x, selecteur.transform.position.y));
- left = Physics2D.Raycast (new Vector2(this.transform.position.x - 0.1f, this.transform.position.y - 0.5f), Vector2.left, 1f);
- right = Physics2D.Raycast (new Vector2(this.transform.position.x + 1.1f, this.transform.position.y - 0.5f), Vector2.right, 1f);
- up = Physics2D.Raycast (new Vector2(this.transform.position.x + 0.5f, this.transform.position.y + 0.1f), Vector2.up, 1f);
- down = Physics2D.Raycast (new Vector2(this.transform.position.x + 0.5f, this.transform.position.y - 1.1f), Vector2.down, 1f);
- float x = 0;
- float y = 0;
- if (Input.GetAxis ("Top360Y") > 0 || Input.GetAxis ("Cross360Y") > 0.5f || Input.GetKeyDown(KeyCode.JoystickButton0)) {
- if (up.transform == null || up.transform.gameObject.layer != BlockingLayer) {
- y = Input.GetAxis ("Top360Y");
- }
- }
- if (Input.GetAxis ("Top360X") < 0 || Input.GetAxis ("Cross360X") < 0){
- if (left.transform == null || left.transform.gameObject.layer != BlockingLayer) {
- x= Input.GetAxis ("Top360X");
- }
- }
- if (Input.GetAxis ("Top360X") > 0 || Input.GetAxis ("Cross360X") > 0){
- if (right.transform == null || right.transform.gameObject.layer != BlockingLayer) {
- x=Input.GetAxis ("Top360X");
- }
- }
- if (Input.GetAxis ("Top360Y") < 0 || Input.GetAxis ("Cross360Y") < -0.5f) {
- if (down.transform == null || down.transform.gameObject.layer != BlockingLayer) {
- y=Input.GetAxis ("Top360Y");
- }
- }
- if (center.transform == null) {
- 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) {
- aspect_selecteur.color = greenAlpha;
- } else {
- aspect_selecteur.color = redAlpha;
- }
- } else if (center.transform.gameObject.tag == "Bloc") {
- aspect_selecteur.color = yellowAlpha;
- }
- /*
- Construire !
- */
- if (Input.GetKeyDown(KeyCode.JoystickButton1)) {
- ouvrir_construction ();
- }
- 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));
- }
- }
- }
- void LateUpdate() {
- 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));
- if (construction == true){
- if (Player2 == false) {
- 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)) {
- selecteur.transform.position = new Vector2 (Mathf.RoundToInt (selecteur.transform.position.x), Mathf.RoundToInt (selecteur.transform.position.y));
- }
- } else {
- if (Input.GetAxis ("Top360Y") == 0 && Input.GetAxis ("Top360X") == 0) {
- selecteur.transform.position = new Vector2 (Mathf.RoundToInt (selecteur.transform.position.x), Mathf.RoundToInt (selecteur.transform.position.y));
- }
- }
- }
- }
- void ouvrir_construction(){
- if (construction == false) {
- constructionMenu.SetActive (true);
- construction = true;
- }else{
- constructionMenu.SetActive (false);
- construction = false;
- }
- }
- void construire(){
- if (Munitions >= 100) {
- Munitions = Munitions - 100;
- GameObject obj = Instantiate (blocPrefab, selecteur.transform.position, Quaternion.identity) as GameObject;
- }
- }
- void upgrade(GameObject obj){
- Bloc bloc = obj.GetComponent<Bloc> ();
- if (bloc.bloc == "Neige") {
- if (Munitions >= 150) {
- bloc.Upgrade ();
- Munitions = Munitions -150;
- }
- } else if (bloc.bloc == "Glace"){
- }
- }
- public void OnTriggerEnter2D(Collider2D other){
- if (other.tag == "SnowPile") {
- SnowPile = true;
- }
- if (other.tag == "SnowBall") {
- Vie = Vie - 5;
- Destroy (other);
- }
- }
- public void OnTriggerExit2D(Collider2D other){
- if (other.tag == "SnowPile") {
- SnowPile = false;
- }
- }
- void tir_ramasser(){
- if (SnowPile == true) {
- Munitions++;
- } else {
- GameObject obj = Instantiate (snowballPrefab, new Vector2(this.transform.position.x + 1, this.transform.position.y + 1), Quaternion.identity) as GameObject;
- Rigidbody2D objRb = obj.GetComponent<Rigidbody2D> ();
- Vector2 mousePos = Input.mousePosition;
- mousePos = Camera.main.ScreenToWorldPoint(mousePos);
- float x = mousePos.x - this.transform.position.x;
- float y = mousePos.y - this.transform.position.y;
- objRb.AddForce (new Vector2 (x * vitesse * 10 , y * vitesse * 10));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement