Skip to content

Commit

Permalink
Modified to allow multiple playsets
Browse files Browse the repository at this point in the history
GUI is ugly; should get redone with the new UI stuff.
  • Loading branch information
ianhorswill committed Nov 7, 2015
1 parent d5c49a7 commit df00658
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 43 deletions.
109 changes: 67 additions & 42 deletions Assets/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Prolog;
using UnityEngine;

// ReSharper disable once CheckNamespace
public class Driver : MonoBehaviour
{
public GUIStyle CharacterNameStyle;
Expand Down Expand Up @@ -30,41 +31,65 @@ public class Driver : MonoBehaviour
private readonly object[] playerRelationships = new object[MaxPlayers];
private readonly bool[] playerRelationshipPin = new bool[MaxPlayers];

int currentPlaysetNumber;
Playset currentPlayset;

internal void Start()
{
SetPlayset(0);
}

void SetPlayset(int playsetNumber)
{
currentPlaysetNumber = playsetNumber;
currentPlayset = Playset.Playsets[playsetNumber];
Array.Clear(playerDetailPin, 0, playerDetailPin.Length);
Array.Clear(playerRelationshipPin, 0, playerRelationshipPin.Length);
}

internal void OnGUI()
{
var nameHeight = CharacterNameStyle.lineHeight;

GUI.Label(new Rect(30, 30, 0, 0), "Fiascomatic", CharacterNameStyle);
GUI.Label(new Rect(30, 60, 100, 30), "Players:");
countSelection = GUI.Toolbar(new Rect(80, 60, 100, 20), countSelection, playerCountStrings);

GUI.Label(new Rect(30, 60, 100, 30), "Playset:");
var playsetSelection = GUI.Toolbar(new Rect(0, 0, Screen.width, 20), currentPlaysetNumber, Playset.PlaysetNames);
if (playsetSelection != currentPlaysetNumber)
{
SetPlayset(playsetSelection);
}

GUI.Label(new Rect(30, 90, 100, 30), "Players:");
countSelection = GUI.Toolbar(new Rect(80, 90, 100, 20), countSelection, playerCountStrings);
playerCount = countSelection + 3;

GUI.Label(new Rect(30, 175, 150, 300), "Click on a character name to edit it");
GUI.Label(new Rect(30, 205, 150, 300), "Click on a character name to edit it");

if (GUI.Button(new Rect(30, 100, 200, 60), "<b>Let the suffering begin!</b>"))
if (GUI.Button(new Rect(30, 130, 200, 60), "<b>Let the suffering begin!</b>"))
{
this.Setup();
Setup();
}

for (int i = 0; i < playerCount; i++)
{
var textposition = this.PlayerScreenPosition(i);
var textposition = PlayerScreenPosition(i);
playerNames[i] = GUI.TextField(
new Rect(textposition.x, textposition.y, 100, nameHeight),
playerNames[i],
this.CharacterNameStyle);
var detailposition = this.DetailScreenPosition(i);
CharacterNameStyle);
var detailposition = DetailScreenPosition(i);
GUI.Label(
new Rect(detailposition.x, detailposition.y, 160, 300),
playerDetailStrings[i],
this.DetailStyle);
DetailStyle);
if (playerDetails[i] != null)
playerDetailPin[i] = GUI.Toggle(new Rect(detailposition.x - 25, detailposition.y, 20, 20), playerDetailPin[i], "");
var relationshipposition = this.RelationshipScreenPosition(i);
var relationshipposition = RelationshipScreenPosition(i);
GUI.Label(
new Rect(relationshipposition.x, relationshipposition.y, 160, 300),
playerRelationshipStrings[i],
this.RelationshipStyle);
RelationshipStyle);
if (playerRelationships[i] != null)
playerRelationshipPin[i] = GUI.Toggle(new Rect(relationshipposition.x - 25, relationshipposition.y, 20, 20), playerRelationshipPin[i], "");
}
Expand All @@ -73,70 +98,70 @@ internal void OnGUI()
private void Setup()
{
PrologContext.DefaultStepLimit = 1000000;
switch (this.playerCount)
switch (playerCount)
{
case 3:
this.SolveSetup(new[]
SolveSetup(new[]
{
this.RelationshipTuple(0, 1), this.RelationshipTuple(1, 2), this.RelationshipTuple(2, 0),
this.DetailTuple(0), this.DetailTuple(1), this.DetailTuple(2)
RelationshipTuple(0, 1), RelationshipTuple(1, 2), RelationshipTuple(2, 0),
DetailTuple(0), DetailTuple(1), DetailTuple(2)
});
break;

case 4:
this.SolveSetup(new[]
SolveSetup(new[]
{
this.RelationshipTuple(0, 1), this.RelationshipTuple(1, 2), this.RelationshipTuple(2, 3),
this.RelationshipTuple(3, 0),
this.DetailTuple(0), this.DetailTuple(1), this.DetailTuple(2),
this.DetailTuple(3)
RelationshipTuple(0, 1), RelationshipTuple(1, 2), RelationshipTuple(2, 3),
RelationshipTuple(3, 0),
DetailTuple(0), DetailTuple(1), DetailTuple(2),
DetailTuple(3)
});
break;

case 5:
this.SolveSetup(new[]
SolveSetup(new[]
{
this.RelationshipTuple(0, 1), this.RelationshipTuple(1, 2), this.RelationshipTuple(2, 3),
this.RelationshipTuple(3, 4), this.RelationshipTuple(4, 0),
this.DetailTuple(0), this.DetailTuple(1), this.DetailTuple(2),
this.DetailTuple(3), this.DetailTuple(4)
RelationshipTuple(0, 1), RelationshipTuple(1, 2), RelationshipTuple(2, 3),
RelationshipTuple(3, 4), RelationshipTuple(4, 0),
DetailTuple(0), DetailTuple(1), DetailTuple(2),
DetailTuple(3), DetailTuple(4)
});
break;

case 6:
this.SolveSetup(new[]
SolveSetup(new[]
{
this.RelationshipTuple(0, 1), this.RelationshipTuple(1, 2), this.RelationshipTuple(2, 3),
this.RelationshipTuple(3, 4), this.RelationshipTuple(4, 5), this.RelationshipTuple(5, 0),
this.DetailTuple(0), this.DetailTuple(1), this.DetailTuple(2),
this.DetailTuple(3), this.DetailTuple(4), this.DetailTuple(5)
RelationshipTuple(0, 1), RelationshipTuple(1, 2), RelationshipTuple(2, 3),
RelationshipTuple(3, 4), RelationshipTuple(4, 5), RelationshipTuple(5, 0),
DetailTuple(0), DetailTuple(1), DetailTuple(2),
DetailTuple(3), DetailTuple(4), DetailTuple(5)
});
break;
}
}

private void SolveSetup(object[] setup)
{
using (var prologContext = PrologContext.Allocate(KnowledgeBase.Global, null))
using (var prologContext = PrologContext.Allocate(currentPlayset.KnowledgeBase, null))
{
if (!prologContext.IsTrue("make_setup", Prolog.Prolog.IListToPrologList(setup)))
Debug.Log("Failed to make setup!");
else
{
for (int i = 0; i < this.playerCount; i++)
for (int i = 0; i < playerCount; i++)
{
this.playerRelationships[i] = Term.CopyInstantiation(setup[i]);
this.playerRelationshipStrings[i] =
playerRelationships[i] = Term.CopyInstantiation(setup[i]);
playerRelationshipStrings[i] =
string.Format("<b>{0}</b>",
((Structure)(setup[i])).Argument(1).ToString().Replace("/", " / ")).Replace("_", " ");
}
for (int i = 0; i < this.playerCount; i++)
for (int i = 0; i < playerCount; i++)
{
this.playerDetails[i] = Term.CopyInstantiation(setup[this.playerCount + i]);
this.playerDetailStrings[i] = string.Format(
playerDetails[i] = Term.CopyInstantiation(setup[playerCount + i]);
playerDetailStrings[i] = string.Format(
"{0}:\n<b>{1}</b>",
((Structure)(setup[this.playerCount + i])).Argument(1),
((Structure)(setup[this.playerCount + i])).Argument(2)).Replace("_", " ");
((Structure)(setup[playerCount + i])).Argument(1),
((Structure)(setup[playerCount + i])).Argument(2)).Replace("_", " ");
}
}
}
Expand Down Expand Up @@ -167,7 +192,7 @@ object DetailTuple(int player)
private Vector2 PlayerScreenPosition(int i)
{
var center = new Vector2(Screen.width / 2f , Screen.height / 2f - 50);
var angle = i * Math.PI * 2 / this.playerCount;
var angle = i * Math.PI * 2 / playerCount;
var radius = (Math.Min(Screen.width, Screen.height) * 0.5f) - 70;
var textposition = center + radius * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
return textposition;
Expand All @@ -176,14 +201,14 @@ private Vector2 PlayerScreenPosition(int i)
private Vector2 RelationshipScreenPosition(int i)
{
var center = new Vector2(Screen.width / 2f , Screen.height / 2f - 50);
var angle = (i+0.5f) * Math.PI * 2 / this.playerCount;
var angle = (i+0.5f) * Math.PI * 2 / playerCount;
var radius = (Math.Min(Screen.width, Screen.height) * 0.5f) - 70;
var textposition = center + radius * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
return textposition;
}

private Vector2 DetailScreenPosition(int i)
{
return this.PlayerScreenPosition(i) + new Vector2(0f, CharacterNameStyle.lineHeight);
return PlayerScreenPosition(i) + new Vector2(0f, CharacterNameStyle.lineHeight);
}
}
Binary file modified Assets/MainScene.unity
Binary file not shown.
43 changes: 43 additions & 0 deletions Assets/Playset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using Prolog;

public class Playset
{
const string PlaysetsDirectory = "Playsets";

public static readonly List<Playset> Playsets = new List<Playset>();
public static readonly string[] PlaysetNames;
static Playset()
{
string playsetsPath = Path.Combine(Application.dataPath, PlaysetsDirectory);
string[] playsetsDirectory = Directory.GetFiles(playsetsPath);
var names = new List<string>();
foreach (var playset in playsetsDirectory)
{
if (Path.GetExtension(playset) == ".prolog")
{
var playsetName = Path.GetFileNameWithoutExtension(playset);
if (playsetName != null)
{
Playsets.Add(new Playset(playset));
names.Add(playsetName);
}
}
}
PlaysetNames = names.ToArray();
}


private Playset(string path)
{
KnowledgeBase = new KnowledgeBase(Path.GetFileNameWithoutExtension(path), null);
KnowledgeBase.Consult(Path.Combine(Application.dataPath, "Solver.prolog"));
KnowledgeBase.Consult(path);
}

public readonly KnowledgeBase KnowledgeBase;
}

12 changes: 12 additions & 0 deletions Assets/Playset.cs.meta

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

Loading

0 comments on commit df00658

Please sign in to comment.