Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement state pattern for card's states #57

Merged
merged 2 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Scripts/Card/Interfaces.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Assets/Scripts/Card/Interfaces/IState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public interface IState
{
void OnMouseUp();
void OnMouseDown();
void Update();
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Card/Interfaces/IState.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 8 additions & 45 deletions Assets/Scripts/Card/SimpleCard.cs
Original file line number Diff line number Diff line change
@@ -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()
{

}
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/Card/States.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions Assets/Scripts/Card/States/Movable.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Card/States/Movable.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Assets/Scripts/Card/States/Received.cs
Original file line number Diff line number Diff line change
@@ -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");

}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Card/States/Received.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Assets/Scripts/Interfaces/Interactable.cs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down