Skip to content

Instantly share code, notes, and snippets.

@Samvid95
Created March 23, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Samvid95/272dfd6360f32b4d20de1badd8c9a14c to your computer and use it in GitHub Desktop.
Save Samvid95/272dfd6360f32b4d20de1badd8c9a14c to your computer and use it in GitHub Desktop.
Conversation opened. 1 read message.
Skip to content
Using UC Santa Cruz Mail with screen readers
More
29 of 894
Petbehavior script
Inbox
x
Samvid Jhaveri <snjhaver@ucsc.edu>
AttachmentsMar 20 (3 days ago)
to Samvid
Attachments area
Click here to Reply or Forward
Using 19.9 GB
Manage
Program Policies
Powered by Google
Last account activity: 24 minutes ago
Details
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class PetBehavior : MonoBehaviour {
private Dictionary<string, int> needs = new Dictionary<string, int>();
private int maxCuriosity = 100;
private int maxHunger = 100;
private int maxWariness = 100;
private int maxAffection = 100;
string CurrentNeed;
string PrevNeed = null;
// Use this for initialization
void Start () {
needs.Add("wariness", 100);
needs.Add("hunger", 10);
needs.Add("affection", -1);
needs.Add("curiosity", 0);
Invoke("StateManager", 1.0f);
}
void StateManager()
{
CurrentNeed = FindCurrentNeed();
Debug.Log("CurrentNeed: " + CurrentNeed + " and Previous Need: " + PrevNeed);
if (PrevNeed != CurrentNeed)
{
Debug.Log("Change of state from " + PrevNeed + " to " + CurrentNeed);
PrevNeed = CurrentNeed;
StartCoroutine(CurrentNeed);
return;
}
else
{
//These are the conditional checks!
if (CurrentNeed == "wariness")
{
//Going for the condition1
StartCoroutine("Condition1");
return;
}
else if(CurrentNeed == "hunger" && needs["affection"] < 0)
{
StartCoroutine("Condition2");
return;
}
else if (CurrentNeed == "hunger" && needs["affection"] >= 0)
{
StartCoroutine("Condition5");
return;
}
else if (CurrentNeed == "curiosity")
{
StartCoroutine("Condition3");
return;
}
else if(CurrentNeed == "affection")
{
StartCoroutine("Condition4");
return;
}
}
Debug.Log("This is the end of the statemanager.");
}
//Wariness and where it leads to
IEnumerator wariness()
{
//Do Wariness stuff here!!
Debug.Log("State: Wariness");
needs["wariness"] = 0;
needs["hunger"] = 10;
needs["affection"] = -1;
needs["curiosity"] = 0;
//Write a sequence where the pet moves away from the player and also do this shake!
transform.DOShakeScale(9.0f, new Vector3(1f, 0.3f, 1f), 10, 45, true);
yield return new WaitForSeconds(9.0f);
Invoke("StateManager", 1.0f);
}
IEnumerator Condition1()
{
Debug.Log("State: Condition1");
//Add when wary behavior
float coinFlip = Random.Range(0.0f, 1.0f);
if(coinFlip < 0.5f)
{
needs["hunger"] += 10;
}
else
{
needs["curiosity"] += 10;
}
yield return new WaitForSeconds(2.0f);
Invoke("StateManager", 1.0f);
}
//Hunger and where it leads to
IEnumerator hunger()
{
Debug.Log("State: Hunger");
needs["wariness"] = 0;
needs["hunger"] = 10;
needs["curiosity"] = 10;
//Add when hungry behavior
yield return new WaitForSeconds(9.0f);
Invoke("StateManager", 1.0f);
}
IEnumerator Condition2()
{
Debug.Log("State: Condition2");
float coinFlip = Random.Range(0.0f, 1.0f);
if (coinFlip < 0.5f)
{
needs["curiosity"] += 15;
}
//Add Behavior when Hungry
yield return new WaitForSeconds(2.0f);
Invoke("StateManager", 1.0f);
}
IEnumerator Condition5()
{
Debug.Log("State: Condition5");
float coinFlip = Random.Range(0.0f, 1.0f);
if (coinFlip < 0.5f)
{
needs["affection"] += 5;
}
//Add Behavior when Hungry
yield return new WaitForSeconds(2.0f);
Invoke("StateManager", 1.0f);
}
//Affection and where it leads to
IEnumerator affection()
{
Debug.Log("State: Affection");
needs["wariness"] = 0;
needs["hunger"] = 0;
needs["curiosity"] = 0;
needs["affection"] = 0;
//Add behavior when affectionated
yield return new WaitForSeconds(9.0f);
Invoke("StateManager", 1.0f);
}
IEnumerator Condition4()
{
Debug.Log("State: Condition4");
//Add Behavior when Curious
float coinFlip = Random.Range(0.0f, 1.0f);
if (coinFlip < 0.5f)
{
needs["hunger"] += 10;
}
else
{
needs["wariness"] += 5;
}
yield return new WaitForSeconds(2.0f);
Invoke("StateManager", 1.0f);
}
//Curiosity and where it leads to
IEnumerator curiosity()
{
Debug.Log("State: Curiosity");
needs["wariness"] = 0;
needs["hunger"] = 0;
needs["curiosity"] = 0;
//Add when curios behavior
yield return new WaitForSeconds(9.0f);
Invoke("StateManager", 1.0f);
}
IEnumerator Condition3()
{
Debug.Log("State: Condition3");
//Add Behavior when Curious
float coinFlip = Random.Range(0.0f, 1.0f);
if (coinFlip < 0.5f)
{
needs["affection"] += 10;
}
else
{
needs["wariness"] += 5;
}
yield return new WaitForSeconds(2.0f);
Invoke("StateManager", 1.0f);
}
string FindCurrentNeed()
{
/*int maxValue=0;
string maxNeed= null;
foreach (KeyValuePair<string,int> entity in needs)
{
Debug.Log(entity.Key + " : " + entity.Value);
if (entity.Value > maxValue)
{
maxNeed = entity.Key;
maxValue = entity.Value;
}
}
return maxNeed;*/
int w=0, h=0, a=0, c=0;
Debug.Log("Wariness: " + needs["wariness"] + " | Hunger: " + needs["hunger"] + " | Affection: " + needs["affection"] + " | Curiosity: " + needs["curiosity"]);
foreach (KeyValuePair<string, int> entity in needs)
{
// Debug.Log(entity.Key + " : " + entity.Value);
if (entity.Key == "wariness")
{
w = entity.Value;
}
if(entity.Key == "hunger")
{
h = entity.Value;
}
if(entity.Key == "affection")
{
a = entity.Value;
}
if(entity.Key == "curiosity")
{
c = entity.Value;
}
}
if (w > 80 && h <= 80 && c <= 80 && a <= 80)
{
return "wariness";
}
else if (h > 80 && c <= 80 && w <= 80 && a <= 80)
{
return "hunger";
}
else if(c> 80 && h <= 80 && w <= 80 && a <= 80)
{
return "curiosity";
}
else if(a > 80 && h <= 80 && w <= 80 && c <= 80)
{
return "affection";
}
else
{
return CurrentNeed;
}
}
}
PetBehavior.cs
Displaying PetBehavior.cs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment