added reflection in mapobject creation and fixed Collisions

This commit is contained in:
SergoDobro 2024-08-19 17:25:05 +03:00
parent 4a04bf7b7f
commit f44ad37c1b
15 changed files with 72 additions and 47 deletions

View file

@ -23,7 +23,6 @@ using System.Web;
using ZoFo.GameCore.GUI;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using ZoFo.GameCore.Graphics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
@ -50,17 +49,17 @@ namespace ZoFo.GameCore
networkManager.AddData(new UpdateInput()
{
InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection,
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection
});
};
AppManager.Instance.InputManager.OnInteract += () =>
{
networkManager.AddData(new UpdateInputInteraction() { });
};
AppManager.Instance.InputManager.ShootEvent += () =>
AppManager.Instance.InputManager.ShootEvent += () =>
{
networkManager.AddData(new UpdateInputShoot() { });
networkManager.AddData(new UpdateInputShoot() { });
};
}
@ -95,7 +94,7 @@ namespace ZoFo.GameCore
Player myPlayer;
List<MapObject> mapObjects = new List<MapObject>();
List<GameObject> gameObjects = new List<GameObject>();
List<GameObject> gameObjects = new List<GameObject>();
List<Player> players = new List<Player>();
List<StopObject> stopObjects = new List<StopObject>();
@ -112,8 +111,13 @@ namespace ZoFo.GameCore
}
networkManager.SendData();//set to ticks
if (myPlayer!=null)
GraphicsComponent.CameraPosition = (myPlayer.position + myPlayer.graphicsComponent.ObjectDrawRectangle.Size.ToVector2()/2 - AppManager.Instance.CurentScreenResolution.ToVector2()/(2*GraphicsComponent.scaling)).ToPoint();
if (myPlayer != null)
GraphicsComponent.CameraPosition =
((GraphicsComponent.CameraPosition.ToVector2() *0.9f +
(myPlayer.position + myPlayer.graphicsComponent.ObjectDrawRectangle.Size.ToVector2() / 2 - AppManager.Instance.CurentScreenResolution.ToVector2() / (2 * GraphicsComponent.scaling)
) * 0.1f
) )
.ToPoint();
}
internal void Draw(SpriteBatch spriteBatch)
{
@ -151,47 +155,47 @@ namespace ZoFo.GameCore
(update as UpdateStopObjectCreated).Size.GetPoint().ToVector2(),
(update as UpdateStopObjectCreated).sourceRectangle.GetRectangle(),
(update as UpdateStopObjectCreated).tileSetName,
(update as UpdateStopObjectCreated).collisions.Select(x =>x.GetRectangle()).ToArray()
(update as UpdateStopObjectCreated).collisions.Select(x => x.GetRectangle()).ToArray()
));
}
else if (update is UpdateGameObjectCreated)
{
GameObject created_gameObject;
if ((update as UpdateGameObjectCreated).GameObjectType == "EntittyForAnimationTests")
gameObjects.Add(new EntittyForAnimationTests((update as UpdateGameObjectCreated).position));
if ((update as UpdateGameObjectCreated).GameObjectType == "Player")
GameObject created_gameObject;
if((update as UpdateGameObjectCreated).GameObjectType == "Player")
{
created_gameObject = new Player((update as UpdateGameObjectCreated).position);
players.Add(created_gameObject as Player);
myPlayer = players[0];
myPlayer = players[0];
gameObjects.Add(created_gameObject);
}
if ((update as UpdateGameObjectCreated).GameObjectType == "Ammo")
else if((update as UpdateGameObjectCreated).GameObjectType == "Ammo")
gameObjects.Add(new Ammo((update as UpdateGameObjectCreated).position));
if ((update as UpdateGameObjectCreated).GameObjectType == "Zombie")
else if((update as UpdateGameObjectCreated).GameObjectType == "Zombie")
gameObjects.Add(new Zombie((update as UpdateGameObjectCreated).position));
else
{
Type t = Type.GetType("ZoFo.GameCore.GameObjects." + (update as UpdateGameObjectCreated).GameObjectType);
GameObject gameObject = Activator.CreateInstance(t, (update as UpdateGameObjectCreated).position) as GameObject;
if (gameObject is Entity)
(gameObject as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
gameObjects.Add(gameObject);
}
(gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
(gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
//var a = Assembly.GetAssembly(typeof(GameObject));
//gameObjects.Add( TODO reflection
//Activator.CreateInstance(Type.GetType("ZoFo.GameCore.GameObjects.Entities.EntittyForAnimationTests")
///*(update as UpdateGameObjectCreated).GameObjectType*/, new []{ new Vector2(100, 100) })
//as GameObject
//);
}
else if (update is UpdatePosition)
{
var ent = FindEntityById(update.IdEntity);
ent.position = (update as UpdatePosition).NewPosition;
if (ent != null)
ent.position = (update as UpdatePosition).NewPosition;
}
else if (update is UpdateAnimation)
{
var ent = FindEntityById(update.IdEntity);
if (ent != null)
((ent as Entity).graphicsComponent as AnimatedGraphicsComponent).StartAnimation((update as UpdateAnimation).animationId);
//DebugHUD.Instance.Log("new Animation " + ent.position);
//DebugHUD.Instance.Log("new Animation " + ent.position);
}
else if (update is UpdateGameObjectDeleted)
{

View file

@ -105,7 +105,7 @@ namespace ZoFo.GameCore.GameManagers
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); }
Keyboard.GetState().IsKeyDown(Keys.Escape)) { server?.CloseConnection(); Exit(); }
// debugHud.Set("key", "value");

View file

@ -30,9 +30,10 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
/// <summary>
/// минимальный накоп изменения перед перевдижением
/// </summary>
const float minimalValueToChange = 4;
const float minimalValueToChange = 0;
public void CheckComponentCollision(CollisionComponent componentOfEntity)
{
//ADD CHECKSPEED TODO
var entity = componentOfEntity.gameObject as LivingEntity;
//for (int i = 0; i < ObjectsWithCollisions.Count; i++)
//{
@ -74,7 +75,8 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
}
else
{
entity.position.X += entity.velocity.X; //update player position
entity.position.X += (int)entity.velocity.X; //update player position
//entity.position.X = (float)Math.Round(entity.position.X, 2);
newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
}
@ -112,7 +114,8 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
}
else
{
entity.position.Y += entity.velocity.Y;
entity.position.Y += (int)entity.velocity.Y;
//entity.position.Y = (float)Math.Round(entity.position.Y, 2);
newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК
}
entity.velocity.Y = 0;

