Implemented the death noticing and friendship beam coloring.

Signed-off-by: RosimInc <rosim_inc@hotmail.com>
This commit is contained in:
RosimInc 2015-08-15 15:13:50 -04:00
parent 9d06c3fe30
commit d05abccd45
10 changed files with 262 additions and 81 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -154,12 +154,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9c9bbff116cac264d9d7fdcce8cf92e5, type: 3}
m_Name:
m_EditorClassIdentifier:
HighlightedColor: {r: 1, g: 1, b: 1, a: .501960814}
HighlightedColor: {r: 1, g: 1, b: 1, a: .258823544}
StartPoint: {fileID: 495136}
EndPoint: {fileID: 495134}
BeamLine: {fileID: 12095142}
StartObject: {fileID: 0}
EndObject: {fileID: 0}
--- !u!120 &12095142
LineRenderer:
m_ObjectHideFlags: 1

View File

@ -1,11 +1,22 @@
using UnityEngine;
using System.Collections;
using System.Collections;
using DeathBook.Util;
using DeathBook.Model;
[RequireComponent(typeof(LineRenderer))]
public class Link : MonoBehaviour
{
public Color HighlightedColor = new Color(1f, 1f, 1f, 0.5f);
public class Link : MonoBehaviour, IObserver
{
private float highlightAlpha = 0.8f;
private float defaultAlpha = 0.5f;
private Color currentDefaultColor;
private Color currentHighlightColor;
private static float defaultScale = 0.03f;
private float hightlightScale = 0.2f;
private bool isHighlighted = false;
[SerializeField]
private Transform StartPoint;
@ -16,16 +27,28 @@ public class Link : MonoBehaviour
private LineRenderer BeamLine;
//public ParticleSystem BeamParticles;
[SerializeField]
private Transform StartObject;
[SerializeField]
private Transform EndObject;
private Transform EndObject;
private FriendshipLink model;
public FriendshipLink Model
{
get { return model; }
set
{
model = value;
model.Subscribe(this);
//Make it between 0.1 and 0.4
GetColors(Model.Awareness);
hightlightScale = Model.Importance * 0.3f + 0.1f;
Highlight(false);
}
}
private float LIFETIME_RATIO = 0.025f;
private Renderer _renderer;
private Color _defaultColor;
void Awake()
{
@ -36,14 +59,21 @@ public class Link : MonoBehaviour
_renderer.material = Instantiate(_renderer.material);
_defaultColor = _renderer.material.GetColor("_TintColor");
//_defaultColor = _renderer.material.GetColor("_TintColor");
//Activate(false);
}
}
public void Notify()
{
GetColors(Model.Awareness);
UpdateBeam();
//TODO SR
}
void Update()
{
UpdateVisualEffects();
{
UpdateVisualEffects();
}
public void Activate(bool state)
@ -79,10 +109,35 @@ public class Link : MonoBehaviour
EndObject = destination.transform;
}
public void Highlight(bool state, float weight)
{
// For now, the weight does nothing but it should eventually influence the intensity and size of the link
_renderer.material.SetColor("_TintColor", state ? HighlightedColor : _defaultColor);
}
public void Highlight(bool state)
{
; isHighlighted = state;
UpdateBeam();
}
private void GetColors(float level)
{
//If level is 0.0, green [0,1,0].
//If level is 0.5, yellow [1,1,0].
//If level is 1.0, red [1,0,0].
float r = 1f;
float g = 1f;
if (level < 0.5f)
r = Mathf.Lerp(0, 1, level*2);
else
g = Mathf.Lerp(1, 0, level * 2 - 1);
currentDefaultColor = new Color(r, g, 0f, defaultAlpha);
currentHighlightColor = new Color(r, g, 0f, highlightAlpha);
}
private void UpdateBeam()
{
float width = isHighlighted ? hightlightScale : defaultScale;
BeamLine.SetWidth(width, width);
_renderer.material.SetColor("_TintColor", isHighlighted ? currentHighlightColor : currentDefaultColor);
}
}

View File

