Skip to content

Commit

Permalink
fixed audio playing multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Perdue committed Mar 13, 2022
1 parent d57e330 commit 2428099
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using tileEngine.SDK.Components;
using tileEngine.SDK.Diagnostics;
using tileEngine.SDK.Input;
using tileEngine.SDK.Utility;

namespace CampusCrawl.Characters
{
Expand Down Expand Up @@ -77,10 +78,14 @@ public class Character : GameObject
protected Weapon weapon;
protected Entity doNotPickUp;
public string playerModelPath;
protected bool soundPlaying = false;
protected Timer soundDone;

public Character()
{

soundDone = new Timer(0.25f);
soundDone.OnTick += soundCooldown;
soundDone.Loop = false;
}

public void UpdateSprite(SpriteComponent _sprite, BoxColliderComponent boxCollider = null)
Expand All @@ -104,6 +109,7 @@ public override void Initialize()
{
base.Initialize();
attacking = false;
soundDone.Start();
damageSound = TileEngine.Instance.Sound.LoadSound("Sound/testSound.mp3");
}

Expand Down Expand Up @@ -154,12 +160,21 @@ public void PushSelf(int x, int y)
pushStats.setPush(x, y);
}

public void soundCooldown()
{
soundPlaying = false;
}

public void onDamage(float damage, float[] attackDirection, int pushAmt)
{
health -= damage;
PushSelf(-(int)(attackDirection[0] * pushAmt), -(int)(attackDirection[1] * pushAmt));

TileEngine.Instance.Sound.PlaySound(damageSound);
if (!soundPlaying)
{
TileEngine.Instance.Sound.PlaySound(damageSound);
soundDone.Start();
soundPlaying = true;
}
}

public void scanAndPickUpEntities()
Expand Down Expand Up @@ -187,9 +202,9 @@ public override void Update(GameTime delta)
base.Update(delta);
var time = (float)(delta.ElapsedGameTime.TotalSeconds);
var movement = InputHandler.GetEvent("Movement");

soundDone.Update(delta);
// Code to pick up entities
if(weapon == null)
if (weapon == null)
scanAndPickUpEntities();

// Code to move/push
Expand Down

0 comments on commit 2428099

Please sign in to comment.