-
Notifications
You must be signed in to change notification settings - Fork 0
/
TraderClickableScript.cs
108 lines (97 loc) · 4.13 KB
/
TraderClickableScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[RequireComponent(typeof(TradeShip))]
class TraderClickableScript : MonoBehaviour
{
const int CONST_BASE_PRICE = 100;
const int DEFAULT_CARGO_SLOTS = 2;
const int PIRATE_CONVERSION_MULTIPLIER = 3;
const int TRADER_LIMIT = 5;
void OnClick()
{
var tradeShip = GetTradeShip();
var owner = tradeShip.Owner;
var gameShip = tradeShip.gameShip;
var sameFaction = gameShip.factionID == MyPlayer.factionID;
if (tradeShip.Owner != null && tradeShip.Owner.id != MyPlayer.id)
{
UIStatusBar.Show(Localization.Format("Owned by", gameShip.name, tradeShip.Owner.name));
}
else
{
UIPopupList popupList = UIGameWindow.popupList;
popupList.Clear();
if (owner == null)
{
string gold = Format.FormatGold(CalculatePrice());
popupList.AddItem(Localization.Format("Hire as trader", Format.FormatShip(gameShip), gold), "hire");
EventDelegate.Set(popupList.onChange, new EventDelegate.Callback(HirePopupCallBack));
}
else if (owner.id == MyPlayer.id)
{
var profitText = tradeShip.profit >= 0 ? Localization.Format("profit") : Localization.Format("loss");
UIStatusBar.Show(Localization.Format("Status", Format.FormatShip(gameShip), tradeShip.cargoSlots, profitText, Format.FormatGold(Math.Abs(tradeShip.profit))));
string gold = Format.FormatGold(CalculatePrice());
popupList.AddItem(Localization.Format("Fire trader", Format.FormatShip(gameShip)), "fire");
//popupList.AddItem(Localization.Format("Upgrade for 1000g", gameShip.name), "upgrade");
EventDelegate.Set(popupList.onChange, new EventDelegate.Callback(FirePopupCallBack));
}
popupList.Show();
}
}
private void HirePopupCallBack()
{
var tradeShip = GetTradeShip();
var gameShip = tradeShip.gameShip;
int price = CalculatePrice();
if (FindObjectsOfType<TradeShip>().Where(x => x.Owner != null && x.Owner.id == MyPlayer.id).Count() >= TRADER_LIMIT)
{
UIStatusBar.Show(Localization.Format("Trader limit", TRADER_LIMIT));
} else if (MyPlayer.GetResource("gold") >= price)
{
MyPlayer.ModifyResource("gold", -price, true);
MyPlayer.Sync();
tradeShip.cost = price;
tradeShip.UpdateTraderOwner(GamePlayer.me.netPlayer.id);
UIStatusBar.Show(Localization.Format("Hired", Format.FormatShip(gameShip), Format.FormatGold(price), tradeShip.cargoSlots));
}
else
{
UIStatusBar.Show(Localization.Format("Cant afford to hire", Format.FormatShip(gameShip), Format.FormatGold(price)));
}
UIPopupList popupList = UIGameWindow.popupList;
popupList.Clear();
EventDelegate.Remove(popupList.onChange, new EventDelegate.Callback(HirePopupCallBack));
}
private void FirePopupCallBack()
{
var tradeShip = GetTradeShip();
var gameShip = tradeShip.gameShip;
int price = CalculatePrice();
tradeShip.UpdateTraderOwner(null);
UIPopupList popupList = UIGameWindow.popupList;
string data = UIPopupList.current.data as string;
popupList.Clear();
switch (data)
{
case "fire":
tradeShip.UpdateTraderOwner(null);
tradeShip.TradeMissions = new List<TradeMission>();
UIStatusBar.Show(Localization.Format("Fired", Format.FormatShip(gameShip)));
break;
}
EventDelegate.Remove(popupList.onChange, new EventDelegate.Callback(FirePopupCallBack));
}
private TradeShip GetTradeShip()
{
return GetComponent<TradeShip>();
}
private int CalculatePrice()
{
var tradeShip = GetTradeShip();
var gameShip = tradeShip.gameShip;
return tradeShip.cargoSlots * CONST_BASE_PRICE * (gameShip.factionID == 0 ? PIRATE_CONVERSION_MULTIPLIER : 1);
}
}