Skip to content

Commit 2698109

Browse files
committed
feat: upgrade items change based on XP
1 parent 0ff8e9e commit 2698109

File tree

8 files changed

+107
-42
lines changed

8 files changed

+107
-42
lines changed

crates/hyperion-item/src/builder.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ impl ItemBuilder {
115115
self
116116
}
117117

118+
pub const fn kind(mut self, kind: ItemKind) -> Self {
119+
self.kind = kind;
120+
self
121+
}
122+
118123
/// Sets a custom name for the item
119124
pub fn name(mut self, name: impl Into<String>) -> Self {
120125
let nbt = self.nbt.get_or_insert_with(nbt::Compound::new);
@@ -127,7 +132,13 @@ impl ItemBuilder {
127132

128133
// Create a new display compound with the name
129134
let mut new_display = display;
130-
new_display.insert("Name", Value::String(name.into()));
135+
136+
let name = name.into();
137+
138+
// '{"text":"Your Custom Name"}'
139+
let name = format!(r#"{{"text":"{name}"}}"#);
140+
141+
new_display.insert("Name", Value::String(name));
131142

132143
// Insert the updated display compound
133144
nbt.insert("display", Value::Compound(new_display));

crates/hyperion-rank-tree/src/inventory.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl Rank {
3232
inventory: &mut PlayerInventory,
3333
world: &World,
3434
build_count: i8,
35+
extra_levels: u8,
3536
) {
3637
inventory.clear();
3738
let upgrade_not_available = ItemBuilder::new(ItemKind::GrayDye);
@@ -75,16 +76,33 @@ impl Rank {
7576

7677
inventory.set_boots(boots);
7778

78-
let upgrades = ["Speed", "Health", "Armor", "Damage"];
79+
let upgrades = ["Speed", "Health", "Armor", "Damage"]
80+
.into_iter()
81+
.map(|title| match extra_levels {
82+
0 => format!("§6§l{title}"),
83+
_ => format!("§6§l{title}§e §c({extra_levels})"),
84+
});
85+
86+
let upgrade_available = [
87+
ItemKind::BlueDye, // Speed
88+
ItemKind::RedDye, // Health
89+
ItemKind::YellowDye, // Armor
90+
ItemKind::GreenDye, // Damage
91+
];
7992

8093
world.get::<&Handles>(|handles| {
81-
for (i, upgrade) in upgrades.into_iter().enumerate() {
94+
for ((i, upgrade), available_item) in upgrades.enumerate().zip(upgrade_available) {
8295
let slot = u16::try_from(i).unwrap() + UPGRADE_START_SLOT;
83-
let item = upgrade_not_available
96+
let mut item = upgrade_not_available
8497
.clone()
8598
.name(upgrade)
86-
.handler(handles.speed)
87-
.build();
99+
.handler(handles.speed);
100+
101+
if extra_levels > 0 {
102+
item = item.kind(available_item);
103+
}
104+
105+
let item = item.build();
88106
inventory.set_hotbar(slot, item);
89107
}
90108
});

crates/hyperion-rank-tree/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
use clap::ValueEnum;
22
use flecs_ecs::{
3-
core::{Entity, IdOperations, World},
3+
core::{flecs, Entity, IdOperations, World},
44
macros::Component,
55
prelude::Module,
66
};
7-
use hyperion::storage::{ClickEvent, EventFn};
7+
use hyperion::{
8+
simulation::Player,
9+
storage::{ClickEvent, EventFn},
10+
};
811

912
pub mod inventory;
1013
pub mod skin;
1114

12-
#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, Component)]
15+
#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, Component, Default)]
1316
#[repr(C)]
1417
pub enum Rank {
1518
/// ![Widget Example](https://i.imgur.com/pW7v0Xn.png)
1619
///
1720
/// The stick is the starting rank.
21+
#[default]
1822
Stick, // -> [Pickaxe | Sword | Bow ]
1923

2024
Archer,
@@ -28,8 +32,11 @@ pub enum Rank {
2832
Builder,
2933
}
3034

31-
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, PartialOrd, Ord)]
35+
#[derive(
36+
Copy, Clone, Debug, PartialEq, Eq, ValueEnum, PartialOrd, Ord, Component, Default
37+
)]
3238
pub enum Team {
39+
#[default]
3340
Blue,
3441
Green,
3542
Red,
@@ -47,9 +54,18 @@ pub struct Handles {
4754
impl Module for RankTree {
4855
fn module(world: &World) {
4956
world.import::<hyperion_item::ItemModule>();
57+
world.component::<Team>();
5058
world.component::<Rank>();
5159
world.component::<Handles>();
5260

61+
world
62+
.component::<Player>()
63+
.add_trait::<(flecs::With, Team)>();
64+
65+
world
66+
.component::<Player>()
67+
.add_trait::<(flecs::With, Rank)>();
68+
5369
let handler: EventFn<ClickEvent> = |query, _| {
5470
let cursor = query.inventory.get_cursor();
5571
println!("clicked {cursor:?}");

events/tag/src/command/rank.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl MinecraftCommand for ClassCommand {
5252
|(stream, uuid, inventory, position, yaw, pitch, main_block_count)| {
5353
inventory.clear();
5454

55-
rank.apply_inventory(team, inventory, world, **main_block_count);
55+
rank.apply_inventory(team, inventory, world, **main_block_count, 0);
5656

5757
let minecraft_id = caller.minecraft_id();
5858
let mut bundle = DataBundle::new(compose);

events/tag/src/component.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

events/tag/src/component/team.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

events/tag/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use hyperion::{
1717
use hyperion_clap::hyperion_command::CommandRegistry;
1818
use module::block::BlockModule;
1919

20-
mod component;
2120
mod module;
2221

2322
use derive_more::{Deref, DerefMut};
2423
use hyperion::glam::IVec3;
24+
use hyperion_rank_tree::Team;
2525
use module::{attack::AttackModule, level::LevelModule, regeneration::RegenerationModule};
2626

2727
use crate::{
@@ -57,15 +57,14 @@ impl Module for ProofOfConceptModule {
5757
.component::<Player>()
5858
.add_trait::<(flecs::With, MainBlockCount)>();
5959

60-
world.component::<component::team::Team>();
6160
world.import::<hyperion_rank_tree::RankTree>();
6261

6362
world.component::<OreVeins>();
6463
world.set(OreVeins::default());
6564

6665
world
6766
.component::<Player>()
68-
.add_trait::<(flecs::With, component::team::Team)>();
67+
.add_trait::<(flecs::With, Team)>();
6968

7069
world.import::<SpawnModule>();
7170
world.import::<ChatModule>();

events/tag/src/module/level.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,68 @@
11
use flecs_ecs::{
2-
core::{World, flecs},
2+
core::{SystemAPI, World, WorldProvider, flecs},
33
macros::Component,
44
prelude::Module,
55
};
6-
use hyperion::simulation::Player;
6+
use hyperion::{
7+
Prev,
8+
simulation::{Player, Xp},
9+
};
10+
use hyperion_inventory::PlayerInventory;
11+
use hyperion_rank_tree::{Rank, Team};
12+
13+
use crate::MainBlockCount;
714

815
#[derive(Component)]
916
pub struct LevelModule;
1017

1118
#[derive(Component, Default, Copy, Clone, Debug)]
1219
#[meta]
13-
pub struct Level {
14-
pub value: usize,
20+
pub struct UpgradedTo {
21+
pub value: u8,
1522
}
1623

1724
impl Module for LevelModule {
1825
#[allow(clippy::excessive_nesting)]
1926
fn module(world: &World) {
20-
world.component::<Level>().meta();
27+
world.component::<UpgradedTo>().meta();
2128
world
2229
.component::<Player>()
23-
.add_trait::<(flecs::With, Level)>(); // todo: how does this even call Default? (IndraDb)
30+
.add_trait::<(flecs::With, UpgradedTo)>(); // todo: how does this even call Default? (IndraDb)
31+
32+
// on Xp gain,
33+
world
34+
.system_named::<(
35+
&(Prev, Xp),
36+
&Xp,
37+
&UpgradedTo,
38+
&Rank,
39+
&Team,
40+
&MainBlockCount,
41+
&mut PlayerInventory,
42+
)>("level_up")
43+
.multi_threaded()
44+
.kind::<flecs::pipeline::PreStore>()
45+
.each_entity(
46+
|entity, (prev, xp, upgraded_to, rank, team, main_block_count, inventory)| {
47+
if *xp <= *prev {
48+
// we are only considering gains
49+
return;
50+
}
51+
52+
let prev_level = prev.get_visual().level;
53+
let new_level = xp.get_visual().level;
54+
55+
if new_level <= prev_level {
56+
// only considering our level increasing
57+
return;
58+
}
59+
60+
let world = entity.world();
61+
62+
let level_diff = new_level - upgraded_to.value;
63+
64+
rank.apply_inventory(*team, inventory, &world, **main_block_count, level_diff);
65+
},
66+
);
2467
}
2568
}

0 commit comments

Comments
 (0)