2021-04-10 14:08:20 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
using SynthySound;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using EvilSatellites.Objects;
|
2021-04-11 02:01:24 +02:00
|
|
|
using DavyKager;
|
2021-04-11 14:12:55 +02:00
|
|
|
using EvilSatellites.Quadtree;
|
2021-04-10 14:08:20 +02:00
|
|
|
|
|
|
|
|
namespace EvilSatellites.Screens
|
|
|
|
|
{
|
|
|
|
|
public class GameScreen : IScreen
|
|
|
|
|
{
|
|
|
|
|
public ScreenManager Manager { get; set; }
|
|
|
|
|
public ScreenState State { get; set; }
|
|
|
|
|
public List<IGameObject> Objects = new List<IGameObject>();
|
2021-04-11 14:12:55 +02:00
|
|
|
private Quadtree<IGameObject> objtree = new Quadtree<IGameObject>(new Vector3(10000, 10000, 10000), 10, 20);
|
2021-04-10 14:08:20 +02:00
|
|
|
private float Theta = 0;
|
2021-04-11 02:01:24 +02:00
|
|
|
private Player _player;
|
|
|
|
|
private ISoundEffect radarNoise;
|
2021-04-10 14:08:20 +02:00
|
|
|
public void Activate()
|
|
|
|
|
{
|
|
|
|
|
State = ScreenState.Active;
|
|
|
|
|
Player player = new Player(this);
|
|
|
|
|
Objects.Add(player);
|
2021-04-11 02:01:24 +02:00
|
|
|
_player = player;
|
|
|
|
|
SynthyProp prop = new SynthyProp(this, new Vector3(15, 0, 15));
|
|
|
|
|
Objects.Add(prop);
|
|
|
|
|
radarNoise = Manager.Sound.Load(@"sfx/ui/voiceError.wav", false);
|
|
|
|
|
radarNoise.Loop = true;
|
|
|
|
|
radarNoise.Play();
|
2021-04-10 14:08:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deactivate()
|
|
|
|
|
{
|
|
|
|
|
State = ScreenState.Hidden;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-11 02:01:24 +02:00
|
|
|
|
2021-04-10 14:08:20 +02:00
|
|
|
public void Update(GameTime gameTime)
|
|
|
|
|
{
|
2021-04-11 14:12:55 +02:00
|
|
|
objtree.Clear();
|
2021-04-10 14:08:20 +02:00
|
|
|
foreach (var obj in Objects)
|
|
|
|
|
{
|
2021-04-11 14:12:55 +02:00
|
|
|
objtree.Add(obj);
|
2021-04-10 14:08:20 +02:00
|
|
|
obj.Update(gameTime);
|
|
|
|
|
if (obj.Type == ObjectType.Player)
|
|
|
|
|
{
|
|
|
|
|
Manager.Sound.ListenerPosition = new System.Numerics.Vector3(obj.Position.X, obj.Position.Y, obj.Position.Z);
|
|
|
|
|
Manager.Sound.ListenerOrientationForward = System.Numerics.Vector3.Normalize(new System.Numerics.Vector3(obj.Rotation.Forward.X, obj.Rotation.Forward.Y, obj.Rotation.Forward.Z));
|
|
|
|
|
Manager.Sound.ListenerOrientationUpwards = System.Numerics.Vector3.Normalize(new System.Numerics.Vector3(obj.Rotation.Up.X, obj.Rotation.Up.Y, obj.Rotation.Up.Z));
|
|
|
|
|
Manager.Listener.Position = obj.Position;
|
|
|
|
|
Manager.Listener.Forward = obj.Rotation.Forward;
|
|
|
|
|
Manager.Listener.Up = obj.Rotation.Up;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-11 02:01:24 +02:00
|
|
|
Vector3 radarTarget = (Objects[1].Position - _player.Position);
|
|
|
|
|
radarTarget.Normalize();
|
|
|
|
|
radarNoise.Pitch = MathHelper.Clamp(0.1f / Vector3.Distance(_player.Rotation.Forward, radarTarget)*3f, 0, 1);
|
2021-04-11 14:12:55 +02:00
|
|
|
CheckCollisions();
|
2021-04-10 14:08:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleInput(GameTime gameTime, KeyboardState inputs)
|
|
|
|
|
{
|
2021-04-11 14:12:55 +02:00
|
|
|
Vector3 move = new Vector3(0, 0, 0);
|
2021-04-11 02:01:24 +02:00
|
|
|
if (inputs.IsKeyDown(Keys.W))
|
|
|
|
|
{
|
2021-04-11 14:12:55 +02:00
|
|
|
move.Z = -0.035f;
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.S))
|
|
|
|
|
{
|
|
|
|
|
move.Z = 0.035f;
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.A))
|
|
|
|
|
{
|
|
|
|
|
move.X = -0.035f;
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.D))
|
|
|
|
|
{
|
|
|
|
|
move.X = 0.035f;
|
2021-04-11 02:01:24 +02:00
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.Left))
|
|
|
|
|
{
|
|
|
|
|
_player.RotateY(0.05f);
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.Right))
|
|
|
|
|
{
|
|
|
|
|
_player.RotateY(-0.05f);
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.Up))
|
|
|
|
|
{
|
|
|
|
|
_player.RotateX(0.05f);
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.Down))
|
|
|
|
|
{
|
|
|
|
|
_player.RotateX(-0.05f);
|
|
|
|
|
}
|
2021-04-11 14:12:55 +02:00
|
|
|
_player.Accelerate(move);
|
2021-04-11 02:01:24 +02:00
|
|
|
if (inputs.IsKeyDown(Keys.V))
|
|
|
|
|
{
|
|
|
|
|
Tolk.Speak(_player.Movement.Length().ToString(), true);
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.X))
|
|
|
|
|
{
|
|
|
|
|
Tolk.Speak(radarNoise.Pitch.ToString(), true);
|
|
|
|
|
}
|
|
|
|
|
if (inputs.IsKeyDown(Keys.C))
|
|
|
|
|
{
|
|
|
|
|
Tolk.Speak(_player.Position.ToString());
|
|
|
|
|
}
|
2021-04-10 14:08:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-11 14:12:55 +02:00
|
|
|
private void CheckCollisions()
|
|
|
|
|
{
|
|
|
|
|
foreach (var obj in Objects)
|
|
|
|
|
{
|
|
|
|
|
var candidates = objtree.Find(obj.Position, obj.Dimensions);
|
|
|
|
|
foreach (var target in candidates)
|
|
|
|
|
{
|
|
|
|
|
if (obj == target) continue;
|
|
|
|
|
if (Intersects(obj, target))
|
|
|
|
|
{
|
|
|
|
|
obj.OnCollision(target);
|
|
|
|
|
target.OnCollision(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Intersects(IGameObject obj1, IGameObject obj2)
|
|
|
|
|
{
|
|
|
|
|
return (obj1.Position.X <= (obj2.Position.X + obj2.Dimensions.X) && (obj1.Position.X + obj1.Dimensions.X) >= obj2.Position.X) && (obj1.Position.Y <= (obj2.Position.Y + obj2.Dimensions.Y) && (obj1.Position.Y + obj1.Dimensions.Y) >= obj2.Position.Y) && (obj1.Position.Z <= (obj2.Position.Z + obj2.Dimensions.Z) && (obj1.Position.Z + obj1.Dimensions.Z) >= obj2.Position.Z);
|
|
|
|
|
}
|
2021-04-10 14:08:20 +02:00
|
|
|
}
|
|
|
|
|
}
|