View file

@ -6,7 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities
namespace ZoFo.GameCore.GameObjects
{
internal class EntittyForAnimationTests : Entity
{

View file

@ -6,14 +6,14 @@ using Microsoft.Xna.Framework.Graphics;
using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager;
namespace ZoFo.GameCore.GameObjects.Entities
namespace ZoFo.GameCore.GameObjects
{
public abstract class Entity : GameObject
{
//public override GraphicsComponent graphicsComponent => null;
public CollisionComponent collisionComponent { get; protected set; }
public int Id { get; set; }
static int totalEntitiesCreated = 0;
static int totalEntitiesCreated = 1;
protected Entity(Vector2 position) : base(position)
{
Id = totalEntitiesCreated;

View file

@ -11,7 +11,7 @@ using ZoFo.GameCore.Graphics;
using Microsoft.Xna.Framework.Graphics;
using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
namespace ZoFo.GameCore.GameObjects
{
class Ammo:Collectable
{

View file

@ -4,7 +4,7 @@ using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
namespace ZoFo.GameCore.GameObjects;
public class Collectable : Interactable
{
protected static readonly string _path = "Textures/icons/Collectables/";

View file

@ -5,7 +5,7 @@ using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities.Interactables;
namespace ZoFo.GameCore.GameObjects;
public class Interactable : Entity
{

View file

@ -5,7 +5,7 @@ using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
namespace ZoFo.GameCore.GameObjects;
public class Enemy : LivingEntity
{
protected float speed;

View file

@ -8,7 +8,7 @@ using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.AssetsManager;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies
namespace ZoFo.GameCore.GameObjects
{
class Zombie : Enemy
{

View file

@ -6,7 +6,7 @@ using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities;
namespace ZoFo.GameCore.GameObjects;
public class LivingEntity : Entity
{
/// <summary>

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player
namespace ZoFo.GameCore.GameObjects
{
class LootData
{

View file

@ -10,7 +10,7 @@ using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
namespace ZoFo.GameCore.GameObjects;
public class Player : LivingEntity
{
@ -29,7 +29,7 @@ public class Player : LivingEntity
{
graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 30, 30);
collisionComponent.stopRectangle = new Rectangle(0, 20, 30, 10);
speed = 10;
speed = 5;
//isTryingToInteract = false;
//IsTryingToShoot = false;
@ -44,7 +44,7 @@ public class Player : LivingEntity
}
public void MovementLogic()
{
velocity = InputPlayerRotation * speed;
velocity += InputPlayerRotation * speed;
}
public void HandleNewInput(UpdateInput updateInput)
{

View file

@ -10,7 +10,7 @@ using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.MapObjects
namespace ZoFo.GameCore.GameObjects
{
public class MapObject : GameObject
{
@ -34,6 +34,25 @@ namespace ZoFo.GameCore.GameObjects.MapObjects
(graphicsComponent as StaticGraphicsComponent).LoadContent();
this.sourceRectangle = sourceRectangle;
}
/// <summary>
/// Конструктор для объектов с карты с анимациями, REDO
/// </summary>
/// <param name="position"></param>
/// <param name="size"></param>
/// <param name="sourceRectangle"></param>
/// <param name="textureName"></param>
/// <param name="sourceRectangles"></param>
/// <param name="frameSize"></param>
public MapObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName, Rectangle sourceRectangles, int frameSize) : base(position)
{
//graphicsComponent for source
(graphicsComponent as StaticGraphicsComponent)._textureName = textureName;
(graphicsComponent as StaticGraphicsComponent).BuildComponent(textureName);
(graphicsComponent as StaticGraphicsComponent).ObjectDrawRectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
(graphicsComponent as StaticGraphicsComponent).LoadContent();
this.sourceRectangle = sourceRectangle;
}
public override void Draw(SpriteBatch spriteBatch)
{

View file

@ -16,8 +16,7 @@ using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameObjects;
using ZoFo.GameCore.GameObjects.Entities;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.GameObjects.MapObjects;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;