Files
evilsatellites/EvilSatellites/Objects/Player.cs
T

243 lines
7.2 KiB
C#
Raw Normal View History

2021-04-10 14:08:20 +02:00
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
2021-04-11 02:01:24 +02:00
using SynthySound;
2021-04-11 17:09:57 +02:00
using EvilSatellites.Screens;
using EvilSatellites.Weapons;
2021-04-10 14:08:20 +02:00
namespace EvilSatellites.Objects
{
public class Player : AbstractGameObject
{
private Vector3 _PrevPos;
2021-04-11 20:43:29 +02:00
private Vector3 MaxSpeed = new Vector3(0.2f, 0.2f, 0.2f);
2021-04-19 17:22:14 +02:00
public Vector3 Acceleration = new Vector3(0.0035f, 0.004f, 0.004f);
2021-04-11 20:43:29 +02:00
private Vector3 Deceleration = new Vector3(0.0025f, 0.0025f, 0.0025f);
2021-04-11 02:01:24 +02:00
private ISoundEffect sfxEngine;
private ISoundEffect sfxWhine;
2021-04-19 17:22:14 +02:00
private ISoundEffect sfxDamage;
2021-04-18 22:35:49 +02:00
private SoundPatch sfxElevator;
private SoundPatch sfxVacuum;
private SoundPatch sfxTurn;
2021-04-19 17:22:14 +02:00
private AudioAsset sfxTaps;
2021-04-11 17:09:57 +02:00
public IWeapon CurrentWeapon;
2021-04-18 22:35:49 +02:00
private int Turning;
private int Elevating;
2021-04-19 15:41:37 +02:00
public bool Vacuuming;
public float Health;
private Random rnd = new Random();
2021-04-19 17:22:14 +02:00
private bool IsDamaged;
2021-04-10 14:08:20 +02:00
public Player(IScreen screen): base(screen)
{
2021-04-11 18:42:22 +02:00
ShouldDestroy = false;
Active = true;
2021-04-10 14:08:20 +02:00
Type = ObjectType.Player;
2021-04-24 02:47:17 +02:00
Position = new Vector3(25000, 25000, 25000);
2021-04-11 02:01:24 +02:00
Dimensions = new Vector3(5, 5, 5);
Movement = new Vector3(0, 0, 0);
2021-04-10 14:08:20 +02:00
Rotation = Matrix.Identity;
Orientation = Rotation.Forward;
2021-04-11 18:42:22 +02:00
CurrentWeapon = new LaserMG(this, (GameScreen)Screen);
2021-04-18 22:35:49 +02:00
Turning = 0;
2021-04-19 15:41:37 +02:00
Elevating = 0;
Vacuuming = false;
Health = 100;
2021-04-10 14:08:20 +02:00
Activate();
}
override public void Activate()
{
2021-04-11 02:01:24 +02:00
sfxEngine = Screen.Manager.Sound.Load(@"sfx/engine/int/engineThrust.wav", false);
sfxWhine = Screen.Manager.Sound.Load(@"sfx/engine/int/engineWhine.wav", false);
2021-04-18 22:35:49 +02:00
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);
2021-04-11 02:01:24 +02:00
sfxEngine.Loop = true;
sfxWhine.Loop = true;
sfxEngine.ConnectToEffect(Screen.Manager.EnvironmentEffect);
sfxWhine.ConnectToEffect(Screen.Manager.EnvironmentEffect);
2021-04-11 02:01:24 +02:00
sfxEngine.Play();
sfxWhine.Play();
2021-04-19 17:22:14 +02:00
sfxDamage = Screen.Manager.Sound.Load(@"sfx/ui/alarmDamage.wav", false);
sfxDamage.Loop = true;
sfxTaps = new AudioAsset(Screen.Manager.Sound, @"sfx/ship/taps", 1, 20);
2021-04-10 14:08:20 +02:00
return;
}
public override void Destroy()
{
2021-04-19 21:33:56 +02:00
sfxEngine.Destroy();
sfxWhine.Destroy();
sfxVacuum.Destroy();
sfxTurn.Destroy();
sfxTaps.Destroy();
sfxElevator.Destroy();
sfxDamage.Destroy();
2021-04-10 14:08:20 +02:00
return;
}
override public void Update(GameTime gameTime)
{
2021-04-11 17:09:57 +02:00
CurrentWeapon.Update(gameTime);
2021-04-11 02:01:24 +02:00
Move(Movement);
2021-04-18 22:35:49 +02:00
if (Turning == -1)
{
RotateY(0.035f);
}
if (Turning == 1)
{
RotateY(-0.035f);
}
if (Elevating == -1)
{
RotateX(-0.035f);
}
if (Elevating == 1)
{
RotateX(0.035f);
}
2021-04-11 02:01:24 +02:00
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);
2021-04-18 22:35:49 +02:00
sfxTurn.Update(gameTime);
sfxElevator.Update(gameTime);
sfxVacuum.Update(gameTime);
2021-04-19 23:37:40 +02:00
if (Health < 100)
{
IncreaseHealth(0.01f);
}
2021-04-11 02:01:24 +02:00
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;
}
}
}
2021-04-11 14:12:55 +02:00
public void Accelerate(Vector3 direction)
2021-04-11 02:01:24 +02:00
{
if (Math.Abs(Movement.X) < MaxSpeed.X && Math.Abs(Movement.Y) < MaxSpeed.Y && Math.Abs(Movement.Z) < MaxSpeed.Z)
{
2021-04-11 14:12:55 +02:00
Movement += direction;
2021-04-11 02:01:24 +02:00
}
2021-04-10 14:08:20 +02:00
}
2021-04-18 22:35:49 +02:00
public void StartTurning(int dir, GameTime time)
{
Turning = dir;
sfxTurn.Start(time);
2021-04-19 17:22:14 +02:00
PlayTap();
2021-04-18 22:35:49 +02:00
}
public void StopTurning()
{
Turning = 0;
sfxTurn.Stop();
}
public void StartElevating(int dir, GameTime time)
{
Elevating = dir;
sfxElevator.Start(time);
2021-04-19 17:22:14 +02:00
PlayTap();
2021-04-18 22:35:49 +02:00
}
public void StopElevating()
{
Elevating = 0;
sfxElevator.Stop();
}
2021-04-19 15:41:37 +02:00
public void StartVacuuming(GameTime time)
{
Vacuuming = true;
sfxVacuum.Start(time);
2021-04-19 17:22:14 +02:00
PlayTap();
2021-04-19 15:41:37 +02:00
}
public void StopVacuuming()
{
Vacuuming = false;
sfxVacuum.Stop();
}
public void TakeDamage(float dmg)
{
Health -= dmg;
2021-04-19 17:22:14 +02:00
if (Health < 25 && !IsDamaged)
{
IsDamaged = true;
sfxDamage.Play();
}
2021-04-19 15:41:37 +02:00
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);
2021-04-24 02:47:17 +02:00
Screen.Manager.Vibrations.AddVibration(1, 1, 500);
2021-04-21 14:14:14 +02:00
if (Health < 1)
{
Destroy();
Screen.Manager.Sound.PlayOneshot(@"sfx/physics/crashes/1.wav");
2021-04-24 02:47:17 +02:00
Screen.Manager.Vibrations.AddVibration(1, 1, 1000);
2021-04-23 20:28:06 +02:00
var scr = (GameScreen)Screen;
scr.EndGame();
2021-04-21 14:14:14 +02:00
}
2021-04-19 15:41:37 +02:00
}
2021-04-19 17:22:14 +02:00
2021-04-19 23:37:40 +02:00
public void IncreaseHealth(float amount)
{
Health += amount;
if (Health > 25 && IsDamaged)
{
IsDamaged = false;
sfxDamage.Pause();
}
}
2021-04-19 17:22:14 +02:00
public void PlayTap()
{
sfxTaps.Play();
}
2021-04-19 23:43:23 +02:00
override public void OnCollision(IGameObject obj)
{
if (obj.Type == ObjectType.Projectile)
{
var proj = (Projectile)obj;
if (proj.Owner == this) return;
TakeDamage(rnd.Next(20));
}
}
2021-04-10 14:08:20 +02:00
}
}