1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using Itmo . ObjectOrientedProgramming . Lab1 . Obstacles ;
5
+ using Itmo . ObjectOrientedProgramming . Lab1 . Obstacles . Interfaces ;
6
+ using Itmo . ObjectOrientedProgramming . Lab1 . Router ;
7
+ using Itmo . ObjectOrientedProgramming . Lab1 . Ships ;
8
+ using Itmo . ObjectOrientedProgramming . Lab1 . Ships . Engines ;
9
+
10
+ namespace Itmo . ObjectOrientedProgramming . Lab1 . Environments ;
11
+
12
+ public class HighDensity : IEnvironment
13
+ {
14
+ private readonly List < IHighDensityObstacle > _obstacles ;
15
+
16
+ public HighDensity ( IEnumerable < IHighDensityObstacle > obstacles )
17
+ {
18
+ _obstacles = obstacles . ToList ( ) ;
19
+ }
20
+
21
+ public HighDensity ( )
22
+ : this ( Enumerable . Empty < IHighDensityObstacle > ( ) )
23
+ {
24
+ }
25
+
26
+ public TraversalResult TraverseEnvironment ( IShip ship , int length )
27
+ {
28
+ if ( ship . JumpEngine == null || ship . JumpEngine . GetRange ( ) < length )
29
+ return new TraversalResult . LostShip ( "Ship lost in a channel" ) ;
30
+
31
+ foreach ( IHighDensityObstacle highDensityObstacle in _obstacles )
32
+ {
33
+ if ( highDensityObstacle is AntimatterFlare && ! ship . PhotonDeflector )
34
+ return new TraversalResult . DeathOfCrew ( ) ;
35
+ highDensityObstacle . GiveDamage ( ship ) ;
36
+ }
37
+
38
+ EngineConsumption consumption = ship . JumpEngine . GetConsumption ( length ) ;
39
+ return new TraversalResult . Success ( consumption . Time , new [ ] { consumption . Consumption } ) ;
40
+ }
41
+
42
+ public void AddObstacle ( IObstacle obstacle )
43
+ {
44
+ if ( obstacle is not IHighDensityObstacle highDensityObstacle )
45
+ throw new ArgumentException ( "You can't add this obstacle to this environment." ) ;
46
+ _obstacles . Add ( highDensityObstacle ) ;
47
+ }
48
+ }
0 commit comments