65 lines
1.9 KiB
GDScript
65 lines
1.9 KiB
GDScript
class_name Experience
|
|
extends RefCounted
|
|
|
|
# Cumulative thresholds: level 2 begins at 100 XP, then each following level
|
|
# costs another 100 XP. Keeping the calculation here gives combat, companions,
|
|
# and the character screen one authoritative level boundary.
|
|
const XP_PER_LEVEL_STEP := 100
|
|
|
|
var total_xp := 0
|
|
var level := 1
|
|
var attribute_points := 0
|
|
var skill_points := 0
|
|
|
|
func _init(initial_xp: int = 0) -> void:
|
|
set_total_xp(initial_xp)
|
|
|
|
func xp_for_level(target_level: int) -> int:
|
|
var normalized_level := maxi(1, target_level)
|
|
return XP_PER_LEVEL_STEP * (normalized_level - 1) * normalized_level / 2
|
|
|
|
func next_level_xp() -> int:
|
|
return xp_for_level(level + 1)
|
|
|
|
func set_total_xp(value: int) -> void:
|
|
total_xp = maxi(0, value)
|
|
level = _level_for(total_xp)
|
|
# A restored ledger has no allocation history in this slice, so its earned
|
|
# rewards are available rather than silently lost.
|
|
attribute_points = level - 1
|
|
skill_points = level - 1
|
|
|
|
func grant(amount: int) -> Dictionary:
|
|
var gained := maxi(0, amount)
|
|
var previous_level := level
|
|
total_xp += gained
|
|
level = _level_for(total_xp)
|
|
var levels_gained := level - previous_level
|
|
attribute_points += levels_gained
|
|
skill_points += levels_gained
|
|
return {"xp_gained": gained, "levels_gained": levels_gained}
|
|
|
|
static func split_reward(amount: int, companion_eligible: bool) -> Dictionary:
|
|
var reward := maxi(0, amount)
|
|
if not companion_eligible:
|
|
return {"player_xp": reward, "companion_xp": 0}
|
|
return {"player_xp": (reward + 1) / 2, "companion_xp": reward / 2}
|
|
|
|
func spend_attribute_point() -> bool:
|
|
if attribute_points <= 0:
|
|
return false
|
|
attribute_points -= 1
|
|
return true
|
|
|
|
func spend_skill_point() -> bool:
|
|
if skill_points <= 0:
|
|
return false
|
|
skill_points -= 1
|
|
return true
|
|
|
|
func _level_for(xp: int) -> int:
|
|
var resolved_level := 1
|
|
while xp >= xp_for_level(resolved_level + 1):
|
|
resolved_level += 1
|
|
return resolved_level
|