Last active
February 3, 2024 15:20
-
-
Save chuwilliamson/573682bc5522dbbd5bf1 to your computer and use it in GitHub Desktop.
interface example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
public interface IDamageable | |
{ | |
void TakeDamage(int totalDamage); | |
} | |
class Wall : IDamageable | |
{ | |
void crumble() | |
{ | |
Console.WriteLine("crumbling"); | |
} | |
public Wall(int numbricks) | |
{ | |
m_bricks = numbricks; | |
} | |
public void TakeDamage(int totalDamage) | |
{ | |
m_bricks -= totalDamage; | |
Console.WriteLine("Current bricks that make me awesome: " + m_bricks.ToString()); | |
if (m_bricks <= 5) | |
Console.WriteLine("i'm dedz"); | |
} | |
int m_bricks; | |
//if i have 5 or less bricks then I am dead :9 | |
} | |
class Car : IDamageable | |
{ | |
void driveaway() | |
{ | |
Console.WriteLine("driving away...."); | |
} | |
int m_armor; | |
public Car(int armorPoints) | |
{ | |
m_armor = armorPoints; | |
} | |
public void TakeDamage(int dam) | |
{ | |
m_armor -= dam; | |
Console.WriteLine("Current armor = " + m_armor.ToString()); | |
Console.WriteLine("Took it to the face"); | |
} | |
//when a cars armor hits 0 then the car is dead :( | |
} | |
class Ninja : IDamageable | |
{ | |
private int m_hp; | |
public Ninja(int hp) | |
{ | |
m_hp = hp; | |
} | |
public void MyDefinitionOfTakeDamage(int dam) | |
{ | |
m_hp -= dam; | |
} | |
public void TakeDamage(int totalDamage) | |
{ | |
Console.WriteLine("i am ben i am ninja taking " + totalDamage.ToString() + " damages"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<IDamageable> damageableGameObjects = new List<IDamageable>(); | |
Car willsCar = new Car(25); | |
Wall andrew = new Wall(10); | |
Ninja ben = new Ninja(3); | |
Console.WriteLine("TAKE DAMAGE WILL the CAR"); | |
//willsCar.TakeDamage(25); | |
Console.WriteLine("TAKE DAMAGE MANDREW the WALL"); | |
//andrew.TakeDamage(5); | |
damageableGameObjects.Add(willsCar); | |
damageableGameObjects.Add(andrew); | |
damageableGameObjects.Add(ben); | |
for(int i = 0; i < damageableGameObjects.Count; ++i) | |
{ | |
damageableGameObjects[i].TakeDamage(i); | |
} | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment