1
- use std:: collections:: { HashMap , HashSet } ;
2
-
1
+ use macroquad:: color:: BLACK ;
3
2
use macroquad:: math:: u32;
4
3
use macroquad:: prelude:: draw_text;
4
+ use macroquad:: shapes:: draw_circle;
5
5
use macroquad:: ui:: Ui ;
6
6
7
7
use server:: game:: Game ;
@@ -13,21 +13,15 @@ use crate::dialog_ui::active_dialog_window;
13
13
use crate :: select_ui:: { confirm_update, ConfirmSelection } ;
14
14
use crate :: { hex_ui, player_ui} ;
15
15
16
+ use itertools:: Itertools ;
17
+
16
18
pub fn draw_unit ( unit : & Unit , index : u32 ) {
17
19
let c = hex_ui:: center ( unit. position ) ;
18
- let r = if unit. unit_type == UnitType :: Settler {
19
- 25.
20
- } else {
21
- 40.
22
- } ;
23
- let p = hex_ui:: rotate_around ( c, r, ( 90 * index) as i32 + 45 ) ;
24
- draw_text (
25
- unit_symbol ( unit) ,
26
- p. x - 7.0 ,
27
- p. y + 7.0 ,
28
- 25.0 ,
29
- player_ui:: player_color ( unit. player_index ) ,
30
- ) ;
20
+ let r = 40.0 ;
21
+ let p = hex_ui:: rotate_around ( c, r, ( 40 * index) as i32 + 45 ) ;
22
+ draw_circle ( p. x , p. y , 11.0 , BLACK ) ;
23
+ draw_circle ( p. x , p. y , 9.0 , player_ui:: player_color ( unit. player_index ) ) ;
24
+ draw_text ( unit_symbol ( unit) , p. x - 5.0 , p. y + 5.0 , 20.0 , BLACK ) ;
31
25
}
32
26
33
27
fn unit_symbol ( unit : & Unit ) -> & str {
@@ -52,22 +46,17 @@ pub fn non_leader_names() -> [(UnitType, &'static str); 5] {
52
46
}
53
47
54
48
pub fn draw_units ( game : & Game ) {
55
- for p in & game. players {
56
- let mut positions: HashSet < & Position > = HashSet :: new ( ) ;
57
- let mut city_unit_index: HashMap < Position , u32 > = HashMap :: new ( ) ;
58
- let mut settler_index: HashMap < Position , u32 > = HashMap :: new ( ) ;
59
- for unit in & p. units {
60
- let map = if unit. unit_type == UnitType :: Settler {
61
- & mut settler_index
62
- } else {
63
- & mut city_unit_index
64
- } ;
65
- let e = map. entry ( unit. position ) . or_default ( ) ;
66
- * e += 1 ;
67
- draw_unit ( unit, * e) ;
68
-
69
- if positions. insert ( & unit. position ) { }
70
- }
49
+ for ( _pos, units) in & game
50
+ . players
51
+ . iter ( )
52
+ . flat_map ( |p| & p. units )
53
+ . sorted_by_key ( |u| u. position )
54
+ . chunk_by ( |a| a. position )
55
+ {
56
+ let vec = units. collect :: < Vec < _ > > ( ) ;
57
+ vec. iter ( ) . enumerate ( ) . for_each ( |( i, u) | {
58
+ draw_unit ( u, i. try_into ( ) . unwrap ( ) ) ;
59
+ } ) ;
71
60
}
72
61
}
73
62
0 commit comments