Klasės
Pamokos turinys
- Klasių paveldimumas, raktinis ženklas
:
Pradinis kodas
Geriausia susikurti naują C# failą ir pavadinti Class8.cs
using UnityEngine;
using UnityEditor;
public class Class10 : MonoBehaviour {
[MenuItem("MyMenu/Class10")]
static void Main()
{
Debug.Log("Class10 Main()");
}
}
Kodas
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class Class10 : MonoBehaviour
{
[MenuItem("MyMenu/Class10")]
static void Main()
{
Debug.Log("Class10 Main()");
Debug.Log("Number of enemies: " + Enemy.EnemyCount());
Enemy e1 = new Enemy();
e1.hp = 100;
e1.TakeDamage(10);
Debug.Log("e1 hp: " + e1.hp);
Debug.Log("Number of enemies: " + Enemy.EnemyCount());
ToughEnemy e2 = new ToughEnemy();
ToughEnemy e3 = e2;
e2.hp = 100;
e2.TakeDamage(10);
Debug.Log("e2 hp: " + e2.hp);
Debug.Log("Number of enemies: " + Enemy.EnemyCount());
e3.TakeDamage(100);
Debug.Log("e2 hp: " + e2.hp);
Enemy.PrintAllHealth();
// Shapes
Debug.Log("Shapes");
Shape fig1 = new Square(4.3f);
fig1.Draw();
Debug.Log("Area of fig1: " + fig1.Area());
Debug.Log("Corners of fig1: " + fig1.NumberOfCorners());
Debug.Log(fig1 is Square);
Shape fig2 = new Circle(4.3f);
fig2.Draw();
Debug.Log("Area of fig2: " + fig2.Area());
Debug.Log("Corners of fig2: " + fig2.NumberOfCorners());
// Rock paper scissors
Sign a1 = new Rock();
Sign a2 = new Paper();
Sign a3 = new Rock();
Sign b1 = new Paper();
Sign b2 = new Scissors();
Sign b3 = new Rock();
int pointsA = 0;
int pointsB = 0;
if (a1.Beats(b1))
pointsA++;
else if (b1.Beats(a1))
pointsB++;
if (a2.Beats(b2))
pointsA++;
else if (b2.Beats(a2))
pointsB++;
if (a3.Beats(b3))
pointsA++;
else if (b3.Beats(a3))
pointsB++;
Debug.Log("Player A points: "+ pointsA);
Debug.Log("Player B points: "+ pointsB);
// Hero time
Hero hero = new Hero();
Item key = new Item("key", 0.02f);
Item mace = new Weapon("mace", 12, 20);
Item cheesburger = new Item("cheesburger", 30);
Item sword = new Weapon("sword", 20, 100);
hero.DoDamage();
hero.PickUp(key);
hero.DoDamage();
hero.PickUp(mace);
hero.DoDamage();
hero.PickUp(cheesburger);
hero.DoDamage();
hero.PickUp(sword);
hero.DoDamage();
}
}
class Enemy {
public int hp;
private static int numberOfEnemies = 0;
private static List<Enemy> enemies = new List<Enemy>();
public Enemy()
{
numberOfEnemies += 1;
enemies.Add(this);
}
public void TakeDamage(int damage)
{
hp -= damage;
}
public static int EnemyCount()
{
return numberOfEnemies;
}
public static void PrintAllHealth()
{
int n = 0;
foreach (Enemy e in enemies)
{
Debug.Log("Enemy number " + n + " health is: " + e.hp);
n += 1;
}
}
}
class ToughEnemy : Enemy {
new public void TakeDamage(int damage)
{
damage = damage / 2;
base.TakeDamage(damage);
}
}
abstract class Shape {
public abstract void Draw();
public abstract float Area();
public abstract int NumberOfCorners();
}
class Square : Shape {
public float a; // Kraštinės ilgis
public Square(float a)
{
this.a = a;
}
public override void Draw()
{
Debug.Log("▢");
}
public override float Area()
{
return a * a;
}
public override int NumberOfCorners()
{
return 4;
}
}
class Circle : Shape {
public float radius;
public Circle(float radius)
{
this.radius = radius;
}
public override void Draw()
{
Debug.Log("○");
}
public override float Area()
{
return radius * radius * Mathf.PI;
}
public override int NumberOfCorners()
{
return 0;
}
}
abstract class Sign {
public abstract string Name();
public abstract bool Beats(Sign other);
}
class Rock : Sign {
public override string Name()
{
return "rock";
}
public override bool Beats(Sign other)
{
return other.Name() == "scissors";
}
}
class Paper : Sign {
public override string Name()
{
return "paper";
}
public override bool Beats(Sign other)
{
return other is Rock;
}
}
class Scissors : Sign {
public override string Name()
{
return "scissors";
}
public override bool Beats(Sign other)
{
return other is Paper;
}
}
class Hero
{
float maxInventoryWeight = 50;
float currentInventoryWeight = 0;
float baseDamage = 4;
Weapon currentWeapon = null;
// List<Item> inventory = new List<Item>();
public void PickUp(Item item)
{
if (currentInventoryWeight + item.weight > maxInventoryWeight)
{
Debug.Log("Can't pick up " + item.name);
return;
}
currentInventoryWeight += item.weight;
Debug.Log("Picked up " + item.name);
if (item is Weapon)
currentWeapon = (Weapon)item;
}
public void DoDamage()
{
float damage = baseDamage;
if (currentWeapon != null)
damage += currentWeapon.damage;
Debug.Log("Hero deals " + damage + " damage");
}
}
class Item
{
public string name;
public float weight;
public Item(string name, float weight)
{
this.name = name;
this.weight = weight;
}
}
class Weapon : Item
{
public float damage;
public Weapon(string name, float weight, float damage) : base(name, weight)
{
this.damage = damage;
}
}