Skip to content

Commit

Permalink
#6: Update server creation ui to better control initial budget of eac…
Browse files Browse the repository at this point in the history
…h team and whatever each team will auto generate new bots along with player, fix the health heal not reflect immeidately in UI, fix issue where after server finish a game, cannot create another server game
  • Loading branch information
Danil Ko committed Mar 13, 2021
1 parent db9185a commit a11b3b9
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 35 deletions.
11 changes: 11 additions & 0 deletions GameStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,24 @@ public enum InputAction {
//This will be filled only on the server
public Dictionary<int, Dictionary<int, PlayerInput>> playerInputs = new Dictionary<int, Dictionary<int, PlayerInput>>();

public Godot.Collections.Array<TeamMapAISetting> _teamMapAISettings = null;

private String messages;

public override void _Ready()
{
set_update_rate(updateRate);
}

public Godot.Collections.Array<TeamMapAISetting> GetTeamMapAISettings()
{
return _teamMapAISettings;
}

public void SetTeamMapAISettings(Godot.Collections.Array<TeamMapAISetting> teamMapAISettings)
{
_teamMapAISettings = teamMapAISettings;
}

public void cacheInput(int net_id, PlayerInput playerInput)
{
Expand Down
2 changes: 2 additions & 0 deletions agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ public void Heal(int amount)
Particles2D smoke = (Particles2D)GetNode("Smoke");
smoke.Emitting = false;
}

EmitSignal(nameof(HealthChangedSignal), _health);
}

public void Explode()
Expand Down
2 changes: 2 additions & 0 deletions agents/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void SetHUD(HUD hud, InventoryManager _inventoryManager)
if (!_teamMapAI.IsConnected(nameof(TeamMapAI.TeamUnitUsageAmountChangeSignal), _hud, nameof(HUD.UpdateTeamUnitUsageAmount)))
{
_teamMapAI.Connect(nameof(TeamMapAI.TeamUnitUsageAmountChangeSignal), _hud, nameof(HUD.UpdateTeamUnitUsageAmount));
// Notify the HUD to change
_teamMapAI.ChargeAmount(0);
}
}

Expand Down
33 changes: 32 additions & 1 deletion ai/TeamMapAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ enum BaseCaptureStartOrder

private int _unitCost = 100;


private Timer _advancedTimer;

[Export]
private Boolean _autoSpawnMember = true;

public override void _Ready()
{
_team = (Team)GetNode("Team");
Expand All @@ -52,6 +54,16 @@ public override void _Ready()
_advancedTimer.WaitTime = _advancedWaitInterval;
}

public Boolean GetAutoSpawnMember()
{
return _autoSpawnMember;
}

public void SetAutoSpawnMember(Boolean autoSpawnMember)
{
_autoSpawnMember = autoSpawnMember;
}

public Team.TeamCode GetCurrentTeam()
{
return _team.CurrentTeamCode;
Expand All @@ -66,6 +78,13 @@ public void SetMaxUnitUsageAmount(int maxUnitUsageAmount)
{
_maxUnitUsageAmount = maxUnitUsageAmount;
_currentUnitUsageAmount = _maxUnitUsageAmount;

EmitSignal(nameof(TeamUnitUsageAmountChangeSignal), _currentUnitUsageAmount);

if (GetTree().NetworkPeer != null && GetTree().IsNetworkServer())
{
Rpc(nameof(_clientSetAmount), _currentUnitUsageAmount);
}
}

public bool ChargeAmount(int chargeAmount)
Expand All @@ -88,6 +107,18 @@ public bool ChargeAmount(int chargeAmount)
}
}

public void SyncTeamMapAICurrentUnitAmount(int rpcId)
{
if (rpcId != -1)
{
RpcId(rpcId, nameof(_clientSetAmount), _currentUnitUsageAmount);
}
else
{
Rpc(nameof(_clientSetAmount), _currentUnitUsageAmount);
}
}