@ -5,7 +5,7 @@ using DeathBook.Util;
namespace DeathBook.Model
{
public class Friendship : Observable, Updatable
public class Friendship : Updatable
{
private Person self;
public Person Self { get { return self; } }
@ -18,21 +18,28 @@ namespace DeathBook.Model
private FriendshipLink link;
public FriendshipLink Link { get { return link; } }
private float awareness = 0; //on a scale from 0 to 1
public float Awareness { get { return awareness; } }
private bool noticedDeath = false;
public Friendship(Person self, Person friend)
public Friendship(Person self, Person friend, FriendshipLink link)
{
this.self = self;
this.friend = friend;
this.link = link;
}
public void Update(float deltaTime)
{
//This function is only called when friend is dead
awareness = Mathf.Max(awareness + deltaTime * CalculateWeight(), 100);
if (noticedDeath)
return;
NotifyObservers();
//This function is only called when friend is dead
//awareness = Mathf.Min(awareness + deltaTime * CalculateWeight(), 1);
link.Awareness = Mathf.Min(link.Awareness + deltaTime * 0.1f, 1f);
if (link.Awareness >= 1f)
{
self.NoticeDeath(this);
noticedDeath = true;
}
}
//returns a number between 0 and 1
@ -41,13 +48,14 @@ namespace DeathBook.Model
float weight = 0;
weight += link.Importance;
//weight += friend.TimeBetweenPosts;
return weight;
return weight * 0.1f;
}
internal enum Knowledge
/*internal enum Knowledge
{
Alive, Doubt, Dead
}
}*/
}
}

View File

@ -13,8 +13,12 @@ namespace DeathBook.Model
private float importance; //on a scale from 0 to 1
public float Importance { get { return importance; } }
private float risk = 0; //on a scale from 0 to 1
public float Risk { get { return risk; } }
private float awareness = 0; //on a scale from 0 to 1
public float Awareness
{
get { return awareness; }
set { awareness = value; NotifyObservers(); }
}
public FriendshipLink(Person p1, Person p2, float importance)
{

View File

@ -111,8 +111,6 @@ namespace DeathBook.Model
}
list.Remove(smallest);
}
Debug.Log("Friends + " + p1.FriendCount);
}
return friendships;
@ -120,9 +118,9 @@ namespace DeathBook.Model
private FriendshipLink CreateFriendship(Person p1, Person p2)
{
FriendshipLink f = new FriendshipLink(p1, p2, Random.Range(1,100));
Friendship f1 = new Friendship(p1, p2);
Friendship f2 = new Friendship(p2, p1);
FriendshipLink f = new FriendshipLink(p1, p2, Random.value);
Friendship f1 = new Friendship(p1, p2, f);
Friendship f2 = new Friendship(p2, p1, f);
f1.Other = f2;
f2.Other = f1;

View File

@ -43,10 +43,7 @@ namespace DeathBook.Model
private bool alive = true;
public bool Alive { get { return alive; } }
//private int happiness = 1; //on a scale from 0 to 1
//public int Happiness { get { return happiness; } }
private bool online = false;
private bool online = true;
public bool Online { get { return online; } }
public Person(int id, Vector3 pos)
@ -66,21 +63,45 @@ namespace DeathBook.Model
friendCount++;
}
public void KillFriend(Friendship f)
public void NotifyFriendWasKilled(Friendship f)
{
Debug.Log("I am " + id + " and my friend " + f.Friend.Id + " was killed");
numAliveFriends--;
numDeadFriends++;
deadFriendsList.Add(f);
}
public void Kill()
{
Debug.Log("Person " + id + " died!");
alive = false;
foreach (Friendship f in friendsList)
f.Friend.NotifyFriendWasKilled(f.Other);
NotifyObservers();
}
public void NoticeDeath(Friendship f)
{
//TODO apply more rules here
awarenessLevel = Mathf.Min(AwarenessLevel + 0.2f, 1f);
Debug.Log("I am " + id + " and I know my friend " + f.Friend.Id + " was killed.. " + AwarenessLevel);
//TODO remove from dead friends list to accelerate
NotifyObservers();
}
public void Update(float deltaTime)
{
//TODO Update if connected
int time = LevelManager.Instance.GameLevel.GameTime;
//The following actions are only performed if user is online
if (!Online)
return;
foreach (Friendship f in deadFriendsList)
f.Update(deltaTime);
}
}
}

