//AttemptMove overrides the AttemptMove function in the base class MovingObject //AttemptMove takes a generic parameter T which for Player will be of the type Wall, it also takes integers for x and y direction to move in. protected override void AttemptMove <T>(int xDir, int yDir) { //Hit allows us to reference the result of the Linecast done in Move. RaycastHit2D hit; if (Move(xDir, yDir, out hit)) { GenerateTerrain(xDir, yDir); playerDirection = new Vector2(xDir, yDir); playerIcon.position = playerIcon.position + new Vector3(xDir, yDir, 0) * moveScaleFactor; //SoundManager.instance.RandomizeSfx(moveSound1, moveSound2); if (equippedSkill == -2 || equippedSkill == -3)//Skills that require an enemy be the target { equippedSkill = -1; Text instance = Instantiate(dialogueText); instance.gameObject.GetComponent <FadeOut>().SetColorAndText("No enemy is attacked.", Color.white); } else if (equippedSkill == -5)//Healing { equippedSkill = -1; Text instance = Instantiate(dialogueText); instance.gameObject.GetComponent <FadeOut>().SetColorAndText("You patch your wounds.", Color.white); int heal = playerStat.maxHealth / 4; playerStat.health = Mathf.Clamp((playerStat.health + heal), 0, playerStat.maxHealth); playerStat.focus -= 5; DamageCounter damageCounter = (Instantiate(canvasGroup, gameObject.transform.position, Quaternion.identity) as GameObject).GetComponent <DamageCounter>(); damageCounter.setDamage(heal); damageCounter.GetComponentInChildren <Image>().color = Color.green; } if (playerStat.health < playerStat.maxHealth)//Naturally heal 1 health during a move { playerStat.health += 1; if (waitCounter > 3) { playerStat.health += waitCounter; } } UpdateFocus(1); } else { T hitComponent = hit.transform.GetComponent <T>(); OnCantMove(hitComponent); } UpdateHealth(); //Update health GUI CheckIfGameOver(); //The player could have potentially died in the last turn. //Set the playersTurn boolean of GameManager to false now that players turn is over. GameManager.instance.playersTurn = false; }
//LoseFood is called when an enemy attacks the player. //It takes a parameter loss which specifies how many points to lose. public void LoseFood(int loss) { waitCounter = 0; SoundManager.instance.RandomizeSfx(hitSound, hitSound2, hitSound3); //Subtract lost food points from the players total. loss = loss - playerStat.defence; if (loss < 0) { loss = 0; } playerStat.health -= loss; UpdateFocus(-1); //Add a damage counter to show damage taken DamageCounter damageCounter = (Instantiate(canvasGroup, gameObject.transform.position, Quaternion.identity) as GameObject).GetComponent <DamageCounter>(); damageCounter.setDamage(Mathf.Clamp(loss, 0, loss)); damageCounter.GetComponentInChildren <Image>().color = Color.red; if ((double)loss / (double)playerStat.maxHealth > 0.5) { damageCounter.GetComponentInChildren <Image>().color = Color.HSVToRGB(0.08f, 1, 1); SoundManager.instance.PlaySingle(hitSound4); } else { SoundManager.instance.RandomizeSfx(hitSound, hitSound2, hitSound3); } //Updates health UpdateHealth(); //Check to see if game has ended. CheckIfGameOver(); }
public void DamageWall(int loss) { loss = loss - defence; if (loss < 0) { loss = 0; } hp -= loss; //Add a damage counter to show damage taken DamageCounter damageCounter = (Instantiate(canvasGroup, gameObject.transform.position, Quaternion.identity) as GameObject).GetComponent <DamageCounter>(); damageCounter.setPosition(gameObject.transform.position); damageCounter.setDamage(Mathf.Clamp(loss, 0, loss)); damageCounter.GetComponentInChildren <Image>().color = Color.red; if ((double)loss / (double)maxHp > 0.5) { damageCounter.GetComponentInChildren <Image>().color = Color.HSVToRGB(0.08f, 1, 1); } if (hp <= 0) { if (this.tag != "Wall") { //system.transform.position = this.transform.position; //system.Emit(10); Player.instance.AddExperience(experienceValue); int[] itemsDropped = new int[4]; for (int i = 0; i < 4; i++) { if (Random.Range(0, rarity) == 0 && itemDrop.Length > i) { itemsDropped[i] = itemDrop[i]; } else { itemsDropped[i] = -1; } } if (!(itemsDropped[0] == -1 && itemsDropped[1] == -1 && itemsDropped[2] == -1 && itemsDropped[3] == -1)) { //Handles the case when an enemy drops something on an exisiting bag GameObject[] pickups = GameObject.FindGameObjectsWithTag("Pickup"); bool bagExists = false; for (int i = 0; i < pickups.Length; i++) { if (pickups[i].transform.position == transform.position) { bagExists = true; int pickupsIndex = 0; Pickup matchingPickup = pickups[i].GetComponent <Pickup>(); for (int j = 0; j < 4; j++) { if (matchingPickup.itemId[i] == -1) { matchingPickup.itemId[i] = itemsDropped[pickupsIndex]; pickupsIndex++; } } } } if (!bagExists) { BoardManager.instance.AddPickup((int)transform.position.x, (int)transform.position.y, itemsDropped); } } } Delete(); UpdateHealthBar(); } }