diff --git a/DungeonShooting_Godot/resource/test/Brush1.png b/DungeonShooting_Godot/resource/test/Brush1.png index 0b63f40..24b23b1 100644 --- a/DungeonShooting_Godot/resource/test/Brush1.png +++ b/DungeonShooting_Godot/resource/test/Brush1.png Binary files differ diff --git a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs index 32ea79f..64a6675 100644 --- a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs +++ b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs @@ -176,7 +176,7 @@ List callDrawingCompleteList = null; using (var image = _viewportTexture.GetImage()) { - var startTime = DateTime.Now; + var startTime = DateTime.UtcNow; //File.WriteAllBytes("d:/image.png", image.SavePngToBuffer()); //绘制完成需要调用回调的列表 do @@ -210,9 +210,9 @@ ReclaimRenderSprite(item.RenderSprite); item.RenderSprite = null; } - } while (_drawingQueueItems.Count > 0 && (DateTime.Now - startTime).TotalMilliseconds < step1Time); + } while (_drawingQueueItems.Count > 0 && (DateTime.UtcNow - startTime).TotalMilliseconds < step1Time); - //Debug.Log($"当前帧绘制完成数量: {index}, 绘制队列数量: {_drawingQueueItems.Count}, 用时: {(DateTime.Now - startTime).TotalMilliseconds}毫秒"); + //Debug.Log($"当前帧绘制完成数量: {index}, 绘制队列数量: {_drawingQueueItems.Count}, 用时: {(DateTime.UtcNow - startTime).TotalMilliseconds}毫秒"); } //重绘画布 @@ -240,7 +240,7 @@ //处理下一批image if (_queueItems.Count > 0) { - var startTime = DateTime.Now; + var startTime = DateTime.UtcNow; var hasFail = false; //执行绘制操作 for (var i = 0; i < _queueItems.Count; i++) @@ -270,13 +270,13 @@ } //计算超时 - if ((DateTime.Now - startTime).TotalMilliseconds >= step2Time) + if ((DateTime.UtcNow - startTime).TotalMilliseconds >= step2Time) { break; } } - //Debug.Log($"当前帧进入绘制绘队列数量: {index}, 待绘制队列数量: {_queueItems.Count}, 绘制队列数量: {_drawingQueueItems.Count}, 用时: {(DateTime.Now - startTime).TotalMilliseconds}毫秒"); + //Debug.Log($"当前帧进入绘制绘队列数量: {index}, 待绘制队列数量: {_queueItems.Count}, 绘制队列数量: {_drawingQueueItems.Count}, 用时: {(DateTime.UtcNow - startTime).TotalMilliseconds}毫秒"); } } diff --git a/DungeonShooting_Godot/src/test/TestMask2.cs b/DungeonShooting_Godot/src/test/TestMask2.cs index 458d712..96a0377 100644 --- a/DungeonShooting_Godot/src/test/TestMask2.cs +++ b/DungeonShooting_Godot/src/test/TestMask2.cs @@ -51,7 +51,10 @@ public int Y; public Color Color; public byte Type; + public float Timer; public float Speed; + public bool IsRun; + public float TempTime; } [Export] @@ -69,6 +72,11 @@ private ImagePixel[,] _imagePixels; private List _cacheImagePixels = new List(); + private float _runTime = 0; + private int _executeIndex = -1; + + //程序每帧最多等待执行时间, 超过这个时间的像素点将交到下一帧执行, 单位: 毫秒 + private float _maxWaitTime = 1f; public override void _Ready() { @@ -82,52 +90,119 @@ public override void _Process(double delta) { - var newDelta = (float)delta; - var pos = (GetGlobalMousePosition() / 4).AsVector2I(); - if (Input.IsActionPressed("fire")) + var time = DateTime.UtcNow; + //更新消除逻辑 + if (_cacheImagePixels.Count > 0) { - var time = DateTime.Now; - RunTest(_brushData1, pos, 4f); - Debug.Log("用时: " + (DateTime.Now - time).TotalMilliseconds); - } - else if (Input.IsActionPressed("roll")) - { - var time = DateTime.Now; - RunTest(_brushData2, pos, 4f); - Debug.Log("用时: " + (DateTime.Now - time).TotalMilliseconds); - } - - if (Input.IsActionJustPressed("meleeAttack")) - { - var mousePosition = GetGlobalMousePosition(); - var time = DateTime.Now; - var pixel = _image.GetPixel((int)mousePosition.X / 4, (int)mousePosition.Y / 4); - Debug.Log("用时: " + (DateTime.Now - time).TotalMilliseconds + ", 是否碰撞: " + (pixel.A > 0)); - } - - if (Input.IsActionJustPressed("exchangeWeapon")) - { - _image.Fill(new Color(1, 1, 1, 0)); - _texture.Update(_image); - } - - if (Input.IsActionPressed("move_left")) - { - var time = DateTime.Now; - foreach (var imagePixel in _cacheImagePixels) + var startIndex = _executeIndex; + if (_executeIndex < 0 || _executeIndex >= _cacheImagePixels.Count) { - if (imagePixel.Color.A > 0) + _executeIndex = _cacheImagePixels.Count - 1; + } + + var startTime = DateTime.UtcNow; + var isOver = false; + var index = 0; + for (; _executeIndex >= 0; _executeIndex--) + { + index++; + var imagePixel = _cacheImagePixels[_executeIndex]; + if (UpdateImagePixel(imagePixel)) //移除 { - imagePixel.Color.A -= imagePixel.Speed * newDelta; - _image.SetPixel(imagePixel.X, imagePixel.Y, imagePixel.Color); + _cacheImagePixels.RemoveAt(_executeIndex); + if (_executeIndex < startIndex) + { + startIndex--; + } + } + + if (index > 200) + { + index = 0; + if ((DateTime.UtcNow - startTime).TotalMilliseconds > _maxWaitTime) //超过最大执行时间 + { + isOver = true; + break; + } } } - Debug.Log("用时: " + (DateTime.Now - time).TotalMilliseconds + ", 数量: " + _cacheImagePixels.Count); + + if (!isOver && startIndex >= 0 && _executeIndex < 0) + { + _executeIndex = _cacheImagePixels.Count - 1; + for (; _executeIndex >= startIndex; _executeIndex--) + { + index++; + var imagePixel = _cacheImagePixels[_executeIndex]; + if (UpdateImagePixel(imagePixel)) //移除 + { + _cacheImagePixels.RemoveAt(_executeIndex); + } + + if (index > 200) + { + index = 0; + if ((DateTime.UtcNow - startTime).TotalMilliseconds > _maxWaitTime) //超过最大执行时间 + { + break; + } + } + } + } } + + var pos = (GetGlobalMousePosition() / 4).AsVector2I(); + if (Input.IsMouseButtonPressed(MouseButton.Left)) //绘制画笔1 + { + DrawBrush(_brushData1, pos, 5f, 3); + } + else if (Input.IsMouseButtonPressed(MouseButton.Right)) //绘制画笔2 + { + DrawBrush(_brushData2, pos, 5f, 3); + } + + //碰撞检测 + if (Input.IsKeyPressed(Key.Space)) + { + var mousePosition = GetGlobalMousePosition(); + var pixel = _image.GetPixel((int)mousePosition.X / 4, (int)mousePosition.Y / 4); + Debug.Log("是否碰撞: " + (pixel.A > 0)); + } + _texture.Update(_image); + _runTime += (float)delta; + Debug.Log("用时: " + (DateTime.UtcNow - time).TotalMilliseconds); } - private void RunTest(ImageData brush, Vector2I position, float time) + private bool UpdateImagePixel(ImagePixel imagePixel) + { + if (imagePixel.Color.A > 0) + { + if (imagePixel.Timer > 0) + { + imagePixel.Timer -= _runTime - imagePixel.TempTime; + imagePixel.TempTime = _runTime; + } + else + { + imagePixel.Color.A -= imagePixel.Speed * (_runTime - imagePixel.TempTime); + _image.SetPixel(imagePixel.X, imagePixel.Y, imagePixel.Color); + if (imagePixel.Color.A <= 0) + { + imagePixel.IsRun = false; + return true; + } + else + { + imagePixel.TempTime = _runTime; + } + } + } + + return false; + } + + private void DrawBrush(ImageData brush, Vector2I position, float duration, float writeOffTime) { var pos = position - new Vector2I(brush.Width, brush.Height) / 2; var canvasWidth = _texture.GetWidth(); @@ -148,16 +223,26 @@ Y = y, Color = brushPixel.Color, Type = 0, - Speed = brushPixel.Color.A / time, + Timer = duration, + Speed = brushPixel.Color.A / writeOffTime, }; _imagePixels[x, y] = temp; _cacheImagePixels.Add(temp); + temp.IsRun = true; + temp.TempTime = _runTime; } else { temp.Color = brushPixel.Color; - temp.Speed = brushPixel.Color.A / time; + temp.Speed = brushPixel.Color.A / writeOffTime; + temp.Timer = duration; + if (!temp.IsRun) + { + _cacheImagePixels.Add(temp); + temp.IsRun = true; + temp.TempTime = _runTime; + } } } }