diff --git a/Assets/Prefabs.meta b/Assets/Prefabs.meta new file mode 100644 index 0000000..97878aa --- /dev/null +++ b/Assets/Prefabs.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: d5f769927ebd19846af8f5f75ec7ea05 +folderAsset: yes +timeCreated: 1439424313 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Creator.unity b/Assets/Scenes/Creator.unity new file mode 100644 index 0000000..cb05099 --- /dev/null +++ b/Assets/Scenes/Creator.unity @@ -0,0 +1,341 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +SceneSettings: + m_ObjectHideFlags: 0 + m_PVSData: + m_PVSObjectsArray: [] + m_PVSPortalsArray: [] + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: .25 + backfaceThreshold: 100 +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 6 + m_Fog: 0 + m_FogColor: {r: .5, g: .5, b: .5, a: 1} + m_FogMode: 3 + m_FogDensity: .00999999978 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: .211999997, g: .226999998, b: .259000003, a: 1} + m_AmbientEquatorColor: {r: .114, g: .125, b: .133000001, a: 1} + m_AmbientGroundColor: {r: .0469999984, g: .0430000015, b: .0350000001, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: .5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} +--- !u!127 &3 +LevelGameManager: + m_ObjectHideFlags: 0 +--- !u!157 &4 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 5 + m_GIWorkflowMode: 0 + m_LightmapsMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_TemporalCoherenceThreshold: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 1 + m_LightmapEditorSettings: + serializedVersion: 3 + m_Resolution: 2 + m_BakeResolution: 40 + m_TextureWidth: 1024 + m_TextureHeight: 1024 + m_AOMaxDistance: 1 + m_Padding: 2 + m_CompAOExponent: 0 + m_LightmapParameters: {fileID: 0} + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherRayCount: 1024 + m_LightmapSnapshot: {fileID: 0} + m_RuntimeCPUUsage: 25 +--- !u!196 &5 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentRadius: .5 + agentHeight: 2 + agentSlope: 45 + agentClimb: .400000006 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + accuratePlacement: 0 + minRegionArea: 2 + cellSize: .166666672 + manualCellSize: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &61719360 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 61719365} + - 20: {fileID: 61719364} + - 92: {fileID: 61719363} + - 124: {fileID: 61719362} + - 81: {fileID: 61719361} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &61719361 +AudioListener: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 61719360} + m_Enabled: 1 +--- !u!124 &61719362 +Behaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 61719360} + m_Enabled: 1 +--- !u!92 &61719363 +Behaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 61719360} + m_Enabled: 1 +--- !u!20 &61719364 +Camera: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 61719360} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438} + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: .300000012 + far clip plane: 1000 + field of view: 49 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_HDR: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: .0219999999 +--- !u!4 &61719365 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 61719360} + m_LocalRotation: {x: -.259112954, y: .110958092, z: -.0299803261, w: -.958983839} + m_LocalPosition: {x: 2.95000005, y: 9.18000031, z: -15.3199997} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 +--- !u!1 &736567784 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 736567788} + - 33: {fileID: 736567787} + - 135: {fileID: 736567786} + - 23: {fileID: 736567785} + - 114: {fileID: 736567789} + - 54: {fileID: 736567790} + m_Layer: 0 + m_Name: Center + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!23 &736567785 +MeshRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_Materials: + - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} + m_SubsetIndices: + m_StaticBatchRoot: {fileID: 0} + m_UseLightProbes: 1 + m_ReflectionProbeUsage: 1 + m_ProbeAnchor: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 1 + m_ImportantGI: 0 + m_AutoUVMaxDistance: .5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingOrder: 0 +--- !u!135 &736567786 +SphereCollider: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Radius: .5 + m_Center: {x: 0, y: 0, z: 0} +--- !u!33 &736567787 +MeshFilter: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &736567788 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 +--- !u!114 &736567789 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a24d489629b440b4d9fcfafc24239241, type: 3} + m_Name: + m_EditorClassIdentifier: + LinkObj: {fileID: 11495142, guid: fab430cecad80ad4391987a06b550cb7, type: 2} + PersonObj: {fileID: 11406500, guid: 646dd6566f9e1374caa3af8ad37c43d3, type: 2} + NumPeople: 100 + AvgNumFriends: 20 + SphereRadius: 7 + rotationSpeed: .699999988 + torqueForce: 35 +--- !u!54 &736567790 +Rigidbody: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 736567784} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: .800000012 + m_UseGravity: 0 + m_IsKinematic: 0 + m_Interpolate: 0 + m_Constraints: 0 + m_CollisionDetection: 0 +--- !u!1 &1126063559 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1126063561} + - 108: {fileID: 1126063560} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &1126063560 +Light: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1126063559} + m_Enabled: 1 + serializedVersion: 6 + m_Type: 1 + m_Color: {r: 1, g: .956862748, b: .839215696, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_CookieSize: 10 + m_Shadows: + m_Type: 2 + m_Resolution: -1 + m_Strength: 1 + m_Bias: .0500000007 + m_NormalBias: .400000006 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_Lightmapping: 4 + m_BounceIntensity: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 + m_AreaSize: {x: 1, y: 1} +--- !u!4 &1126063561 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1126063559} + m_LocalRotation: {x: .408217937, y: -.234569728, z: .109381676, w: .875426054} + m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 diff --git a/Assets/Scenes/Creator.unity.meta b/Assets/Scenes/Creator.unity.meta new file mode 100644 index 0000000..df3b858 --- /dev/null +++ b/Assets/Scenes/Creator.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 99408b15168559b46a7d7832ffcbddde +timeCreated: 1439446751 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Models/Friendship.cs b/Assets/Scripts/Models/Friendship.cs index caba40d..a5563a9 100644 --- a/Assets/Scripts/Models/Friendship.cs +++ b/Assets/Scripts/Models/Friendship.cs @@ -3,9 +3,16 @@ using System.Collections.Generic; namespace DeathBook.Model { - class Friendship + public class Friendship { - private Person friend1, friend2; + public Person friend1, friend2; private int importance; //on a scale from 1 to 100 + + public Friendship(Person p1, Person p2, int scale) + { + friend1 = p1; + friend2 = p2; + importance = scale; + } } } diff --git a/Assets/Scripts/Models/Level.cs b/Assets/Scripts/Models/Level.cs index d07b8ed..ba99bd7 100644 --- a/Assets/Scripts/Models/Level.cs +++ b/Assets/Scripts/Models/Level.cs @@ -7,12 +7,18 @@ namespace DeathBook.Model { private int score; - private List peeps; - private List friendships; + public List people; + public List friendships; //private Generator gen; private int gameTime; private int globalAwareness; + + public Level(List people, List friendships) + { + this.people = people; + this.friendships = friendships; + } } } diff --git a/Assets/Scripts/Models/LevelGenerator.cs b/Assets/Scripts/Models/LevelGenerator.cs index cdd84f0..5167279 100644 --- a/Assets/Scripts/Models/LevelGenerator.cs +++ b/Assets/Scripts/Models/LevelGenerator.cs @@ -5,41 +5,42 @@ namespace DeathBook.Model { public class LevelGenerator { - private int numFriends; + private int numPeople; private int avgConnections; private float radius; - public Level GenerateLevel() + public Level GenerateLevel(int numPeople, int avgFriends, float radius) { - //and here... - + this.numPeople = numPeople; + this.avgConnections = avgFriends; + this.radius = radius; List people = CreatePeople(); List friendships = CreateFriendships(people); - return null; + return new Level(people, friendships); } private List CreatePeople() { - List people = new List(numFriends); + List people = new List(numPeople); float dlong = Mathf.PI * (3 - Mathf.Sqrt(5)); //~2.39996323 - float dz = (2f / numFriends) * radius; + float dz = (2f / numPeople) * radius; float longitude = 0f; float z = radius - dz / 2; float r, x, y; Person p; - for (int i = 0; i < numFriends; i++) + for (int i = 0; i < numPeople; i++) { r = Mathf.Sqrt(radius * radius - z * z); x = Mathf.Cos(longitude) * r; y = Mathf.Sin(longitude) * r; - p = new Person(x,y,z); + p = new Person(i, x, y, z); people.Add(p); z -= dz; @@ -47,31 +48,91 @@ namespace DeathBook.Model longitude += dlong; } + Debug.Log("People: " + people.Count); + return people; } private List CreateFriendships(List people) { + Debug.Log("Creating friendships"); + List friendships = new List(); + Person p1, p2; - float rSq = radius * radius; + int totalCount = people.Count; + int missing; - for (int i = 0, count = people.Count; i < count; i++) + LinkedList list = new LinkedList(); + + for (int i = 0; i < totalCount; i++) { - for (int j = i+1; j < count; j++) + Debug.Log("Person " + (i+1)); + p1 = people[i]; + missing = avgConnections - p1.numFriends; // TODO Add randomness + + if (missing <= 0) + break; + + list.Clear(); + + for (int j = i+1; j < totalCount; j++) { - // Nombre d'amis présents + p2 = people[j]; + if (p2.numFriends < avgConnections * 1.2) + list.AddLast(new DistanceNode(p1, p2)); + } - // + while (list.Count > 0 && missing > 0) + { + DistanceNode smallest = list.First.Value; + foreach (DistanceNode node in list) + { + if (node.dist < smallest.dist) + //TODO Add probabilities + smallest = node; + } + //TODO Code/use a heap instead + friendships.Add(CreateFriendship(p1, smallest.p)); + missing--; + list.Remove(smallest); } } + Debug.Log(friendships.Count); + return friendships; + } - - - - //TODO - return null; + private Friendship CreateFriendship(Person p1, Person p2) + { + Friendship f = new Friendship(p1, p2, Random.Range(1,100)); + p1.AddFriendship(f); + p2.AddFriendship(f); + return f; } } + + internal class DistanceNode + { + public Person p; + public float dist; + + public DistanceNode(Person p1, Person p2) + { + p = p2; + dist = (p2.initialPosition - p1.initialPosition).sqrMagnitude; + } + } + + + /* + * 1. Friendship urgency - 0-1 + * Number of friends missing + * VS number of nodes left + * + * 2. Friendship possibility + * Closeness + * 0 < distance^2 < root(2)*rSq < 4*rSq + * + */ } diff --git a/Assets/Scripts/Models/Person.cs b/Assets/Scripts/Models/Person.cs index 7b0823f..2afc59b 100644 --- a/Assets/Scripts/Models/Person.cs +++ b/Assets/Scripts/Models/Person.cs @@ -5,9 +5,11 @@ namespace DeathBook.Model { public class Person { + public int id; private string name; private List friendList = new List(); public Vector3 initialPosition; + public int numFriends; private int timeBetweenPosts; // f = 1/T; private int connectionTime; private int disconnectionTime; @@ -19,11 +21,18 @@ namespace DeathBook.Model //private Node node; - public Person(float x, float y, float z) + public Person(int id, float x, float y, float z) { + this.id = id; initialPosition = new Vector3(x, y, z); } + public void AddFriendship(Friendship f) + { + friendList.Add(f); + numFriends++; + } + private bool isConnected(int time) { return disconnectionTime > time && time > connectionTime; diff --git a/Assets/Tests/SphereSR.cs b/Assets/Tests/SphereSR.cs new file mode 100644 index 0000000..692681f --- /dev/null +++ b/Assets/Tests/SphereSR.cs @@ -0,0 +1,119 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using DeathBook.Model; + +public class SphereSR : MonoBehaviour +{ + public FriendshipLink LinkObj; + public PersonTest PersonObj; + public int NumPeople = 50; + public int AvgNumFriends = 20; + public float SphereRadius = 1f; + public float rotationSpeed = 0.7f; + + public float torqueForce = 50f; + private bool dragging = false; + private Vector3 delta = new Vector3(); + private Rigidbody rb; + + private PersonTest[] peopleNodes; + //TODO private Friendship[] friendships; + private GameObject[] nodes; + + void Awake() + { + LevelGenerator lGen = new LevelGenerator(); + Level lvl = lGen.GenerateLevel(NumPeople, AvgNumFriends, SphereRadius); + + InstantiateNodes(lvl); + AssignLinks(lvl); + rb = GetComponent(); + } + + void Update() + { + Vector3 screenMousePos = Input.mousePosition; + + screenMousePos.z = transform.position.z - Camera.main.transform.position.z; + + Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos); + + // If the world position of the mouse is greater than the radius of the sphere, we are outside + if (Mathf.Sqrt(worldMousePos.x * worldMousePos.x + worldMousePos.y * worldMousePos.y) > SphereRadius + 1f) + { + transform.Rotate(Vector3.one * Time.deltaTime * rotationSpeed); + } + + //when right btn clicked, call the chnge rotation + if (Input.GetMouseButtonDown(1)) + { + dragging = true; + } + else if (Input.GetMouseButtonUp(1)) + { + dragging = false; + delta = new Vector3(); + } + + if (dragging) + { + MoveSphere(); + } + } + + void MoveSphere() + { + float deltaX = Input.GetAxis("Mouse X"); + float deltaY = Input.GetAxis("Mouse Y"); + if (deltaX == 0 && deltaY == 0) + { + delta = new Vector3(); + rb.angularVelocity *= 0.8f; + } + delta += new Vector3(deltaX, deltaY, 0); + //rigidbody.AddTorque(); + rb.AddTorque(Vector3.down * delta.x * torqueForce * Time.deltaTime, ForceMode.Impulse); + rb.AddTorque(Vector3.right * delta.y * torqueForce * Time.deltaTime, ForceMode.Impulse); + Debug.Log(delta.x + ", " + delta.y); + + + } + + private void InstantiateNodes(Level lvl) + { + /* Sphere uniform distribution using the spiral method with the golden angle + * ~2.39996323 rad, the golden angle (the most irrational angle) + * is used here to make sure that the sin and cos functions + * dont end up drawing clusters of points and the spirals are way + * less visible. + */ + peopleNodes = new PersonTest[lvl.people.Count]; + + int ctr = 0; + foreach (Person p in lvl.people) + { + + PersonTest pInst = Instantiate(PersonObj, p.initialPosition, Quaternion.identity) as PersonTest; + + pInst.transform.parent = this.transform; + + peopleNodes[ctr++] = pInst; + } + } + + private void AssignLinks(Level lvl) + { + foreach (Friendship f in lvl.friendships) + { + FriendshipLink link = Instantiate(LinkObj) as FriendshipLink; + int id1 = f.friend1.id; + int id2 = f.friend2.id; + link.AttachToObjects(peopleNodes[id1].gameObject, peopleNodes[id2].gameObject); + + // Temporary stuff, for testing + peopleNodes[id1].AddLink(link); + peopleNodes[id2].AddLink(link); + } + } +} diff --git a/Assets/Tests/SphereSR.cs.meta b/Assets/Tests/SphereSR.cs.meta new file mode 100644 index 0000000..d55f23a --- /dev/null +++ b/Assets/Tests/SphereSR.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a24d489629b440b4d9fcfafc24239241 +timeCreated: 1439446791 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: