These are my mostly finished classes, Iâm going to comment out a piece in the player script because thatâs the next part it need help with. When my player who has a fleet which can Attack() and Defend() collides with a planet which also has a fleet how can I compare the attack to the defense and then based on those results subtract the correct health. really all I need is the ability to grab the total attack from the planet
Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public Fleet playerFleet;
int sloop = 20;
int corsair = 10;
int frigate = 5;
int carvahal = 5;
int warship = 2;
// Use this for initialization
void Start()
{
playerFleet = new Fleet(sloop, corsair, frigate, carvahal, warship);
}
// Update is called once per frame
void Update ()
{
}
/*
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyPlanet"))
{
if (playerFleet.Attack() > )
{
}
}
}
*/
}
Planet Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Planet : MonoBehaviour
{
public Fleet planetFleet;
public int resources = 200;
public int planetWealth;
public int planetSize;
int numSloop;
int numCor;
int numFrig;
int numCarv;
int numWs;
void Start()
{
RandomWealth();
RandomSize();
resources += planetSize + planetWealth;
while (resources >= 5)
{
RandomFleet();
}
planetFleet = new Fleet(numSloop, numCor, numFrig, numSloop, numWs);
/* Debug.Log("Planet Wealth: " + planetWealth +
"\nPlanet size: " + planetSize +
"\nPlanet Resources: " + resources);
Debug.Log("numSloop: " + numSloop +
"\nnumCor: " + numCor +
"\nnumFrig: " + numFrig +
"\nnumCarv: " + numCarv +
"\nnumWs: " + numWs);*/
}
private void Update()
{
}
public void RandomWealth()
{
int randWealth;
randWealth = Random.Range(1, 3);
if (randWealth == 1)
{
int res = resources;
planetWealth = res / 4; // randomly decides if new planet is rich, middle, or poor
}
else if (randWealth == 2)
{
planetWealth = 0;
}
else
{
int res = resources;
planetWealth = res / 4;
}
}
public void RandomSize()
{
int randSize;
randSize = Random.Range(1, 3);
if (randSize == 1)
{
int res = resources;
planetSize = res / 4;
// randomly sets if planet is Large, med, or small
}
else if (randSize == 2)
{
planetSize = 0 + planetWealth;
}
else
{
int res = resources;
planetSize = res / 4;
}
}
public void RandomFleet()
{
int randomShip;
int sloopCost = 5;
int corCost = 10;
int frigCost = 10;
int carCost = 15;
int wsCost = 20;
randomShip = Random.Range(1, 6);
if (resources >= sloopCost)
{
if (randomShip == 1)
{
if (resources >= sloopCost)
{
numSloop += 1;
resources -= sloopCost;
}
}
else if (randomShip == 2)
{
if (resources >= corCost)
{
numCor += 1;
resources -= corCost;
}
}
else if (randomShip == 3)
{
if (resources >= frigCost)
{
numFrig += 1;
resources -= corCost;
}
}
else if (randomShip == 4)
{
if (resources >= carCost)
{
numCarv += 1;
resources -= corCost;
}
}
else if (randomShip == 5)
{
if (resources >= wsCost)
{
numWs += 1;
resources -= wsCost;
}
}
}
}
}
as soon as I finished those nested if else statements I wished I had just made them switches lol
Fleet class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Fleet
{
public int numSloop;
public int numCorsair;
public int numFrigate;
public int numCarvahal;
public int numWarShip;
public Fleet(int s, int cor, int frig, int carv, int ws)
{
numSloop = carv;
numCorsair = cor;
numFrigate = frig;
numCarvahal = carv;
numWarShip = ws;
}
// set up Ship objects
public Ship sloop = new Ship(1, 2, 2);
public Ship corsair = new Ship(5, 2, 2);
public Ship frigate = new Ship(10, 4, 4);
public Ship carvahal = new Ship(4, 10, 4);
public Ship warship = new Ship(15, 10, 15);
public int Attack()
{
int totalAttack;
totalAttack = (sloop.attk * numSloop) + // collect attack power * numer of current ships
(corsair.attk * numCorsair) + // for each ship type
(frigate.attk * numFrigate) +
(carvahal.attk * numCarvahal) +
(warship.attk * numWarShip);
totalAttack = Random.Range(0, totalAttack);// sets total defense to a random range to resolve battles and add a randomness to attacks
return totalAttack;// returns total attack power for fleet
}
public int Defend()
{
int totalDefense;
totalDefense = (sloop.defense * numSloop) + // collect Defense * numer of current ships
(corsair.defense * numCorsair) + // for each ship type
(frigate.defense * numFrigate) +
(carvahal.defense * numCarvahal) +
(warship.defense * numWarShip);
totalDefense = Random.Range(0, totalDefense);
return totalDefense;// returns total Defense for fleet
}
public int Health()
{
int totalHealth;
totalHealth = (sloop.health * numSloop) + // collect health * number of current ships
(corsair.health * numCorsair) + // for each ship type
(frigate.health * numFrigate) +
(carvahal.health * numCarvahal) +
(warship.health * numWarShip);
return totalHealth;// returns total Health for fleet
}
}
sorry its such a long post, the ships class just stores an int for health, defense, and attack