Files
evilsatellites/EvilSatellites/Objects/Satellite.cs
T

101 lines
3.6 KiB
C#
Raw Normal View History

2021-04-11 17:09:57 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SynthySound;
using EvilSatellites.Screens;
using Microsoft.Xna.Framework;
namespace EvilSatellites.Objects
{
public class Satellite : AbstractGameObject
{
private ISoundEffect3D Sound;
2021-04-14 16:33:05 +02:00
private int Health;
private int SatelliteID;
2021-04-18 00:27:13 +02:00
private Random rnd = new Random();
2021-04-19 15:41:37 +02:00
private Matrix FlightPath;
2021-04-14 16:33:05 +02:00
public Satellite(IScreen screen, Vector3 pos, int satelliteID = 1) : base(screen)
2021-04-11 17:09:57 +02:00
{
2021-04-11 18:42:22 +02:00
Type = ObjectType.Satellite;
2021-04-14 16:33:05 +02:00
SatelliteID = satelliteID;
Sound = Screen.Manager.Sound.Load3D(@"sfx/satellite/"+SatelliteID.ToString()+"/loop.wav");
2021-04-11 17:09:57 +02:00
Sound.Loop = true;
2021-04-19 15:41:37 +02:00
Sound.Pitch = 0.9 + rnd.NextDouble()*0.5;
2021-04-11 17:09:57 +02:00
Position = pos;
Dimensions = new Vector3(5, 5, 5);
Sound.Position = new System.Numerics.Vector3(pos.X, pos.Y, pos.Z);
Sound.ConnectToEffect(Screen.Manager.EnvironmentEffect);
2021-04-19 15:41:37 +02:00
FlightPath = Matrix.CreateFromAxisAngle(Rotation.Right, (float)rnd.NextDouble()*0.1f);
Rotation = FlightPath;
2021-04-11 17:09:57 +02:00
Activate();
}
public override void Activate()
{
Sound.Play();
2021-04-11 18:42:22 +02:00
Active = true;
ShouldDestroy = false;
2021-04-14 16:33:05 +02:00
Health = 100;
2021-04-11 17:09:57 +02:00
}
public override void Destroy()
{
Sound.Destroy();
2021-04-14 16:33:05 +02:00
ShouldDestroy = true;
Active = false;
2021-04-11 17:09:57 +02:00
}
override public void Update(GameTime gameTime)
{
2021-04-19 15:41:37 +02:00
Move(new Vector3(0, 0, -0.05f));
Sound.Position = new System.Numerics.Vector3(Position.X+Dimensions.X/2, Position.Y+Dimensions.Y/2, Position.Z+Dimensions.Z/2);
2021-04-23 15:07:50 +02:00
if (rnd.Next(1024) == 512)
2021-04-19 20:23:23 +02:00
{
var scr = (GameScreen)Screen;
var player = scr.GetPlayer();
2021-04-19 21:33:56 +02:00
var orient = Matrix.Invert(Matrix.CreateTranslation(Position) * Matrix.CreateLookAt(Position, scr.GetPlayer().Position, Rotation.Up));
var dir = Position - player.Position;
dir.Normalize();
2021-04-23 15:07:50 +02:00
Screen.Manager.Sound.PlayOneshot(@"sfx/weapons/LaserCannon/fire/" + (rnd.Next(2) + 1).ToString() + ".wav", new System.Numerics.Vector3(Position.X + Dimensions.X / 2, Position.Y + Dimensions.Y / 2, Position.Z / Dimensions.Z / 2), Screen.Manager.EnvironmentEffect);
2021-04-19 21:33:56 +02:00
var proj = new Projectile(Screen, Position, orient, this);
// proj.Direction = dir;
2021-04-19 20:23:23 +02:00
scr.AddObject(proj);
}
2021-04-11 17:09:57 +02:00
}
public override void OnCollision(IGameObject obj)
{
2021-04-19 20:23:23 +02:00
if (obj.Type == ObjectType.Projectile)
{
var proj = (Projectile)obj;
if (proj.Owner == this) return;
}
2021-04-14 16:33:05 +02:00
Health -= 10;
2021-04-19 15:41:37 +02:00
if (obj.Type == ObjectType.Player)
{
var player = (Player)obj;
player.TakeDamage(66);
Destroy();
}
2021-04-14 16:33:05 +02:00
if (Health <= 0)
{
Screen.Manager.Sound.PlayOneshot(@"sfx/satellite/"+SatelliteID.ToString()+"/explode.wav", new System.Numerics.Vector3(Position.X, Position.Y, Position.Z));
2021-04-18 00:27:13 +02:00
SpawnDebris();
2021-04-14 16:33:05 +02:00
Destroy();
2021-04-23 20:28:06 +02:00
GameStats.SatellitesShot++;
2021-04-14 16:33:05 +02:00
}
2021-04-11 17:09:57 +02:00
}
2021-04-18 00:27:13 +02:00
public void SpawnDebris()
{
2021-04-21 18:39:33 +02:00
var amount = rnd.Next(3) + 1;
for (var i = 0; i < amount; i++)
{
var screen = (GameScreen)Screen;
screen.AddObject(new Debris(screen, Position));
}
2021-04-18 00:27:13 +02:00
}
2021-04-11 17:09:57 +02:00
}
}