Generated buildings
This commit is contained in:
@ -2,11 +2,48 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class Algorithmss
|
||||
{
|
||||
|
||||
public static bool DoPolygonsIntersect(Vector2[] a, Vector2[] b)
|
||||
{
|
||||
foreach (var poly in new[] {a, b})
|
||||
{
|
||||
for (int i = 0; i < poly.Length; i++)
|
||||
{
|
||||
int j = (i + 1) % poly.Length;
|
||||
|
||||
var normal = new Vector2(poly[j].Y - poly[i].Y, poly[i].X - poly[j].X);
|
||||
|
||||
double? minA = null, maxA = null;
|
||||
foreach (var p in a)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minA == null || projected < minA)
|
||||
minA = projected;
|
||||
if (maxA == null || projected > maxA)
|
||||
maxA = projected;
|
||||
}
|
||||
|
||||
double? minB = null, maxB = null;
|
||||
foreach (var p in b)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minB == null || projected < minB)
|
||||
minB = projected;
|
||||
if (maxB == null || projected > maxB)
|
||||
maxB = projected;
|
||||
}
|
||||
|
||||
if (maxA <= minB || maxB <= minA)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,41 +7,55 @@ using System.Threading;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class Task
|
||||
public class Task
|
||||
{
|
||||
private class TaskInfo
|
||||
public bool? Success { get; private set; }
|
||||
|
||||
private Exception thrownException;
|
||||
private Action action;
|
||||
private Thread thread;
|
||||
|
||||
private Task()
|
||||
{
|
||||
public Action Action { get; set; }
|
||||
public bool? Success { get; set; }
|
||||
public Exception ThrownException { get; set; }
|
||||
}
|
||||
|
||||
private static void RunAsync_ActionThread(object info)
|
||||
{
|
||||
TaskInfo taskInfo = (TaskInfo)info;
|
||||
Task taskInfo = (Task)info;
|
||||
|
||||
try
|
||||
{
|
||||
taskInfo.Action();
|
||||
taskInfo.action();
|
||||
taskInfo.Success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
taskInfo.ThrownException = ex;
|
||||
taskInfo.thrownException = ex;
|
||||
taskInfo.Success = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable RunAsync(Action action)
|
||||
public static Task RunAsync(Action action)
|
||||
{
|
||||
// Set up task info object
|
||||
TaskInfo taskInfo = new TaskInfo();
|
||||
taskInfo.Action = action;
|
||||
Task taskInfo = new Task();
|
||||
taskInfo.action = action;
|
||||
taskInfo.thread = new Thread(RunAsync_ActionThread);
|
||||
|
||||
// Start thread and wait
|
||||
var thread = new Thread(RunAsync_ActionThread);
|
||||
thread.Start(taskInfo);
|
||||
// Start thread
|
||||
taskInfo.thread.Start(taskInfo);
|
||||
|
||||
// Return task info
|
||||
return taskInfo;
|
||||
}
|
||||
|
||||
public static IEnumerable Await(Action action)
|
||||
{
|
||||
return Task.RunAsync(action).Await();
|
||||
}
|
||||
|
||||
public IEnumerable Await()
|
||||
{
|
||||
// Wait for thread to finish
|
||||
while (thread.ThreadState == ThreadState.Running)
|
||||
yield return null;
|
||||
@ -49,8 +63,8 @@ namespace TransportGame.Utils
|
||||
thread.Join();
|
||||
|
||||
// Rethrow exception
|
||||
if (taskInfo.Success.HasValue && !taskInfo.Success.Value)
|
||||
throw new Exception("Task failed", taskInfo.ThrownException);
|
||||
if (Success.HasValue && !Success.Value)
|
||||
throw new Exception("Task failed", thrownException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user