From 367c6dcfe496638caad90f238abc362f26a6e2a8 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 30 Jan 2021 16:50:14 +0100 Subject: [PATCH 1/2] Implement possible card states --- Assets/Scripts/Card/Interfaces.meta | 8 +++ Assets/Scripts/Card/Interfaces/IState.cs | 5 ++ Assets/Scripts/Card/Interfaces/IState.cs.meta | 11 ++++ Assets/Scripts/Card/SimpleCard.cs | 53 +++------------- Assets/Scripts/Card/States.meta | 8 +++ Assets/Scripts/Card/States/Movable.cs | 63 +++++++++++++++++++ Assets/Scripts/Card/States/Movable.cs.meta | 11 ++++ Assets/Scripts/Card/States/Received.cs | 26 ++++++++ Assets/Scripts/Card/States/Received.cs.meta | 11 ++++ Assets/Scripts/Interfaces/Interactable.cs | 6 +- 10 files changed, 152 insertions(+), 50 deletions(-) create mode 100644 Assets/Scripts/Card/Interfaces.meta create mode 100644 Assets/Scripts/Card/Interfaces/IState.cs create mode 100644 Assets/Scripts/Card/Interfaces/IState.cs.meta create mode 100644 Assets/Scripts/Card/States.meta create mode 100644 Assets/Scripts/Card/States/Movable.cs create mode 100644 Assets/Scripts/Card/States/Movable.cs.meta create mode 100644 Assets/Scripts/Card/States/Received.cs create mode 100644 Assets/Scripts/Card/States/Received.cs.meta 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..71218c3 --- /dev/null +++ b/Assets/Scripts/Card/Interfaces/IState.cs @@ -0,0 +1,5 @@ +public interface IState{ + void OnMouseUp(); + void OnMouseDown(); + void Update(); +} \ No newline at end of file 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..d751449 --- /dev/null +++ b/Assets/Scripts/Card/States/Movable.cs @@ -0,0 +1,63 @@ +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; + } +} \ No newline at end of file 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..c507c38 --- /dev/null +++ b/Assets/Scripts/Card/States/Received.cs @@ -0,0 +1,26 @@ +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"); + + } +} \ No newline at end of file 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(); From ed72d451c011a5f45cd4856ad0c48d2647d09b9c Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 30 Jan 2021 17:05:53 +0100 Subject: [PATCH 2/2] Fix pre-commit --- Assets/Scripts/Card/Interfaces/IState.cs | 5 +++-- Assets/Scripts/Card/States/Movable.cs | 17 ++++++++++------- Assets/Scripts/Card/States/Received.cs | 14 ++++++++------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Card/Interfaces/IState.cs b/Assets/Scripts/Card/Interfaces/IState.cs index 71218c3..5aebdce 100644 --- a/Assets/Scripts/Card/Interfaces/IState.cs +++ b/Assets/Scripts/Card/Interfaces/IState.cs @@ -1,5 +1,6 @@ -public interface IState{ +public interface IState +{ void OnMouseUp(); void OnMouseDown(); void Update(); -} \ No newline at end of file +} diff --git a/Assets/Scripts/Card/States/Movable.cs b/Assets/Scripts/Card/States/Movable.cs index d751449..0b440b9 100644 --- a/Assets/Scripts/Card/States/Movable.cs +++ b/Assets/Scripts/Card/States/Movable.cs @@ -1,10 +1,12 @@ using UnityEngine; -public class Movable: IState{ +public class Movable : IState +{ - public SimpleCard card {get; set;} - public Movable(SimpleCard _card){ + public SimpleCard card { get; set; } + public Movable(SimpleCard _card) + { this.card = _card; } public void OnMouseDown() @@ -21,10 +23,11 @@ public void OnMouseUp() if (collisionObject == null || !collisionObject.IsReceivable()) this.card.ResetPosition(); - else{ + else + { collisionObject.ReceiveObject(this.card); - this.card.state = new Received(this.card); - } + this.card.state = new Received(this.card); + } } public void Update() @@ -60,4 +63,4 @@ public void Update() return collisionObject; } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Card/States/Received.cs b/Assets/Scripts/Card/States/Received.cs index c507c38..04ae255 100644 --- a/Assets/Scripts/Card/States/Received.cs +++ b/Assets/Scripts/Card/States/Received.cs @@ -1,10 +1,12 @@ using UnityEngine; -public class Received : IState { - - public SimpleCard card {get; set;} +public class Received : IState +{ - public Received(SimpleCard _card){ + public SimpleCard card { get; set; } + + public Received(SimpleCard _card) + { this.card = _card; } @@ -15,7 +17,7 @@ public void OnMouseDown() public void OnMouseUp() { - Debug.Log("Log message"); + Debug.Log("Log message"); } public void Update() @@ -23,4 +25,4 @@ public void Update() Debug.Log("Log message"); } -} \ No newline at end of file +}