GreatSpringGameJam/GreatSpringGameJam/Entity.cs

65 lines
2.4 KiB
C#
Raw Permalink Normal View History

2021-03-08 02:37:40 +01:00
using System;
2021-03-09 18:29:07 +01:00
using System.Linq;
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;
2021-03-14 02:04:57 +01:00
using MLEM.Misc;
2021-03-08 02:37:40 +01:00
using MLEM.Startup;
using MLEM.Textures;
2021-03-08 16:04:51 +01:00
using MonoGame.Extended.Tiled;
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
2021-03-14 02:04:57 +01:00
var penetrations = this.Map.GetPenetrations(getArea, !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-09 20:13:26 +01:00
var (_, below) = this.GetTileBelow();
2021-03-14 15:02:31 +01:00
if (below != null && below.Properties.GetBool("JumpPad") && velocity.Y > 0.1F) {
2021-03-08 16:04:51 +01:00
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-09 18:29:07 +01:00
// disallow exiting the map's sides
var area = getArea();
if (area.Left < 0)
this.Position.X += -area.Left;
if (area.Right >= this.Map.Size.X)
this.Position.X += this.Map.Size.X - area.Right;
2021-03-08 02:37:40 +01:00
}
2021-03-09 20:13:26 +01:00
protected (Point, TiledMapTilesetTile) GetTileBelow(Vector2? offset = null, string layer = null) {
2021-03-08 19:08:41 +01:00
var (x, y) = this.Position + (offset ?? new Vector2(0, 0.5F));
2021-03-09 20:13:26 +01:00
var pos = new Point((x + 0.5F).Floor(), (y + 0.5F).Floor());
var tile = this.Map.GetTile(layer ?? "Ground", pos.X, pos.Y);
return !tile.IsBlank ? (pos, this.Map.GetTilesetTile(tile, this.Map.GetTileset(tile))) : default;
2021-03-08 16:04:51 +01:00
}
2021-03-07 22:23:51 +01:00
}
}