[Remote]
private void _clientSetAmount(int amount)
{
Expand Down
9 changes: 9 additions & 0 deletions ai/TeamMapAISetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Godot;
using System;

public class TeamMapAISetting : Resource
{
public Team.TeamCode TeamCode {get;set;}
public int Budget {get; set;}
public bool AutoSpawnMember {get; set;}
}
2 changes: 2 additions & 0 deletions godot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="ai\PathFinding.cs" />
<Compile Include="ai\AI.cs" />
<Compile Include="ai\TeamMapAI.cs" />
<Compile Include="ai\TeamMapAISetting.cs" />
<Compile Include="effects\AgentExplosionParticle.cs" />
<Compile Include="effects\RemainParticles.cs" />
<Compile Include="environments\CapaturableBaseManager.cs" />
Expand All @@ -46,6 +47,7 @@
<Compile Include="ui\MiniMap.cs" />
<Compile Include="ui\PopUpMessage.cs" />
<Compile Include="ui\ScreenIndicator.cs" />
<Compile Include="ui\TeamSettingPanel.cs" />
<Compile Include="ui\WeaponSlotPanel.cs" />
<Compile Include="weapons\Shield.cs" />
<Compile Include="singletons\audios\AudioManager.cs" />
Expand Down
1 change: 0 additions & 1 deletion inventory/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ public String GetInventoryState()
public void SyncInventoryState(String state, InventoryDatabase inventoryDatabase)
{
String[] stateInfo = state.Split(";");
GD.Print("SYNC INVENTORY PAYLOAD " + _agent.GetUnitName() + " " + state);
int stateIndex = 0;

// Set the current inventory count
Expand Down
17 changes: 16 additions & 1 deletion map/GameWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ protected void InitializeCapaturableBaseManager()

protected void InitializeTeamMapAI()
{
Godot.Collections.Array<TeamMapAISetting> teamMapAISettings = GameStates.GetTeamMapAISettings();

TeamMapAIs = new Godot.Collections.Array<TeamMapAI>();

// Start with neutral and above
Expand All @@ -264,6 +266,12 @@ protected void InitializeTeamMapAI()

ai.Initialize(this, _inventoryManager, CapaturableBaseManager.GetBases(), (Team.TeamCode)index, _pathFinding);

if(teamMapAISettings != null)
{
ai.SetMaxUnitUsageAmount(teamMapAISettings[index].Budget);
ai.SetAutoSpawnMember(teamMapAISettings[index].AutoSpawnMember);
}

TeamMapAIs.Add(ai);

foreach (CapturableBase capturable in CapaturableBaseManager.GetBases())
Expand Down Expand Up @@ -1140,6 +1148,12 @@ private void _initializeNewPlayer(String info)
// Propagate info of obstacles to other
if (GetTree().IsNetworkServer() && pininfo.net_id != 1)
{

foreach (TeamMapAI teamMapAI in TeamMapAIs)
{
teamMapAI.SyncTeamMapAICurrentUnitAmount(pininfo.net_id);
}

// Add current bot info to new player
foreach (String playerIDs in spawnPlayers.Keys)
{
Expand All @@ -1163,6 +1177,7 @@ private void _initializeNewPlayer(String info)
index++;
}


// Sync the destoryed obstacles
_obstacleManager.syncObstacles(pininfo.net_id);

Expand Down Expand Up @@ -1303,7 +1318,7 @@ private void _syncBots()

foreach (TeamMapAI currentAI in TeamMapAIs)
{
if (currentAI.isNewUnitAllow())
if (currentAI.GetAutoSpawnMember() && currentAI.isNewUnitAllow())
{
if (currentAI.GetUnitsContainer().GetChildren().Count <= smallestUnitCount)
{
Expand Down
4 changes: 3 additions & 1 deletion map/SimulateGameWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class SimulateGameWorld : GameWorld
public override void _Ready()
{
GameStates = (GameStates)GetNode("/root/GAMESTATES");

// Clean up previous setup state
GameStates.SetTeamMapAISettings(null);

InitializeInventoryManager();

InitializeCamera();
Expand Down
13 changes: 11 additions & 2 deletions network/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class Network : Node
//when server is successfully created
[Signal]
public delegate void ServerCreatedSignal();
// when server create fail
[Signal]
public delegate void ServerCreateFailSignal();
// When the peer successfully joins a server
[Signal]
public delegate void JoinSuccessSignal();
Expand Down Expand Up @@ -85,7 +88,8 @@ public void createServer(String serverName, int port, int max_players)
// Try to create the server
if (peer.CreateServer(serverinfo.used_port, serverinfo.max_players) != Error.Ok)
{
GD.PrintErr("Failed to create server");
// Tell the server crate fail
EmitSignal(nameof(ServerCreateFailSignal), "SERVER CREATE FAIL AT " + serverName + ":" + port + ". PLEASE CHECK DETAIL AND TRY AGAIN.");
return;
}

Expand All @@ -112,6 +116,10 @@ public void closeServer()
networkPlayers.Clear();

// Close network server
if (GetTree().NetworkPeer != null)
{
GetTree().NetworkPeer.Dispose();
}
GetTree().NetworkPeer = null;
}

Expand All @@ -123,9 +131,10 @@ public void joinServer(String ip, int port)

if (peer.CreateClient(ip, port) != Error.Ok)
{
EmitSignal(nameof(JoinFailSignal));
EmitSignal(nameof(JoinFailSignal), "JOIN SERVER AT " + ip + ":" + port + " FAIL. PLEASE CHECK DETAIL AND TRY AGAIN.");
return;
}

GetTree().NetworkPeer = peer;
}

Expand Down
1 change: 0 additions & 1 deletion ui/InventoryUI.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[sub_resource type="Theme" id=1]

[node name="InventoryUI" type="PopupPanel"]
visible = true
margin_right = 771.0
margin_bottom = 404.0
script = ExtResource( 2 )
Expand Down
42 changes: 42 additions & 0 deletions ui/TeamSettingPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Godot;
using System;

public class TeamSettingPanel : Panel
{
private SpinBox _spinBoxBudget;
private CheckBox _checkboxAutoSpawnMember;

private Team.TeamCode _teamCode;

public override void _Ready()
{
_spinBoxBudget = (SpinBox)GetNode("SpinBoxTeamBudget");
_checkboxAutoSpawnMember = (CheckBox)GetNode("CheckBoxAutoSapwnMembers");
}

public void Initialize(Team.TeamCode teamCode)
{
_teamCode = teamCode;

TextureRect textureRect = (TextureRect)GetNode("TextrectTeam");
textureRect.Modulate = Team.TeamColor[(int)_teamCode];

Label labelTeamName = (Label)GetNode("LabelTeamName");
labelTeamName.Text = "" + _teamCode;
}

public Team.TeamCode GetTeamCode()
{
return _teamCode;
}

public int GetTeamBudget()
{
return (int)_spinBoxBudget.Value;
}

public bool GetTeamAutoSpawnMember()
{
return _checkboxAutoSpawnMember.Pressed;
}
}

0 comments on commit a11b3b9

Please sign in to comment.