Skip to content

Commit

Permalink
Add JumpOrder
Browse files Browse the repository at this point in the history
Fleets can now be given the Jump order to travel a known jump gate
  • Loading branch information
behindcurtain3 committed Apr 18, 2024
1 parent 21a0e32 commit 52681b5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
85 changes: 85 additions & 0 deletions Pulsar4X/GameEngine/Engine/Orders/Actions/JumpOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Linq;
using Pulsar4X.Datablobs;

namespace Pulsar4X.Engine.Orders;

public class JumpOrder : EntityCommand
{
public override ActionLaneTypes ActionLanes => ActionLaneTypes.InstantOrder | ActionLaneTypes.Movement;

public override bool IsBlocking => false;

public override string Name { get; } = "Travel through jump gate";

public override string Details { get; } = "Travel through the specified jump gate";

Entity _factionEntity;
Entity _entityCommanding;

public JumpPointDB JumpGate { get; private set; }

internal override Entity EntityCommanding { get { return _entityCommanding; } }
bool _isFinished = false;
string NewName;

public static void CreateAndExecute(Game game, Entity faction, Entity fleetEntity, JumpPointDB jumpGate)
{
var cmd = new JumpOrder()
{
RequestingFactionGuid = faction.Id,
EntityCommandingGuid = fleetEntity.Id,
CreatedDate = fleetEntity.Manager.ManagerSubpulses.StarSysDateTime,
JumpGate = jumpGate
};

game.OrderHandler.HandleOrder(cmd);
}

internal override void Execute(DateTime atDateTime)
{
if(JumpGate.OwningEntity.Manager.TryGetGlobalEntityById(JumpGate.DestinationId, out var destinationEntity))
{
if(!_entityCommanding.TryGetDatablob<FleetDB>(out var fleetDB)) return;

var destinationPositionDB = destinationEntity.GetDataBlob<PositionDB>();
var ships = fleetDB.Children.Where(c => c.HasDataBlob<ShipInfoDB>());

// Transfer each ship in the fleet
foreach(var ship in ships)
{
// Transfer to the new system
destinationEntity.Manager.Transfer(ship);

// Update the position
var positionDB = ship.GetDataBlob<PositionDB>();
positionDB.AbsolutePosition = destinationPositionDB.AbsolutePosition;
positionDB.SetParent(destinationEntity);
}

// Transfer the fleet entity
destinationEntity.Manager.Transfer(_entityCommanding);
}

_isFinished = true;
}

public override bool IsFinished()
{
return _isFinished;
}

internal override bool IsValidCommand(Game game)
{
if (CommandHelpers.IsCommandValid(game.GlobalManager, RequestingFactionGuid, EntityCommandingGuid, out _factionEntity, out _entityCommanding))
{
return true;
}
return false;
}

public override EntityCommand Clone()
{
throw new NotImplementedException();
}
}
26 changes: 25 additions & 1 deletion Pulsar4X/Pulsar4X.Client/EmpireManagement/FleetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ private enum IssueOrderType
{
MoveTo,
GeoSurvey,
JPSurvey
JPSurvey,
Jump,
}

private IssueOrderType selectedIssueOrderType = IssueOrderType.MoveTo;
Expand Down Expand Up @@ -286,6 +287,10 @@ private void DisplayTabs()
{
selectedIssueOrderType = IssueOrderType.JPSurvey;
}
if(ImGui.Selectable("Jump...", selectedIssueOrderType == IssueOrderType.Jump))
{
selectedIssueOrderType = IssueOrderType.Jump;
}

ImGui.EndChild();
}
Expand Down Expand Up @@ -343,6 +348,25 @@ private void DisplayTabs()
}
}
break;
case IssueOrderType.Jump:
var jumpGates = _uiState.SelectedSystem.GetAllDataBlobsOfType<JumpPointDB>();
foreach(var jumpGateDB in jumpGates)
{
if(!jumpGateDB.IsDiscovered.Contains(_uiState.Faction.Id)) continue;

var name = jumpGateDB.OwningEntity?.GetName(_uiState.Faction.Id);
if(ImGui.Button(name + "###jump-gate-button-" + name))
{
if(jumpGateDB.OwningEntity != null)
{
var order = MoveFleetTowardsTargetOrder.CreateCommand(SelectedFleet, jumpGateDB.OwningEntity);
_uiState.Game.OrderHandler.HandleOrder(order);

JumpOrder.CreateAndExecute(_uiState.Game, _uiState.Faction, SelectedFleet, jumpGateDB);
}
}
}
break;
}

ImGui.EndChild();
Expand Down

0 comments on commit 52681b5

Please sign in to comment.