From c15f043b1c89ad5404d5b09f1c74fc5c8b6fc464 Mon Sep 17 00:00:00 2001
From: Ny Bruker <robinhs@stud.ntnu.no>
Date: Thu, 27 Oct 2022 11:12:04 +0200
Subject: [PATCH] Made ItemPref use the icon of the item it represents.

---
 MrBigsock/Assets/Code/PlayerController.cs | 142 +++++++++++-----------
 MrBigsock/Assets/Code/UI/ItemPref.cs      | 103 +++++++++-------
 2 files changed, 127 insertions(+), 118 deletions(-)

diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs
index 05a43506..f7dd925b 100644
--- a/MrBigsock/Assets/Code/PlayerController.cs
+++ b/MrBigsock/Assets/Code/PlayerController.cs
@@ -16,33 +16,33 @@ namespace BigSock {
 	public partial class PlayerController : Character
 	{
 
-			public UtilBar utilBar;
-			
+		public UtilBar utilBar;
+		
 
-			public float collisionOffset = 0.05f;
-			public ContactFilter2D movementFilter;
-			
-			public GameObject attack;
+		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>();
+		Vector2 movementInput;
+		SpriteRenderer spriteRenderer;
+		//Rigidbody2D rb;
+		Animator animator;
+		List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
 
-			bool canMove = true;
+		bool canMove = true;
 
 
 
-			protected IAttack _testAttack;
-			protected IAttack _testAttack2;
-			protected IAbility _dodge;
+		protected IAttack _testAttack;
+		protected IAttack _testAttack2;
+		protected IAbility _dodge;
 
-			public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;
+		public DateTime NextTimeCanAttack { get; private set; } = DateTime.Now;
 
 
 		public PlayerController()
-        {
+		{
 			TryPickUpItem(ItemService.SINGLETON.Get(201));
 			TryPickUpItem(ItemService.SINGLETON.Get(201));
 			TryPickUpItem(ItemService.SINGLETON.Get(202));
@@ -51,70 +51,70 @@ namespace BigSock {
 
 		// Start is called before the first frame update 
 		protected override void Start()
-			{
-				base.Start();
+		{
+			base.Start();
 
-			
+		
 
-				utilBar?.WithXP((int)xp, (int)maxXp)	
-					?.WithHealth(Convert.ToInt32(HP), Convert.ToInt32(MaxHP));
+			utilBar?.WithXP((int)xp, (int)maxXp)	
+				?.WithHealth(Convert.ToInt32(HP), Convert.ToInt32(MaxHP));
 
-				animator = GetComponent<Animator>();
-				spriteRenderer = GetComponent<SpriteRenderer>();
-				
+			animator = GetComponent<Animator>();
+			spriteRenderer = GetComponent<SpriteRenderer>();
+			
 
 
-				//!! 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));
-				TryPickUpItem(ItemService.SINGLETON.Get(101));
-				*/
-				//var tmp = PrefabService.SINGLETON;
-				var tmp = SpriteService.SINGLETON;
+			//!! 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));
+			TryPickUpItem(ItemService.SINGLETON.Get(101));
+			*/
+			//var tmp = PrefabService.SINGLETON;
+			var tmp = SpriteService.SINGLETON;
 
-				_testAttack = (IAttack) AbilityService.SINGLETON.Get(101);
-				_testAttack2 = (IAttack) AbilityService.SINGLETON.Get(102);
-				_dodge = AbilityService.SINGLETON.Get(201);
-			}
+			_testAttack = (IAttack) AbilityService.SINGLETON.Get(101);
+			_testAttack2 = (IAttack) AbilityService.SINGLETON.Get(102);
+			_dodge = AbilityService.SINGLETON.Get(201);
+		}
 
 
 			
 			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;
-							}
-					}
-					
+				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;
+						}
+				}
+				
 
 			}
 
diff --git a/MrBigsock/Assets/Code/UI/ItemPref.cs b/MrBigsock/Assets/Code/UI/ItemPref.cs
index b86c2819..4887c581 100644
--- a/MrBigsock/Assets/Code/UI/ItemPref.cs
+++ b/MrBigsock/Assets/Code/UI/ItemPref.cs
@@ -7,52 +7,61 @@ using BigSock.Item;
 
 namespace BigSock.UI
 {
-    public class ItemPref : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
-    {
-        public IItem item;
-        public ItemSlot itemSlot;
-        private RectTransform rectTransform;
-        private CanvasGroup canvasGroup;
-        public Vector2? position;
-
-        private void Awake()
-        {
-            rectTransform = GetComponent<RectTransform>();
-            canvasGroup = GetComponent<CanvasGroup>();
-        }
-        public void OnBeginDrag(PointerEventData eventData)
-        {
-            
-            position = transform.position;
-            canvasGroup.alpha = .6f;
-            canvasGroup.blocksRaycasts = false;
-        }
-
-        public void OnDrag(PointerEventData eventData)
-        {
-            
-            rectTransform.anchoredPosition += eventData.delta;
-
-        }
-
-        public void OnEndDrag(PointerEventData eventData)
-        {
-            
-            if (position != null)
-            {
-                transform.position = position.Value;
-            }
-            
-            canvasGroup.alpha = 1f;
-            canvasGroup.blocksRaycasts = true;
-        }
-
-        public void OnPointerDown(PointerEventData eventData)
-        {
-            
-        }
-
-        
-    }
+	public class ItemPref : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
+	{
+		public IItem item;
+		public ItemSlot itemSlot;
+		private RectTransform rectTransform;
+		private CanvasGroup canvasGroup;
+		public Vector2? position;
+
+		void Start()
+		{
+			// Change the sprite if the item has one.
+			var sprite = item?.Icon;
+			if(sprite != null)
+				GetComponent<UnityEngine.UI.Image>().overrideSprite = sprite;
+		}
+
+		private void Awake()
+		{
+			rectTransform = GetComponent<RectTransform>();
+			canvasGroup = GetComponent<CanvasGroup>();
+
+		}
+		public void OnBeginDrag(PointerEventData eventData)
+		{
+			
+			position = transform.position;
+			canvasGroup.alpha = .6f;
+			canvasGroup.blocksRaycasts = false;
+		}
+
+		public void OnDrag(PointerEventData eventData)
+		{
+				
+			rectTransform.anchoredPosition += eventData.delta;
+
+		}
+
+		public void OnEndDrag(PointerEventData eventData)
+		{
+			
+			if (position != null)
+			{
+					transform.position = position.Value;
+			}
+			
+			canvasGroup.alpha = 1f;
+			canvasGroup.blocksRaycasts = true;
+		}
+
+		public void OnPointerDown(PointerEventData eventData)
+		{
+				
+		}
+
+		
+	}
 }
 
-- 
GitLab