Files
evilsatellites/EvilSatellites/Objects/Player.cs
T

224 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using SynthySound;
using EvilSatellites.Screens;
using EvilSatellites.Weapons;
namespace EvilSatellites.Objects
{
public class Player : AbstractGameObject
{
private Vector3 _PrevPos;
private Vector3 MaxSpeed = new Vector3(0.2f, 0.2f, 0.2f);
public Vector3 Acceleration = new Vector3(0.0035f, 0.004f, 0.004f);
private Vector3 Deceleration = new Vector3(0.0025f, 0.0025f, 0.0025f);
private ISoundEffect sfxEngine;
private ISoundEffect sfxWhine;
private ISoundEffect sfxDamage;
private SoundPatch sfxElevator;
private SoundPatch sfxVacuum;
private SoundPatch sfxTurn;
private AudioAsset sfxTaps;
public IWeapon CurrentWeapon;
private int Turning;
private int Elevating;
public bool Vacuuming;
public float Health;
private Random rnd = new Random();
private bool IsDamaged;
public Player(IScreen screen): base(screen)
{
ShouldDestroy = false;
Active = true;
Type = ObjectType.Player;
Position = new Vector3(5000, 5000, 4990);
Dimensions = new Vector3(5, 5, 5);
Movement = new Vector3(0, 0, 0);
Rotation = Matrix.Identity;
Orientation = Rotation.Forward;
CurrentWeapon = new LaserMG(this, (GameScreen)Screen);
Turning = 0;
Elevating = 0;
Vacuuming = false;
Health = 100;
Activate();
}
override public void Activate()
{
sfxEngine = Screen.Manager.Sound.Load(@"sfx/engine/int/engineThrust.wav", false);
sfxWhine = Screen.Manager.Sound.Load(@"sfx/engine/int/engineWhine.wav", false);
sfxTurn = new SoundPatch(Screen.Manager.Sound, @"sfx/ship/turn", 200);
sfxVacuum = new SoundPatch(Screen.Manager.Sound, @"sfx/ship/vacuum", 500);
sfxElevator = new SoundPatch(Screen.Manager.Sound, @"sfx/ship/elevator", 250);
sfxEngine.Loop = true;
sfxWhine.Loop = true;
sfxEngine.ConnectToEffect(Screen.Manager.EnvironmentEffect);
sfxWhine.ConnectToEffect(Screen.Manager.EnvironmentEffect);
sfxEngine.Play();
sfxWhine.Play();
sfxDamage = Screen.Manager.Sound.Load(@"sfx/ui/alarmDamage.wav", false);
sfxDamage.Loop = true;
sfxTaps = new AudioAsset(Screen.Manager.Sound, @"sfx/ship/taps", 1, 20);
return;
}
public override void Destroy()
{
sfxEngine.Destroy();
sfxWhine.Destroy();
sfxVacuum.Destroy();
sfxTurn.Destroy();
sfxTaps.Destroy();
sfxElevator.Destroy();
sfxDamage.Destroy();
return;
}
override public void Update(GameTime gameTime)
{
CurrentWeapon.Update(gameTime);
Move(Movement);
if (Turning == -1)
{
RotateY(0.035f);
}
if (Turning == 1)
{
RotateY(-0.035f);
}
if (Elevating == -1)
{
RotateX(-0.035f);
}
if (Elevating == 1)
{
RotateX(0.035f);
}
sfxEngine.Pitch = 0.5 + (Movement.Length() / MaxSpeed.Length()) * 4;
sfxWhine.Pitch = 0.5 + (Movement.Length() / MaxSpeed.Length()) * 1;
sfxWhine.Volume = MathF.Pow(Movement.Length() / MaxSpeed.Length(), 2f);
sfxTurn.Update(gameTime);
sfxElevator.Update(gameTime);
sfxVacuum.Update(gameTime);
if (Health < 100)
{
IncreaseHealth(0.01f);
}
if (Movement.Length() != 0)
{
if (Movement.X > 0)
{
Movement.X -= Deceleration.X;
} else
{
Movement.X += Deceleration.X;
}
if (Movement.Y > 0)
{
Movement.Y -= Deceleration.Y;
}
else
{
Movement.Y += Deceleration.Y;
}
if (Movement.Z > 0)
{
Movement.Z -= Deceleration.Z;
}
else
{
Movement.Z += Deceleration.Z;
}
}
}
public void Accelerate(Vector3 direction)
{
if (Math.Abs(Movement.X) < MaxSpeed.X && Math.Abs(Movement.Y) < MaxSpeed.Y && Math.Abs(Movement.Z) < MaxSpeed.Z)
{
Movement += direction;
}
}
public void StartTurning(int dir, GameTime time)
{
Turning = dir;
sfxTurn.Start(time);
PlayTap();
}
public void StopTurning()
{
Turning = 0;
sfxTurn.Stop();
}
public void StartElevating(int dir, GameTime time)
{
Elevating = dir;
sfxElevator.Start(time);
PlayTap();
}
public void StopElevating()
{
Elevating = 0;
sfxElevator.Stop();
}
public void StartVacuuming(GameTime time)
{
Vacuuming = true;
sfxVacuum.Start(time);
PlayTap();
}
public void StopVacuuming()
{
Vacuuming = false;
sfxVacuum.Stop();
}
public void TakeDamage(float dmg)
{
Health -= dmg;
if (Health < 25 && !IsDamaged)
{
IsDamaged = true;
sfxDamage.Play();
}
string dmgSound = @"sfx/physics/collisions/";
if (dmg > 60)
{
dmgSound += "heavy";
} else if (dmg > 30)
{
dmgSound += "medium";
} else
{
dmgSound += "light";
}
dmgSound += "/int/" + (rnd.Next(2)+1).ToString()+".wav";
Screen.Manager.Sound.PlayOneshot(dmgSound);
}
public void IncreaseHealth(float amount)
{
Health += amount;
if (Health > 25 && IsDamaged)
{
IsDamaged = false;
sfxDamage.Pause();
}
}
public void PlayTap()
{
sfxTaps.Play();
}
}
}