View File

@ -4,22 +4,37 @@ using System.Collections.Generic;
using DeathBook.Model;
public class NetworkingSphere : MonoBehaviour
{
public Link LinkObj;
public PersonNode PersonObj;
public int NumPeople = 50;
public int AvgNumFriends = 20;
public float FriendshipLikeliness = 0.4f;
public float SphereRadius = 1f;
public float rotationSpeed = 0.7f;
{
public GameObjectsOptions gameObjects = new GameObjectsOptions();
public LevelOptions levelOptions = new LevelOptions();
private NetworkDisconnection sphere;
[System.Serializable]
public class GameObjectsOptions
{
public Link LinkObj;
public PersonNode PersonObj;
}
[System.Serializable]
public class LevelOptions
{
public int NumPeople = 50;
public int AvgNumFriends = 20;
public float FriendshipLikeliness = 0.4f;
public float SphereRadius = 1f;
}
public float rotationSpeed = 0.7f;
public float torqueForce = 50f;
public PersonDetailsPanel DetailsPanel;
private bool dragging = false;
private Vector3 delta = new Vector3();
private Rigidbody rb;
private Rigidbody rb;
private LevelManager manager;
private PersonNode[] peopleNodes;
@ -27,17 +42,24 @@ public class NetworkingSphere : MonoBehaviour
void Awake()
{
LevelManager manager = LevelManager.Instance;
manager.NewLevel(NumPeople, AvgNumFriends, FriendshipLikeliness, SphereRadius);
manager = LevelManager.Instance;
manager.NewLevel(levelOptions.NumPeople, levelOptions.AvgNumFriends, levelOptions.FriendshipLikeliness, levelOptions.SphereRadius);
Level lvl = manager.GameLevel;
InstantiateNodes(lvl);
AssignLinks(lvl);
rb = GetComponent<Rigidbody>();
}
}
/*void OnGUI()
{
GUI.Button(new Rect(10, 100, 400, 40), manager.GameLevel.GameTime + "");
}*/
void Update()
{
{
manager.GameLevel.Update(Time.deltaTime);
//TEMPORARY QUICK FIX: Even though we are never moving the sphere, it starts moving as soon as it stops rotating
transform.position = Vector3.zero;
@ -47,13 +69,13 @@ public class NetworkingSphere : MonoBehaviour
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)
// 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) > levelOptions.SphereRadius + 1f)
{
transform.Rotate(Vector3.one * Time.deltaTime * rotationSpeed);
}
//when right btn clicked, call the chnge rotation
//when right btn clicked, call the change rotation
if (Input.GetMouseButtonDown(1))
{
dragging = true;
@ -93,7 +115,7 @@ public class NetworkingSphere : MonoBehaviour
{
Person person = lvl.People[i];
PersonNode pInst = Instantiate(PersonObj, person.InitialPosition, Quaternion.identity) as PersonNode;
PersonNode pInst = Instantiate(gameObjects.PersonObj, person.InitialPosition, Quaternion.identity) as PersonNode;
pInst.OnClicked += OnNodeClicked;
@ -109,7 +131,9 @@ public class NetworkingSphere : MonoBehaviour
if (_selectedNode != null)
{
_selectedNode.Select(false);
}
}
node.Kill();
DetailsPanel.SetModel(node.Model);
node.Select(true);
@ -120,10 +144,11 @@ public class NetworkingSphere : MonoBehaviour
private void AssignLinks(Level lvl)
{
foreach (FriendshipLink f in lvl.Friendships)
{
Link link = Instantiate(LinkObj) as Link;
{
Link link = Instantiate(gameObjects.LinkObj) as Link;
int id1 = f.Friend1.id;
int id2 = f.Friend2.id;
int id2 = f.Friend2.id;
link.Model = f;
link.AttachToObjects(peopleNodes[id1].gameObject, peopleNodes[id2].gameObject);
// Temporary stuff, for testing

View File

@ -7,7 +7,10 @@ using System;
[RequireComponent(typeof(Collider))]
public class PersonNode : MonoBehaviour, IObserver
{
{
private const float UpdateFrequency = 0.5f;
private float time = 0;
public Action<PersonNode> OnClicked;
public Color SelectedColor = Color.blue;
@ -25,7 +28,8 @@ public class PersonNode : MonoBehaviour, IObserver
private Person _model;
private Renderer _renderer;
private Transform _transform;
private Transform _transform;
public Person Model
{
@ -46,7 +50,14 @@ public class PersonNode : MonoBehaviour, IObserver
}
void Update()
{
{
time += Time.deltaTime;
if (time > UpdateFrequency)
{
_model.Update(time);
time = 0;
}
// Find another way to do it if it lags to much
_transform.LookAt(new Vector3(_transform.position.x, _transform.position.y, _transform.position.z + 1));
}
@ -71,16 +82,22 @@ public class PersonNode : MonoBehaviour, IObserver
}
}
private void UpdateLinks(bool state)
private void UpdateLinks(bool isHighlighted)
{
foreach (Link link in _links)
{
link.Highlight(state, 1f);
link.Highlight(isHighlighted);
}
}
}
public void Kill()
{
_model.Kill();
}
public void Notify()
{
{
//Debug.Log("Received notification! " + Model.AwarenessLevel);
UpdateInfo();
}
@ -149,7 +166,5 @@ public class PersonNode : MonoBehaviour, IObserver
{
OnClicked(this);
}
Debug.Log("clicked");
}
}

View File

@ -136,7 +136,7 @@ Camera:
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 2
m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438}
m_BackGroundColor: {r: .329584748, g: .33866781, b: .352941155, a: .0196078438}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@ -581,7 +581,7 @@ Prefab:
objectReference: {fileID: 0}
- target: {fileID: 22417254, guid: c993e87661906234fba410e55ed66832, type: 2}
propertyPath: m_AnchorMin.y
value: .293091238
value: .690773964
objectReference: {fileID: 0}
- target: {fileID: 22417254, guid: c993e87661906234fba410e55ed66832, type: 2}
propertyPath: m_AnchorMax.x
@ -619,9 +619,40 @@ Prefab:
value:
objectReference: {fileID: 21300000, guid: 0c813218655d0a1468d7269b69deb9d5,
type: 3}
- target: {fileID: 22458514, guid: c993e87661906234fba410e55ed66832, type: 2}
propertyPath: m_AnchoredPosition.y
value: -69.4402313
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: c993e87661906234fba410e55ed66832, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1012864059
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1012864060}
m_Layer: 0
m_Name: Level
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1012864060
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1012864059}
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!1 &1057734534
GameObject:
m_ObjectHideFlags: 0
@ -639,7 +670,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1057734535
MonoBehaviour:
m_ObjectHideFlags: 0
@ -707,7 +738,7 @@ RectTransform:
- {fileID: 534364717}
- {fileID: 1608274018}
m_Father: {fileID: 0}
m_RootOrder: 3
m_RootOrder: 4
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
@ -976,7 +1007,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 5
--- !u!1 &2006180048
GameObject:
m_ObjectHideFlags: 0
@ -1076,7 +1107,7 @@ Prefab:
objectReference: {fileID: 0}
- target: {fileID: 433422, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: m_RootOrder
value: 2
value: 3
objectReference: {fileID: 0}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: DetailsPanel
@ -1094,6 +1125,32 @@ Prefab:
propertyPath: AvgNumFriends
value: 4
objectReference: {fileID: 0}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: gameObjects.PersonObj
value:
objectReference: {fileID: 11417564, guid: 7a5b1db47a2c30e419038df40dffa79a,
type: 2}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: gameObjects.LinkObj
value:
objectReference: {fileID: 11495142, guid: fab430cecad80ad4391987a06b550cb7,
type: 2}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: levelOptions.AvgNumFriends
value: 6
objectReference: {fileID: 0}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: levelOptions.SphereRadius
value: 7
objectReference: {fileID: 0}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: levelOptions.NumPeople
value: 50
objectReference: {fileID: 0}
- target: {fileID: 11432906, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
propertyPath: levelOptions.FriendshipLikeliness
value: .600000024
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 136ea38d5deb4c9418beb879167d9b03, type: 2}
m_IsPrefabParent: 0