Files
evilsatellites/EvilSatellites/Screens/GameScreen.cs
T

158 lines
5.3 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;
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; }
2021-04-11 17:09:57 +02:00
private InputManager Input = new InputManager();
2021-04-10 14:08:20 +02:00
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;
2021-04-11 17:09:57 +02:00
private Radar ObjectRadar;
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;
2021-04-11 17:09:57 +02:00
Satellite prop = new Satellite(this, new Vector3(15, 0, 15));
2021-04-11 02:01:24 +02:00
Objects.Add(prop);
2021-04-11 17:09:57 +02:00
ObjectRadar = new Radar(_player, this);
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 17:09:57 +02:00
UpdateRadar();
2021-04-11 14:12:55 +02:00
CheckCollisions();
2021-04-10 14:08:20 +02:00
return;
}
2021-04-11 17:09:57 +02:00
private void UpdateRadar()
{
var candidates = objtree.Find(_player.Position, _player.Dimensions);
ObjectRadar.Update(Objects);
}
2021-04-10 14:08:20 +02:00
public void HandleInput(GameTime gameTime, KeyboardState inputs)
{
2021-04-11 17:09:57 +02:00
Input.Update();
2021-04-11 14:12:55 +02:00
Vector3 move = new Vector3(0, 0, 0);
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.W))
2021-04-11 02:01:24 +02:00
{
2021-04-11 14:12:55 +02:00
move.Z = -0.035f;
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.S))
2021-04-11 14:12:55 +02:00
{
move.Z = 0.035f;
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.A))
2021-04-11 14:12:55 +02:00
{
move.X = -0.035f;
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.D))
2021-04-11 14:12:55 +02:00
{
move.X = 0.035f;
2021-04-11 02:01:24 +02:00
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.Left))
2021-04-11 02:01:24 +02:00
{
_player.RotateY(0.05f);
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.Right))
2021-04-11 02:01:24 +02:00
{
_player.RotateY(-0.05f);
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.Up))
2021-04-11 02:01:24 +02:00
{
_player.RotateX(0.05f);
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.Down))
2021-04-11 02:01:24 +02:00
{
_player.RotateX(-0.05f);
}
2021-04-11 14:12:55 +02:00
_player.Accelerate(move);
2021-04-11 17:09:57 +02:00
if (Input.IsJustPressed(Keys.Space))
{
_player.CurrentWeapon.Shoot(gameTime);
}
if (Input.IsJustReleased(Keys.Space))
{
_player.CurrentWeapon.Stop();
}
if (Input.IsJustPressed(Keys.V))
2021-04-11 02:01:24 +02:00
{
Tolk.Speak(_player.Movement.Length().ToString(), true);
}
2021-04-11 17:09:57 +02:00
if (Input.IsJustPressed(Keys.C))
2021-04-11 02:01:24 +02:00
{
Tolk.Speak(_player.Position.ToString());
}
2021-04-11 17:09:57 +02:00
if (Input.IsJustPressed(Keys.R))
{
if (ObjectRadar.Active)
{
ObjectRadar.Stop();
} else
{
ObjectRadar.Start();
}
}
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
}
}