Пример #1
0
	public IEnumerator commenceBattle(Attackable opponent) {
		// If we are already in a battle, we should abort the current commenceBattle request
		Debug.Log(isInBattle);
		if (isInBattle) yield break;
		Debug.Log("Commencing attack!");
		eventManager.addEvent(opponent.gameObject.transform.position);
		
		// If we are attacking an anthill, the battle will behave somewhat differently
		Anthill anthill = opponent.GetComponent<Anthill>();
		hasSetNewPath = false;
		
		// Set both units to be in 'attack mode' and generate a 'combat cloud'
		GameObject cloud = null;
		opponent.startBattle();
		if (!anthill) {
			isInBattle = true;
			Vector3 pos = mapManager.getTileAtPosition(transform.position).transform.position;
			if (Network.isClient || Network.isServer) {
				cloud = Network.Instantiate(combatCloud,
											new Vector3(pos.x, pos.y, transform.position.z - 1),
											Quaternion.identity, 0) as GameObject;
				networkView.RPC("fixCombatCloud", RPCMode.All, cloud.networkView.viewID);
			}
			else {
				cloud = GameObject.Instantiate(combatCloud, 
										   new Vector3(pos.x, pos.y, transform.position.z - 1), 
										   Quaternion.identity) as GameObject;
				}
			if (getPlayerId() == 1) {
				cloud.transform.Find("RedHead").gameObject.GetComponent<SpriteRenderer>().sprite = this.getFightSprite();
				cloud.transform.Find("BlueHead").gameObject.GetComponent<SpriteRenderer>().sprite = opponent.getFightSprite();
			} else {
				cloud.transform.Find("RedHead").gameObject.GetComponent<SpriteRenderer>().sprite = opponent.getFightSprite();
				cloud.transform.Find("BlueHead").gameObject.GetComponent<SpriteRenderer>().sprite = this.getFightSprite();
			}
			cloud.transform.parent = GameObject.Find("Objects").transform;
			gameObject.renderer.enabled = false;
			opponent.gameObject.renderer.enabled = false;
		} else {
			// If we are attacking an anthill, make sure to 'face' it
			if (transform.position.x < opponent.transform.position.x) transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
		}
		
		// Every second, battle calculations should take place only if we haven't started moving again
		while (true) {
			// Do one round of battle calculations
			exchangeBlows(this, opponent);
			
			// Wait for 1 seconds
			Debug.Log("AttackableOne: " + this.currentHp + ", AttackableTwo: " + opponent.currentHp);
			yield return new WaitForSeconds(1);
			
			// If we are fighting an anthill, we can be interrupted by another battle during our yield
			if (anthill && isInBattle) {
				Debug.Log("Interrupted by another battle!");
				break;
			}
			
			// If we are fighting an anthill, can interrupt our own battle by leaving
			if (anthill && hasSetNewPath) {
				Debug.Log("Cancelled attack on anthill due to moving away!");
				break;
			}
			
			// Check if one of the units has died
			if (this.currentHp <= 0 || opponent.currentHp <= 0) {
				Debug.Log("Cancelled attack since at least one of us is dead!");
				break;
			}
		}
		
		hasSetNewPath = false;
		
		// After the battle, 'cleanup' the unit(s) and the combat cloud
		if (cloud) {
			if (Network.isClient || Network.isServer) {
				Network.Destroy(cloud);
			}
			else {
				GameObject.Destroy(cloud);
			}
		}
		if (transform.localScale.x < 0) transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
		gameObject.renderer.enabled = true;
		opponent.gameObject.renderer.enabled = true;
		this.removeFromBattle();
		opponent.removeFromBattle();
		if (opponent.currentHp <= 0) opponent.kill();
		// Note: if this unit dies, make sure to delete it at the end!
		if (this.currentHp <= 0) this.kill();
	}