11using Backdash . Serialization ;
2- using Backdash . Serialization . Numerics ;
32
43namespace SpaceWar . Logic ;
54
@@ -17,7 +16,7 @@ public sealed record Ship : IBinarySerializable
1716 public int Invincible ;
1817 public int Score ;
1918 public int Thrust ;
20- public Missile Missile ;
19+ public Missile Missile = new ( ) ;
2120 public readonly Bullet [ ] Bullets = new Bullet [ Config . MaxBullets ] ;
2221
2322 public void Serialize ( ref readonly BinaryBufferWriter writer )
@@ -34,28 +33,28 @@ public void Serialize(ref readonly BinaryBufferWriter writer)
3433 writer . Write ( in Invincible ) ;
3534 writer . Write ( in Score ) ;
3635 writer . Write ( in Thrust ) ;
36+ writer . Write ( in Missile ) ;
3737
3838 // Caution: WriteStruct not normalize endianness
39- writer . WriteStruct ( in Missile ) ;
4039 writer . WriteStruct ( in Bullets ) ;
4140 }
4241
4342 public void Deserialize ( ref readonly BinaryBufferReader reader )
4443 {
45- Id = reader . ReadByte ( ) ;
46- Active = reader . ReadBoolean ( ) ;
47- Position = reader . ReadVector2 ( ) ;
48- Velocity = reader . ReadVector2 ( ) ;
49- Radius = reader . ReadInt32 ( ) ;
50- Heading = reader . ReadInt32 ( ) ;
51- Health = reader . ReadInt32 ( ) ;
52- FireCooldown = reader . ReadInt32 ( ) ;
53- MissileCooldown = reader . ReadInt32 ( ) ;
54- Invincible = reader . ReadInt32 ( ) ;
55- Score = reader . ReadInt32 ( ) ;
56- Thrust = reader . ReadInt32 ( ) ;
44+ reader . Read ( ref Id ) ;
45+ reader . Read ( ref Active ) ;
46+ reader . Read ( ref Position ) ;
47+ reader . Read ( ref Velocity ) ;
48+ reader . Read ( ref Radius ) ;
49+ reader . Read ( ref Heading ) ;
50+ reader . Read ( ref Health ) ;
51+ reader . Read ( ref FireCooldown ) ;
52+ reader . Read ( ref MissileCooldown ) ;
53+ reader . Read ( ref Invincible ) ;
54+ reader . Read ( ref Score ) ;
55+ reader . Read ( ref Thrust ) ;
56+ reader . Read ( Missile ) ;
5757
58- reader . ReadStruct ( ref Missile ) ;
5958 reader . ReadStruct ( in Bullets ) ;
6059 }
6160}
@@ -67,7 +66,7 @@ public record struct Bullet
6766 public Vector2 Velocity ;
6867}
6968
70- public record struct Missile
69+ public record Missile : IBinarySerializable
7170{
7271 public bool Active ;
7372 public int ExplodeTimeout ;
@@ -77,5 +76,29 @@ public record struct Missile
7776 public int Heading ;
7877 public Vector2 Position ;
7978 public Vector2 Velocity ;
80- public readonly bool IsExploding ( ) => ExplodeTimeout is 0 && HitBoxTime > 0 ;
79+ public bool IsExploding ( ) => ExplodeTimeout is 0 && HitBoxTime > 0 ;
80+
81+ public void Serialize ( ref readonly BinaryBufferWriter writer )
82+ {
83+ writer . Write ( in Active ) ;
84+ writer . Write ( in ExplodeTimeout ) ;
85+ writer . Write ( in HitBoxTime ) ;
86+ writer . Write ( in ExplosionRadius ) ;
87+ writer . Write ( in ProjectileRadius ) ;
88+ writer . Write ( in Heading ) ;
89+ writer . Write ( in Position ) ;
90+ writer . Write ( in Velocity ) ;
91+ }
92+
93+ public void Deserialize ( ref readonly BinaryBufferReader reader )
94+ {
95+ reader . Read ( ref Active ) ;
96+ reader . Read ( ref ExplodeTimeout ) ;
97+ reader . Read ( ref HitBoxTime ) ;
98+ reader . Read ( ref ExplosionRadius ) ;
99+ reader . Read ( ref ProjectileRadius ) ;
100+ reader . Read ( ref Heading ) ;
101+ reader . Read ( ref Position ) ;
102+ reader . Read ( ref Velocity ) ;
103+ }
81104}
0 commit comments