@@ -5,6 +5,7 @@ pub type CellId = u16;
55pub struct CellData {
66 pub color : CellColor ,
77 pub name : & ' static str ,
8+ pub density : u8 ,
89 pub type_data : CellDataType ,
910}
1011#[ derive( Debug ) ]
@@ -81,22 +82,19 @@ impl CellColor {
8182 }
8283 pub const AIR : Self = Self :: new ( 0x00 , 0x00 , 0x00 , 0x00 ) ;
8384}
84- #[ derive( Clone ) ]
85+ #[ derive( Clone , Debug ) ]
8586pub struct Cell {
86- pub color : CellColor ,
8787 pub cell_data : & ' static CellData ,
88- pub id : CellId ,
89- pub cell_type : CellType ,
9088 pub last_changed : u8 ,
9189 pub velocity : CellVelocity ,
9290 pub friction : CellFriction ,
9391}
94- #[ derive( Clone , Default ) ]
92+ #[ derive( Clone , Default , Debug ) ]
9593pub struct CellVelocity {
9694 pub x : i8 ,
9795 pub y : i8 ,
9896}
99- #[ derive( Clone , Default ) ]
97+ #[ derive( Clone , Default , Debug ) ]
10098pub struct CellFriction {
10199 pub x : u8 ,
102100 pub y : u8 ,
@@ -106,18 +104,15 @@ impl Cell {
106104 pub fn new ( id : CellId ) -> Self {
107105 let cell_data = & CELLS [ id. strict_cast :: < usize > ( ) ] ;
108106 Self {
109- color : cell_data. color ,
110107 cell_data,
111- id,
112- cell_type : cell_data. type_data . cell_type ( ) ,
113108 last_changed : 0 ,
114109 velocity : CellVelocity :: default ( ) ,
115110 friction : CellFriction :: default ( ) ,
116111 }
117112 }
118113 pub fn friction ( & mut self , x : u8 , y : u8 ) {
119114 fn run ( f : & mut i8 , r : & mut u8 , i : u8 ) {
120- let ( n, over) = r. overflowing_shl ( i . strict_cast ( ) ) ;
115+ let ( n, over) = r. overflowing_add ( i ) ;
121116 * r = n;
122117 if over {
123118 if f. is_positive ( ) {
@@ -130,26 +125,41 @@ impl Cell {
130125 run ( & mut self . velocity . x , & mut self . friction . x , x) ;
131126 run ( & mut self . velocity . y , & mut self . friction . y , y) ;
132127 }
128+ pub fn gravity ( & mut self ) {
129+ self . velocity . y -= 1 ;
130+ }
133131 #[ must_use]
134132 pub fn is_air ( & self ) -> bool {
135- self . color . is_air ( )
133+ self . color ( ) . is_air ( )
136134 }
137135 #[ must_use]
138136 pub fn is_collider ( & self ) -> bool {
139- matches ! ( self . cell_type, CellType :: Static | CellType :: Granular )
137+ matches ! ( self . cell_type( ) , CellType :: Static | CellType :: Granular )
138+ }
139+ #[ must_use]
140+ pub fn color ( & self ) -> CellColor {
141+ self . cell_data . color
142+ }
143+ #[ must_use]
144+ pub fn cell_type ( & self ) -> CellType {
145+ self . cell_data . type_data . cell_type ( )
146+ }
147+ #[ must_use]
148+ pub fn id ( & self ) -> CellId {
149+ ( ( ptr:: from_ref ( self . cell_data ) . addr ( ) - ptr:: from_ref ( & CELLS ) . addr ( ) )
150+ / size_of :: < CellData > ( ) )
151+ . strict_cast ( )
152+ }
153+ #[ must_use]
154+ pub fn density ( & self ) -> u8 {
155+ self . cell_data . density
140156 }
141157 #[ must_use]
142158 pub fn can_move ( & self , other : & Self ) -> bool {
143- matches ! (
144- other. cell_type,
145- CellType :: Liquid | CellType :: Gas | CellType :: Air
146- ) && !ptr:: eq ( self . cell_data , other. cell_data )
159+ self . density ( ) > other. density ( )
147160 }
148161 pub fn into ( & mut self , id : CellId ) {
149162 let cell_data = & CELLS [ id. strict_cast :: < usize > ( ) ] ;
150- self . id = id;
151163 self . cell_data = cell_data;
152- self . cell_type = cell_data. type_data . cell_type ( ) ;
153- self . color = cell_data. color ;
154164 }
155165}
0 commit comments