Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/CompanyExcursions.cs

92 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace BigShipTycoon
{
[Serializable]
internal sealed class ShorePortPlan
{
public int Port;
public ShoreOffer[] Offers;
internal static ShoreOffer[] Copy(ShoreOffer[] offers)
{
if (offers == null || offers.Length == 0)
return Array.Empty<ShoreOffer>();
var copy = new ShoreOffer[offers.Length];
for (int i = 0; i < offers.Length; i++)
{
ShoreOffer offer = offers[i];
copy[i] = new ShoreOffer { Tour = offer.Tour, Price = offer.Price, Capacity = offer.Capacity };
}
return copy;
}
}
public sealed partial class Ship
{
[SerializeField] private List<ShorePortPlan> shorePlans;
[SerializeReference] public ShoreCall ShoreCall;
internal ShoreOffer[] ExcursionsFor(int port) => ShorePortPlan.Copy(shorePlans?.Find(plan => plan.Port == port)?.Offers);
internal void SetExcursions(int port, ShoreOffer[] offers)
{
shorePlans ??= new List<ShorePortPlan>();
shorePlans.RemoveAll(plan => plan.Port == port);
if (offers.Length > 0)
shorePlans.Add(new ShorePortPlan { Port = port, Offers = ShorePortPlan.Copy(offers) });
}
internal bool ValidShorePlans()
{
if (shorePlans == null)
return true;
var ports = new HashSet<int>();
foreach (ShorePortPlan plan in shorePlans)
{
if (plan == null || !ports.Add(plan.Port) || plan.Offers == null || plan.Offers.Length == 0
|| !ShoreExcursions.ValidOffers(plan.Port, plan.Offers))
return false;
}
return true;
}
}
public sealed partial class Company
{
private void BookShoreCall(Ship ship, Voyage quote)
{
if (quote.ShoreOffers is not { Length: > 0 })
return;
ShoreCall call = ShoreExcursions.Create(quote, quote.ShoreOffers);
if (call == null)
return;
call.CallId = quote.ShoreCallId = Voyages + 1;
ship.ShoreCall = call;
}
internal ShoreReport CompleteShoreCall(Ship ship)
{
ShoreReport report = ship.ShoreCall?.Report();
if (report == null)
return null;
ship.ShoreCall = null;
Cash += report.NetIncome;
LifetimeProfit += report.NetIncome;
Reputation = Market.Clamp(Reputation + report.ReputationImpact, 0, 100);
Voyage voyage = History.Find(item => item.ShoreCallId == report.CallId);
if (voyage != null)
{
voyage.ShoreReport = report;
if (voyage.Onboard != null)
voyage.Onboard.Happiness = Market.Clamp(voyage.Onboard.Happiness + report.GuestImpact, 0, 100);
voyage.ReputationChange += report.ReputationImpact;
}
return report;
}
}
}