-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Felipe-Devr/develop
feat: added experience component, implement ActorDamageCause enum, reset of hunger, exhaustion, experience and saturation on death
- Loading branch information
Showing
15 changed files
with
294 additions
and
69 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,40 @@ | ||
enum ActorDamageCause { | ||
None = -1, | ||
Override, | ||
Contact, | ||
EntityAttack, | ||
Projectile, | ||
Suffocation, | ||
Fall, | ||
Fire, | ||
FireTick, | ||
Lava, | ||
Drowning, | ||
BlockExplosion, | ||
EntityExplosion, | ||
Void, | ||
SelfDestruct, | ||
Magic, | ||
Wither, | ||
Starve, | ||
Anvil, | ||
Thorns, | ||
FallingBlock, | ||
Piston, | ||
FlyIntoWall, | ||
Magma, | ||
Fireworks, | ||
Lightning, | ||
Charging, | ||
Temperature, | ||
Freezing, | ||
Stalactite, | ||
Stalagmite, | ||
RamAttack, | ||
SonicBoom, | ||
Campfire, | ||
SoulCampfire, | ||
All | ||
} | ||
|
||
export { ActorDamageCause }; |
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
50 changes: 50 additions & 0 deletions
50
packages/world/src/components/player/attribute/experience-level.ts
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,50 @@ | ||
import { AttributeName } from "@serenityjs/protocol"; | ||
|
||
import { EntityAttributeComponent } from "../../entity/attribute/attribute"; | ||
|
||
import type { Player } from "../../../player"; | ||
|
||
class PlayerExperienceLevelComponent extends EntityAttributeComponent { | ||
public static readonly identifier = AttributeName.PlayerLevel; | ||
|
||
public readonly effectiveMin: number = 0; | ||
/** | ||
* Maximum XP Level visible? | ||
*/ | ||
public readonly effectiveMax: number = 24_792; | ||
public readonly defaultValue: number = 0; | ||
|
||
public constructor(player: Player) { | ||
super(player, PlayerExperienceLevelComponent.identifier); | ||
this.setCurrentValue(this.defaultValue, false); | ||
} | ||
|
||
public toExperience(): number { | ||
let totalExperience = 0; | ||
switch (true) { | ||
case this.level <= 16: { | ||
totalExperience = this.level ** 2 + 6 * this.level; | ||
break; | ||
} | ||
case this.level > 16 && this.level <= 31: { | ||
totalExperience = 2.5 * this.level ** 2 - 40.5 * this.level + 360; | ||
break; | ||
} | ||
case this.level > 31: { | ||
totalExperience = 4.5 * this.level ** 2 - 162.5 * this.level + 2220; | ||
break; | ||
} | ||
} | ||
return totalExperience; | ||
} | ||
|
||
public set level(newExperienceLevel: number) { | ||
this.setCurrentValue(newExperienceLevel, true); | ||
} | ||
|
||
public get level(): number { | ||
return this.getCurrentValue(); | ||
} | ||
} | ||
|
||
export { PlayerExperienceLevelComponent }; |
67 changes: 67 additions & 0 deletions
67
packages/world/src/components/player/attribute/experience.ts
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,67 @@ | ||
import { AttributeName } from "@serenityjs/protocol"; | ||
|
||
import { EntityAttributeComponent } from "../../entity/attribute/attribute"; | ||
|
||
import type { Player } from "../../../player"; | ||
|
||
class PlayerExperienceComponent extends EntityAttributeComponent { | ||
public static readonly identifier = AttributeName.PlayerExperience; | ||
public readonly effectiveMin: number = 0; | ||
/** | ||
* Infinity because it converts automatically to xp levels | ||
*/ | ||
public readonly effectiveMax: number = Infinity; | ||
public readonly defaultValue: number = 0; | ||
|
||
public constructor(player: Player) { | ||
super(player, PlayerExperienceComponent.identifier); | ||
this.setCurrentValue(this.defaultValue, false); | ||
} | ||
|
||
public addExperience(experienceAmount: number) { | ||
if (!this.entity.isPlayer()) return; | ||
const experienceLevelComponent = this.entity.getComponent( | ||
"minecraft:player.level" | ||
); | ||
|
||
// ? Add the saved experience. | ||
let newXp = experienceAmount + this.experience; | ||
|
||
// ? While the xp value is greather or equal to the required xp for the next level, do level up | ||
|
||
while (newXp >= this.entity.getNextLevelXp()) { | ||
newXp -= this.entity.getNextLevelXp(); | ||
experienceLevelComponent.increaseValue(1); | ||
} | ||
// ? Remove the current xp and add the remaining xp value | ||
|
||
this.setCurrentValue(newXp, true); | ||
} | ||
|
||
public removeExperience(experienceAmount: number) { | ||
if (!this.entity.isPlayer()) return; | ||
const experienceLevelComponent = this.entity.getComponent( | ||
"minecraft:player.level" | ||
); | ||
|
||
// ? Add the saved experience. | ||
let newXp = experienceAmount - this.experience; | ||
|
||
// ? While the xp value is greather or equal to the previus level, do level down | ||
|
||
while (newXp >= this.entity.getNextLevelXp(this.entity.level - 1)) { | ||
newXp -= this.entity.getNextLevelXp(this.entity.level - 1); | ||
experienceLevelComponent.decreaseValue(1); | ||
} | ||
|
||
// ? Remove the current xp and add the remaining xp value | ||
|
||
this.setCurrentValue(newXp, true); | ||
} | ||
|
||
public get experience(): number { | ||
return this.getCurrentValue(); | ||
} | ||
} | ||
|
||
export { PlayerExperienceComponent }; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./hunger"; | ||
export * from "./exhaustion"; | ||
export * from "./saturation"; | ||
export * from "./experience-level"; | ||
export * from "./experience"; |
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
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
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
Oops, something went wrong.