2020-12-22 03:13:43 +00:00
/ * This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at https : //mozilla.org/MPL/2.0/. */
using System ;
2020-11-27 22:47:33 +00:00
using System.Collections ;
using System.Collections.Generic ;
using System.IO ;
2020-12-12 03:42:58 +00:00
using System.Linq ;
2020-11-29 22:50:12 +00:00
using Cinemachine ;
2020-11-27 22:47:33 +00:00
using MessagePack ;
2020-12-14 02:56:35 +00:00
using TMPro ;
2020-11-29 22:50:12 +00:00
using UniRx ;
2020-11-27 22:47:33 +00:00
using UnityEngine ;
using Unity.Mathematics ;
2020-12-02 16:42:09 +00:00
using UnityEngine.InputSystem ;
2020-12-14 02:56:35 +00:00
using UnityEngine.UI ;
2020-11-27 22:47:33 +00:00
using static Unity . Mathematics . math ;
using float2 = Unity . Mathematics . float2 ;
public class ActionGameManager : MonoBehaviour
{
2021-01-10 03:59:47 +00:00
public PlaceUIElementWorldspace ViewDot ;
public PlaceUIElementWorldspace TargetIndicator ;
public Prototype LockIndicator ;
public PlaceUIElementWorldspace [ ] Crosshairs ;
2021-01-08 12:44:10 +00:00
public EventLog EventLog ;
2020-12-01 00:16:11 +00:00
public GameSettings Settings ;
2020-11-29 22:50:12 +00:00
public SectorRenderer SectorRenderer ;
2020-12-14 02:56:35 +00:00
// public MapView MapView;
2020-12-22 03:13:43 +00:00
public CinemachineVirtualCamera DockCamera ;
2020-11-29 22:50:12 +00:00
public CinemachineVirtualCamera FollowCamera ;
2020-12-02 21:12:25 +00:00
public GameObject GameplayUI ;
2020-12-14 02:56:35 +00:00
public MenuPanel Menu ;
2020-12-27 22:18:22 +00:00
public MapRenderer MenuMap ;
2021-01-05 02:56:17 +00:00
public SchematicDisplay SchematicDisplay ;
2020-12-14 02:56:35 +00:00
public InventoryMenu Inventory ;
2020-12-27 22:18:22 +00:00
public InventoryPanel ShipPanel ;
2020-12-31 19:53:16 +00:00
public ConfirmationDialog ConfirmationDialog ;
2020-12-22 03:13:43 +00:00
public float2 Sensitivity ;
2020-12-14 02:56:35 +00:00
2020-12-02 16:42:09 +00:00
//public PlayerInput Input;
2020-11-29 22:50:12 +00:00
2020-12-22 03:13:43 +00:00
// private CinemachineFramingTransposer _transposer;
// private CinemachineComposer _composer;
2020-11-29 22:50:12 +00:00
private DirectoryInfo _filePath ;
private bool _editMode ;
private float _time ;
2020-12-02 16:42:09 +00:00
private AetheriaInput _input ;
private int _zoomLevelIndex ;
2020-12-19 02:37:13 +00:00
private Ship _currentShip ;
2021-01-05 02:56:17 +00:00
// private ShipInput _shipInput;
2020-12-22 03:13:43 +00:00
private float2 _shipYawPitch ;
private float3 _viewDirection ;
2021-01-10 03:59:47 +00:00
private ( HardpointData [ ] hardpoints , Transform [ ] barrels , PlaceUIElementWorldspace crosshair ) [ ] ArticulationGroups ;
private ( TargetLock targetLock , PlaceUIElementWorldspace indicator , Rotate spin ) [ ] LockingIndicators ;
2020-12-18 13:48:03 +00:00
public List < Ship > PlayerShips { get ; } = new List < Ship > ( ) ;
2020-11-29 22:50:12 +00:00
2020-12-12 03:42:58 +00:00
public DatabaseCache ItemData { get ; private set ; }
public ItemManager ItemManager { get ; private set ; }
2020-11-29 22:50:12 +00:00
public Zone Zone { get ; private set ; }
2020-11-27 22:47:33 +00:00
2021-01-08 12:44:10 +00:00
private readonly ( float2 direction , string name ) [ ] _directions = {
( float2 ( 0 , 1 ) , "Front" ) ,
( float2 ( 1 , 0 ) , "Right" ) ,
( float2 ( - 1 , 0 ) , "Left" ) ,
( float2 ( 0 , - 1 ) , "Rear" )
} ;
2020-12-19 02:37:13 +00:00
2020-11-27 22:47:33 +00:00
void Start ( )
{
2020-12-22 03:13:43 +00:00
// _transposer = DockCamera.GetCinemachineComponent<CinemachineFramingTransposer>();
// _composer = DockCamera.GetCinemachineComponent<CinemachineComposer>();
2021-01-05 02:56:17 +00:00
//_lookAt = new GameObject().transform;
2020-12-22 03:13:43 +00:00
2020-11-29 22:50:12 +00:00
ConsoleController . MessageReceiver = this ;
_filePath = new DirectoryInfo ( Application . dataPath ) . Parent . CreateSubdirectory ( "GameData" ) ;
2020-12-12 03:42:58 +00:00
ItemData = new DatabaseCache ( ) ;
ItemData . Load ( _filePath . FullName ) ;
ItemManager = new ItemManager ( ItemData , Settings . GameplaySettings , Debug . Log ) ;
2020-11-27 22:47:33 +00:00
2020-11-29 22:50:12 +00:00
var zoneFile = Path . Combine ( _filePath . FullName , "Home.zone" ) ;
2020-11-27 22:47:33 +00:00
// If the game has already been run, there will be a Home file containing a ZonePack; if not, generate one
var zonePack = File . Exists ( zoneFile ) ?
2020-12-12 03:42:58 +00:00
MessagePackSerializer . Deserialize < ZonePack > ( File . ReadAllBytes ( zoneFile ) ) :
2020-11-27 22:47:33 +00:00
ZoneGenerator . GenerateZone (
2020-12-01 00:16:11 +00:00
settings : Settings . ZoneSettings ,
2020-12-10 23:20:38 +00:00
// mapLayers: Context.MapLayers,
// resources: Context.Resources,
2020-12-01 00:16:11 +00:00
mass : Settings . DefaultZoneMass ,
radius : Settings . DefaultZoneRadius
2020-11-27 22:47:33 +00:00
) ;
2020-12-01 00:16:11 +00:00
Zone = new Zone ( Settings . PlanetSettings , zonePack ) ;
2020-12-22 03:13:43 +00:00
SectorRenderer . ItemManager = ItemManager ;
2020-11-29 22:50:12 +00:00
SectorRenderer . LoadZone ( Zone ) ;
2020-12-02 16:42:09 +00:00
_input = new AetheriaInput ( ) ;
_input . Player . Enable ( ) ;
2020-12-02 21:12:25 +00:00
_input . Global . Enable ( ) ;
_input . UI . Enable ( ) ;
2020-12-02 16:42:09 +00:00
_zoomLevelIndex = Settings . DefaultMinimapZoom ;
_input . Player . MinimapZoom . performed + = context = >
{
_zoomLevelIndex = ( _zoomLevelIndex + 1 ) % Settings . MinimapZoomLevels . Length ;
SectorRenderer . MinimapDistance = Settings . MinimapZoomLevels [ _zoomLevelIndex ] ;
} ;
2020-12-18 13:48:03 +00:00
_input . Global . MapToggle . performed + = context = >
{
if ( Menu . gameObject . activeSelf & & Menu . CurrentTab = = MenuTab . Map )
{
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . Locked ;
_input . Player . Enable ( ) ;
2020-12-18 13:48:03 +00:00
Menu . gameObject . SetActive ( false ) ;
2021-01-08 12:44:10 +00:00
if ( _currentShip ! = null & & _currentShip . Parent = = null )
{
GameplayUI . SetActive ( true ) ;
SchematicDisplay . ShowShip ( _currentShip ) ;
ShipPanel . Display ( _currentShip ) ;
}
2020-12-18 13:48:03 +00:00
return ;
}
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . None ;
_input . Player . Disable ( ) ;
2020-12-27 22:18:22 +00:00
GameplayUI . SetActive ( false ) ;
2020-12-18 13:48:03 +00:00
Menu . ShowTab ( MenuTab . Map ) ;
2021-01-06 14:56:39 +00:00
MenuMap . Position = _currentShip . Position . xz ;
2020-12-18 13:48:03 +00:00
} ;
_input . Global . Inventory . performed + = context = >
{
if ( Menu . gameObject . activeSelf & & Menu . CurrentTab = = MenuTab . Inventory )
{
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . Locked ;
_input . Player . Enable ( ) ;
2020-12-18 13:48:03 +00:00
Menu . gameObject . SetActive ( false ) ;
2021-01-08 12:44:10 +00:00
if ( _currentShip ! = null & & _currentShip . Parent = = null )
{
GameplayUI . SetActive ( true ) ;
SchematicDisplay . ShowShip ( _currentShip ) ;
ShipPanel . Display ( _currentShip ) ;
}
2020-12-18 13:48:03 +00:00
return ;
}
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . None ;
_input . Player . Disable ( ) ;
2020-12-18 13:48:03 +00:00
Menu . ShowTab ( MenuTab . Inventory ) ;
2020-12-27 22:18:22 +00:00
GameplayUI . SetActive ( false ) ;
var cargo = _currentShip . CargoBays . FirstOrDefault ( ) ;
if ( cargo ! = null )
{
Inventory . GetPanel . Display ( cargo ) ;
}
else Inventory . GetPanel . Clear ( ) ;
Inventory . GetPanel . Display ( _currentShip ) ;
2020-12-18 13:48:03 +00:00
} ;
2020-12-02 21:12:25 +00:00
2020-12-22 03:13:43 +00:00
_input . Global . Dock . performed + = context = >
{
if ( _currentShip . Parent = = null ) Dock ( ) ;
else Undock ( ) ;
} ;
2021-01-10 03:59:47 +00:00
_input . Player . TargetReticle . performed + = context = >
{
var underReticle = Zone . Entities . MaxBy ( x = > dot ( normalize ( x . Position - _currentShip . Position ) , _currentShip . LookDirection ) ) ;
_currentShip . Target . Value = _currentShip . Target . Value = = underReticle ? null : underReticle ;
TargetIndicator . gameObject . SetActive ( _currentShip . Target . Value ! = null ) ;
} ;
_input . Player . TargetNearest . performed + = context = >
{
_currentShip . Target . Value = Zone . Entities . MaxBy ( x = > length ( x . Position - _currentShip . Position ) ) ;
} ;
_input . Player . TargetNext . performed + = context = >
{
var targets = Zone . Entities . OrderBy ( x = > length ( x . Position - _currentShip . Position ) ) . ToArray ( ) ;
if ( _currentShip . Target . Value ! = null )
{
var currentTargetIndex = Array . IndexOf ( targets , _currentShip . Target . Value ) ;
_currentShip . Target . Value = targets [ ( currentTargetIndex + 1 ) % targets . Length ] ;
}
} ;
_input . Player . TargetPrevious . performed + = context = >
{
var targets = Zone . Entities . OrderBy ( x = > length ( x . Position - _currentShip . Position ) ) . ToArray ( ) ;
if ( _currentShip . Target . Value ! = null )
{
var currentTargetIndex = Array . IndexOf ( targets , _currentShip . Target . Value ) ;
_currentShip . Target . Value = targets [ ( currentTargetIndex + targets . Length - 1 ) % targets . Length ] ;
}
} ;
2021-01-05 02:56:17 +00:00
#region Trigger Groups
_input . Player . PreviousWeaponGroup . performed + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
SchematicDisplay . SelectedGroupIndex - - ;
} ;
_input . Player . NextWeaponGroup . performed + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
SchematicDisplay . SelectedGroupIndex + + ;
} ;
_input . Player . PreviousWeapon . performed + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
SchematicDisplay . SelectedItemIndex - - ;
} ;
_input . Player . NextWeapon . performed + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
SchematicDisplay . SelectedItemIndex + + ;
} ;
_input . Player . ToggleWeaponGroup . performed + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
var item = SchematicDisplay . SchematicItems [ SchematicDisplay . SelectedItemIndex ] ;
if ( _currentShip . TriggerGroups [ SchematicDisplay . SelectedGroupIndex ] . Contains ( item . Item ) )
_currentShip . TriggerGroups [ SchematicDisplay . SelectedGroupIndex ] . Remove ( item . Item ) ;
else _currentShip . TriggerGroups [ SchematicDisplay . SelectedGroupIndex ] . Add ( item . Item ) ;
SchematicDisplay . UpdateTriggerGroups ( ) ;
} ;
_input . Player . FireGroup1 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 0 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup1 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 0 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
_input . Player . FireGroup2 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 1 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup2 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 1 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
_input . Player . FireGroup3 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 2 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup3 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 2 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
_input . Player . FireGroup4 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 3 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup4 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 3 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
_input . Player . FireGroup5 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 4 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup5 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 4 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
_input . Player . FireGroup6 . started + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 5 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = true ;
behaviors . Trigger ? . Pull ( ) ;
}
}
} ;
_input . Player . FireGroup6 . canceled + = context = >
{
if ( _currentShip . Parent ! = null ) return ;
foreach ( var item in _currentShip . TriggerGroups [ 5 ] )
{
foreach ( var behaviors in item . BehaviorGroups )
{
if ( behaviors . Switch ! = null )
behaviors . Switch . Activated = false ;
}
}
} ;
#endregion
2020-12-22 03:13:43 +00:00
// ConsoleController.AddCommand("editmode", _ => ToggleEditMode());
2020-12-01 00:16:11 +00:00
ConsoleController . AddCommand ( "savezone" , args = >
{
if ( args . Length > 0 )
Zone . Data . Name = args [ 0 ] ;
SaveZone ( ) ;
} ) ;
2020-12-14 02:56:35 +00:00
2020-12-18 13:48:03 +00:00
var stationType = ItemData . GetAll < HullData > ( ) . First ( x = > x . HullType = = HullType . Station ) ;
var stationHull = ItemManager . CreateInstance ( stationType , 0 , 1 ) as EquippableItem ;
2020-12-23 22:38:30 +00:00
var stationParent = Zone . PlanetInstances . Values . OrderByDescending ( p = > p . BodyData . Mass . Value ) . ElementAt ( 3 ) ;
2021-01-06 14:56:39 +00:00
var stationParentOrbit = stationParent . Orbit . Data . ID ;
var stationParentPos = Zone . GetOrbitPosition ( stationParentOrbit ) ;
var stationPos = stationParentPos + ItemManager . Random . NextFloat2Direction ( ) * stationParent . GravityWellRadius . Value * . 1f ;
var stationOrbit = Zone . CreateOrbit ( stationParentOrbit , stationPos ) ;
2020-12-18 13:48:03 +00:00
var station = new OrbitalEntity ( ItemManager , Zone , stationHull , stationOrbit . ID ) ;
Zone . Entities . Add ( station ) ;
2020-12-19 02:37:13 +00:00
var dockingBayData = ItemData . GetAll < DockingBayData > ( ) . First ( ) ;
var dockingBay = ItemManager . CreateInstance ( dockingBayData , 1 , 1 ) as EquippableItem ;
station . TryEquip ( dockingBay ) ;
2020-12-22 03:13:43 +00:00
station . Active = true ;
2021-01-06 14:56:39 +00:00
var reactorData = ItemData . GetAll < GearData > ( ) . First ( x = > x . HardpointType = = HardpointType . Reactor & & x . Shape . Height = = 2 ) ;
var autocannonData = ItemData . GetAll < GearData > ( ) . First ( x = > x . Name = = "Autocannon" ) ;
var ammoData = ItemData . GetAll < SimpleCommodityData > ( ) . First ( x = > x . Name = = "AC2 Ammo" ) ;
var turretType = ItemData . GetAll < HullData > ( ) . First ( x = > x . HullType = = HullType . Turret ) ;
var turretHull = ItemManager . CreateInstance ( turretType , 0 , 1 ) as EquippableItem ;
var turretParent = Zone . PlanetInstances . Values . OrderByDescending ( p = > p . BodyData . Mass . Value ) . ElementAt ( 5 ) ;
var turretParentOrbit = turretParent . Orbit . Data . ID ;
var turretParentPos = Zone . GetOrbitPosition ( turretParentOrbit ) ;
var turretPos = turretParentPos + ItemManager . Random . NextFloat2Direction ( ) * turretParent . GravityWellRadius . Value * . 05f ;
var turretOrbit = Zone . CreateOrbit ( turretParentOrbit , turretPos ) ;
var turret = new OrbitalEntity ( ItemManager , Zone , turretHull , turretOrbit . ID ) ;
turret . TryEquip ( ItemManager . CreateInstance ( reactorData , . 5f , 1 ) as EquippableItem ) ;
var controlModuleData = ItemData . GetAll < GearData > ( ) . First ( x = > x . Name = = "Murder Module" ) ;
turret . TryEquip ( ItemManager . CreateInstance ( controlModuleData , . 5f , 1 ) as EquippableItem ) ;
turret . TryEquip ( ItemManager . CreateInstance ( autocannonData , . 5f , 1 ) as EquippableItem ) ;
turret . TryEquip ( ItemManager . CreateInstance ( autocannonData , . 5f , 1 ) as EquippableItem ) ;
var cargo1Data = ItemData . GetAll < CargoBayData > ( ) . First ( x = > x . Name = = "Cargo Bay 1x1" ) ;
var cargo1 = ItemManager . CreateInstance ( cargo1Data , . 5f , 1 ) ;
turret . TryEquip ( cargo1 as EquippableItem ) ;
turret . CargoBays . First ( ) . TryStore ( ItemManager . CreateInstance ( ammoData , 400 ) ) ;
turret . Active = true ;
Zone . Entities . Add ( turret ) ;
2020-12-22 03:13:43 +00:00
var hullType = ItemData . GetAll < HullData > ( ) . First ( x = > x . HullType = = HullType . Ship ) ;
var shipHull = ItemManager . CreateInstance ( hullType , 0 , 1 ) as EquippableItem ;
var ship = new Ship ( ItemManager , Zone , shipHull ) ;
PlayerShips . Add ( ship ) ;
_currentShip = ship ;
2021-01-08 12:44:10 +00:00
ship . ArmorDamage . Subscribe ( hit = >
{
var direction = _directions . MaxBy ( d = > dot ( d . direction , normalize ( hit . pos - hullType . Shape . CenterOfMass ) ) ) ;
EventLog . LogMessage (
$"{direction.name} armor hit for {hit.damage} damage, currently {((int) (ship.Armor[hit.pos.x, hit.pos.y] / hullType.Armor * 100)).ToString()}%" ,
Settings . ArmorHitColor ) ;
} ) ;
ship . HullDamage . Subscribe ( hit = > EventLog . LogMessage ( $"Hull Structure hit for {hit} damage" ) ) ;
ship . ItemDamage . Subscribe ( hit = >
{
var data = ItemManager . GetData ( hit . item . EquippableItem ) ;
EventLog . LogMessage (
$"{hit.item.EquippableItem.Name} hit for {hit.damage} damage, currently {((int) (hit.item.EquippableItem.Durability / data.Durability * 100)).ToString()}%" ,
data . HardpointType = = HardpointType . Thermal ? Settings . ThermalHitColor : Settings . GearHitColor ) ;
} ) ;
ship . HardpointDamage . Subscribe ( hit = > EventLog . LogMessage (
$"{Enum.GetName(typeof(HardpointType), hit.hardpoint.Type)} hardpoint hit for {hit.damage} damage, currently {((int) (ship.HardpointArmor[hit.hardpoint] / hit.hardpoint.Armor * 100)).ToString()}%" ,
Settings . HardpointHitColor ) ) ;
2020-12-20 03:04:52 +00:00
var thrusterData = ItemData . GetAll < GearData > ( ) . First ( x = > x . HardpointType = = HardpointType . Thruster & & x . Shape . Height = = 1 ) ;
for ( int i = 0 ; i < 8 ; i + + )
{
2020-12-22 03:13:43 +00:00
ship . TryEquip ( ItemManager . CreateInstance ( thrusterData , . 5f , 1 ) as EquippableItem ) ;
//dockingCargo.TryStore(ItemManager.CreateInstance(thrusterData, .5f, 1));
2020-12-20 03:04:52 +00:00
}
2020-12-22 03:13:43 +00:00
ship . TryEquip ( ItemManager . CreateInstance ( reactorData , . 5f , 1 ) as EquippableItem ) ;
2020-12-31 19:53:16 +00:00
var cockpitData = ItemData . GetAll < GearData > ( ) . First ( x = > x . Behaviors . Any ( b = > b is CockpitData ) ) ;
ship . TryEquip ( ItemManager . CreateInstance ( cockpitData , . 5f , 1 ) as EquippableItem ) ;
2021-01-05 02:56:17 +00:00
ship . TryEquip ( ItemManager . CreateInstance ( autocannonData , . 5f , 1 ) as EquippableItem ) ;
ship . TryEquip ( ItemManager . CreateInstance ( autocannonData , . 5f , 1 ) as EquippableItem ) ;
2021-01-10 03:59:47 +00:00
var lrmmData = ItemData . GetAll < GearData > ( ) . First ( x = > x . Name = = "LRMM72" ) ;
ship . TryEquip ( ItemManager . CreateInstance ( lrmmData , . 5f , 1 ) as EquippableItem ) ;
ship . TryEquip ( ItemManager . CreateInstance ( lrmmData , . 5f , 1 ) as EquippableItem ) ;
2021-01-05 02:56:17 +00:00
var cargoData = ItemData . GetAll < CargoBayData > ( ) . First ( x = > x . Name = = "Cargo Bay 4x4" ) ;
ship . TryEquip ( ItemManager . CreateInstance ( cargoData , . 5f , 1 ) as EquippableItem ) ;
2021-01-06 14:56:39 +00:00
ship . CargoBays . First ( ) . TryStore ( ItemManager . CreateInstance ( ammoData , 200 ) ) ;
2020-12-22 03:13:43 +00:00
//dockingCargo.TryStore(ItemManager.CreateInstance(reactorData, .5f, 1));
2020-12-18 13:48:03 +00:00
2020-12-20 03:04:52 +00:00
Dock ( ) ;
2020-12-19 02:37:13 +00:00
2020-12-14 02:56:35 +00:00
// MapButton.onClick.AddListener(() =>
// {
// if (_currentMenuTabButton == MapPanel) return;
//
// _currentMenuTabButton.GetComponent<TextMeshProUGUI>().color = Color.white;
// _currentMenuTabButton = MapButton;
// });
//
//InventoryPanel.Display(ItemManager, entity);
2020-11-27 22:47:33 +00:00
}
2020-12-20 03:04:52 +00:00
public void Dock ( )
{
2020-12-22 03:13:43 +00:00
if ( _currentShip . Parent ! = null ) return ;
foreach ( var entity in Zone . Entities . ToArray ( ) )
2020-12-20 03:04:52 +00:00
{
2021-01-06 14:56:39 +00:00
if ( lengthsq ( entity . Position . xz - _currentShip . Position . xz ) <
2020-12-20 03:04:52 +00:00
Settings . GameplaySettings . DockingDistance * Settings . GameplaySettings . DockingDistance )
{
var bay = entity . TryDock ( _currentShip ) ;
if ( bay ! = null )
{
2020-12-22 03:13:43 +00:00
DockCamera . enabled = true ;
FollowCamera . enabled = false ;
_currentShip . Active = false ;
var orbital = ( OrbitalEntity ) entity ;
2021-01-05 02:56:17 +00:00
DockCamera . Follow = SectorRenderer . EntityInstances [ orbital ] . Transform ;
2020-12-22 03:13:43 +00:00
var parentOrbit = Zone . Orbits [ orbital . OrbitData ] . Data . Parent ;
var parentPlanet = SectorRenderer . Planets [ Zone . Planets . FirstOrDefault ( p = > p . Value . Orbit = = parentOrbit ) . Key ] ;
DockCamera . LookAt = parentPlanet . Body . transform ;
Menu . ShowTab ( MenuTab . Inventory ) ;
2020-12-20 03:04:52 +00:00
Inventory . GetPanel . Display ( bay ) ;
Inventory . GetPanel . Display ( _currentShip ) ;
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . None ;
_input . Player . Disable ( ) ;
2020-12-27 22:18:22 +00:00
GameplayUI . SetActive ( false ) ;
2021-01-10 03:59:47 +00:00
if ( LockingIndicators ! = null ) foreach ( var ( _ , indicator , _ ) in LockingIndicators )
indicator . GetComponent < Prototype > ( ) . ReturnToPool ( ) ;
2020-12-20 03:04:52 +00:00
}
}
}
}
2020-12-22 03:13:43 +00:00
public void Undock ( )
{
if ( _currentShip . Parent = = null ) return ;
2020-12-31 19:53:16 +00:00
if ( _currentShip . GetBehavior < Cockpit > ( ) = = null )
{
2021-01-10 03:59:47 +00:00
ConfirmationDialog . Clear ( ) ;
2020-12-31 19:53:16 +00:00
ConfirmationDialog . Title . text = "Can't undock. Missing cockpit component!" ;
ConfirmationDialog . Show ( ) ;
}
else if ( _currentShip . GetBehavior < Thruster > ( ) = = null )
{
2021-01-10 03:59:47 +00:00
ConfirmationDialog . Clear ( ) ;
2020-12-31 19:53:16 +00:00
ConfirmationDialog . Title . text = "Can't undock. Missing thruster component!" ;
ConfirmationDialog . Show ( ) ;
}
else if ( _currentShip . GetBehavior < Reactor > ( ) = = null )
{
2021-01-10 03:59:47 +00:00
ConfirmationDialog . Clear ( ) ;
2020-12-31 19:53:16 +00:00
ConfirmationDialog . Title . text = "Can't undock. Missing reactor component!" ;
ConfirmationDialog . Show ( ) ;
}
else if ( _currentShip . Parent . TryUndock ( _currentShip ) )
2020-12-22 03:13:43 +00:00
{
Menu . gameObject . SetActive ( false ) ;
_currentShip . Active = true ;
2021-01-05 02:56:17 +00:00
//_shipInput = new ShipInput(_input.Player, _currentShip);
2020-12-22 03:13:43 +00:00
DockCamera . enabled = false ;
FollowCamera . enabled = true ;
2021-01-05 02:56:17 +00:00
FollowCamera . LookAt = SectorRenderer . EntityInstances [ _currentShip ] . LookAtPoint ;
FollowCamera . Follow = SectorRenderer . EntityInstances [ _currentShip ] . Transform ;
2021-01-10 03:59:47 +00:00
ArticulationGroups = _currentShip . Equipment
. Where ( item = > item . Behaviors . Any ( x = > x . Data is WeaponData & & ! ( x . Data is LauncherData ) ) )
. GroupBy ( item = > SectorRenderer . EntityInstances [ _currentShip ]
. GetBarrel ( _currentShip . Hardpoints [ item . Position . x , item . Position . y ] )
. GetComponentInParent < ArticulationPoint > ( ) ? . Group ? ? - 1 )
. Select ( ( group , index ) = > {
return (
group . Select ( item = > _currentShip . Hardpoints [ item . Position . x , item . Position . y ] ) . ToArray ( ) ,
group . Select ( item = > SectorRenderer . EntityInstances [ _currentShip ] . GetBarrel ( _currentShip . Hardpoints [ item . Position . x , item . Position . y ] ) ) . ToArray ( ) ,
Crosshairs [ index ]
) ;
} ) . ToArray ( ) ;
foreach ( var crosshair in Crosshairs )
crosshair . gameObject . SetActive ( false ) ;
foreach ( var group in ArticulationGroups )
group . crosshair . gameObject . SetActive ( true ) ;
LockingIndicators = _currentShip . GetBehaviors < TargetLock > ( ) . Select ( x = >
{
var i = LockIndicator . Instantiate < PlaceUIElementWorldspace > ( ) ;
return ( x , i , i . GetComponent < Rotate > ( ) ) ;
} ) . ToArray ( ) ;
2020-12-22 03:13:43 +00:00
Cursor . lockState = CursorLockMode . Locked ;
2020-12-27 22:18:22 +00:00
GameplayUI . SetActive ( true ) ;
2020-12-22 03:13:43 +00:00
_input . Player . Enable ( ) ;
2021-01-10 03:59:47 +00:00
ShipPanel . Display ( _currentShip , true ) ;
2021-01-05 02:56:17 +00:00
SchematicDisplay . ShowShip ( _currentShip ) ;
2020-12-22 03:13:43 +00:00
}
2020-12-31 19:53:16 +00:00
else
{
ConfirmationDialog . Title . text = "Can't undock. Must empty docking bay!" ;
ConfirmationDialog . Show ( ) ;
}
2020-12-22 03:13:43 +00:00
}
2020-12-01 00:16:11 +00:00
public void SaveZone ( ) = > File . WriteAllBytes (
Path . Combine ( _filePath . FullName , $"{Zone.Data.Name}.zone" ) , MessagePackSerializer . Serialize ( Zone . Pack ( ) ) ) ;
2020-12-22 03:13:43 +00:00
// public void ToggleEditMode()
// {
// _editMode = !_editMode;
// FollowCamera.gameObject.SetActive(!_editMode);
// TopDownCamera.gameObject.SetActive(_editMode);
// }
2020-12-19 02:37:13 +00:00
public IEnumerable < EquippedCargoBay > AvailableCargoBays ( )
{
if ( _currentShip . Parent ! = null )
{
foreach ( var bay in _currentShip . Parent . DockingBays )
{
if ( PlayerShips . Contains ( bay . DockedShip ) ) yield return bay ;
}
}
}
public IEnumerable < Ship > AvailableShips ( )
{
if ( _currentShip . Parent ! = null )
{
foreach ( var ship in PlayerShips )
{
if ( _currentShip . Parent . Children . Contains ( ship ) ) yield return ship ;
}
}
else
yield return _currentShip ;
}
2020-11-27 22:47:33 +00:00
2020-11-29 22:50:12 +00:00
void Update ( )
2020-11-27 22:47:33 +00:00
{
2020-11-29 22:50:12 +00:00
if ( ! _editMode )
{
_time + = Time . deltaTime ;
2020-12-12 03:42:58 +00:00
ItemManager . Time = Time . time ;
2021-01-05 02:56:17 +00:00
if ( _currentShip . Parent = = null )
2020-12-22 03:13:43 +00:00
{
var look = _input . Player . Look . ReadValue < Vector2 > ( ) ;
_shipYawPitch = float2 ( _shipYawPitch . x + look . x * Sensitivity . x , clamp ( _shipYawPitch . y + look . y * Sensitivity . y , - . 45f * PI , . 45f * PI ) ) ;
_viewDirection = mul ( float3 ( 0 , 0 , 1 ) , Unity . Mathematics . float3x3 . Euler ( float3 ( _shipYawPitch . yx , 0 ) , RotationOrder . YXZ ) ) ;
2021-01-05 02:56:17 +00:00
_currentShip . LookDirection = _viewDirection ;
_currentShip . MovementDirection = _input . Player . Move . ReadValue < Vector2 > ( ) ;
2020-12-22 03:13:43 +00:00
}
2020-11-29 22:50:12 +00:00
}
2020-12-01 00:16:11 +00:00
Zone . Update ( _time , Time . deltaTime ) ;
2020-12-01 20:38:28 +00:00
SectorRenderer . Time = _time ;
2020-11-27 22:47:33 +00:00
}
2021-01-10 03:59:47 +00:00
private void LateUpdate ( )
{
UpdateTargetIndicators ( ) ;
}
private void UpdateTargetIndicators ( )
{
if ( _currentShip = = null | | _currentShip . Parent ! = null ) return ;
ViewDot . Target = SectorRenderer . EntityInstances [ _currentShip ] . LookAtPoint . position ;
if ( _currentShip . Target . Value ! = null )
TargetIndicator . Target = _currentShip . Target . Value . Position ;
var distance = length ( ( float3 ) ViewDot . Target - _currentShip . Position ) ;
foreach ( var ( _ , barrels , crosshair ) in ArticulationGroups )
{
var averagePosition = Vector3 . zero ;
foreach ( var barrel in barrels )
averagePosition + = barrel . position + barrel . forward * distance ;
averagePosition / = barrels . Length ;
crosshair . Target = averagePosition ;
}
foreach ( var ( targetLock , indicator , spin ) in LockingIndicators )
{
var showLockingIndicator = targetLock . Lock > . 01f & & _currentShip . Target . Value ! = null ;
indicator . gameObject . SetActive ( showLockingIndicator ) ;
if ( showLockingIndicator )
indicator . Target = _currentShip . Target . Value . Position ;
indicator . NoiseAmplitude = Settings . GameplaySettings . LockIndicatorNoiseAmplitude * ( 1 - targetLock . Lock ) ;
indicator . NoiseFrequency = Settings . GameplaySettings . LockIndicatorFrequency . Evaluate ( targetLock . Lock ) ;
spin . Speed = Settings . GameplaySettings . LockSpinSpeed . Evaluate ( targetLock . Lock ) ;
}
}
2020-12-22 03:13:43 +00:00
}