Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- [RequireComponent (typeof (Controller2D))]
- public class Player : MonoBehaviour {
- public float automatic_knife_trigger_distance;
- public float knife_speed;
- public float moveSpeed;
- public float min_jump_height;
- public float max_jump_height;
- public float timeToJumpApex;
- public float accelerationTimeAirborne;
- public float accelerationTimeGrounded;
- float min_jump_velocity;
- float max_jump_velocity;
- [Space]
- public float step_dust_rate;
- private float step_dust_rate_count;
- public float knife_dust_rate;
- private float knife_dust_rate_count;
- public float knife_dust_time;
- private float knife_dust_time_count;
- float gravity;
- [HideInInspector] public Vector3 velocity;
- float velocityXSmoothing;
- bool savedFlip;
- Controller2D controller;
- SpriteRenderer sprite;
- AvatarStates avatar_states;
- Animator animator;
- LevelManager level_manager;
- GameObject slash;
- GameObject dust;
- Vector3 knife_dir;
- GameObject child;
- Vector2 input;
- float input_save;
- void Start() {
- child = transform.Find("Graphics").gameObject;
- animator = child.GetComponent<Animator>();
- sprite = child.GetComponent<SpriteRenderer>();
- controller = GetComponent<Controller2D> ();
- dust = Resources.Load<GameObject>("Prefabs/Switch_Dust");
- slash = Resources.Load<GameObject>("Prefabs/Slash");
- level_manager = FindObjectOfType<LevelManager>();
- gravity = -(2 * max_jump_height) / Mathf.Pow (timeToJumpApex, 2);
- min_jump_velocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * min_jump_height);
- max_jump_velocity = Mathf.Abs(gravity) * timeToJumpApex;
- avatar_states = new AvatarStates();
- AvatarStates.OnLand += reset_step_dust;
- Ballon.OnKill += OnBallonKill;
- }
- void reset_step_dust() {
- step_dust_rate_count = 0F;
- }
- bool is_ballon_in_avatar_angle(Ballon ballon) {
- var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mouse_pos.z = 0F;
- var mouse_dir = (mouse_pos - transform.position).normalized;
- var ballon_dir = (ballon.transform.position - transform.position).normalized;
- var angle = 20F;
- if (Vector2.Angle(mouse_dir, ballon_dir) <= angle) {
- return true;
- }
- return false;
- }
- void OnBallonKill() {
- if (AvatarStates.states == AvatarStates.States.KNIFING) {
- Ballon closest_ballon = level_manager.get_closest_ballon();
- if (closest_ballon != null) {
- if (is_ballon_in_avatar_angle(closest_ballon)) {
- if (Vector2.Distance(closest_ballon.transform.position, transform.position) < automatic_knife_trigger_distance) {
- knife_dir = (closest_ballon.transform.position - transform.position).normalized;
- StartCoroutine(reset_knife_dir());
- } else {
- var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mouse_pos.z = 0F;
- var mouse_dir = (mouse_pos - transform.position).normalized;
- if (Mathf.Sign(mouse_dir.x) > 0) {
- input_save = 1;
- } else {
- input_save = -1;
- }
- if (mouse_dir.y < 0F) {
- mouse_dir.y = 0F;
- }
- knife_dir = mouse_dir;
- StartCoroutine(reset_knife_dir());
- }
- } else {
- var mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- mouse_pos.z = 0F;
- var mouse_dir = (mouse_pos - transform.position).normalized;
- if (Mathf.Sign(mouse_dir.x) > 0) {
- input_save = 1;
- } else {
- input_save = -1;
- }
- //input_save = Mathf.Sign(mouse_dir.x);
- if (mouse_dir.y < 0F) {
- mouse_dir.y = 0F;
- }
- knife_dir = mouse_dir;
- StartCoroutine(reset_knife_dir());
- }
- } else {
- knife_dir = Vector3.zero;
- }
- } else {
- knife_dir = Vector3.zero;
- }
- }
- IEnumerator reset_knife_dir() {
- yield return new WaitForSeconds(0.1F);
- knife_dir = Vector3.zero;
- }
- void Update() {
- if (controller.collisions.above || controller.collisions.below) {
- velocity.y = 0;
- }
- input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
- if (AvatarStates.states == AvatarStates.States.KNIFING) {
- input.x = 0F;
- } else {
- if (Input.GetKeyDown (KeyCode.Space) && controller.collisions.below) {
- AvatarAnimations.GroundedJumpDust(this);
- velocity.y = max_jump_velocity;
- AvatarStates.states = AvatarStates.States.JUMPING;
- }
- if (Input.GetKeyUp(KeyCode.Space)) {
- if (velocity.y > min_jump_velocity) {
- velocity.y = min_jump_velocity;
- }
- }
- }
- if (Input.GetKeyDown(KeyCode.Mouse0)) {
- input_save = input.x;
- // Dust et reset
- Instantiate(dust, transform.position, Quaternion.identity);
- step_dust_rate_count = step_dust_rate;
- knife_dust_rate_count = knife_dust_rate;
- knife_dust_time_count = knife_dust_time;
- }
- if (controller.collisions.below) {
- input_save = 0;
- }
- if (knife_dir == Vector3.zero) {
- float targetVelocityX = (input_save != 0 ? input_save : input.x) * moveSpeed;
- velocity.x = Mathf.SmoothDamp (velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
- velocity.y += gravity * Time.deltaTime;
- controller.Move (velocity * Time.deltaTime);
- } else {
- if (knife_dir.y < 0F) {
- knife_dir.y = 0F;
- }
- velocity = knife_dir * knife_speed;
- controller.Move(velocity * Time.deltaTime);
- }
- // Dust step
- if (controller.collisions.below && input.x != 0) {
- if (step_dust_rate_count <= 0F) {
- AvatarAnimations.GroundedRunDust(this);
- step_dust_rate_count = step_dust_rate;
- } else {
- step_dust_rate_count -= Time.deltaTime;
- }
- }
- // Dust knife
- if (AvatarStates.states == AvatarStates.States.KNIFING) {
- if (knife_dust_time_count > 0F) {
- knife_dust_time_count -= Time.deltaTime;
- if (knife_dust_rate_count <= 0F) {
- AvatarAnimations.GroundedRunDust(this);
- knife_dust_rate_count = knife_dust_rate;
- } else {
- knife_dust_rate_count -= Time.deltaTime;
- }
- }
- }
- if (input.x < 0) {
- sprite.flipX = true;
- }
- else if (input.x > 0) {
- sprite.flipX = false;
- }
- else {
- sprite.flipX = savedFlip;
- }
- savedFlip = sprite.flipX;
- if (Input.GetKeyDown(KeyCode.R)) {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- if (Input.GetKey(KeyCode.Mouse0)) {
- metamorphose();
- }
- if (Input.GetKeyUp(KeyCode.Mouse0)) {
- input_save = 0;
- child.transform.up = Vector3.zero;
- var switch_dust = Instantiate(Resources.Load<GameObject>("Prefabs/Switch_Dust"), transform.position, Quaternion.identity);
- }
- avatar_states.StateChange();
- AnimationUpdate();
- }
- private void metamorphose() {
- AvatarStates.states = AvatarStates.States.KNIFING;
- child.transform.up = velocity;
- }
- private void AnimationUpdate()
- {
- switch (AvatarStates.states)
- {
- case AvatarStates.States.IDLE:
- AvatarAnimations.AnimationIdle(animator);
- break;
- case AvatarStates.States.JUMPING:
- AvatarAnimations.AnimationJumping(animator);
- break;
- case AvatarStates.States.RUNNING:
- AvatarAnimations.AnimationRunning(animator);
- break;
- case AvatarStates.States.KNIFING:
- AvatarAnimations.AnimationKnifing(animator);
- break;
- case AvatarStates.States.FALLING:
- AvatarAnimations.AnimationFalling(animator);
- break;
- case AvatarStates.States.DEATH:
- AvatarAnimations.AnimationDeath(animator);
- break;
- }
- }
- void OnDestroy() {
- AvatarStates.OnLand -= reset_step_dust;
- Ballon.OnKill -= OnBallonKill;
- }
- void OnTriggerEnter2D(Collider2D col) {
- if (col.gameObject.CompareTag("NextLevel")) {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
- }
- if (AvatarStates.states != AvatarStates.States.KNIFING) {
- if (col.gameObject.CompareTag("Lethal")) {
- Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
- GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
- AvatarStates.states = AvatarStates.States.DEATH;
- }
- }
- if (AvatarStates.states == AvatarStates.States.KNIFING) {
- if (col.gameObject.CompareTag("Knifable")) {
- Instantiate(slash, col.gameObject.transform.position, Quaternion.identity);
- Instantiate(dust, col.gameObject.transform.position, Quaternion.identity);
- Destroy(col.gameObject);
- }
- }
- }
- void OnTriggerStay2D(Collider2D col) {
- if (AvatarStates.states == AvatarStates.States.KNIFING) {
- if (col.gameObject.CompareTag("Knifable")) {
- Instantiate(slash, col.gameObject.transform.position, Quaternion.identity);
- Instantiate(dust, col.gameObject.transform.position, Quaternion.identity);
- Destroy(col.gameObject);
- }
- }
- if (AvatarStates.states != AvatarStates.States.KNIFING) {
- if (col.gameObject.CompareTag("Lethal")) {
- Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
- GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
- AvatarStates.states = AvatarStates.States.DEATH;
- }
- }
- }
- void OnTriggerExit2D(Collider2D col) {
- if (AvatarStates.states != AvatarStates.States.KNIFING) {
- if (col.gameObject.CompareTag("Lethal")) {
- Instantiate(Resources.Load<GameObject>("Prefabs/Death_Particules"), transform.position, Quaternion.identity);
- GameObject obj = Instantiate(dust, transform.position, Quaternion.identity) as GameObject;
- AvatarStates.states = AvatarStates.States.DEATH;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement