Created
July 31, 2025 09:19
-
-
Save shahnawaz/6d46f83ced3081f106ba47766ba16ec6 to your computer and use it in GitHub Desktop.
humanvszombie.ts
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 IZombie { | |
| health: number; // 20 | 60 | 80 | |
| takeDamage: (value: number) => void; | |
| } | |
| interface IHuman { | |
| weapons: IWeapon[]; | |
| activeWeaponIndex: number; | |
| switchWeapon: () => void; | |
| reload: () => void; | |
| } | |
| interface IWeapon { | |
| magazineSize: number; | |
| damage: number; | |
| bulletCount: number; | |
| reload: () => void; | |
| shoot: (target: IZombie) => void; | |
| } | |
| class Zombie implements IZombie { | |
| constructor(public health: 20 | 60 | 80){} | |
| takeDamage(value: number) { | |
| this.health -= value; | |
| // TODO: what if health is -ve | |
| } | |
| } | |
| class Weapon implements IWeapon { | |
| magazineSize: number; | |
| damage: number; | |
| bulletCount: number = 0; | |
| constructor(weaponType: 'rifle' | 'pistol'){ | |
| if (weaponType === 'rifle') { | |
| this.magazineSize = 50; | |
| this.damage = 40; | |
| } else { | |
| this.magazineSize = 10; | |
| this.damage = 15; | |
| } | |
| this.reload(); | |
| } | |
| reload() { | |
| this.bulletCount = this.magazineSize; | |
| }; | |
| shoot(zombie: IZombie) { | |
| if (this.bulletCount <= 0) { | |
| return; | |
| } | |
| zombie.takeDamage(this.damage); | |
| this.bulletCount--; | |
| }; | |
| } | |
| const createWeapon = (weaponType: 'rifle' | 'pistol'): Weapon => { | |
| // TODO: create weapon based on weapon type | |
| } | |
| /* | |
| const pistol: Weapon = { | |
| magazineSize: 10, | |
| damage: 15, | |
| bulletCount: 10 | |
| } | |
| const rifle: Weapon = { | |
| magazineSize: 50, | |
| damage: 40, | |
| bulletCount: 50 | |
| } | |
| */ | |
| // create human, zombie, create pistol, create rifle | |
| // shooting a zombie, reload weapon, changing weapon | |
| // damage zombie |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment