Skip to content

Commit

Permalink
#6: Initial rough inventory system and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Ko committed Feb 22, 2021
1 parent b9db2c7 commit 91c19ae
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 119 deletions.
4 changes: 2 additions & 2 deletions agents/AIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public override void _Ready()
_agentAI = (AI)GetNode("AI");
}

public override void Initialize(GameWorld gameWorld, String unitName, String displayName, Team.TeamCode inputTeamCode, PathFinding pathFinding)
public override void Initialize(GameWorld gameWorld, String unitName, String displayName, TeamMapAI teamMapAI, PathFinding pathFinding)
{
base.Initialize(gameWorld, unitName, displayName, inputTeamCode, pathFinding);
base.Initialize(gameWorld, unitName, displayName, teamMapAI, pathFinding);
if (GetTree().IsNetworkServer())
{
_agentAI.Initialize(_gameWorld, this, pathFinding, DetectRadius);
Expand Down
66 changes: 43 additions & 23 deletions agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public class Agent : KinematicBody2D

private Team _team;

public TeamMapAI _teamMapAI;

protected Inventory CurrentInventory;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Expand All @@ -97,6 +101,8 @@ public override void _Ready()
Particles2D smoke = (Particles2D)GetNode("Smoke");
smoke.Emitting = false;

CurrentInventory = (Inventory)GetNode("Inventory");

_health = MaxHealth;
_energy = MaxEnergy;

Expand All @@ -107,27 +113,29 @@ public override void _Ready()
EmitSignal(nameof(EnergyChangedSignal), _health * 100 / MaxHealth);
}

public virtual void Initialize(GameWorld gameWorld, String unitName, String displayName, Team.TeamCode inputTeamCode, PathFinding pathFinding)
public virtual void Initialize(GameWorld gameWorld, String unitName, String displayName, TeamMapAI teamMapAI, PathFinding pathFinding)
{
_team = (Team)GetNode("Team");
_teamMapAI = teamMapAI;

_gameWorld = gameWorld;
SetCurrentTeam(inputTeamCode);
SetCurrentTeam(_teamMapAI.GetCurrentTeam());
SetUnitName(unitName);
SetDisplayName(displayName);

_health = MaxHealth;
_energy = MaxEnergy;

CurrentInventory.Initialize(this);

// Temporary script to automatic load weapon
UpdateRightWeapon((PackedScene)GD.Load("res://weapons/Rifile.tscn"));
UpdateRightWeapon((PackedScene)GD.Load("res://weapons/LightSaber.tscn"));
UpdateRightWeapon((PackedScene)GD.Load("res://weapons/LaserGun.tscn"));

UpdateLeftWeapon((PackedScene)GD.Load("res://weapons/Shield.tscn"));
UpdateLeftWeapon((PackedScene)GD.Load("res://weapons/MissleLauncher.tscn"));
UpdateLeftWeapon((PackedScene)GD.Load("res://weapons/LaserGun.tscn"));

}

public virtual void changeRightWeapon(int weaponIndex)
Expand Down Expand Up @@ -309,6 +317,13 @@ public Team.TeamCode GetCurrentTeam()
return _team.CurrentTeamCode;
}

public TeamMapAI GetCurrentTeamMapAI()
{
return _teamMapAI;
}



public String GetDisplayName()
{
return _displayName;
Expand Down Expand Up @@ -389,34 +404,39 @@ public void RotateToward(Vector2 location, float delta)

public void Fire(int rightWeapon, int leftWeapon)
{

if (rightWeapon == (int)GameStates.PlayerInput.InputAction.RELOAD)
if (currentLeftWeaponIndex != -1)
{
ReloadRightWeapon();
}
if (rightWeapon == (int)GameStates.PlayerInput.InputAction.RELOAD)
{
ReloadRightWeapon();
}

if (leftWeapon == (int)GameStates.PlayerInput.InputAction.RELOAD)
{
ReloadLeftWeapon();
if (rightWeapon == (int)GameStates.PlayerInput.InputAction.TRIGGER && currentRightWeaponIndex != -1)
{
// knock back effect
if (((Weapon)RightWeapons[currentRightWeaponIndex]).Fire(target) && MaxSpeed != 0)
{
Vector2 dir = (new Vector2(1, 0)).Rotated(GlobalRotation);
MoveAndSlide(dir * -10 * ((Weapon)RightWeapons[currentRightWeaponIndex]).KnockbackForce);
}
}
}

if (rightWeapon == (int)GameStates.PlayerInput.InputAction.TRIGGER && currentRightWeaponIndex != -1)
if (currentRightWeaponIndex != -1)
{
// knock back effect
if (((Weapon)RightWeapons[currentRightWeaponIndex]).Fire(target) && MaxSpeed != 0)
if (leftWeapon == (int)GameStates.PlayerInput.InputAction.RELOAD)
{
Vector2 dir = (new Vector2(1, 0)).Rotated(GlobalRotation);
MoveAndSlide(dir * -10 * ((Weapon)RightWeapons[currentRightWeaponIndex]).KnockbackForce);
ReloadLeftWeapon();
}
}

if (leftWeapon == (int)GameStates.PlayerInput.InputAction.TRIGGER && currentLeftWeaponIndex != -1)
{
// knock back effect
if (((Weapon)LeftWeapons[currentLeftWeaponIndex]).Fire(target) && MaxSpeed != 0)
if (leftWeapon == (int)GameStates.PlayerInput.InputAction.TRIGGER && currentLeftWeaponIndex != -1)
{
Vector2 dir = (new Vector2(1, 0)).Rotated(GlobalRotation);
MoveAndSlide(dir * -10 * ((Weapon)LeftWeapons[currentLeftWeaponIndex]).KnockbackForce);
// knock back effect
if (((Weapon)LeftWeapons[currentLeftWeaponIndex]).Fire(target) && MaxSpeed != 0)
{
Vector2 dir = (new Vector2(1, 0)).Rotated(GlobalRotation);
MoveAndSlide(dir * -10 * ((Weapon)LeftWeapons[currentLeftWeaponIndex]).KnockbackForce);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion agents/Agent.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=2]
[gd_scene load_steps=14 format=2]

[ext_resource path="res://agents/Agent.cs" type="Script" id=1]
[ext_resource path="res://effects/Explosion.tscn" type="PackedScene" id=2]
Expand All @@ -7,6 +7,7 @@
[ext_resource path="res://ui/UnitDisplay.tscn" type="PackedScene" id=5]
[ext_resource path="res://assets/ui/blue_tick.png" type="Texture" id=6]
[ext_resource path="res://effects/AgentExplosionParticle.tscn" type="PackedScene" id=7]
[ext_resource path="res://inventory/Inventory.tscn" type="PackedScene" id=8]

[sub_resource type="Gradient" id=1]
offsets = PoolRealArray( 0, 0.540416, 1 )
Expand Down Expand Up @@ -73,5 +74,7 @@ texture = ExtResource( 6 )

[node name="AgentExplosionParticle" parent="." instance=ExtResource( 7 )]
scale = Vector2( 3, 3 )

[node name="Inventory" parent="." instance=ExtResource( 8 )]
[connection signal="HealthChangedSignal" from="." to="UnitDisplay" method="UpdateUnitBar"]
[connection signal="animation_finished" from="Explosion" to="." method="_OnExplosionAnimationFinished"]
19 changes: 18 additions & 1 deletion agents/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Player : Agent
{

private HUD _hud = null;
private InventoryUI _inventoryUI = null;

[Remote]
public void serverGetPlayerInput(String inputData)
Expand Down Expand Up @@ -67,6 +68,10 @@ public void SetHUD(HUD hud)
ScreenIndicator screenIndicator = (ScreenIndicator)((PackedScene)GD.Load("res://ui/ScreenIndicator.tscn")).Instance();
AddChild(screenIndicator);
screenIndicator.Initialize(this);

// Setup Inventory UI
_inventoryUI = (InventoryUI)_hud.GetNode("controlGame/InventoryUI");
_inventoryUI.Initialize(_gameWorld.GetInventoryManager(), CurrentInventory);
}

protected override void DisconnectWeapon(Weapon currentWeapon, Weapon.WeaponOrder weaponOrder)
Expand Down Expand Up @@ -106,6 +111,18 @@ public void gatherInput(float delta)
{
GameStates.PlayerInput playerInput = new GameStates.PlayerInput();

if(Input.IsActionJustReleased("inventory"))
{
if(!_inventoryUI.Visible)
{
_inventoryUI.PopupCentered();
}
else
{
_inventoryUI.Hide();
}
}

if (Input.IsActionPressed("turn_right"))
{
playerInput.Right = (int)(GameStates.PlayerInput.InputAction.TRIGGER);
Expand Down Expand Up @@ -250,7 +267,7 @@ public override void _PhysicsProcess(float delta)
//some major lag in the game
gameStates.currentTime -= gameStates.updateDelta;

if (IsNetworkMaster())
if (GetTree().NetworkPeer != null && IsNetworkMaster())
{
gatherInput(delta);
}
Expand Down
25 changes: 23 additions & 2 deletions ai/TeamMapAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum BaseCaptureStartOrder
private Node _unitsContainer;

private GameWorld _gameWorld;
private InventoryManager _inventoryManager;

private PathFinding _pathFinding;

Expand Down Expand Up @@ -64,6 +65,25 @@ public void SetMaxUnitUsageAmount(int maxUnitUsageAmount)
_currentUnitUsageAmount = _maxUnitUsageAmount;
}

public bool ChargeAmount(int chargeAmount)
{
if (_currentUnitUsageAmount - chargeAmount < 0)
{
return false;
}
else
{
_currentUnitUsageAmount = _currentUnitUsageAmount- chargeAmount;
return true;
}
}

public void AddAmount(int chargeAmount)
{
_currentUnitUsageAmount = _currentUnitUsageAmount + chargeAmount;

}

public bool isUnitUsageAmountAllowed()
{
if (_currentUnitUsageAmount < _unitCost || _currentUnitUsageAmount < 0)
Expand Down Expand Up @@ -116,6 +136,7 @@ public bool isNewUnitAllow()

public void Initialize(GameWorld gameWorld, Godot.Collections.Array bases, Team.TeamCode team, PathFinding pathFinding)
{
_inventoryManager = gameWorld.GetInventoryManager();
_bases = bases;
_team.CurrentTeamCode = team;
_gameWorld = gameWorld;
Expand Down Expand Up @@ -209,13 +230,13 @@ public Agent CreateUnit(String unitName, String displayName, Boolean enableAI)
_unitsContainer.AddChild(unit);

// Set the info afterward as some of these depend on child node to be available
unit.Initialize(_gameWorld, unitName, displayName, _team.CurrentTeamCode, _pathFinding);
unit.Initialize(_gameWorld, unitName, displayName, this, _pathFinding);

// Trigger reload of UI
unit.changeRightWeapon(0);
unit.changeLeftWeapon(0);

_currentUnitUsageAmount -= _unitCost;
ChargeAmount(_unitCost);

return unit;
}
Expand Down
1 change: 1 addition & 0 deletions godot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="environments\CapturableBase.cs" />
<Compile Include="environments\ObstacleManager.cs" />
<Compile Include="inventory\Inventory.cs" />
<Compile Include="inventory\InventoryManager.cs" />
<Compile Include="items\ItemResource.cs" />
<Compile Include="map\SimulateGameWorld.cs" />
<Compile Include="projectiles\Projectile.cs" />
Expand Down

0 comments on commit 91c19ae

Please sign in to comment.