Files
evilsatellites/EvilSatellites/Screens/GameScreen.cs
T

232 lines
8.0 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 18:42:22 +02:00
public List<IGameObject> Removable = new List<IGameObject>();
public List<IGameObject> Addable = 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-19 19:49:13 +02:00
private SatelliteSpawner Spawner;
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-19 19:49:13 +02:00
// Satellite prop = new Satellite(this, new Vector3(5000, 5000, 5000));
// Objects.Add(prop);
2021-04-11 17:09:57 +02:00
ObjectRadar = new Radar(_player, this);
2021-04-19 19:49:13 +02:00
Spawner = new SatelliteSpawner(this);
Spawner.Spawn();
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 18:42:22 +02:00
foreach (var obj in Removable)
{
Objects.Remove(obj);
}
if (Addable.Count > 0)
{
foreach (var obj in Addable)
{
Objects.Add(obj);
}
Addable.Clear();
}
Removable.Clear();
2021-04-11 14:12:55 +02:00
objtree.Clear();
2021-04-11 18:42:22 +02:00
foreach (var obj in Objects)
2021-04-10 14:08:20 +02:00
{
obj.Update(gameTime);
2021-04-11 18:42:22 +02:00
objtree.Add(obj);
2021-04-10 14:08:20 +02:00
if (obj.Type == ObjectType.Player)
{
2021-04-19 15:41:37 +02:00
Manager.Sound.ListenerPosition = new System.Numerics.Vector3(obj.Position.X+obj.Dimensions.X/2, obj.Position.Y+obj.Dimensions.Y/2, obj.Position.Z+obj.Dimensions.Z/2);
2021-04-10 14:08:20 +02:00
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 18:42:22 +02:00
if (obj.ShouldDestroy)
{
Removable.Add(obj);
}
2021-04-10 14:08:20 +02:00
}
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()
{
2021-04-11 18:42:22 +02:00
var candidates = objtree.Find(_player.Position, new Vector3(1000, 1000, 1000));
2021-04-19 19:49:13 +02:00
ObjectRadar.Update(Objects);
2021-04-11 17:09:57 +02:00
}
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 20:43:29 +02:00
move.Z = -_player.Acceleration.Z;
2021-04-11 14:12:55 +02:00
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.S))
2021-04-11 14:12:55 +02:00
{
2021-04-11 20:43:29 +02:00
move.Z = _player.Acceleration.Z;
2021-04-11 14:12:55 +02:00
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.A))
2021-04-11 14:12:55 +02:00
{
2021-04-11 20:43:29 +02:00
move.X = -_player.Acceleration.X;
2021-04-11 14:12:55 +02:00
}
2021-04-11 17:09:57 +02:00
if (Input.IsPressed(Keys.D))
2021-04-11 14:12:55 +02:00
{
2021-04-11 20:43:29 +02:00
move.X = _player.Acceleration.X;
2021-04-11 02:01:24 +02:00
}
2021-04-19 17:22:14 +02:00
if (Input.IsJustPressed(Keys.A) || Input.IsJustPressed(Keys.S) || Input.IsJustPressed(Keys.D) || Input.IsJustPressed(Keys.W))
{
_player.PlayTap();
}
2021-04-18 22:35:49 +02:00
if (Input.IsJustPressed(Keys.Left))
2021-04-11 02:01:24 +02:00
{
2021-04-18 22:35:49 +02:00
_player.StartTurning(-1, gameTime);
2021-04-11 02:01:24 +02:00
}
2021-04-18 22:35:49 +02:00
if (Input.IsJustPressed(Keys.Right))
2021-04-11 02:01:24 +02:00
{
2021-04-18 22:35:49 +02:00
_player.StartTurning(1, gameTime);
2021-04-11 02:01:24 +02:00
}
2021-04-18 22:35:49 +02:00
if (Input.IsJustReleased(Keys.Left) || Input.IsJustReleased(Keys.Right))
2021-04-11 02:01:24 +02:00
{
2021-04-18 22:35:49 +02:00
_player.StopTurning();
2021-04-11 02:01:24 +02:00
}
2021-04-18 22:35:49 +02:00
if (Input.IsJustPressed(Keys.Up))
2021-04-11 02:01:24 +02:00
{
2021-04-18 22:35:49 +02:00
_player.StartElevating(-1, gameTime);
}
if (Input.IsJustPressed(Keys.Down))
{
_player.StartElevating(1, gameTime);
}
if (Input.IsJustReleased(Keys.Up) || Input.IsJustReleased(Keys.Down))
{
_player.StopElevating();
2021-04-11 02:01:24 +02:00
}
2021-04-19 15:41:37 +02:00
if (Input.IsJustPressed(Keys.V))
{
_player.StartVacuuming(gameTime);
}
if (Input.IsJustReleased(Keys.V))
{
_player.StopVacuuming();
}
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);
2021-04-19 17:22:14 +02:00
_player.PlayTap();
2021-04-11 17:09:57 +02:00
}
if (Input.IsJustReleased(Keys.Space))
{
_player.CurrentWeapon.Stop();
}
if (Input.IsJustPressed(Keys.R))
{
2021-04-19 17:22:14 +02:00
_player.PlayTap();
2021-04-11 17:09:57 +02:00
if (ObjectRadar.Active)
{
ObjectRadar.Stop();
} else
{
ObjectRadar.Start();
}
}
2021-04-19 15:41:37 +02:00
if (Input.IsJustPressed(Keys.T))
2021-04-11 18:42:22 +02:00
{
2021-04-19 17:22:14 +02:00
_player.PlayTap();
2021-04-19 15:41:37 +02:00
if (ObjectRadar.Filter == ObjectType.Satellite)
{
ObjectRadar.SetObjectType(ObjectType.Debris);
Tolk.Output("Debris");
} else
{
ObjectRadar.SetObjectType(ObjectType.Satellite);
Tolk.Output("Satellites");
}
2021-04-11 18:42:22 +02:00
}
2021-04-19 23:37:40 +02:00
if (Input.IsJustPressed(Keys.H))
{
_player.PlayTap();
Tolk.Output(_player.Health.ToString() + " health", true);
}
if (Input.IsJustPressed(Keys.X))
{
_player.PlayTap();
Tolk.Output(_player.CurrentWeapon.RemainingAmmo.ToString() + " rounds", true);
}
2021-04-19 19:49:13 +02:00
if (Input.IsJustPressed(Keys.O))
{
DavyKager.Tolk.Output(Objects.Count.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)
{
2021-04-11 18:42:22 +02:00
if (!obj.Active) continue;
2021-04-11 14:12:55 +02:00
var candidates = objtree.Find(obj.Position, obj.Dimensions);
foreach (var target in candidates)
{
2021-04-11 18:42:22 +02:00
if (obj == target || !target.Active) continue;
2021-04-11 14:12:55 +02:00
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-11 18:42:22 +02:00
public void AddObject(IGameObject obj)
{
Addable.Add(obj);
}
2021-04-19 20:23:23 +02:00
public Player GetPlayer()
{
return _player;
}
2021-04-10 14:08:20 +02:00
}
}