Skip to content
Snippets Groups Projects
Commit c15f043b authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Made ItemPref use the icon of the item it represents.

parent 282095da
No related branches found
No related tags found
1 merge request!45Alexander
......@@ -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;
}
}
}
......
......@@ -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)
{
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment