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 CharControls : MonoBehaviour
- {
- //hp managing
- public int health;
- public GameObject hpBar;
- public GameObject blood;
- public GameObject hurtEff;
- //setup stuff and RB
- public int ownedByCtrlNum;
- private string XaxisName;
- private string YaxisName;
- private string AbuttonName;
- private string BbuttonName;
- private float x;
- private float y;
- public float moveSpeed = 720f;
- private Rigidbody2D RB;
- //is grounded?
- private bool isGrounded;
- public Transform groundCheck;
- public float gcRadius;
- public LayerMask groundMask;
- public bool hasExploded;
- public float jumpForce;
- public bool jumpInProg;
- //animations
- public AnimationClip Idle;
- public AnimationClip Walk;
- public AnimationClip Jump;
- public AnimationClip Toss;
- public AnimationClip Reload;
- private Animator anim;
- //shooting
- public int snowballCount = 3;
- private float shtTimer;
- private bool isShooting;
- public GameObject snowball;
- public GameObject sandball;
- public GameObject polkaball;
- public Transform SP;
- private GameObject Sbclone;
- private float fireRate;
- //reloading
- private float rldTimer;
- private bool reloading;
- //invincible status
- public bool invincible;
- public GameObject invSprt;
- //specials related
- public bool saw;
- public bool smg;
- public bool shield;
- private float shieldTimer;
- private float sawTimer;
- private float smgTimer;
- public GameObject smgSprt;
- public GameObject sawSprt;
- private int smgCount;
- //Audio
- public AudioSource audioSource;
- public AudioClip[] clips;
- //secrets
- public bool sawkill;
- public bool smgkill;
- public bool bombkill;
- private void Awake()
- {
- invincible = true;
- health = 4;
- DontDestroyOnLoad(this.gameObject);
- }
- void Start()
- {
- RB = this.GetComponent<Rigidbody2D>();
- anim = this.GetComponent<Animator>();
- }
- void Update()
- {
- //NEEDS WORK
- if(ownedByCtrlNum == 4 && Input.GetJoystickNames().Length < 4){
- //keyboard exception for player4 if controller isnt connected.
- AbuttonName = "AK";
- BbuttonName = "BK";
- XaxisName = "LRK";
- YaxisName = "UDK";
- }
- else
- {
- //identification
- XaxisName = "LR" + ownedByCtrlNum.ToString();
- YaxisName = "UD" + ownedByCtrlNum.ToString();
- AbuttonName = "A" + ownedByCtrlNum.ToString();
- BbuttonName = "B" + ownedByCtrlNum.ToString();
- }
- //Flip Charcter
- if(x == 1 || x == -1)
- {
- this.gameObject.transform.localScale = new Vector3(x, 1, 1);
- }
- //Movement x
- if(Input.GetAxis(XaxisName) == 1 || Input.GetAxis(XaxisName) == -1)
- {
- x = Input.GetAxis(XaxisName);
- if(isGrounded == true && isShooting == false)
- {
- anim.Play(Walk.name.ToString());
- }
- }
- else
- {
- x = 0f;
- if (isGrounded == true && isShooting == false && reloading == false)
- {
- anim.Play(Idle.name.ToString());
- }
- }
- if(isGrounded == false && isShooting == false)
- {
- anim.Play(Jump.name.ToString());
- }
- //Jump & Hold
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, gcRadius, groundMask);
- if (Input.GetButtonDown(BbuttonName) && isGrounded == true)
- {
- RB.velocity = new Vector2(RB.velocity.x, jumpForce);
- jumpInProg = true;
- }
- //Shoot
- shtTimer += Time.deltaTime;
- if(Input.GetButtonDown(AbuttonName) && snowballCount > 0 && isShooting == false && reloading == false && smg == false)
- {
- fireRate = 0.3f;
- //print("Reg");
- snowballCount -= 1;
- anim.Play(Toss.name.ToString());
- shtTimer = 0f;
- isShooting = true;
- audioSource.clip = clips[1];
- audioSource.Play();
- }else if(Input.GetButton(AbuttonName) && snowballCount > 0 && isShooting == false && reloading == false && smg == true)
- {
- print("Smg");
- if(smg == true){
- smgCount += 1;
- }
- snowballCount -= 1;
- anim.Play(Toss.name.ToString());
- shtTimer = 0f;
- isShooting = true;
- }
- if(isShooting == true)
- {
- if(shtTimer > fireRate)
- {
- float bs;
- if(GameObject.Find("GameManagerFollow").GetComponent<GameManager>().MapName == "Egypt" && smg == false){
- Sbclone = Instantiate(sandball, SP.transform.position, SP.transform.rotation);
- //The sandy exception
- }
- else if(smg == true){
- //smg exception
- Sbclone = Instantiate(polkaball, SP.transform.position, SP.transform.rotation);
- }
- else{
- //regular snowball
- Sbclone = Instantiate(snowball, SP.transform.position, SP.transform.rotation);
- }
- if(this.RB.velocity.x > 0){
- bs = this.RB.velocity.x;
- //print(bs);
- }
- else
- {
- bs = this.RB.velocity.x;
- bs = -bs;
- //print(bs);
- }
- Sbclone.transform.localScale = this.transform.localScale;
- Sbclone.GetComponent<SnowballLogic>().ballSpeed = bs;
- Sbclone = null;
- isShooting = false;
- }
- }
- //Reload
- y = Input.GetAxis(YaxisName);
- if(y == 1)
- {
- reloading = true;
- }
- if(reloading == true)
- {
- if(snowballCount < 3 && x < 1 && x > -1 && isGrounded == true)
- {
- anim.Play(Reload.name.ToString());
- rldTimer += Time.deltaTime;
- if (rldTimer > 1.5f)
- {
- snowballCount = 3;
- }
- }
- else
- {
- reloading = false;
- rldTimer = 0;
- }
- }
- //Health manager
- hpBar.GetComponent<hpBar>().health = health;
- if(health <= 0)
- {
- //secrets & unlockables realted to char death
- if(this.gameObject.transform.name == "PlayableChar(Clone)" && sawkill == true){
- PlayerPrefs.SetInt("Dead",1);
- }
- if(this.gameObject.transform.name == "PlayableChar 1(Clone)" && smgkill == true){
- PlayerPrefs.SetInt("Shot",1);
- }
- if(this.gameObject.transform.name == "PlayableChar 3(Clone)" && bombkill == true){
- PlayerPrefs.SetInt("Burn",1);
- }
- //THE ACTUAL DEATH
- Instantiate(blood.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
- Destroy(this.gameObject);
- }
- //Invincible status
- invSprt.SetActive(invincible);
- //Items or Specials
- if(saw == true){
- sawTimer += Time.deltaTime;
- sawSprt.SetActive(true);
- snowballCount = 0;
- if(sawTimer >= 4f){
- saw = false;
- }
- }
- else
- {
- sawSprt.SetActive(false);
- sawTimer = 0f;
- }
- if(smg == true){
- smgSprt.SetActive(true);
- smgTimer += Time.deltaTime;
- fireRate = 0.1f;
- snowballCount = 3;
- if(smgTimer >= 8f)
- {
- snowballCount = 0;
- smg = false;
- }
- else if(smgCount >= 10)
- {
- isShooting = false;
- snowballCount = 0;
- smg = false;
- }
- }
- else
- {
- smgSprt.SetActive(false);
- smgTimer = 0f;
- smg = false;
- smgCount = 0;
- }
- if(shield == true){
- invincible = true;
- shieldTimer += Time.deltaTime;
- if(shieldTimer >= 3f){
- shieldTimer = 0f;
- invincible = false;
- shield = false;
- }
- }
- }
- void FixedUpdate()
- {
- this.GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed * x, this.GetComponent<Rigidbody2D>().velocity.y);
- //jump holding
- if(jumpInProg == true && !Input.GetButton(BbuttonName))
- {
- RB.velocity -= new Vector2(0, 2.5f);
- }
- if (RB.velocity.y < 0f)
- {
- jumpInProg = false;
- }
- }
- public void takeDamage(int dmg){
- Instantiate(hurtEff.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
- sawkill = false;
- health -= dmg;
- }
- public void takeDamageSaw(int dmg){
- sawkill = true;
- health -= dmg;
- }
- public void takeDamageSmg(int dmg){
- smgkill = true;
- health -= dmg;
- }
- public void takeDamageBomb(int dmg){
- Instantiate(hurtEff.gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z - 1f), transform.rotation);
- bombkill = true;
- health -= dmg;
- }
- public void Heal(){
- if(health < 4){
- audioSource.clip = clips[0];
- audioSource.Play();
- health += 1;
- }
- else{
- audioSource.clip = clips[4];
- audioSource.Play();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement