Skip to content

Commit

Permalink
Better logic for selecting and highlighting.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomoon committed Jan 25, 2015
1 parent faf0b26 commit c3aced0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Assets/Scenes/Game Scene.unity
Expand Up @@ -18,7 +18,7 @@ RenderSettings:
m_FogDensity: .00999999978
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientLight: {r: .200000003, g: .200000003, b: .200000003, a: 1}
m_AmbientLight: {r: 1, g: 1, b: 1, a: 1}
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: .5
m_FlareStrength: 1
Expand Down Expand Up @@ -351,7 +351,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHandSize: 2
cardXOffset: .75
cardXOffset: 1
cardZOffset: 1
cardsInHand: []
--- !u!1 &1625143387
Expand Down Expand Up @@ -395,7 +395,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHandSize: 4
cardXOffset: .75
cardXOffset: 1
cardZOffset: 1
cardsInHand: []
--- !u!1 &1632473607
Expand Down Expand Up @@ -439,7 +439,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHandSize: 4
cardXOffset: .75
cardXOffset: 1
cardZOffset: 1
cardsInHand: []
--- !u!1 &1784388743
Expand Down Expand Up @@ -573,7 +573,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHandSize: 4
cardXOffset: .75
cardXOffset: 1
cardZOffset: 1
cardsInHand: []
--- !u!1 &1896595282
Expand Down Expand Up @@ -693,7 +693,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHandSize: 4
cardXOffset: .75
cardXOffset: 1
cardZOffset: 1
cardsInHand: []
--- !u!1001 &2082255367
Expand Down
67 changes: 63 additions & 4 deletions Assets/Scripts/CardLogic.cs
Expand Up @@ -6,9 +6,19 @@ public class CardLogic : MonoBehaviour {
public string cardName = "none";
public bool faceUp = false;
public bool locked = false;
public bool selected = false;

private bool busy = false;
private bool mouseIn = false;

private bool visLocked = false;
private bool visSelected = false;
private bool visFaceUp = false;

private static Quaternion faceUpRotation = new Quaternion(0, 180, 0, 0);
private static Color highlightColor = new Color(1.0f, 0.75f, 0.75f, 1.0f);
private static Color selectedColor = new Color(0.75f, 0.75f, 1.0f, 1.0f);

// Called by GameLogic.CardInstantiate
public void SetUp () {
var cardMaterial = Resources.Load("Materials/Cards/" + name, typeof(Material)) as Material;
Expand All @@ -23,14 +33,63 @@ public class CardLogic : MonoBehaviour {

// Update is called once per frame
void Update () {
//
// Transition into states
if (faceUp != visFaceUp){
visFaceUp = faceUp;
transform.rotation = transform.rotation * new Quaternion(0, 180, 0, 0);
if (faceUp) {
transform.rotation = transform.rotation * faceUpRotation;
}else{
transform.rotation = transform.rotation * Quaternion.Inverse(faceUpRotation);
}

}

if (selected != visSelected){
visSelected = selected;
if (selected) {
transform.Translate(0.0f, 0.25f, 0.0f);
}else{
transform.Translate(0.0f, -0.25f, 0.0f);
}
}

// Color based on state
var shaderColor = Color.white;
if (selected) {
shaderColor = shaderColor * selectedColor;
}

if (mouseIn && !locked && !busy && faceUp){
shaderColor = shaderColor * highlightColor;
}
}

var frontQuad = this.transform.FindChild("Front").gameObject;
frontQuad.renderer.material.color = shaderColor;
}

void OnMouseDown() {
Debug.Log(string.Format("Clicked `{0}` in hand `{1}`", name, this.transform.parent.name));
//Debug.Log(string.Format("MouseDown `{0}`, hand `{1}`", name, this.transform.parent.name));
if (!locked && !busy && faceUp) {
//Check if any other card in the same hand is locked
var children = transform.parent.GetComponentsInChildren<CardLogic>();
foreach (var child in children) {
if (child.selected && child != this) {
child.selected = false;
}
}

//Swap selected state
selected = !selected;
}
}

void OnMouseEnter() {
//Debug.Log(string.Format("MouseEnter `{0}`, hand `{1}`", name, this.transform.parent.name));
mouseIn = true;
}

void OnMouseExit() {
//Debug.Log(string.Format("MouseExit `{0}`, hand `{1}`", name, this.transform.parent.name));
mouseIn = false;
}
}

0 comments on commit c3aced0

Please sign in to comment.