Last active
December 23, 2016 10:44
-
-
Save Zorgatone/015672447fe82e99a2adbd6b9793d983 to your computer and use it in GitHub Desktop.
OOP game 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
interface IWearable { | |
wear: () => void; | |
} | |
interface IPlayable {} | |
class GameObject { | |
toString() { | |
return JSON.stringify(this, null, " "); | |
} | |
} | |
class Item extends GameObject { | |
public ID: number; | |
public name: string; | |
constructor(ID: number, name: string) { | |
super(); | |
this.ID = ID; | |
this.name = name; | |
} | |
} | |
class Weapon extends Item { | |
public damage: number; | |
constructor(ID: number, name: string, damage: number) { | |
super(ID, name); | |
this.damage = damage; | |
} | |
} | |
class Armor extends Item implements IWearable { | |
public protection: number; | |
constructor(ID: number, name: string, protection: number) { | |
super(ID, name); | |
this.protection = protection; | |
} | |
wear() {} | |
} | |
class Inventory extends GameObject { | |
public items: Item[]; | |
constructor() { | |
super(); | |
this.items = []; | |
} | |
sort() { | |
this.items = this.items.sort((a, b) => a.ID - b.ID); | |
} | |
} | |
class Entity extends GameObject { | |
public ID: number; | |
public name: string; | |
constructor(ID: number, name: string) { | |
super(); | |
this.ID = ID; | |
this.name = name; | |
} | |
} | |
class Player extends Entity implements IPlayable { | |
public inventory: Inventory; | |
constructor(ID: number, name: string) { | |
super(ID, name); | |
this.inventory = new Inventory(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment