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 91c19ae commit 45a3503
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 47 deletions.
17 changes: 17 additions & 0 deletions agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public class Agent : KinematicBody2D

protected Inventory CurrentInventory;

protected Timer DamageEffectTimer;

protected Sprite Body;

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

CurrentInventory = (Inventory)GetNode("Inventory");
DamageEffectTimer = (Timer)GetNode("DamageEffectTimer");
Body = (Sprite)GetNode("Body");

_health = MaxHealth;
_energy = MaxEnergy;
Expand Down Expand Up @@ -481,6 +487,12 @@ public void TakeDamage(int amount, Vector2 dir, Agent source, Team sourceTeam)

if (trackDamage)
{
if (DamageEffectTimer.IsStopped())
{
Body.SelfModulate = new Color(10.0f, 10.0f, 10.0f, 1.0f);
DamageEffectTimer.Start();
}

_health -= amount;

EmitSignal(nameof(HealthChangedSignal), _health * 100 / MaxHealth);
Expand Down Expand Up @@ -514,6 +526,11 @@ public void SetCameraRemotePath(Camera2D camera)
_remoteTransform2D.RemotePath = camera.GetPath();
}

public void DamageEffectTimerTimeout()
{
Body.SelfModulate = new Color(1.0f, 1.0f, 1.0f, 1.0f);
}

public void ReloadRightWeapon()
{
((Weapon)RightWeapons[currentRightWeaponIndex]).StartReload();
Expand Down
5 changes: 5 additions & 0 deletions agents/Agent.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@ texture = ExtResource( 6 )
scale = Vector2( 3, 3 )

[node name="Inventory" parent="." instance=ExtResource( 8 )]

[node name="DamageEffectTimer" type="Timer" parent="."]
wait_time = 0.5
one_shot = true
[connection signal="HealthChangedSignal" from="." to="UnitDisplay" method="UpdateUnitBar"]
[connection signal="animation_finished" from="Explosion" to="." method="_OnExplosionAnimationFinished"]
[connection signal="timeout" from="DamageEffectTimer" to="." method="DamageEffectTimerTimeout"]
102 changes: 55 additions & 47 deletions singletons/audios/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,73 @@
public class AudioManager : Node
{

AudioStreamPlayer [] soundEffectPlayers = new AudioStreamPlayer[100];
AudioStreamPlayer[] soundEffectPlayers = new AudioStreamPlayer[100];

AudioStream musicHitClip = (AudioStream)GD.Load("res://assets/sounds/bullethit.wav");
AudioStream musicHitClip = (AudioStream)GD.Load("res://assets/sounds/bullethit.wav");


public void playMusic(AudioStream musicClip) {
AudioStreamPlayer musicPlayer = (AudioStreamPlayer)GetNode("Music/AudioStreamPlayer");
musicPlayer.Stream = musicClip;
musicPlayer.Play();
}
public void playMusic(AudioStream musicClip)
{
AudioStreamPlayer musicPlayer = (AudioStreamPlayer)GetNode("Music/AudioStreamPlayer");
musicPlayer.Stream = musicClip;
musicPlayer.Play();
}

public override void _Ready()
{
for (int index = 0; index < soundEffectPlayers.Length; index++){
soundEffectPlayers[index] = new AudioStreamPlayer();
GetNode("SoundEffect").AddChild(soundEffectPlayers[index]);
}
}
public override void _Ready()
{

for (int index = 0; index < soundEffectPlayers.Length; index++)
{
soundEffectPlayers[index] = new AudioStreamPlayer();
GetNode("SoundEffect").AddChild(soundEffectPlayers[index]);
}
}

public void playSoundEffect(AudioStream musicClip) {
int index = 0;
for (index = 0; index < soundEffectPlayers.Length; index++){
if (! soundEffectPlayers[index].Playing)
{
soundEffectPlayers[index].Stream = musicClip;
soundEffectPlayers[index].Play();

break;
}
}
// if no available player found, use the longest player
if(index == soundEffectPlayers.Length)
{
AudioStreamPlayer currentPlayer = findOldestSoundEffectPlayer();
if(currentPlayer != null){
currentPlayer.Stream = musicClip;
currentPlayer.Play();

public void playSoundEffect(AudioStream musicClip)
{
int index = 0;
for (index = 0; index < soundEffectPlayers.Length; index++)
{
if (!soundEffectPlayers[index].Playing)
{
soundEffectPlayers[index].Stream = musicClip;
soundEffectPlayers[index].Play();

break;
}
}
// if no available player found, use the longest player
if (index == soundEffectPlayers.Length)
{
AudioStreamPlayer currentPlayer = findOldestSoundEffectPlayer();
if (currentPlayer != null)
{
currentPlayer.Stream = musicClip;
currentPlayer.Play();
}
}
}
}
}

private AudioStreamPlayer findOldestSoundEffectPlayer() {
private AudioStreamPlayer findOldestSoundEffectPlayer()
{

AudioStreamPlayer lastPlayer = null;
AudioStreamPlayer lastPlayer = null;

for (int index = 0; index < soundEffectPlayers.Length; index++){
if (lastPlayer == null)
{
lastPlayer = soundEffectPlayers[index];
}
for (int index = 0; index < soundEffectPlayers.Length; index++)
{
if (lastPlayer == null)
{
lastPlayer = soundEffectPlayers[index];
}

if(soundEffectPlayers[index].GetPlaybackPosition() > lastPlayer.GetPlaybackPosition())
{
lastPlayer = soundEffectPlayers[index];
}
}
if (soundEffectPlayers[index].GetPlaybackPosition() > lastPlayer.GetPlaybackPosition())
{
lastPlayer = soundEffectPlayers[index];
}
}

return lastPlayer;
return lastPlayer;
}

}

0 comments on commit 45a3503

Please sign in to comment.