11use super :: {
2- gamelog:: GameLog , Equipped , InBackpack , LootTable , Map , Name , Player , Pools , Position ,
3- RunState , SufferDamage ,
2+ gamelog:: GameLog , mana_at_level , player_hp_at_level , Attributes , Equipped , InBackpack ,
3+ LootTable , Map , Name , Player , Pools , Position , RunState , SufferDamage ,
44} ;
55use crate :: prefabs:: { get_item_drop, spawn_named_item, SpawnType , PREFABS } ;
66use legion:: prelude:: * ;
@@ -11,21 +11,55 @@ pub fn build() -> Box<(dyn Schedulable + 'static)> {
1111 . write_component :: < Pools > ( )
1212 . read_component :: < Position > ( )
1313 . write_resource :: < Map > ( )
14- . build ( |command_buffer, world, map, query| unsafe {
15- for ( entity, mut damage) in query. iter_entities_unchecked ( world) {
16- if let Some ( mut stats) = world. get_component_mut_unchecked :: < Pools > ( entity) {
17- stats. hit_points . current -= damage. amount . iter ( ) . sum :: < i32 > ( ) ;
18- }
14+ . read_resource :: < Entity > ( )
15+ . read_component :: < Attributes > ( )
16+ . build (
17+ |command_buffer, world, ( map, player_entity) , query| unsafe {
18+ let mut xp_gain = 0 ;
19+ for ( entity, mut damage) in query. iter_entities_unchecked ( world) {
20+ if let Some ( mut stats) = world. get_component_mut_unchecked :: < Pools > ( entity) {
21+ for ( dmg, from_player) in damage. amount . iter ( ) {
22+ stats. hit_points . current -= dmg;
23+
24+ if stats. hit_points . current < 1 && * from_player {
25+ xp_gain += stats. level * 100 ;
26+ }
27+ }
28+
29+ if let Some ( pos) = world. get_component :: < Position > ( entity) {
30+ let idx = map. xy_idx ( pos. x , pos. y ) ;
31+ map. bloodstains . insert ( idx) ;
32+ }
33+ }
1934
20- if let Some ( pos) = world. get_component :: < Position > ( entity) {
21- let idx = map. xy_idx ( pos. x , pos. y ) ;
22- map. bloodstains . insert ( idx) ;
35+ damage. amount . clear ( ) ;
36+ command_buffer. remove_component :: < SufferDamage > ( entity) ;
2337 }
2438
25- damage. amount . clear ( ) ;
26- command_buffer. remove_component :: < SufferDamage > ( entity) ;
27- }
28- } )
39+ if xp_gain != 0 {
40+ let player_attributes =
41+ * ( world. get_component :: < Attributes > ( * * player_entity) . unwrap ( ) ) ;
42+ let mut player_stats =
43+ world. get_component_mut :: < Pools > ( * * player_entity) . unwrap ( ) ;
44+ player_stats. experience += xp_gain;
45+ if player_stats. experience >= player_stats. level * 1000 {
46+ // We've gone up a level!
47+ player_stats. level = player_stats. experience / 1000 + 1 ;
48+ player_stats. hit_points . max = player_hp_at_level (
49+ player_attributes. fitness . base + player_attributes. fitness . modifiers ,
50+ player_stats. level ,
51+ ) ;
52+ player_stats. hit_points . current = player_stats. hit_points . max ;
53+ player_stats. mana . max = mana_at_level (
54+ player_attributes. intelligence . base
55+ + player_attributes. intelligence . modifiers ,
56+ player_stats. level ,
57+ ) ;
58+ player_stats. mana . current = player_stats. mana . max ;
59+ }
60+ }
61+ } ,
62+ )
2963}
3064
3165pub fn delete_the_dead ( world : & mut World , resources : & mut Resources ) {
0 commit comments