diff --git a/Assets/Scripts/Card/Interfaces.meta b/Assets/Scripts/Card/Interfaces.meta new file mode 100644 index 0000000..480b222 --- /dev/null +++ b/Assets/Scripts/Card/Interfaces.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 97ade3b356eda4e199b6f88ee2c39633 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Card/Interfaces/IState.cs b/Assets/Scripts/Card/Interfaces/IState.cs new file mode 100644 index 0000000..5aebdce --- /dev/null +++ b/Assets/Scripts/Card/Interfaces/IState.cs @@ -0,0 +1,6 @@ +public interface IState +{ + void OnMouseUp(); + void OnMouseDown(); + void Update(); +} diff --git a/Assets/Scripts/Card/Interfaces/IState.cs.meta b/Assets/Scripts/Card/Interfaces/IState.cs.meta new file mode 100644 index 0000000..a898217 --- /dev/null +++ b/Assets/Scripts/Card/Interfaces/IState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e78248987973b4aa385f044bef133f4b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Card/SimpleCard.cs b/Assets/Scripts/Card/SimpleCard.cs index 533c39e..900bc65 100644 --- a/Assets/Scripts/Card/SimpleCard.cs +++ b/Assets/Scripts/Card/SimpleCard.cs @@ -1,73 +1,36 @@ using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; + public class SimpleCard : MonoBehaviour { GameObject cardFace; public Guid cardGuid; - public bool isSelected { get; private set; } = false; - public bool isPlacedOnBattlefield { get; private set; } = false; + public bool isSelected { get; set; } = false; + public IState state { get; set; } public void Setup(GameObject _cardFaceTemplate, Guid _cardGuid) { cardFace = Instantiate(_cardFaceTemplate, this.transform); cardGuid = _cardGuid; + this.state = new Movable(this); } private void OnMouseDown() { - this.isSelected = true; + this.state.OnMouseDown(); } private void OnMouseUp() { - Debug.Log("Mouse button is released; Do not move card " + this.cardGuid); - - this.isSelected = false; - IInteractable? collisionObject = this.GetColissionObject(); - - if (collisionObject == null || !collisionObject.IsReceivable()) - this.ResetPosition(); - else - collisionObject.ReceiveObject(this); + this.state.OnMouseUp(); } private void Update() { - if (this.isSelected) - { - // Debug.Log("Mouse button is clicked; Updating card position " + this.cardGuid); - - Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); - this.transform.position = new Vector3(cursorPosition.x, cursorPosition.y, -5.0f); - - // Debug.Log("Mouse position " + Input.mousePosition + " Card position " + this.transform.position); - } - } - - private IInteractable? GetColissionObject() - { - RaycastHit hit; - bool isHit = Physics.Raycast( - this.transform.position, - transform.TransformDirection(Vector3.forward), - out hit, - Mathf.Infinity, - Physics.DefaultRaycastLayers - ); - - Debug.DrawRay(this.transform.position, transform.TransformDirection(Vector3.forward) * 20.0f, isHit ? Color.yellow : Color.red); - - if (!isHit) - return null; - - IInteractable collisionObject = (IInteractable)hit.collider.GetComponent(hit.collider.name); - - return collisionObject; + this.state.Update(); } - private void ResetPosition() + public void ResetPosition() { } diff --git a/Assets/Scripts/Card/States.meta b/Assets/Scripts/Card/States.meta new file mode 100644 index 0000000..56c407c --- /dev/null +++ b/Assets/Scripts/Card/States.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 17075f32c5b014e959c433927bd8a36e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Card/States/Movable.cs b/Assets/Scripts/Card/States/Movable.cs new file mode 100644 index 0000000..0b440b9 --- /dev/null +++ b/Assets/Scripts/Card/States/Movable.cs @@ -0,0 +1,66 @@ +using UnityEngine; + + +public class Movable : IState +{ + + public SimpleCard card { get; set; } + public Movable(SimpleCard _card) + { + this.card = _card; + } + public void OnMouseDown() + { + this.card.isSelected = true; + } + + public void OnMouseUp() + { + Debug.Log("Mouse button is released; Do not move card " + this.card.cardGuid); + + this.card.isSelected = false; + IInteractable? collisionObject = this.GetColissionObject(); + + if (collisionObject == null || !collisionObject.IsReceivable()) + this.card.ResetPosition(); + else + { + collisionObject.ReceiveObject(this.card); + this.card.state = new Received(this.card); + } + } + + public void Update() + { + if (this.card.isSelected) + { + // Debug.Log("Mouse button is clicked; Updating card position " + this.cardGuid); + + Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); + this.card.transform.position = new Vector3(cursorPosition.x, cursorPosition.y, -5.0f); + + // Debug.Log("Mouse position " + Input.mousePosition + " Card position " + this.transform.position); + } + } + + private IInteractable? GetColissionObject() + { + RaycastHit hit; + bool isHit = Physics.Raycast( + this.card.transform.position, + this.card.transform.TransformDirection(Vector3.forward), + out hit, + Mathf.Infinity, + Physics.DefaultRaycastLayers + ); + + Debug.DrawRay(this.card.transform.position, this.card.transform.TransformDirection(Vector3.forward) * 20.0f, isHit ? Color.yellow : Color.red); + + if (!isHit) + return null; + + IInteractable collisionObject = (IInteractable)hit.collider.GetComponent(hit.collider.name); + + return collisionObject; + } +} diff --git a/Assets/Scripts/Card/States/Movable.cs.meta b/Assets/Scripts/Card/States/Movable.cs.meta new file mode 100644 index 0000000..5388a74 --- /dev/null +++ b/Assets/Scripts/Card/States/Movable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 38c3cdb49c3314566818b4bb1c14f5a0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Card/States/Received.cs b/Assets/Scripts/Card/States/Received.cs new file mode 100644 index 0000000..04ae255 --- /dev/null +++ b/Assets/Scripts/Card/States/Received.cs @@ -0,0 +1,28 @@ +using UnityEngine; + +public class Received : IState +{ + + public SimpleCard card { get; set; } + + public Received(SimpleCard _card) + { + this.card = _card; + } + + public void OnMouseDown() + { + Debug.Log("Log message"); + } + + public void OnMouseUp() + { + Debug.Log("Log message"); + } + + public void Update() + { + Debug.Log("Log message"); + + } +} diff --git a/Assets/Scripts/Card/States/Received.cs.meta b/Assets/Scripts/Card/States/Received.cs.meta new file mode 100644 index 0000000..719398c --- /dev/null +++ b/Assets/Scripts/Card/States/Received.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f6751236f4f2b46e2a66ecc5310a9ec5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Interfaces/Interactable.cs b/Assets/Scripts/Interfaces/Interactable.cs index 311f5aa..7ed8d64 100644 --- a/Assets/Scripts/Interfaces/Interactable.cs +++ b/Assets/Scripts/Interfaces/Interactable.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using BoardGame.Cards; using UnityEngine; -interface IInteractable +public interface IInteractable { void ReceiveObject(MonoBehaviour obj); bool IsReceivable();