public void setTarget(int index) { float enemyPos = (float)(Math.Pow(main.enemies[index].position.Y - realPosition.Y, 2) + // This is the circle equation, but just the left side of the equation where the enemies position is subtracted by the towers position. The circle equation: https://upload.wikimedia.org/math/a/7/7/a7714e2972d817c45f9615006c5242cb.png Math.Pow(main.enemies[index].position.X - realPosition.X, 2)); double calculatedRange = Math.Pow(range, 2); // The right side of the circle equation where Math.Pow is used as range to the power of 2. The circle equation: https://upload.wikimedia.org/math/a/7/7/a7714e2972d817c45f9615006c5242cb.png if (target != null) // If the tower has a target and the target isn't an enemy on the field, then set the target to nothing (null) { if (!main.enemies.Contains(target)) // the " ! " means that main.enemies does NOT contain target { target = null; } } if (calculatedRange > enemyPos) // If the enemys position is inside the towers range, then set the target of the tower to the enemy (This is where the circle equation gets checked) { if (target == null) { target = main.enemies[index]; } } else if (target == main.enemies[index]) // Else if the target is outside the range and the tower has the enemy as a target, then set the towers target to nothing (null) { target = null; } }
public Bullet(Tower parent, Texture2D bulletTexture) { this.parent = parent; // Sets the input parent to the class parent target = parent.target; // The towers target is also the bullets target texture = bulletTexture; position = parent.realPosition; // Puts the bullet in the middle of the towers position (in the origin of the tower) speed = 5; // bullet speederino }
// The function that creates the enemies, this function can be used with createEnemy( "insert wave", "insert wave path" ) private void createEnemy(List<int> wave, List<Vector2> wavePath) { Enemy enemy = new Enemy(wave[waveCounter], wavePath); // Adds the enemy to the game enemy.position = tiles[(int)(wavePath[enemy.currentNode].X), (int)(wavePath[enemy.currentNode].Y)].position; // The spawn position enemy.currentNode += 1; // the current node is the second node after the enemy has spawned (the first node is the spawn position) enemy.target = tiles[(int)enemy.wavePath[enemy.currentNode].X, (int)enemy.wavePath[enemy.currentNode].Y].position; // The target tile the enemy wants to move to enemies.Add(enemy); // Adds the enemy to the list of all the enemies waveCounter += 1; // Adds the enemy to the waveCounter }