FixLowFpsBug

This commit is contained in:
Timofey06 2023-08-17 16:17:33 +03:00
parent 591bdf24b6
commit 81dd082141
9 changed files with 47 additions and 58 deletions

View file

@ -23,7 +23,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
{
Width = 24;
Height = 40;
monster_speed = 3;
monster_speed = 1;
name = "Zombie";
leftBorder = (int)position.X - 100;
rightBorder = (int)position.X + 100;
@ -114,7 +114,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
public void Target()
{
if(physicsManager.RayCast(this, AppManager.Instance.GameManager.players[0]) == null)
if(AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X-50, (int)Pos.Y, Width+100, Height), typeof(Player))!=null)
{
if(isGoRight && this._pos.X <= AppManager.Instance.GameManager.players[0].Pos.X)
{

View file

@ -19,12 +19,12 @@ public abstract class LivingEntity : Entity
public override void Update(GameTime gameTime)
{
if (Vector2.Distance(Pos, targetPosition) > 0.5f)
{
Vector2 dir = targetPosition - Pos;
dir.Normalize();
_pos += dir * velocity;
}
//if (Vector2.DistanceSquared(Pos, targetPosition) > 0.25f)
//{
// Vector2 dir = targetPosition - Pos;
// dir.Normalize();
// _pos += dir * velocity;
//}
base.Update(gameTime);
}

View file

@ -7,7 +7,7 @@ namespace DangerousD.GameCore.GameObjects;
public abstract class MapObject : GameObject
{
public bool IsColliderOn;
public virtual bool IsColliderOn { get; protected set; } = true;
private Rectangle _sourceRectangle;
protected override GraphicsComponent GraphicsComponent { get; } = new("tiles");
public MapObject(Vector2 position, Vector2 size, Rectangle sourceRectangle) : base(position)

View file

@ -5,8 +5,9 @@ namespace DangerousD.GameCore.GameObjects.MapObjects;
public class Platform : MapObject
{
public override bool IsColliderOn { get; protected set; } = true;
public Platform(Vector2 position, Vector2 size, Rectangle sourceRectangle) : base(position, size, sourceRectangle)
{
IsColliderOn = true;
}
}

View file

@ -5,8 +5,8 @@ namespace DangerousD.GameCore.GameObjects.MapObjects;
public class StopTile : MapObject
{
public override bool IsColliderOn { get; protected set; } = true;
public StopTile(Vector2 position, Vector2 size, Rectangle sourceRectangle) : base(position, size, sourceRectangle)
{
IsColliderOn = true;
}
}

View file

@ -5,8 +5,8 @@ namespace DangerousD.GameCore.GameObjects.MapObjects;
public class Tile : MapObject
{
public override bool IsColliderOn { get; protected set; } = false;
public Tile(Vector2 position, Vector2 size, Rectangle sourceRectangle) : base(position, size, sourceRectangle)
{
IsColliderOn = false;
{
}
}

View file

@ -17,6 +17,7 @@ namespace DangerousD.GameCore
public List<LivingEntity> livingEntities;
public List<Entity> entities;
public List<MapObject> mapObjects;
public List<MapObject> BackgroundObjects;
public List<GameObject> others;
public MapManager mapManager;
public PhysicsManager physicsManager;
@ -29,6 +30,7 @@ namespace DangerousD.GameCore
GetAllGameObjects = new List<GameObject>();
livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>();
BackgroundObjects = new List<MapObject>();
entities = new List<Entity>();
players = new List<Player>();
mapManager = new MapManager(1);
@ -46,23 +48,27 @@ namespace DangerousD.GameCore
internal void Register(GameObject gameObject)
{
if (gameObject is Player)
//GetAllGameObjects.Add(gameObject);
if (gameObject is Player objPl)
{
livingEntities.Add(gameObject as LivingEntity);
players.Add(gameObject as Player);
players.Add(objPl);
GetPlayer1 = players[0];
}
else if (gameObject is LivingEntity)
else if (gameObject is LivingEntity objLE)
{
livingEntities.Add(gameObject as LivingEntity);
livingEntities.Add(objLE);
}
else if (gameObject is Entity)
else if (gameObject is Entity objE)
{
entities.Add(gameObject as Entity);
entities.Add(objE);
}
else if (gameObject is MapObject)
else if (gameObject is MapObject obj)
{
mapObjects.Add(gameObject as MapObject);
if (obj.IsColliderOn)
mapObjects.Add(obj);
else
BackgroundObjects.Add(obj);
}
else
{
@ -72,6 +78,8 @@ namespace DangerousD.GameCore
public void Draw(SpriteBatch _spriteBatch)
{
foreach (var item in BackgroundObjects)
item.Draw(_spriteBatch);
foreach (var item in mapObjects)
item.Draw(_spriteBatch);
foreach (var item in entities)
@ -84,15 +92,15 @@ namespace DangerousD.GameCore
public void Update(GameTime gameTime)
{
foreach (var item in BackgroundObjects)
item.Update(gameTime);
foreach (var item in mapObjects)
item.Update(gameTime);
foreach (var item in entities)
item.Update(gameTime);
for (int i = 0; i < livingEntities.Count; i++)
{
livingEntities[i].Update(gameTime);
}
foreach (var item in otherObjects)
item.Update(gameTime);

View file

@ -8,6 +8,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;
using DangerousD.GameCore.GameObjects;
using System.Globalization;
namespace DangerousD.GameCore.Managers
{
@ -46,8 +47,8 @@ namespace DangerousD.GameCore.Managers
private void InstantiateTiles(XmlNode layer, Vector2 tileSize)
{
string tileType = layer.Attributes["class"].Value;
float offsetX = layer.Attributes["offsetx"] is not null ? float.Parse(layer.Attributes["offsetx"].Value) : 0;
float offsetY = layer.Attributes["offsety"] is not null ? float.Parse(layer.Attributes["offsety"].Value) : 0;
float offsetX = layer.Attributes["offsetx"] is not null ? float.Parse(layer.Attributes["offsetx"].Value, CultureInfo.InvariantCulture) : 0;
float offsetY = layer.Attributes["offsety"] is not null ? float.Parse(layer.Attributes["offsety"].Value, CultureInfo.InvariantCulture) : 0;
Debug.Write(layer.SelectNodes("data/chunk").Count);
@ -105,7 +106,7 @@ namespace DangerousD.GameCore.Managers
foreach (XmlNode entity in group.ChildNodes)
{
Type type = Type.GetType($"DangerousD.GameCore.GameObjects.{entityType}");
Entity inst = (Entity)Activator.CreateInstance(type, new Vector2(float.Parse(entity.Attributes["x"].Value) + offsetX, float.Parse(entity.Attributes["y"].Value) + offsetY) * _scale);
Entity inst = (Entity)Activator.CreateInstance(type, new Vector2(float.Parse(entity.Attributes["x"].Value, CultureInfo.InvariantCulture) + offsetX, float.Parse(entity.Attributes["y"].Value, CultureInfo.InvariantCulture) + offsetY) * _scale);
inst.SetPosition(new Vector2(inst.Pos.X, inst.Pos.Y - inst.Height));
inst.Height *= _scale;
inst.Width *= _scale;

View file

@ -36,33 +36,24 @@ namespace DangerousD.GameCore.Managers
public void CheckCollisions(List<LivingEntity> livingEntities,
List<MapObject> mapObjects)
{
LivingEntity currentEntity;
Rectangle oldRect;
for (int i = 0; i < livingEntities.Count; i++)
{
var currentEntity = livingEntities[i];
Rectangle oldRect = currentEntity.Rectangle;
currentEntity = livingEntities[i];
oldRect = currentEntity.Rectangle;
bool isXNormalise = true;
bool isYNormalise = true;
oldRect.Offset((int)currentEntity.velocity.X / 2, 0);
oldRect.Offset((int)currentEntity.velocity.X, 0);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
isXNormalise = false;
oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0);
break;
}
}
if (isXNormalise)
{
oldRect.Offset((int)currentEntity.velocity.X / 2, 0);
for (int j = 0; j < mapObjects.Count; j++)
if (Math.Abs(mapObjects[i].Pos.X - currentEntity.Pos.X)< currentEntity.velocity.X*2 && Math.Abs(mapObjects[i].Pos.Y - currentEntity.Pos.Y) < 50)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
isXNormalise = false;
oldRect.Offset(-(int)currentEntity.velocity.X / 2, 0);
oldRect.Offset(-(int)currentEntity.velocity.X, 0);
break;
}
}
@ -71,29 +62,17 @@ namespace DangerousD.GameCore.Managers
currentEntity.velocity.X = 0;
oldRect.Offset(0, (int)currentEntity.velocity.Y/2);
oldRect.Offset(0, (int)currentEntity.velocity.Y);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
isYNormalise = false;
oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2);
oldRect.Offset(0, -(int)currentEntity.velocity.Y);
break;
}
}
if (isYNormalise)
{
oldRect.Offset(0, (int)currentEntity.velocity.Y / 2);
for (int j = 0; j < mapObjects.Count; j++)
{
if (oldRect.Intersects(mapObjects[j].Rectangle))
{
isYNormalise = false;
oldRect.Offset(0, -(int)currentEntity.velocity.Y / 2);
break;
}
}
}
if (!isYNormalise)
currentEntity.velocity.Y = 0;
currentEntity.SetPosition(new Vector2(oldRect.X, oldRect.Y));