void Update() { if (start) { start = false; Pathfinding.Pathfinder path = new Pathfinding.Pathfinder(); int N1x = Random.Range(1, 11); int N1y = Random.Range(1, 3); int N2x = Random.Range(1, 11); int N2y = Random.Range(11, 13); Node startNode = grid [N1x, N1y]; Node end = grid [N2x, N2y]; path.start = startNode; path.end = end; List <Node> p = path.FindPath(); reset = p; startNode.vis.SetActive(false); reset.Add(startNode); foreach (Node n in p) { n.vis.SetActive(false); } } }
public void Flee(float mag, GameObject caster) { //I'm stealing large swathes of this code from the launching code I wrote earlier, hence some of the variables. Vector2 direct = this.transform.position - caster.transform.position; RaycastHit2D[] hit = Physics2D.RaycastAll(this.transform.position, direct, mag); Debug.DrawRay(this.transform.position, direct, Color.red, 100, false); bool hitWall = false; foreach (RaycastHit2D h in hit) { if (h.collider.gameObject.tag != "Tile") { hitWall = true; } else if (h.collider.gameObject.GetComponent <CoordinateHolder>().isWall == true) { hitWall = true; } } if (hitWall == false) { Node sendNode = null; float tempDist = 0f; foreach (RaycastHit2D h in hit) { if (h.collider.gameObject.tag == "Tile") { Node n = GridHandler.instance.GetNode(Mathf.RoundToInt(h.collider.gameObject.GetComponent <CoordinateHolder>().corX), Mathf.RoundToInt(h.collider.gameObject.GetComponent <CoordinateHolder>().corY)); if (Vector2.Distance(this.gameObject.transform.position, n.worldObject.transform.position) > tempDist) { tempDist = Vector2.Distance(this.gameObject.transform.position, n.worldObject.transform.position); sendNode = n; } } } if (sendNode != null) { Pathfinding.Pathfinder path = new Pathfinding.Pathfinder(); Node startNode = GridHandler.instance.GetNode(Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corX), Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corY)); Node end = sendNode; path.startPosition = startNode; path.endPosition = end; path.pather = this.gameObject; controller.pathway = path.FindPath(); controller.fleeingMove = true; if (controller.pathway != null) { controller.Movement(controller.pathway); } controller.actionCount++; fledCheck = false; } else if (mag > 0.01f) { Flee(mag - 0.01f, caster); } else { fledCheck = false; controller.cannotActRepair(); } } else if (mag > 0.01f) { Flee(mag - 0.01f, caster); } else { fledCheck = false; controller.cannotActRepair(); } }
void Update() { if (this.gameObject.GetComponent <CharacterData>().isUnconscious == true) { willHostile = false; hostile = false; canAct = false; } if (hostile == false && willHostile == true && FactionHandler.instance.hasSetUp == true && GridHandler.instance.hasGenerated == true) { if (this.gameObject.GetComponent <CharacterData>().canSee == true) { actors = GameObject.FindGameObjectsWithTag("Actor"); foreach (GameObject g in actors) { foreach (PhysicalProperties.Relationship r in this.gameObject.GetComponent <PhysicalProperties>().animate.mind.relationships) { if (r.relTarget == g && r.relationshipNumber < -100) { if (Vector3.Distance(g.transform.position, this.gameObject.transform.position) < this.gameObject.GetComponent <CharacterData>().hostileDist) { hostile = true; rnd.Startuptime(); } } else if (r.relTarget == g && r.relationshipNumber < -25 && this.gameObject.GetComponent <CharacterData>().factionArray.relationships[g.gameObject.GetComponent <CharacterData>().factionID].atWar == true) { if (Vector3.Distance(g.transform.position, this.gameObject.transform.position) < this.gameObject.GetComponent <CharacterData>().hostileDist) { hostile = true; rnd.Startuptime(); } } } } } } else if (willHostile == false && hostile == true) { hostile = false; } else if (hostile == true && willHostile == true && rnd.inCombat == false) { rnd.Startuptime(); } myPosition = this.transform.position; if (isLaunching == true) { if (hasAn == false) { hasAn = true; } Vector3 weightlessMod = new Vector3(0, 0, 0); if (isWeightless == false) { transform.position = Vector3.Lerp(myPosition, landNode.worldObject.transform.position + this.gameObject.GetComponent <CharacterData>().alignMod, 0.1f * launchSpeed); } else { weightlessMod = new Vector3(myPosition.x, this.gameObject.GetComponent <PhysicalProperties>().floatPosition.y, 0f); transform.position = Vector3.Lerp(myPosition, landNode.worldObject.transform.position + this.gameObject.GetComponent <CharacterData>().alignMod + weightlessMod, 0.1f * launchSpeed); } if (Vector2.Distance(landNode.worldObject.transform.position + this.gameObject.GetComponent <CharacterData>().alignMod + weightlessMod, this.transform.position) < 0.05f) { this.transform.parent = landNode.worldObject.transform; if (this.gameObject.GetComponent <PhysicalProperties>().xMomentum == 0 && this.gameObject.GetComponent <PhysicalProperties>().yMomentum == 0) { canAct = true; stunCounter++; isLaunching = false; landNode = null; launchSpeed = 0f; hasAn = false; } } } // if (isWeightless == true) { // transform.position = Vector3.Lerp(this.transform.position, this.gameObject.GetComponent<PhysicalProperties>().floatPosition, 0.05f); // } if (Input.GetButtonDown("Pick Up") && grabbingItems == false && isMoving == false && bookOpen == false && this.gameObject.GetComponent <CharacterData>().isDead == false && isCasting == false) { grabbingItems = true; items = new List <GameObject>(); foreach (GameObject g in GameObject.FindGameObjectsWithTag("Item")) { if (g.GetComponent <ItemData>().isInInven == false) { items.Add(g); } } int t = 0; if (items != null) { pickableItems = new List <GameObject>(); foreach (GameObject i in items) { if (Vector2.Distance(i.transform.position, this.transform.position) <= 0.25f && i.GetComponent <ItemData>().isInInven == false) { pickableItems.Add(i); t++; } } } } else if (Input.GetButtonDown("Pick Up") && grabbingItems == true) { grabbingItems = false; items = null; pickableItems = null; } if (isMoving == true) { grabbingItems = false; items = null; pickableItems = null; } if (this.gameObject.GetComponent <CharacterData>().isDead == true) { isTurn = false; rnd.NextRound(); } if (hasTakenTurn == true && isTurn == true) { if (this.gameObject.GetComponent <CharacterData>().bleedCounter > 0) { this.gameObject.GetComponent <PhysicalProperties>().TakeBioDamage(5f, (PhysicalProperties.DamageType) 4); this.gameObject.GetComponent <CharacterData>().bleedCounter--; } isTurn = false; Invoke("AllowTurn", 1f); } tiles = GameObject.FindGameObjectsWithTag("Tile"); if (this.transform.parent != null && isMoving == false && isLaunching == false && isWeightless == false) { this.transform.position = this.transform.parent.position + this.gameObject.GetComponent <CharacterData>().alignMod; } if (isMoving == true) { if (myPosition.x > destination.x) { isFlipped = true; this.GetComponent <SpriteRenderer>().flipX = true; } if (myPosition.x < destination.x) { isFlipped = false; this.GetComponent <SpriteRenderer>().flipX = false; } if (Manager.instance.walkType == 1) { transform.position = Vector3.MoveTowards(transform.position, destination, this.gameObject.GetComponent <CharacterData>().speed *Time.deltaTime); } else if (Manager.instance.walkType == 2) { transform.position = Vector3.Lerp(myPosition, destination, this.GetComponent <CharacterData>().speed * 10 * Time.deltaTime); } if (Vector2.Distance(myPosition, destination) < 0.01f) { if (isRoaming == true) { FreeMovement(pathway); } if (isRoaming == false && isRepairing == false) { Movement(pathway); } } } if (stunCounter > 0 && isTurn == true && hasTakenTurn == false) { stunCounter--; actionCount = 0; hasTakenTurn = true; AnnouncerManager.instance.ReceiveText(this.gameObject.GetComponent <CharacterData>().charName + " is stunned.", false); } if (isTurn == true && hasTakenTurn == false && canAct == false) { actionCount = 0; hasTakenTurn = true; } if (isTurn == true && hasTakenTurn == false && isLaunching == false && canAct == true) { if (this.GetComponent <CharacterData>().isPlayer == false) { if (actionCount < maxActionCount && isMoving == false && isAttacking == false) { actors = null; actors = GameObject.FindGameObjectsWithTag("Actor"); enemTarg = NearestEnemyCalc().worldObject.transform.GetChild(0).gameObject; if (hostile == true) { Pathfinding.Pathfinder path = new Pathfinding.Pathfinder(); Node startNode = gridBase.GetNode(Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corX), Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corY)); Node end = targNode; path.startPosition = startNode; path.endPosition = end; path.pather = this.gameObject; pathway = path.FindPath(); if (pathway != null && pathway.Count > 2) { farPlay = true; } else if (pathway != null && pathway.Count == 2) { semiPlay = true; } else if (pathway != null && pathway.Count == 1) { nearPlay = true; } else if (pathway.Count == 0) { herePlay = true; } else if (farPlay != true && semiPlay != true && nearPlay != true && herePlay != true) { Invoke("cannotActRepair", 1.5f); } } else if (actionCount >= maxActionCount) { Invoke("cannotActRepair", 1.5f); } } } if (this.GetComponent <CharacterData>().isPlayer == true && CharacterData.instance.isDead == false) { if (actionCount < maxActionCount && isMoving == false && EquipmentRenderer.instance.testInven == false && bookOpen == false && isCasting == false && isAttacking == false) { if (moveTake == true && this.gameObject.GetComponent <CharacterData>().moveDistance > 0 && canMove == true) { moveSelect = false; moveTake = false; FindSelectedTile(); Pathfinding.Pathfinder path = new Pathfinding.Pathfinder(); Node startNode = gridBase.GetNode(Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corX), Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corY)); Node end = targNode; path.startPosition = startNode; path.endPosition = end; path.pather = this.gameObject; pathway = path.FindPath(); bool canPath = true; if (pathway == null) { canPath = false; } else if (pathway != null) { if (pathway[0] == startNode) { canPath = false; } } if (pathway.Count > 0 && canPath == true) { actionCount++; actionBar.GetComponent <ActionBar>().CutAction(); Movement(pathway); } else if (canPath != true) { AnnouncerManager.instance.ReceiveText("You cannnot move there.", true); } } if (isAttacking == false && Input.GetButtonDown("ShiftA") && EquipmentRenderer.instance.weaponEquip.GetComponent <Animator>().enabled == true && this.gameObject.GetComponent <CharacterData>().canAttack == true) { isAttacking = true; Node launchNode = gridBase.GetNode(Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corX), Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corY)); attackableNodes = new List <Node>(); attackableEnemies = new List <GameObject>(); for (int x = -1 * Mathf.RoundToInt(CharacterData.instance.range); x <= Mathf.RoundToInt(CharacterData.instance.range); x++) { for (int y = -1 * Mathf.RoundToInt(CharacterData.instance.range); y <= Mathf.RoundToInt(CharacterData.instance.range); y++) { if (x == 0 && y == 0) { //Nothing } else { Node searchPos = gridBase.GetNode(launchNode.x + x, launchNode.y + y); if (searchPos != null && searchPos.worldObject.transform.childCount > 0) { attackableNodes.Add(searchPos); if (searchPos.worldObject.transform.GetChild(0).transform.gameObject.tag == "Actor") { attackableEnemies.Add(searchPos.worldObject.transform.GetChild(0).transform.gameObject); } } } } } foreach (Node n in attackableNodes) { n.worldObject.GetComponent <GridHighlight>().enabled = false; n.worldObject.GetComponent <SpriteRenderer>().enabled = true; n.worldObject.GetComponent <SpriteRenderer>().color = Color.red; } foreach (GameObject g in attackableEnemies) { g.GetComponent <NPCHandler>().recepientAttack = true; } } else if (isAttacking == true && Input.GetButtonDown("ShiftA")) { isAttacking = false; foreach (Node n in attackableNodes) { n.worldObject.GetComponent <GridHighlight>().enabled = true; n.worldObject.GetComponent <SpriteRenderer>().enabled = false; n.worldObject.GetComponent <SpriteRenderer>().color = Color.white; } foreach (GameObject g in attackableEnemies) { g.GetComponent <Renderer>().material = g.GetComponent <CharacterData>().stand; } attackableEnemies = null; attackableNodes = null; } } } else if (CharacterData.instance.isDead == true) { //Do nothing for now. } } if (this.GetComponent <CharacterData>().isPlayer == true && freeRoam == true && isMoving == false && isAttacking == false && ItemManager.instance.isRummaging == false && bookOpen == false && isCasting == false) { canMove = true; if (moveTake == true) { moveSelect = false; moveTake = false; FindSelectedTile(); Pathfinding.Pathfinder path = new Pathfinding.Pathfinder(); Node startNode = gridBase.GetNode(Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corX), Mathf.RoundToInt(this.GetComponentInParent <CoordinateHolder>().corY)); Node end = targNode; path.startPosition = startNode; path.endPosition = end; path.pather = this.gameObject; pathway = path.FindPath(); if (pathway.Count > 0) { isRoaming = true; FreeMovement(pathway); } } } if (align) { align = false; } if (Input.GetButtonDown("Pass Round") && this.GetComponent <CharacterData>().isPlayer == true && isMoving == false && isAttacking == false && ItemManager.instance.isRummaging == false && bookOpen == false && isCasting == false) { actionCount++; actionBar.GetComponent <ActionBar>().CutAction(); if (RoundController.instance.inCombat == false && actionCount >= maxActionCount) { RoundController.instance.ArtificialRound(); } if (RoundController.instance.inCombat == true && actionCount >= maxActionCount) { actionCount = 0; hasTakenTurn = true; } } }