Skip to content

Instantly share code, notes, and snippets.

@Zorgatone
Last active December 23, 2016 10:44
Show Gist options
  • Save Zorgatone/015672447fe82e99a2adbd6b9793d983 to your computer and use it in GitHub Desktop.
Save Zorgatone/015672447fe82e99a2adbd6b9793d983 to your computer and use it in GitHub Desktop.
OOP game example
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