aboutsummaryrefslogtreecommitdiff
path: root/source/game/systems/LightingSystem.cs
blob: 73c6cf8969ad575658841b814c0b9cbd061e7222 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Celesteia.Graphics;
using Celesteia.Graphics.Lighting;
using Celesteia.Resources;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Entities.Systems;
using Celesteia.Resources.Types;
using Celesteia.Game.Planets;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Celesteia.Game.Systems {
    public class LightingSystem : IUpdateSystem, IDrawSystem
    {
        private readonly Camera2D _camera;
        private readonly SpriteBatch _spriteBatch;
        private readonly ChunkMap _chunkMap;

        public LightingSystem(Camera2D camera, SpriteBatch spriteBatch, ChunkMap chunkMap) {
            _camera = camera;
            _spriteBatch = spriteBatch;
            _chunkMap = chunkMap;
        }
        public void Dispose() { }
        
        private int _lightRenderDistance = 5;

        private Dictionary<byte, BlockLightProperties> lightingDictionary;
        public void Initialize(MonoGame.Extended.Entities.World world) {
            int _size = 2 * _lightRenderDistance * Chunk.CHUNK_SIZE;

            _lightMap = new LightMap(_size, _size);
            _texture = new Texture2D(_spriteBatch.GraphicsDevice, _size, _size);

            lightingDictionary = new Dictionary<byte, BlockLightProperties>();
        }

        private LightMap _lightMap;
        private Texture2D _texture;

        private bool drawTexture = false;
        private Task _lightUpdate;
        public void Update(GameTime gameTime)
        {
            if (_lightUpdate == null || (_lightUpdate != null && _lightUpdate.IsCompleted))
                    _lightUpdate = Task.Factory.StartNew(() => UpdateLight());
                
            if (drawTexture) UpdateTexture();
        }
        
        private Point _position;
        private void UpdatePosition() {
            _position = ChunkVector.FromVector2(_camera.Center).Resolve() - new Point(_lightRenderDistance * Chunk.CHUNK_SIZE);
        }

        private void UpdateTexture() {
            _drawPosition = _position.ToVector2();
            _texture.SetData<Color>(_lightMap.GetColors(), 0, _lightMap.GetColorCount());
            drawTexture = false;
        }

        private void UpdateLight() {
            UpdatePosition();
            
            UpdateEmission();
            _lightMap.Propagate();
            _lightMap.CreateColorMap();

            drawTexture = true;
        }

        private BlockState _block;
        private void UpdateEmission() {
            for (int i = 0; i < _lightMap.Width; i++) {
                for (int j = 0; j < _lightMap.Height; j++) {
                    if (!(_block = _chunkMap.GetForeground(i + _position.X, j + _position.Y)).Empty && _lightMap.AddForeground(i, j, _block.Type.Light)) continue;
                    if (!(_block = _chunkMap.GetBackground(i + _position.X, j + _position.Y)).Empty && _lightMap.AddBackground(i, j, _block.Type.Light)) continue;

                    _lightMap.AddLight(i, j, true, LightColor.ambient, 4);
                }
            }
        }

        private BlendState multiply = new BlendState() {
            ColorBlendFunction = BlendFunction.Add,
            ColorSourceBlend = Blend.DestinationColor,
            ColorDestinationBlend = Blend.Zero,
        };

        private Vector2 _drawPosition;
        public void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin(SpriteSortMode.Immediate, multiply, SamplerState.LinearClamp, null, null, null, _camera.GetViewMatrix());

            _spriteBatch.Draw(_texture, _drawPosition, Color.White);

            _spriteBatch.End();
        }
    }
}