A WIP automation game
1using System.Numerics;
2using Raylib_cs;
3
4namespace Electropunk.World;
5
6public class Entity
7{
8 public Vector2 Pos = Vector2.Zero, Vel = Vector2.Zero;
9 public float Speed = 0.100f;
10 public bool Dead = false;
11 public float Width = 0, Height = 0;
12 public MovementMode MoveMode = MovementMode.Default;
13 public RenderStep Step = RenderStep.Entities;
14 public bool Selectable = true;
15
16 public Rectangle GetRectangle() => new(Pos.X - Width / 2, Pos.Y - Height / 2, Width, Height);
17
18 public bool IsVisible(View view) => Raylib.CheckCollisionRecs(GetRectangle(), view.GetWorldBoundsRec());
19
20 public bool CanMoveInDirection(Level level, Vector2 direction)
21 {
22 Rectangle rect = GetRectangle();
23 rect.X += direction.X;
24 rect.Y += direction.Y;
25
26 /* Get a range of tiles to check */
27 int sx = (int)Math.Max(Math.Floor(rect.X / View.TileSize), 0);
28 int sy = (int)Math.Max(Math.Floor(rect.Y / View.TileSize), 0);
29 int ex = (int)Math.Min(Math.Max(rect.Width, View.TileSize) + sx, level.Width);
30 int ey = (int)Math.Min(Math.Max(rect.Height, View.TileSize) + sy, level.Height);
31
32 /* Check if any tiles will collide with the future rect */
33 Tile tile;
34 for (int x = sx; x <= ex; x++)
35 for (int y = sy; y <= ey; y++)
36 {
37 tile = level.GetTileAt(x, y);
38 if (tile.Collides && Raylib.CheckCollisionRecs(tile.GetRectangle(level, x, y), rect))
39 return false; /* We're colliding with a tile */
40 }
41
42 return true;
43 }
44
45 public virtual void Update(Level level)
46 {
47 MoveMode.Move(level, this);
48 }
49
50 public virtual void Render(Level level, View view)
51 { }
52}