Skip to content
Snippets Groups Projects
PlayerController.cs 4.93 KiB
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;


using BigSock.UI;
using BigSock.Item;
using BigSock.Service;


namespace BigSock {

	// Takes and handles input and movement for a player character
	public class PlayerController : Character
	{

			public HPBar hpBar;
			public XpBar xpBar;
			

			public float collisionOffset = 0.05f;
			public ContactFilter2D movementFilter;
			
			public GameObject attack;

			Vector2 movementInput;
			SpriteRenderer spriteRenderer;
			//Rigidbody2D rb;
			Animator animator;
			List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();

			bool canMove = true;



			protected IAttack _testAttack;
			protected IAttack _testAttack2;

			public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;




			// Start is called before the first frame update 
			protected override void Start()
			{
				base.Start();

				xpBar.SetMaxXp(maxXp);
				xpBar.SetXp(xp);

				animator = GetComponent<Animator>();
				spriteRenderer = GetComponent<SpriteRenderer>();
				hpBar.SetMaxHealth(Convert.ToInt32(MaxHP));
				hpBar.SetHealth(Convert.ToInt32(HP));

				//!! DEBUG: Add item to player at start to test if it works.
				TryPickUpItem(ItemService.SINGLETON.Get(201));
				TryPickUpItem(ItemService.SINGLETON.Get(201));
				TryPickUpItem(ItemService.SINGLETON.Get(202));
				var tmp = PrefabService.SINGLETON;
				_testAttack = (IAttack) AbilityService.SINGLETON.Get(101);
				_testAttack2 = (IAttack) AbilityService.SINGLETON.Get(102);
			}


			
			private void FixedUpdate() {
					if(canMove) {
							// If movement input is not 0, try to move
							if(movementInput != Vector2.zero){
									
									bool success = TryMove(movementInput);

									if(!success) {
											success = TryMove(new Vector2(movementInput.x, 0));
									}

									if(!success) {
											success = TryMove(new Vector2(0, movementInput.y));
									}
									
									animator.SetBool("isMoving", success);
							} else {
									animator.SetBool("isMoving", false);
							}

							// Set direction of sprite to movement direction
							var mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
							var pos = transform.position;
							var temp = (mouse - pos);
							//temp.z = 0;
							//direction = temp.normalized;

							if(temp.x < 0) {
									spriteRenderer.flipX = true;
							} else if (temp.x > 0) {
									spriteRenderer.flipX = false;
							}
					}
					

			}

			private void Update() {
				if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0)) {
					// Manage attack cooldown.
					if(NextTimeCanAttack <= DateTime.Now) {
						//NextTimeCanAttack = DateTime.Now.AddSeconds(AttackCooldown);

						//var bullet = Instantiate(attack, new Vector3(transform.position.x, transform.position.y, transform.position.z), attack.transform.rotation);
						//bullet.GetComponent<AttackMovement>().Actor = this;

						_testAttack.Use(this, Camera.main.ScreenToWorldPoint(Input.mousePosition));
					
					}
				}

				if(Input.GetKey(KeyCode.Z)) {
					_testAttack2.Use(this, Camera.main.ScreenToWorldPoint(Input.mousePosition));
				}

				//!! Code for testing the new item stuff.
				if(Input.GetKeyDown(KeyCode.Space)) {
					var item = ItemService.SINGLETON.GetRandom(); // new ItemRunningShoes();
					TryPickUpItem(item);
				}
			}


			

			private bool TryMove_OLD(Vector2 direction) {
					if(direction != Vector2.zero) {
							// Check for potential collisions
							int count = rb.Cast(
									direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
									movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
									castCollisions, // List of collisions to store the found collisions into after the Cast is finished
									(float) MovementSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset

							if(count == 0){
									rb.MovePosition(rb.position + direction * (float) MovementSpeed * Time.fixedDeltaTime);
									return true;
							} else {
									return false;
							}
					} else {
							// Can't move if there's no direction to move in
							return false;
					}
			
					// 
			}

			void OnMove(InputValue movementValue) {
					movementInput = movementValue.Get<Vector2>();
			}


			public void LockMovement() {
					canMove = false;
			}

			public void UnlockMovement() {
					canMove = true;
			}

			/*
			Method for what to do when the character takes damage.
			*/
			protected override void AfterDamage(IAttackStats attack)  {
				base.AfterDamage(attack);
				hpBar.SetHealth(Convert.ToInt32(HP));
			}

			public void GainXp(float xp){
				GiveXp(xp);
				CheckXp();
				xpBar.SetXp(this.xp);
			}

			private void CheckXp(){
				if(xp > maxXp){
					level += 1; 
					xp -= maxXp;
					maxXp = (level + 1) * 100;
					xpBar.SetMaxXp(maxXp);
					
				}
			}
	}
}