GreatSpringGameJam/GreatSpringGameJam/Entity.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2021-03-08 02:37:40 +01:00
using System;
2021-03-07 22:23:51 +01:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2021-03-08 02:37:40 +01:00
using MLEM.Extensions;
using MLEM.Startup;
using MLEM.Textures;
using RectangleF = MonoGame.Extended.RectangleF;
2021-03-07 22:23:51 +01:00
namespace GreatSpringGameJam {
public class Entity {
2021-03-08 02:37:40 +01:00
public static readonly UniformTextureAtlas StuffTexture = new(MlemGame.LoadContent<Texture2D>("Textures/Stuff"), 8, 8);
protected static readonly Random Random = new();
2021-03-07 22:23:51 +01:00
public readonly Map Map;
public Vector2 Position;
public Entity(Map map, Vector2 position) {
this.Map = map;
this.Position = position;
}
public virtual void Update(GameTime time) {
}
public virtual void Draw(GameTime time, SpriteBatch batch) {
}
2021-03-08 02:37:40 +01:00
protected void Collide(Func<RectangleF> getArea, ref Vector2 velocity, out bool onGround) {
onGround = false;
foreach (var (normal, penetration) in this.Map.Collisions.GetPenetrations(getArea)) {
if (penetration.Equals(0, 0.015F))
continue;
this.Position -= normal * penetration;
velocity *= new Vector2(Math.Abs(normal.Y), Math.Abs(normal.X));
if (normal.Y > 0)
onGround = true;
}
}
2021-03-07 22:23:51 +01:00
}
}