public void CreateSocialCircle(SocialAgent otherAgent) { if (!isInCircle && isSocial) { //Debug.Log("social circle"); if (WorldState.GetInstance().socialCircles.ContainsKey((otherAgent.transform.position + transform.position) / 2)) { //Debug.Log("Joining social circle"); WorldState.GetInstance().socialCircles[(otherAgent.transform.position + transform.position) / 2].Add(this); //isInCircle = true; //isSocial = true; } else { //Debug.Log("Creating social circle"); centerOfSocial = (otherAgent.transform.position + transform.position) / 2; WorldState.GetInstance().socialCircles.Add(centerOfSocial, new List <SocialAgent>() { this }); otherAgent.EnterSocialCircle(centerOfSocial); } isInCircle = true; isSocial = true; //otherAgent.isInCircle = true; //otherAgent.isSocial = true; } }
// Use this for initialization void Start() { ChooseMaterial(); socialAgent = GetComponent <SocialAgent>(); travellingAgent = GetComponent <TravellingAgent>(); wanderingAgent = GetComponent <WanderingAgent>(); }
public void EnterSocialCircle(SocialAgent sAgent) { if (!isInCircle && isSocial) { Debug.Log("Entering social circle"); WorldState.GetInstance().socialCircles[sAgent.GetSocialCircle()].Add(this); centerOfSocial = sAgent.GetSocialCircle(); isInCircle = true; isSocial = true; } }
void FixedUpdate() { Debug.Log(state); //If wandering, update its state if (state == 0) { if (decide_next_target < 0) { NextTarget(); decide_next_target = 2f; } decide_next_target -= Time.deltaTime; Seek(); int index = NearSocialAgent(); if (index >= 0) { //Other social agents around if (new System.Random(System.Guid.NewGuid().GetHashCode()).Next(0, 100) < 5) { nearest_social_agent = social_agent_list[index]; state = 1; } } } else if (state == 1) { //Self is ready to form a group SocialAgent other = nearest_social_agent.GetComponent <SocialAgent>(); //If far away from the other agent, then back to wandering if (Vector3.Distance(transform.position, other.transform.position) > 2f) { state = 0; } else { if (other.state == 1) { //Other is ready to form a group, then create group socialGroup.SetCenter(transform.position, other.transform.position); target = (socialGroup.center + transform.position) / 2; socialGroup.agent_number++; state = 2; } else if (other.state == 2 || other.state == 3) { //Other is forming or already in group, then join other.socialGroup.agent_number++; socialGroup = other.socialGroup; target = (socialGroup.center + transform.position) / 2; state = 2; } this.other = other; } } else if (state == 2) { velocity = new Vector3(0, 0, 0); Seek(); transform.rotation = Quaternion.LookRotation(other.transform.position - transform.position); //Apply steer force to from a group if (Vector3.Distance(transform.position, target) < 1.2f) { //Complete forming process, transfer to state 3 state = 3; conversation_timer = new System.Random(System.Guid.NewGuid().GetHashCode()).Next(50, 200) / 100f; } } else if (state == 3) { //In conversation, stay still //Do nothing transform.rotation = Quaternion.LookRotation(other.transform.position - transform.position); velocity = new Vector3(0, 0, 0); //In a random period of time, leave group if (conversation_timer < 0) { state = 4; renderer.material = wander_material; head.ChangeToWanderMaterial(); cool_down_timer = 2f; socialGroup.agent_number--; } if (socialGroup.agent_number <= 1) { state = 4; renderer.material = wander_material; head.ChangeToWanderMaterial(); cool_down_timer = 2f; socialGroup.agent_number = 0; socialGroup.SetCenter(0, 0, 0); } conversation_timer -= Time.deltaTime; } else { //Wandering if (decide_next_target < 0) { NextTarget(); decide_next_target = 2f; } decide_next_target -= Time.deltaTime; Seek(); //In cooldown if (cool_down_timer < 0) { state = 0; renderer.material = social_material; head.ChangeToSocialMaterial(); } cool_down_timer -= Time.deltaTime; } }
private void FixedUpdate() { if (_socialCircle != null) { if (Time.time > nextActionTime) { _socialCircle.Leave(); _socialCircle = null; freeUntil = Time.time + Random.Range(3, 6); } } GameObject socialAgentAround = TalkingAvailableSocialAgentAround(); if (socialAgentAround != null && _socialCircle == null && Time.time > freeUntil) { SocialAgent socialAgent = socialAgentAround.GetComponent <SocialAgent>(); if (socialAgent._socialCircle == null) { _socialCircle = new SocialCircle(transform.position + Vector3.Normalize(transform.forward)); } else { _socialCircle = socialAgent._socialCircle; } nextActionTime = Time.time + Random.Range(3, 10); _socialCircle.Join(); } if (_socialCircle != null) { destination = _socialCircle.Center; } Vector3 desiredVelocity = destination - transform.position; var distance = desiredVelocity.magnitude; var slowingRange = 4; var facingCenter = false; if (_socialCircle != null && Math.Abs(distance - _socialCircle.Radius) < 0.2) { _rigidbody.velocity = new Vector3(); facingCenter = true; if (_socialCircle.MemberCount == 1) { _socialCircle.Leave(); _socialCircle = null; } } else if (distance < 0.5 || Physics.CheckSphere(destination, 3) && _socialCircle == null) { UpdateDestination(); } else if (distance < slowingRange) { if (_socialCircle != null && distance < _socialCircle.Radius) { _rigidbody.velocity = -transform.forward * _speed; } else { _rigidbody.velocity = transform.forward * _speed * distance / slowingRange; } } else { _rigidbody.velocity = transform.forward * _speed; } if (facingCenter || !ReactToObstacles()) { var rotation = Quaternion.LookRotation(destination - transform.position); _rigidbody.MoveRotation(Quaternion.RotateTowards(transform.rotation, rotation, _rotateSpeed)); } }