summaryrefslogtreecommitdiff
path: root/source/game/systems/physics/PhysicsCollisionDebugSystem.cs
blob: f605c30c63d00bf70cac846f4386f05992d9a0ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Celesteia.Game.Components.Physics;
using Celesteia.Game.Planets;
using Celesteia.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using MonoGame.Extended.Entities;
using MonoGame.Extended.Entities.Systems;

namespace Celesteia.Game.Systems.Physics {    
    public class PhysicsCollisionDebugSystem : EntityDrawSystem
    {
        private readonly Camera2D _camera;
        private readonly SpriteBatch _spriteBatch;
        private readonly ChunkMap _chunkMap;

        private ComponentMapper<Transform2> transformMapper;
        private ComponentMapper<CollisionBox> collisionBoxMapper;

        public PhysicsCollisionDebugSystem(Camera2D camera, SpriteBatch spriteBatch, ChunkMap chunkMap) : base(Aspect.All(typeof(Transform2), typeof(CollisionBox))) {
            _camera = camera;
            _spriteBatch = spriteBatch;
            _chunkMap = chunkMap;
        }

        public override void Initialize(IComponentMapperService mapperService)
        {
            transformMapper = mapperService.GetMapper<Transform2>();
            collisionBoxMapper = mapperService.GetMapper<CollisionBox>();
        }

        public override void Draw(GameTime gameTime)
        {
            if (!GameInstance.DebugMode) return;

            _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, null, null, null, _camera.GetViewMatrix());
            
            foreach (int entityId in ActiveEntities) {
                Rectangle box = collisionBoxMapper.Get(entityId).Rounded;                

                int minX = box.X;
                int maxX = box.X + box.Width;

                int minY = box.Y;
                int maxY = box.Y + box.Height;

                for (int i = minX; i < maxX; i++)
                    for (int j = minY; j < maxY; j++) {
                        RectangleF? blockBox = _chunkMap.TestBoundingBox(i, j);
                        if (blockBox.HasValue) {
                            _spriteBatch.DrawRectangle(new RectangleF(i, j, blockBox.Value.Width, blockBox.Value.Height), Color.Red, .05f, 0f);
                        } else {
                            _spriteBatch.DrawRectangle(new RectangleF(i, j, 1f, 1f), Color.Green, .05f, 0f);
                        }
                    }
            }

            _spriteBatch.End();
        }
    }
}