Skip to content

Commit

Permalink
Units now only bought when resources available, and despawn from store
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuskilian committed May 3, 2020
1 parent 672cf26 commit c87d981
Show file tree
Hide file tree
Showing 48 changed files with 130 additions and 51 deletions.
Binary file modified Assets/Photon/PhotonBolt/assemblies/bolt.user.dll
Binary file not shown.
18 changes: 5 additions & 13 deletions Assets/Photon/PhotonBolt/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@
"$type": "Bolt.Compiler.EventDefinition, bolt.compiler",
"Properties": [
{
"Name": "Unit",
"Name": "StoreIdx",
"Enabled": true,
"Expanded": true,
"ReplicationMode": 1,
"Priority": 1,
"PropertyType": {
"$type": "Bolt.Compiler.PropertyTypeNetworkId, bolt.compiler"
"$type": "Bolt.Compiler.PropertyTypeInteger, bolt.compiler",
"MaxValue": 255
},
"AssetSettings": {
"$type": "Bolt.Compiler.PropertyEventSettings, bolt.compiler"
Expand All @@ -318,7 +319,7 @@
],
"EntitySenders": 3,
"GlobalSenders": 1,
"Name": "StoreDespawnUnitEvent",
"Name": "StoreUnitCaughtEvent",
"Guid": "af7458ca-3920-448a-9b8a-206b2b601898",
"Groups": []
},
Expand All @@ -331,15 +332,6 @@
"Guid": "1d3f1b09-ed3e-419a-b995-a51b7a351e4f",
"Groups": []
},
{
"$type": "Bolt.Compiler.EventDefinition, bolt.compiler",
"Properties": [],
"EntitySenders": 3,
"GlobalSenders": 1,
"Name": "EventManServerInitializedEvent",
"Guid": "bc9bba5c-8de2-4661-85da-a010d863055e",
"Groups": []
},
{
"$type": "Bolt.Compiler.EventDefinition, bolt.compiler",
"Properties": [
Expand All @@ -360,7 +352,7 @@
],
"EntitySenders": 3,
"GlobalSenders": 2,
"Name": "ClientTryBuyUnitEvent",
"Name": "ClientTryCatchUnitEvent",
"Guid": "bb45d88c-5f36-4a5f-a985-4a61bcd8885a",
"Groups": []
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ MonoBehaviour:
showDebugInfo: 1
overrideTimeScale: 1
logUncaughtExceptions: 0
debugEditorMode: 1
debugEditorMode: 2
consoleToggleKey: 9
consoleVisibleByDefault: 0
compilationWarnLevel: 4
Expand Down
13 changes: 8 additions & 5 deletions Assets/Scripts/Client/ClientGlobalEventMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public override void SceneLoadLocalDone(string scene, Bolt.IProtocolToken token)
#region Receiving Global Events
public Action<Player> PlayerReceivedEvent;
public Action<StoreUnit[]> NewStoreEvent;
public Action<int> UnitCaughtEvent;

public override void ControlOfEntityGained(BoltEntity entity) {
if (entity.StateIs<IPlayerState>())
Expand All @@ -34,20 +35,22 @@ public override void OnEvent(StoreNewStoreEvent evnt) {
};
NewStoreEvent?.Invoke(Units);
}

public override void OnEvent(StoreUnitCaughtEvent evnt) { UnitCaughtEvent?.Invoke(evnt.StoreIdx); }
#endregion

#region Sending Global Events
private void SubscribeLocalEventHandlers() {
StoreButtonMan.Instance.TryBuyUnitEvent += HandleTryBuyUnitEvent;
StoreButtonMan.Instance.TryCatchUnitEvent += HandleTryCatchUnitEvent;

InputMan input = InputMan.Instance;
input.TryRerollStoreEvent += HandleTryRerollStoreEvent;
}

private void HandleTryBuyUnitEvent(int idx) {
var tryBuyUnitEvent = ClientTryBuyUnitEvent.Create(GlobalTargets.OnlyServer);
tryBuyUnitEvent.StoreIdx = idx;
tryBuyUnitEvent.Send();
private void HandleTryCatchUnitEvent(int idx) {
var tryCatchUnitEvent = ClientTryCatchUnitEvent.Create(GlobalTargets.OnlyServer);
tryCatchUnitEvent.StoreIdx = idx;
tryCatchUnitEvent.Send();
}

private void HandleTryRerollStoreEvent() { ClientTryRerollStoreEvent.Create(GlobalTargets.OnlyServer).Send(); }
Expand Down
20 changes: 13 additions & 7 deletions Assets/Scripts/Client/ClientStoreMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ private void SpawnNewStore(StoreUnit[] Units) {

private void DespawnStore() {
StopAllCoroutines();
for (int idx = 0; idx < StoreUnits.Length; idx++) {
StoreUnit storeUnit = StoreUnits[idx];
if (storeUnit == null) continue;
storeUnit.ResetUnitPosition();
}
for (int idx = 0; idx < StoreUnits.Length; idx++) DespawnUnit(idx);
}

private void DespawnUnit(int storeIdx) {
StoreUnit storeUnit = StoreUnits[storeIdx];
if (storeUnit == null) return;
StoreUnits[storeIdx] = null;
storeUnit.ResetUnitPosition();
}

private IEnumerator WaitThenSpawn(StoreUnit storeUnit, int index) {
Expand All @@ -59,11 +62,14 @@ private Vector3 GetUnitPosition(int index) {

#region Local Event Handlers
private void SubscribeLocalEventHandlers() {
ClientGlobalEventMan clientMan = ClientGlobalEventMan.Instance;
clientMan.NewStoreEvent += HandleNewStoreEvent;
ClientGlobalEventMan global = ClientGlobalEventMan.Instance;
global.NewStoreEvent += HandleNewStoreEvent;
global.UnitCaughtEvent += HandleUnitCaughtEvent;
}

private void HandleNewStoreEvent(StoreUnit[] Units) { DespawnStore(); SpawnNewStore(Units); }

private void HandleUnitCaughtEvent(int storeIdx) { DespawnUnit(storeIdx); }
#endregion

}
22 changes: 16 additions & 6 deletions Assets/Scripts/Client/UI/StoreButtonMan.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
using UnityEngine;
using System;
using Bolt;

public class StoreButtonMan : MonoBehaviour {
public class StoreButtonMan : GlobalEventListener {

public static StoreButtonMan Instance { get; private set; }
private void Awake() { if (Instance == null) Instance = this; }

#region Local Events
public Action<int> TryBuyUnitEvent;
public Action<int> TryCatchUnitEvent;
#endregion

[SerializeField] private GameObject CatchButtonTemplate = null;
[SerializeField] private int xOffsetMax = 520;

private CatchUnitButton[] CatchUnitButtons = new CatchUnitButton[PlayerStoreMan.StoreSize];

private void Start() { InitStoreButtons(); SubscribeLocalEventHandlers(); }
private void Start() { InitStoreButtons(); }

public override void OnEvent(EventManClientInitializedEvent evnt) { SubscribeLocalEventHandlers(); }

private void InitStoreButtons() {
for (int idx = 0; idx < CatchUnitButtons.Length; idx++) {
GameObject buttonGO = Instantiate(CatchButtonTemplate);
buttonGO.transform.SetParent(transform);

float xOffset = (((float)idx / (float)(PlayerStoreMan.StoreSize-1)) * xOffsetMax * 2) - xOffsetMax;
float xOffset = (((float)idx / (float)(PlayerStoreMan.StoreSize - 1)) * xOffsetMax * 2) - xOffsetMax;
buttonGO.transform.localPosition = Vector3.right * xOffset;
buttonGO.transform.localScale = Vector3.one;

Expand All @@ -38,10 +41,17 @@ private void InitStoreButtons() {

#region Local Event Handlers
private void SubscribeLocalEventHandlers() {
ClientStoreMan.Instance.UnitArrivedInStoreEvent += HandleUnitArrivedInStoreEvent;
ClientGlobalEventMan global = ClientGlobalEventMan.Instance;
global.UnitCaughtEvent += HandleUnitCaughtEvent;
global.NewStoreEvent += HandleNewStoreEvent;

ClientStoreMan store = ClientStoreMan.Instance;
store.UnitArrivedInStoreEvent += HandleUnitArrivedInStoreEvent;
}

private void HandleUnitArrivedInStoreEvent(int idx) { ActivateStoreButton(idx); }
private void HandleUnitArrivedInStoreEvent(int storeIdx) { ActivateStoreButton(storeIdx); }
private void HandleUnitCaughtEvent(int storeIdx) { DeactivateStoreButton(storeIdx); }
private void HandleNewStoreEvent(StoreUnit[] _) { for (int idx = 0; idx < PlayerStoreMan.StoreSize; idx++) DeactivateStoreButton(idx); }
#endregion

}
2 changes: 1 addition & 1 deletion Assets/Scripts/Other/Buttons/CatchUnitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class CatchUnitButton : MonoBehaviour {
private int storeIdx;

public void OnButtonClick() {
StoreButtonMan.Instance.TryBuyUnitEvent?.Invoke(storeIdx);
StoreButtonMan.Instance.TryCatchUnitEvent?.Invoke(storeIdx);
}

public void SetStoreIdx(int idx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ public static class GameInfo {
public static System.Random RNG = new System.Random();

public enum Rarity { COMMON, UNCOMMON, RARE, EPIC, SECRET, LEGENDARY };
public static readonly int[] CatchChances = { 1, 2, 3, 4, 5, 10 };

public enum EvlChain { BASE, MID, TOP }

public static readonly int NumLevels = 10;
public static readonly int NumRarities = Enum.GetValues(typeof(Rarity)).Length;


public static Rarity[] Rarities {
get {
Rarity[] list = new Rarity[Enum.GetValues(typeof(Rarity)).Length];
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/Scripts/Other/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ private void AttachPlayerMen() {
gameObject.AddComponent<PlayerStoreMan>(),
gameObject.AddComponent<PlayerLevelMan>(),
gameObject.AddComponent<PlayerFinanceMan>(),
gameObject.AddComponent<PlayerBoardMan>()
gameObject.AddComponent<PlayerBoardMan>(),
gameObject.AddComponent<PlayerBagMan>()
};
}

Expand Down
18 changes: 14 additions & 4 deletions Assets/Scripts/Server/PerPlayer/PlayerManagers/PlayerBoardMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@ private Unit SpawnUnit(Unit unitPrefab) {
return unit;
}

public bool CanSpawnUnit(Unit unit) {
for (int i = 0; i < BenchSize; i++) if (!Bench[i].IsTileFilled) return true;
// TODO: Check for evolution
return false;
}

#region Local Event Handlers
private void SubscribeLocalEventHandlers() {
var store = player.GetPlayerMan<PlayerStoreMan>();
store.TryBuyUnitEvent += HandleTryBuyUnitEvent;
store.UnitCaughtEvent += HandleUnitCaughtEvent;
}

private void HandleTryBuyUnitEvent(StoreUnit storeUnit) {
Unit unit = SpawnUnit(storeUnit.unit);
for (int i = 0; i < BenchSize; i++) if (!Bench[i].IsTileFilled) { Bench[i].FillTile(unit); return; }
private void HandleUnitCaughtEvent(StoreUnit storeUnit) {
for (int i = 0; i < BenchSize; i++) if (!Bench[i].IsTileFilled) {
Bench[i].FillTile(SpawnUnit(storeUnit.unit));
return;
}
}
#endregion

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;
using Bolt;
using System;
using static GameInfo;

public class PlayerBagMan : PlayerManager {

public int PokeballCount { get; private set; } = 0;

private void Start() { PokeballCount = 100; }

public bool TryCatchUnit(Rarity rarity) {
if (PokeballCount <= 0) return false;
PokeballCount--;
return RNG.Next(CatchChances[(int)rarity]) == 0;
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,48 @@ private void DespawnActiveStore() {
if (ActiveStore[i] == null) continue;
StoreUnit storeUnit = ActiveStore[i];
ActiveStore[i] = null;
DespawnUnitEvent?.Invoke(storeUnit);
UnitDespawnEvent?.Invoke(storeUnit);
}
}

private void CatchUnit(int storeIdx) {
StoreUnit storeUnit = ActiveStore[storeIdx];
ActiveStore[storeIdx] = null;

UnitCaughtEvent?.Invoke(storeUnit);
var unitCaughtEvent = StoreUnitCaughtEvent.Create(player.Connection);
unitCaughtEvent.StoreIdx = storeIdx;
unitCaughtEvent.Send();
}



#region Local Events
public Action<StoreUnit> TryBuyUnitEvent;
public Action<StoreUnit> DespawnUnitEvent;
public Action<StoreUnit> UnitDespawnEvent;
public Action<StoreUnit> UnitCaughtEvent;
#endregion

#region Local Event Handlers
private void SubscribeLocalEventHandlers() {
GameMan.Instance.AllPlayersLoadedEvent += HandleAllPlayersLoadedEvent;
player.GetPlayerMan<PlayerFinanceMan>().RerollStoreEvent += HandleRerollStoreEvent;

var finance = player.GetPlayerMan<PlayerFinanceMan>();
finance.RerollStoreEvent += HandleRerollStoreEvent;
}

private void HandleAllPlayersLoadedEvent() { SpawnNewStore(); }
private void HandleRerollStoreEvent() { DespawnActiveStore(); SpawnNewStore(); }
private void HandleUnitCaughtAndSpawnedEvent(Unit unit) { }
#endregion

#region Global Event Handlers
public override void OnEvent(ClientTryBuyUnitEvent evnt) {
public override void OnEvent(ClientTryCatchUnitEvent evnt) {
if (!IsThisPlayer(evnt.RaisedBy)) return;
TryBuyUnitEvent?.Invoke(ActiveStore[evnt.StoreIdx]);
StoreUnit storeUnit = ActiveStore[evnt.StoreIdx];
if (storeUnit == null) return;
if (!player.GetPlayerMan<PlayerBoardMan>().CanSpawnUnit(storeUnit.unit)) return;
if (!player.GetPlayerMan<PlayerBagMan>().TryCatchUnit(storeUnit.unit.properties.rarity)) return;
CatchUnit(evnt.StoreIdx);
}
#endregion
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/Scripts/Server/Singleton/PoolMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ private void SubscribeLocalEventHandlers() {
private void SubscribePlayerEventHandlers() {
foreach (Player p in GameMan.Instance.Players) {
PlayerStoreMan store = p.GetPlayerMan<PlayerStoreMan>();
store.DespawnUnitEvent += HandleDespawnUnitEvent;
store.UnitDespawnEvent += HandleUnitDespawnEvent;
}
}

private void HandleGameLoadedEvent() { InitPools(); }
private void HandleAllPlayersLoadedEvent() { FreezeAllStoreEntities(); SubscribePlayerEventHandlers(); }
private void HandleDespawnUnitEvent(StoreUnit storeUnit) { EnqueueUnit(storeUnit); }
private void HandleUnitDespawnEvent(StoreUnit storeUnit) { EnqueueUnit(storeUnit); }
#endregion

}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit c87d981

Please sign in to comment.