Skip to content

Instantly share code, notes, and snippets.

@daichi-takezawa
Created April 14, 2020 11:55
Show Gist options
  • Save daichi-takezawa/a141a5299bff07e598966da54f726415 to your computer and use it in GitHub Desktop.
Save daichi-takezawa/a141a5299bff07e598966da54f726415 to your computer and use it in GitHub Desktop.
public class ReaderAI : MonoBehaviour {
public GameObject namePrefab, myNameOb, DropItem, myCrown, kill;
private float radian, time, randomTime, randomRoteSpeed, goalAngle;
public float raydistance, rotespeed, raydistanceSide, rayDiagonal, changeUp, changeRight, rayrote, calltimeForFreetime;
public float speed;
private float dropShortDis, dropDis;
private Rigidbody2D myrb;
private bool rightgo, downCount, rightCount, deadoneplay;
private GameObject giveBirthChara, playerOb, migawari;
private string myname, AiDirection;
private Sprite mysp;
[SerializeField] private int waruKazu, kimattakazu, randomGiveBirth;
public int scoreCount, bornCount, randomMax;
public List<GameObject> myChaceChara = new List<GameObject> ();
public List<GameObject> Drops = new List<GameObject> ();
private List<ChaceChara> ChaceSc = new List<ChaceChara> ();
/*public Dictionary<GameObject,float> dropDictionary = new Dictionary<GameObject, float> ();*/
private GameSceneManager gamema;
private ChaceChara giveBirthChaceChara2;
private TextMesh myChaceCharaCount, myNameDisplay;
private Ray2D ray1, ray2, ray3, rayLeft, rayRight, rayDropSearch;
private RaycastHit2D hit1, hit2, hit3, hitLeft, hitRight, hitDropSearch;
private LayerMask layerMask, DropLayerMask;
private Vector3 axis, diff;
private ChaceChara chace;
public SpriteRenderer myChildSpren;
void Awake () {
Declaration ();
}
void OnEnable () {
onenableSengen ();
}
// Update is called once per frame
void Update () {
raycast ();
AIBrain ();
showScore ();
}
void FixedUpdate () {
RotateByAiDirection ();
move ();
}
void onenableSengen () {
//リセットと宣言
deadoneplay = true;
DropItem = null;
ChaceSc.Clear ();
myChaceChara.Clear ();
speed = 1.13f;
scoreCount = 1;
waruKazu = 1;
kimattakazu = 10;
AiDirection = "FreeTime";
downCount = true;
raydistance = Random.Range (1, 2); //2
rayDiagonal = Random.Range (0.5f, 1.5f); //1.5
raydistanceSide = Random.Range (0, 1); //1
changeUp = 1f;
changeRight = 1f;
rotespeed = 10f;
rayrote = 5f;
myname = gameObject.name;
if (myNameOb == null) {
InstatiateName ();
} else {
myNameDisplay.text = myname;
myNameOb.SetActive (true);
}
if (gamema.gamemode == "vs") {
for (int i = 0; i < 2; i++) {
GiveBirth ();
}
}
if (gamema.gamemode == "score") {
for (int i = 0; i < bornCount; i++) {
GiveBirth ();
}
}
}
void Declaration () {
myrb = GetComponent<Rigidbody2D> ();
gamema = GameObject.FindGameObjectWithTag ("gamescene").GetComponent<GameSceneManager> ();
mysp = GetComponent<SpriteRenderer> ().sprite;
rightgo = true;
layerMask = LayerMask.GetMask (new string[] { "Chacechara", "wall" });
DropLayerMask = LayerMask.GetMask (new string[] { "DropItem" });
playerOb = GameObject.FindGameObjectWithTag ("Player");
transform.GetChild (0).name = myname;
myChildSpren.sprite = mysp;
}
//rayをだして敵がいるかどうか検知するんだけど、自身にrayが触れないようにして検知しないようにする。
void raycast () {
rayDropSearch = new Ray2D (transform.position, transform.up * changeUp + transform.right * changeRight);
rayLeft = new Ray2D (transform.position + transform.right * -0.1f, transform.up + transform.right * -3f); //0.1f,-3f
ray1 = new Ray2D (transform.position + transform.up * 0.1f + transform.right * 0.1f, transform.up + transform.right * 0.5f); //0.1,0.1,0.5
ray2 = new Ray2D (transform.position + transform.up * 0.2f, transform.up); //0.2
ray3 = new Ray2D (transform.position + transform.up * 0.1f + transform.right * -0.1f, transform.up + transform.right * -0.5f); //0.1,-0.1,-0.5
rayRight = new Ray2D (transform.position + transform.right * 0.1f, transform.up + transform.right * 3f); //0.1f,3f
hitDropSearch = Physics2D.Raycast (rayDropSearch.origin, rayDropSearch.direction, 1.5f, DropLayerMask);
hitLeft = Physics2D.Raycast (rayLeft.origin, rayLeft.direction, raydistanceSide, layerMask);
hit1 = Physics2D.Raycast (ray1.origin, ray1.direction, rayDiagonal, layerMask);
hit2 = Physics2D.Raycast (ray2.origin, ray2.direction, raydistance, layerMask);
hit3 = Physics2D.Raycast (ray3.origin, ray3.direction, rayDiagonal, layerMask);
hitRight = Physics2D.Raycast (rayRight.origin, rayRight.direction, raydistanceSide, layerMask);
/*Debug.DrawRay (rayDropSearch.origin, rayDropSearch.direction * 2.5f, Color.blue);*/
if (downCount == true) {
changeUp -= Time.deltaTime * rayrote;
}
if (downCount == false) {
changeUp += Time.deltaTime * rayrote;
}
if (changeUp < -1) {
downCount = false;
}
if (changeUp > 1) {
downCount = true;
}
if (rightCount == true) {
changeRight -= Time.deltaTime * rayrote;
}
if (rightCount == false) {
changeRight += Time.deltaTime * rayrote;
}
if (changeRight < -1) {
rightCount = false;
}
if (changeRight > 1) {
rightCount = true;
}
if (hitDropSearch.collider != null) {
Drops.Add (hitDropSearch.collider.gameObject);
/*Debug.DrawRay (rayDropSearch.origin, rayDropSearch.direction * 1.5f, Color.blue);*/
}
/*if (hitLeft.collider == null) {
Debug.DrawRay (rayLeft.origin, rayLeft.direction * raydistanceSide, Color.green);
}*/
if (hitLeft.collider != null) {
if (hitLeft.collider.gameObject.name != myname) {
if (hitLeft.collider.tag == "wall" || hitLeft.collider.tag == "ChaceChara") {
rightgo = false;
AiDirection = "Turn";
/*Debug.DrawRay (rayLeft.origin, rayLeft.direction * raydistanceSide, Color.red);*/
}
}
}
/*if (hit1.collider == null) {
Debug.DrawRay (ray1.origin, ray1.direction * rayDiagonal, Color.green);
}*/
if (hit1.collider != null) {
if (hit1.collider.gameObject.name != myname) {
if (hit1.collider.tag == "wall" || hit1.collider.tag == "ChaceChara") {
rightgo = true;
AiDirection = "Turn";
/*Debug.DrawRay (ray1.origin, ray1.direction * rayDiagonal, Color.red);*/
}
}
}
/*if (hit2.collider == null) {
Debug.DrawRay (ray2.origin, ray2.direction * raydistance, Color.green);
}*/
if (hit2.collider != null) {
if (hit2.collider.gameObject.name != myname) {
if (hit2.collider.tag == "wall" || hit2.collider.tag == "ChaceChara") {
/*int random = Random.Range (0, 2);
if (random == 0) {
rightgo = true;
} else {
rightgo = false;
}*/
AiDirection = "Turn";
/*Debug.DrawRay (ray2.origin, ray2.direction * raydistance, Color.red);*/
}
}
}
/*
if (hit3.collider == null) {
Debug.DrawRay (ray3.origin, ray3.direction * rayDiagonal, Color.green);
}*/
if (hit3.collider != null) {
if (hit3.collider.gameObject.name != myname) {
if (hit3.collider.tag == "wall" || hit3.collider.tag == "ChaceChara") {
rightgo = false;
AiDirection = "Turn";
/*Debug.DrawRay (ray3.origin, ray3.direction * rayDiagonal, Color.red);*/
}
}
}
/*if (hitRight.collider == null) {
Debug.DrawRay (rayRight.origin, rayRight.direction * raydistanceSide, Color.green);
}*/
if (hitRight.collider != null) {
if (hitRight.collider.gameObject.name != myname) {
if (hitRight.collider.tag == "wall" || hitRight.collider.tag == "ChaceChara") {
rightgo = true;
AiDirection = "Turn";
/*Debug.DrawRay (rayRight.origin, rayRight.direction * raydistanceSide, Color.red);*/
}
}
}
}
void AIBrain () {
//0.2秒ごとにFreeTimeにして曲がり続けるのをふせぐ
time -= Time.deltaTime;
if (time < 0) {
time = Random.Range (0, 0.5f);
rotespeed = Random.Range (7.5f, 12.5f);
if (AiDirection == "Turn") {
AiDirection = "FreeTime";
}
}
}
void RotateByAiDirection () {
if (AiDirection == "Turn") {
if (rightgo == true) {
myrb.transform.Rotate (0, 0f, rotespeed);
}
if (rightgo == false) {
myrb.transform.Rotate (0, 0f, rotespeed * -1);
}
}
//もしrightとleftの時に0.1f毎にFreeTimeとなる。
if (AiDirection == "FreeTime") {
FreeTime ();
}
}
void move () {
/*transform.position = Vector2.Lerp (transform.position, transform.position + transform.up, Time.deltaTime);*/
myrb.velocity = transform.up * speed;
}
//Dropを持っていない時にはランダム移動、持っているときには追いかける。
void FreeTime () {
/* calltimeForFreetime -= Time.deltaTime;
if (calltimeForFreetime < 0) {
calltimeForFreetime = 0.2f; */
if (Drops.Count == 0) {
//回転スピード(ゆるやかな回転)と何秒間(しばらくの間)その状態でいるのか決める。
randomTime -= Time.deltaTime;
if (randomTime < 0) {
randomTime = Random.Range (0, 3f);
randomRoteSpeed = Random.Range (-2, 2);
}
myrb.transform.Rotate (0, 0, randomRoteSpeed);
}
//DropItemを確認しておっけーなら追いかける。
if (Drops.Count > 0) {
dropShortDis = 10;
//Dropsから整理して一番近いDropをDropItemとして出す。
//まずDropsすべての距離を割り出して距離が小さいDropをDropItemにいれる。
for (int i = 0; i < Drops.Count; i++) {
dropDis = Vector2.Distance (Drops[i].transform.position, transform.position);
if (Drops[i].activeSelf == true && Drops[i].tag == "DropChara" && dropDis < 6 && dropDis > 0.4f) {
//もっとも小さい距離のDropをListから探す。
if (dropShortDis > dropDis) {
dropShortDis = dropDis;
DropItem = Drops[i];
}
} else {
Drops.RemoveAt (i);
}
}
if (DropItem != null) {
//DropItemに向かう処理
/* float dropDistance = Vector2.Distance (DropItem.transform.position, myrb.transform.position); */
//DropItemという目的地に向かって角度を調整する。
diff = DropItem.transform.position - transform.position;
axis = Vector3.Cross (transform.up, diff);
goalAngle = Vector3.Angle (transform.up, diff) * (axis.z < 0 ? -1 : 1);
//0度まで曲がり切るのを待つのと直進させない。
if (goalAngle < -30) {
myrb.transform.Rotate (0, 0f, -10f);
}
if (goalAngle > 30) {
myrb.transform.Rotate (0, 0f, 10f);
}
}
}
}
//ChaceCharaを生んで、ChaceCharaの設定をする。
void GiveBirth () {
if (scoreCount > kimattakazu) {
waruKazu++;
kimattakazu += 10;
}
if (scoreCount % waruKazu == 0) {
giveBirthChara = gamema.GetChaceChara ();
giveBirthChaceChara2 = giveBirthChara.GetComponent<ChaceChara> ();
giveBirthChaceChara2.speed = this.speed;
if (myChaceChara.Count == 0) {
giveBirthChara.transform.position = this.gameObject.transform.position;
giveBirthChaceChara2.target = this.gameObject;
} else {
giveBirthChara.transform.position = myChaceChara[myChaceChara.Count - 1].transform.position;
giveBirthChaceChara2.target = myChaceChara[myChaceChara.Count - 1].gameObject;
}
giveBirthChara.name = myname;
giveBirthChara.GetComponent<SpriteRenderer> ().sprite = mysp;
giveBirthChara.SetActive (true);
ChaceSc.Add (giveBirthChaceChara2);
myChaceChara.Add (giveBirthChara);
}
scoreCount++;
}
void InstatiateName () {
myNameOb = Instantiate (namePrefab, new Vector2 (-100, -100), transform.rotation);
myNameOb.GetComponent<MyName> ().target = this.gameObject;
myChaceCharaCount = myNameOb.transform.GetChild (0).GetComponent<TextMesh> ();
myNameDisplay = myNameOb.transform.GetChild (1).GetComponent<TextMesh> ();
myNameDisplay.text = myname;
myCrown = myNameOb.transform.GetChild (2).gameObject;
myCrown.SetActive (false);
myNameOb.SetActive (true);
}
void showScore () {
if (myChaceCharaCount != null) {
/*int score = myChaceChara.Count + 1;*/
myChaceCharaCount.text = scoreCount.ToString ();
}
}
void OnTriggerEnter2D (Collider2D other) {
if (other.tag == "DropChara") {
chace = other.GetComponent<ChaceChara> ();
if (chace.eaten == false) {
chace.eatenbytarget = this.gameObject;
chace.eaten = true;
randomGiveBirth = Random.Range (1, 101);
if (randomGiveBirth < randomMax) {
randomGiveBirth = 1;
} else {
randomGiveBirth = 2;
}
for (int i = 0; i < randomGiveBirth; i++) {
GiveBirth ();
}
}
}
if (other.gameObject.name != myname) {
if (other.tag == "wall" || other.tag == "ChaceChara") {
if (deadoneplay == true) {
deadoneplay = false;
if (other.name == playerOb.name) {
//killと表示
gamema.killcount++;
/* gamema.killcounttext.text = gamema.killcount.ToString () + "kill"; */
Instantiate (kill, transform.position + transform.up * -0.01f, Quaternion.Euler (0, 0, 0));
}
//リーダーの代わりをおく
migawari = gamema.GetChaceChara ();
migawari.transform.position = transform.position;
migawari.GetComponent<SpriteRenderer> ().sprite = mysp;
migawari.SetActive (true);
//ChaceCharaをDropItemにする。
for (int i = 0; i < myChaceChara.Count; i++) {
myChaceChara[i].GetComponent<ChaceChara> ().target = null;
}
gamema._liveChara.Remove (this.gameObject);
gamema.deadChara.Add (this.gameObject);
myNameOb.SetActive (false);
gameObject.SetActive (false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment