//walk from point to point //done void Patrol() { if (Health <= 75) { changeStatus(alertStatus.flee); return; } //Debug.Log("patrol"); Mtr.setTarget(patrolRoute[i].position); float leeway = 1.2f; //jim - bbigger leeway = stuck less if (Mtr.stuckCheck()) { leeway = float.MaxValue; Debug.Log(name + " ::patrol stuck"); } if (Mtr.Trnsfrm.position.x > patrolRoute[i].position.x - leeway && Mtr.Trnsfrm.position.x < patrolRoute[i].position.x + leeway) { if (Mtr.Trnsfrm.position.y > patrolRoute[i].position.y - leeway && Mtr.Trnsfrm.position.y < patrolRoute[i].position.y + leeway) { if (randomPatrol) { i = Random.Range(0, patrolRoute.Count); } else { if (i + 1 == patrolRoute.Count) { //Debug.Log("change: full rotation"); i = 0; } else { //Debug.Log("change: next point"); i++; } } Mtr.setTarget(patrolRoute[i].position); } } //Debug.Log(i); }
// Update is called once per frame void Update() { var position = Camera.main.WorldToScreenPoint(transform.position); mouseDirection = Input.mousePosition - position; Motor.Speed = DefSpeed; if (CurAttack != null) { if (Motor.Target == CurAttack.LastTarget && Motor.Target != null) { var ai = Motor.Target.GetComponent <AIcontrol>(); if (ai != null) { foreach (var v in Physics2D.OverlapCircleAll(Motor.Trnsfrm.position, AIcontrol.MaxVisRange, VilagerLayerMask)) { var aic = v.GetComponent <AIcontrol>(); if (aic == ai || aic == null) { continue; } aic.check(Motor); } } } if (Time.fixedTime - CurAttack.LastAttack > CurAttack.Duration) { //Debug.Log("dmg " + CurAttack.Damage); if (Motor.Target == CurAttack.LastTarget && Motor.Target != null) { var ai = Motor.Target.GetComponent <AIcontrol>(); if (ai != null) { GetComponent <PlayerData>().IncreaseMana(250); Destroy(ai.gameObject); Debug.Log("dead"); VillageStatus.vStatus.IncreaseDeathCount(); } else { Motor.Target.applyDamage(CurAttack.Damage, Motor); } } CurAttack = null; } else { Motor.Speed = 0; } } else { if (Input.GetButton("Fire1")) { var mr = Camera.main.ScreenPointToRay(Input.mousePosition); float intr; if (new Plane(Vector3.back, 0).Raycast(mr, out intr)) { var p = mr.GetPoint(intr); CharMotor trgt = null; var col = Physics2D.OverlapCircle(p, 0.25f, AttackMask); if (col && (trgt = col.GetComponent <CharMotor>()) != null) { // p = Motor.transform.position; Motor.Target = trgt; var go = (GameObject)Instantiate(waypoint, p, Quaternion.identity); go.transform.localScale *= 1.5f; } else { Instantiate(waypoint, p, Quaternion.identity); Motor.setTarget(p); } } } if (Motor.Target != null) { var vec = ((Vector2)Motor.Target.Trnsfrm.position - (Vector2)Motor.Trnsfrm.position); var mag = vec.magnitude; vec /= mag; //Debug.Log("mag " + mag + " dt " + Vector2.Dot(vec, -RotObj.transform.up)); if (Vector2.Dot(vec, -Motor.RotObj.transform.up) > 0.8f) { bool inRange = true; foreach (var a in Attacks) { if (mag > a.Range) { inRange = false; continue; } if (Time.fixedTime - a.LastAttack > a.Duration + a.RoF) { CurAttack = a; var anim = Motor.RotObj.GetComponent <Animation>(); if (anim != null) { anim.Play(); } a.LastAttack = Time.fixedTime; a.LastTarget = Motor.Target; var ai = Motor.Target.GetComponent <AIcontrol>(); if (ai != null) { ai.Mtr.enabled = false; ai.enabled = false; ai.Mtr.Bdy.velocity = Vector2.zero; a.LastAttack += 1.5f; } break; } } if (inRange) { Motor.Speed = 0; } } } } }