void EnemySetup() { Vector2 k1p, k2p, k3p; k1p = new Vector3(16, 10); k2p = new Vector3(14, 10); k3p = new Vector3(16, 13); NonPlayerCharacters k1 = knight.GetComponent <NonPlayerCharacters>(); k1.x = (int)k1p.x; k1.y = (int)k1p.y; k1.setCharacter(0); enemies.Add(Instantiate(k1, k1p, Quaternion.identity)); NonPlayerCharacters k2 = knight.GetComponent <NonPlayerCharacters>(); k2.x = (int)k2p.x; k2.y = (int)k2p.y; k2.setCharacter(0); enemies.Add(Instantiate(k2, k2p, Quaternion.identity)); NonPlayerCharacters k3 = knight.GetComponent <NonPlayerCharacters>(); k3.x = (int)k3p.x; k3.y = (int)k3p.y; k3.setCharacter(0); enemies.Add(Instantiate(k3, k3p, Quaternion.identity)); }
private void MoveEnemyNext2Player(NonPlayerCharacters enemy, PlayerCharacters player) { int x, y; x = player.x + 1; y = player.y; int distance = System.Math.Abs(x - enemy.x) + System.Math.Abs(y - enemy.y); if (distance <= enemy.moveRange) { enemy.x = x; enemy.y = y; enemy.gameObject.transform.position = new Vector3(x, y, 0); return; } x = player.x - 1; y = player.y; distance = System.Math.Abs(x - enemy.x) + System.Math.Abs(y - enemy.y); if (distance <= enemy.moveRange) { enemy.x = x; enemy.y = y; enemy.gameObject.transform.position = new Vector3(x, y, 0); return; } x = player.x; y = player.y + 1; distance = System.Math.Abs(x - enemy.x) + System.Math.Abs(y - enemy.y); if (distance <= enemy.moveRange) { enemy.x = x; enemy.y = y; enemy.gameObject.transform.position = new Vector3(x, y, 0); return; } x = player.x; y = player.y - 1; distance = System.Math.Abs(x - enemy.x) + System.Math.Abs(y - enemy.y); if (distance <= enemy.moveRange) { enemy.x = x; enemy.y = y; enemy.gameObject.transform.position = new Vector3(x, y, 0); return; } }
public void Attack() { GameObject playerObj = GameObject.FindWithTag("SelectedPlayer"); PlayerCharacters player = playerObj.GetComponent <PlayerCharacters>(); GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy"); foreach (GameObject enemyObject in enemies) { NonPlayerCharacters enemy = enemyObject.GetComponent <NonPlayerCharacters>(); int distanceToEnemy = System.Math.Abs(player.x - enemy.x) + System.Math.Abs(player.y - enemy.y); if (distanceToEnemy <= player.attackRange) { AttackFields f = attackField.GetComponent <AttackFields>(); f.player = player; f.enemy = enemy; Instantiate(f, new Vector3(enemy.x, enemy.y, 0f), Quaternion.identity); } } }
public void EnemyTurn() { GameObject text = GameObject.Find("Info"); Text t2 = (Text)text.GetComponent(typeof(Text)); GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy"); foreach (GameObject enemyObject in enemies) { NonPlayerCharacters enemy = enemyObject.GetComponent <NonPlayerCharacters>(); GameObject[] characterObjects = GameObject.FindGameObjectsWithTag("Player"); PlayerCharacters closestCharacter = null; int distance2ClosestChar = 1000; foreach (GameObject characterObject in characterObjects) { PlayerCharacters character = characterObject.GetComponent <PlayerCharacters>(); int distance = System.Math.Abs(character.x - enemy.x) + System.Math.Abs(character.y - enemy.y); if (!alerted && distance < 6) { t2.text += "\nThe knight screams:\"Demons! Cultists! Tax-Evaders! Ready your arms men!\""; t2.text += "\nYou have alerted the order of the fifth dawn."; alerted = true; } if (distance < distance2ClosestChar) { distance2ClosestChar = distance; closestCharacter = character; } } if (alerted) { if (distance2ClosestChar <= enemy.moveRange + 1) { MoveEnemyNext2Player(enemy, closestCharacter); float roll = Random.Range(0.0f, 100.0f); if (roll >= 50) { int damage = (enemy.damage - closestCharacter.defence); closestCharacter.health -= damage; if (closestCharacter.health > 0) { t2.text += "\n<color=#008000ff>" + enemy.name + "</color> attacked <color=#F62D2DFF>" + closestCharacter.name + "</color> and did <b>" + damage + "</b>"; } else { t2.text += "\nIn an act guided by the fifth dawn itself<color=#008000ff>" + enemy.name + "</color> kills <color=#F62D2DFF>" + closestCharacter.name + "</color>. It is a good day to be good!"; Destroy(closestCharacter.gameObject); } } else { t2.text += "\nThe knight stumbled and fell over his sword! You laugh and spit on him!"; } } else { int movement = enemy.moveRange; Vector3 goHere = new Vector3(enemy.x, enemy.y, 0); while (movement > 0) { if (closestCharacter.x != goHere.x) { goHere.x += Math.Sign(closestCharacter.x - goHere.x); } else { goHere.y += Math.Sign(closestCharacter.y - goHere.y); } movement -= 1; } enemy.x = (int)goHere.x; enemy.y = (int)goHere.y; enemy.gameObject.transform.position = goHere; } } } }