From 8c8cb94763e190083d03eddf4a188898e62e0ad3 Mon Sep 17 00:00:00 2001 From: HadiSharifi Date: Sun, 10 May 2026 12:18:32 +0330 Subject: [PATCH] implement xp/level logic --- .../org/project/entity/players/Player.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Java-Knight/src/main/java/org/project/entity/players/Player.java b/Java-Knight/src/main/java/org/project/entity/players/Player.java index 8b46a0b..ee57d7b 100644 --- a/Java-Knight/src/main/java/org/project/entity/players/Player.java +++ b/Java-Knight/src/main/java/org/project/entity/players/Player.java @@ -1,6 +1,7 @@ package org.project.entity.players; import org.project.entity.Entity; +import org.project.entity.enemies.Dragon; import org.project.entity.enemies.Goblin; import org.project.entity.enemies.Skeleton; import org.project.item.armors.Armor; @@ -26,6 +27,10 @@ public abstract class Player implements Entity, CombatOptions { private boolean hasVampireKey = false; private boolean successfulAction = true; private Consumable flask; + private int level = 1; + private int xp = 0; + private static final int BASE_XP = 50; + private static final double XP_SCALE = 1.4; public Player(String name, int hp, int mp, Weapon weapon, Armor armor) { this.name = name; @@ -216,4 +221,40 @@ public abstract class Player implements Entity, CombatOptions { return flask; } + public int xpToNextLevel() { + return (int) (BASE_XP * Math.pow(XP_SCALE, level - 1)); + } + + private void levelUp() { + level++; + maxHP += 10; + maxMP += 8; + setHP(getHP() + 10); // heal by the amount gained + setMP(getMP() + 8); + System.out.println("★ LEVEL UP! " + getClass().getSimpleName() + + " is now level " + level + + " | Max HP +" + 10 + " | Max MP +" + 8); + } + + public void gainXP(int amount) { + xp += amount; + System.out.println(getClass().getSimpleName() + " gained " + amount + " XP! (" + + xp + "/" + xpToNextLevel() + ")"); + while (xp >= xpToNextLevel()) { + xp -= xpToNextLevel(); + levelUp(); + } + } + + public static int xpRewardFor(Entity enemy) { + if (enemy instanceof Dragon) return 300; + if (enemy instanceof Goblin) return 40; + if (enemy instanceof Skeleton) return 50; + return 60; + } + + public int getLevel() { return level; } + + public int getXP() { return xp; } + }