-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split NPCs, Player, and Enemies into classes
- Loading branch information
Showing
6 changed files
with
134 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Monster extends Entity { | ||
constructor(name, x, y, health, damage, loot) { | ||
super(name, x, y, health, damage); | ||
this._loot = loot; // e.g., gold or items | ||
this.color = "red"; | ||
this.width = 32; | ||
this.height = 32; | ||
} | ||
|
||
// Getter for loot | ||
get loot() { return this._loot; } | ||
|
||
// Monster attack method | ||
attack(target) { | ||
if (target instanceof Player) { | ||
target.health -= this._damage; | ||
console.log(`${this.name} attacks ${target.name} for ${this._damage} damage!`); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Entity { | ||
constructor(name, x, y, health, damage) { | ||
this._name = name; | ||
this._x = x; | ||
this._y = y; | ||
this._health = health; | ||
this._damage = damage; | ||
} | ||
|
||
// Getters | ||
get name() { return this._name; } | ||
get x() { return this._x; } | ||
get y() { return this._y; } | ||
get health() { return this._health; } | ||
get damage() { return this._damage; } | ||
|
||
// Setters | ||
set health(value) { | ||
this._health = Math.max(0, value); // Prevent negative health | ||
} | ||
|
||
set x(value) { this._x = value; } | ||
set y(value) { this._y = value; } | ||
|
||
// Common method for moving an entity | ||
move(dx, dy) { | ||
this._x += dx; | ||
this._y += dy; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class NPC extends Entity { | ||
constructor(name, x, y, dialogue) { | ||
super(name, x, y, 100, 0); // NPCs don't fight, so no damage | ||
this.width = 32; | ||
this.height = 32; | ||
this._dialogue = dialogue; | ||
} | ||
|
||
// Getter for dialogue | ||
get dialogue() { return this._dialogue; } | ||
|
||
// Setter for dialogue | ||
set dialogue(newDialogue) { this._dialogue = newDialogue; } | ||
|
||
speak() { | ||
console.log(`${this.name}: "${this._dialogue}"`); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Player extends Entity { | ||
constructor(name, x, y, health, damage, level, gold) { | ||
super(name, x, y, health, damage); | ||
this._level = level; | ||
this._gold = gold; | ||
this.color = "blue"; | ||
this.width = 32; | ||
this.height = 32; | ||
} | ||
|
||
// Getters | ||
get level() { return this._level; } | ||
get gold() { return this._gold; } | ||
|
||
// Setters | ||
set level(value) { this._level = Math.max(1, value); } // Prevent level from dropping below 1 | ||
set gold(value) { this._gold = Math.max(0, value); } | ||
|
||
// Level up method | ||
levelUp() { | ||
this._level += 1; | ||
this._health += 10; // Increase health on level up | ||
this._damage += 2; // Increase attack power | ||
console.log(`${this.name} leveled up to ${this._level}!`); | ||
} | ||
} |
This file contains 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