Klasės (dirbtiniai tipai)

Pamokos turinys

Kodas

using UnityEngine;
using UnityEditor;

public class Class9 : MonoBehaviour
{
    [MenuItem("MyMenu/Class9")]
    static void Main()
    {
        Debug.Log("Class9 Main()");

		Quest slayDragon = new Quest(10);
		Debug.Log(slayDragon);

		slayDragon.Finish();
		Debug.Log(slayDragon);
		
		slayDragon.Finish();
		Debug.Log(slayDragon);

		Debug.Log("Total points: " + Quest.totalPoints);

		Quest bringItem = new Quest(2);
		bringItem.Finish();
		Debug.Log(bringItem);

        Debug.Log("Total points: " + Quest.totalPoints);

		// uzd2

		Player player = new Player(2018);
		Building b1 = new Building("Castle", 1000);
		Building b2 = new Building("Farm", 800);
		Building b3 = new Building("Seimas", 500);

		player.Build(b1);
		player.Build(b2);
		player.Build(b3);

		// uzd 3

		Gun uzi = new Gun(6, 99);
		Bullet bu1 = uzi.Fire();
		Debug.Log("Uzi hits for: " + bu1.damage);
		Bullet bu2 = uzi.Fire();
		Debug.Log("Uzi hits for: " + bu2.damage);
    }
}

public class Quest
{
	private int reward;
	private int points = 0;
	public bool done = false;

	public static int totalPoints = 0;

	public Quest(int questReward)
	{
		reward = questReward;
	}

	public void Finish()
	{
		if (!done)
		{
			points += reward;
			totalPoints += reward;
			done = true;
		}
	}

	public override string ToString()
	{
		if (done)
		{
			return "Quest finished for " + points + " points";
		}
		else
		{
			return "Quest not completed. " + reward + " points available";
		}
	}
}

class Building
{
	public string name;
    public int cost;

	public Building(string buildingName, int buildingCost)
	{
		name = buildingName;
		cost = buildingCost;
	}
}

class Player
{
	private int money;

	public Player(int playerMoney)
	{
		money = playerMoney;
	}

	public void Build(Building b)
	{
		if (money >= b.cost)
		{
			money -= b.cost;
			Debug.Log(b.name + " succesfully built");
		}
		else
		{
			Debug.Log("Not enough money for: " + b.name + ". Costs: " + b.cost + ", have: " + money);
		}
	}
}

class Bullet
{
	public int damage;
}

class Gun
{
	private int minDamage;
	private int maxDamage;

	public Gun(int gunMinDamage, int gunMaxDamage)
	{
		minDamage = gunMinDamage;
		maxDamage = gunMaxDamage;
	}

	public Bullet Fire()
	{
		Bullet b = new Bullet();
		b.damage = Random.Range(minDamage, maxDamage);
		return b;
	}
}