GreatSpringGameJam/GreatSpringGameJam/Entity.cs

55 lines
2 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 16:04:51 +01:00
using MLEM.Extended.Tiled;
2021-03-08 02:37:40 +01:00
using MLEM.Extensions;
using MLEM.Startup;
using MLEM.Textures;
2021-03-08 16:04:51 +01:00
using MonoGame.Extended.Tiled;
2021-03-08 02:37:40 +01:00
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 16:04:51 +01:00
protected void Collide(Func<RectangleF> getArea, ref Vector2 velocity, ref bool onGround) {
// if we're not on the ground, we should prioritize x collisions to be pushed out of walls first
var penetrations = this.Map.Collisions.GetPenetrations(getArea, prioritizeX: !onGround);
2021-03-08 02:37:40 +01:00
onGround = false;
2021-03-08 16:04:51 +01:00
foreach (var (normal, penetration) in penetrations) {
2021-03-08 02:37:40 +01:00
this.Position -= normal * penetration;
2021-03-08 16:04:51 +01:00
if (normal.Y > 0) {
2021-03-08 02:37:40 +01:00
onGround = true;
2021-03-08 16:04:51 +01:00
var below = this.GetTileBelow();
if (below != null && below.Properties.GetBool("JumpPad") && velocity.Y > 0) {
velocity.Y = -velocity.Y;
continue;
}
}
velocity *= new Vector2(Math.Abs(normal.Y), Math.Abs(normal.X));
2021-03-08 02:37:40 +01:00
}
}
2021-03-08 16:04:51 +01:00
protected TiledMapTilesetTile GetTileBelow() {
var tile = this.Map.GetTile("Ground", (this.Position.X + 0.5F).Floor(), (this.Position.Y + 1).Floor());
return !tile.IsBlank ? this.Map.GetTilesetTile(tile) : null;
}
2021-03-07 22:23:51 +01:00
}
}