Beispiel #1
0
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 300, _height);

            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            y = startY;

            await WriteLine($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", context);
            await WriteLine($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", context);
            await WriteLine($"FPS: {fps:###}", context);

            if (AsteroidsSpawner is not null)
            {
                await WriteLine($"Asteroids alive: {AsteroidsSpawner.Alive:###}", context);
            }

            if (BulletSpawner is not null)
            {
                await WriteLine($"Bullets spawned: {BulletSpawner.Alive:###}", context);
            }
        }
        private async Task DrawRect(RenderElement element, string style)
        {
            var oldStyle = _context.FillStyle;
            await _context.SetFillStyleAsync(style);

            await _context.FillRectAsync(element.Position.X, element.Position.Y,
                                         element.Dimensions.Width, element.Dimensions.Height);

            await _context.SetFillStyleAsync(oldStyle);
        }
Beispiel #3
0
        public async ValueTask DrawText(string text, Point pos, Color color)
        {
            Point p = pos;

            p.Offset(_origin);

            // todo: avoid heap allocation
            await _canvas2DContext.SetFillStyleAsync($"rgb({color.R},{color.G},{color.B})");

            await _canvas2DContext.FillTextAsync(text, p.X, p.Y);
        }
Beispiel #4
0
        public async Task Draw(int width, int height, Canvas2DContext canvas)
        {
            var index = 0;

            for (var y = 0; y < _data.Height; ++y)
            {
                for (var x = 0; x < _data.Width; ++x, ++index)
                {
                    var val = _data.Density[index];
                    if (val == 0)
                    {
                        continue;
                    }

                    //todo: highlight the visible rectangle.
                    bool inside = true;

                    var clr = ChooseColor(val);
                    if (!inside)
                    {
                        DarkenColor(clr);
                    }
                    await canvas.SetFillStyleAsync(ColorToString(clr));

                    await canvas.RectAsync(x, y, 1, 1);
                }
            }
        }
Beispiel #5
0
        public override async void Draw(Canvas2DContext batch)
        {
            await batch.SetFillStyleAsync("green");

            await batch.ArcAsync(95, 50, 40, 0, 2 *Math.PI);

            await batch.StrokeAsync();
        }
        /// <summary>
        /// 画表盘圆
        ///</summary>
        ///<param name="strokeStyle"></param>
        ///<param name="radius">半径</param>
        ///<param name="fillStyle">填充色</param>
        /// <returns></returns>
        private async Task DrawCicleAsync(string strokeStyle, double radius, string fillStyle)
        {
            await _context.SaveAsync();

            await _context.TranslateAsync(radius, radius);

            await _context.BeginPathAsync();

            await _context.SetStrokeStyleAsync(strokeStyle);

            await _context.ArcAsync(0, 0, radius, 0, 2 *Math.PI, true);

            await _context.SetFillStyleAsync(fillStyle);

            await _context.FillAsync();

            await _context.RestoreAsync();
        }
Beispiel #7
0
        public static async Task DrawBar(this Canvas2DContext con, int barValue, string color, int xPos, int chartHeight)
        {
            await con.MoveToAsync(barWidth + xPos + spaceX, chartHeight - spaceY);

            await con.LineToAsync(barWidth + xPos + spaceX, chartHeight - spaceY);

            await con.StrokeAsync();

            int barHeight = -barValue * 2;

            await con.SetFillStyleAsync(color);

            await con.FillRectAsync(xPos + spaceX, chartHeight - spaceY - 1, barWidth, barHeight);

            await con.FillTextAsync(barValue.ToString(), xPos + spaceX + 5, chartHeight - spaceY + barHeight - 2);

            await con.SetFillStyleAsync(COLOR.Black);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 300, _height);

            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            y = startY;

            await WriteLine($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", context);
            await WriteLine($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", context);
            await WriteLine($"FPS: {fps:###}", context);
        }
Beispiel #9
0
        public async ValueTask Render()
        {
            await _context.ClearRectAsync(0, 0, _width, _height);

            await _context.BeginBatchAsync();

            await _context.SetFillStyleAsync("white");

            await _context.SetFontAsync("32px consolas");

            var text   = $"Points: {_points.ToString("D3")} High score: {HighScore} {HighScoreName} Lives: {_lives.ToString("D3")}";
            var length = await _context.MeasureTextAsync(text);

            await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 25);

            if (GameOver)
            {
                await RenderGameOver();
            }
            else if (Won)
            {
                await RenderWonGame();
            }
            else if (Started)
            {
                await RenderGameFrame();
            }
            else if (!Started)
            {
                await _context.SetFillStyleAsync("white");

                text   = $"Use arrow keys to move, space to fire";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 150);

                text   = $"Press space to start";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 200);
            }

            await _context.EndBatchAsync();
        }
Beispiel #10
0
        public override async Task Draw(Canvas2DContext context)
        {
            await context.BeginPathAsync();

            await context.ArcAsync(Position.X, Position.Y, Size / 2, 0, 2 *Math.PI);

            await context.SetFillStyleAsync("blue");

            await context.FillAsync();
        }
Beispiel #11
0
        private static async Task RenderText(Canvas2DContext context, string text, int x)
        {
            await context.SetFillStyleAsync("#fff");

            await context.SetFontAsync("18px verdana");

            await context.SetTextBaselineAsync(TextBaseline.Middle);

            await context.FillTextAsync(text, x, 0);
        }
        async ValueTask drawPage(Page p)
        {
            await _outputCanvasContext.ClearRectAsync(0, 0, _canvasReference.Width, _canvasReference.Height);

            await _outputCanvasContext.SetFillStyleAsync("black");

            await _outputCanvasContext.FillRectAsync(0, 0, _canvasReference.Width, _canvasReference.Height);

            for (int i = 0; i < p.elements.Count; i++)
            {
                PageElement e = p.elements[i];
                await _outputCanvasContext.SetFontAsync(fontCode(e.font));

                await _outputCanvasContext.SetFillStyleAsync("#ffffff");

                await _outputCanvasContext.FillTextAsync(e.text, e.x *skala, e.y *skala);
            }
            await Task.Delay(p.time * 1000);
        }
Beispiel #13
0
        public async ValueTask DrawRectangle(int left, int top, int width, int height, string color = "", string fillColor = "")
        {
            await _canvas.SetStrokeStyleAsync(color);

            if (color != "")
            {
                await _canvas.StrokeRectAsync(left, top, width, height);

                await _canvas.SetStrokeStyleAsync("");
            }

            await _canvas.SetFillStyleAsync(fillColor);

            if (fillColor != "")
            {
                await _canvas.FillRectAsync(left, top, width, height);

                await _canvas.SetFillStyleAsync("");
            }
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (!this.Owner.Enabled || !this.Initialized)
            {
                return;
            }

            var oldPattern = context.FillStyle;

            var pattern = await context.CreatePatternAsync(Sprite.ElementRef, RepeatPattern);

            await context.SetFillStyleAsync(pattern);

            var w = _transform.World.Scale.X * this.Sprite.Bounds.Width;
            var h = _transform.World.Scale.Y * this.Sprite.Bounds.Height;

            await context.FillRectAsync(_transform.World.Position.X, _transform.World.Position.X, w, h);

            await context.SetFillStyleAsync(oldPattern);
        }
        public static async Task DrawOn(this IDisplayable element, Canvas2DContext context)
        {
            await context.BeginPathAsync();

            await context.ArcAsync(element.Position.X, element.Position.Y, element.Size / 2, 0, Math.PI * 2);

            await context.SetFillStyleAsync(element.Color);

            await context.FillAsync();

            await context.ClosePathAsync();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            await context.SetFillStyleAsync("#fff").ConfigureAwait(false);

            await context.SetFontAsync("18px verdana").ConfigureAwait(false);

            var hiScore  = Math.Max(_score, _maxScore);
            var text     = $"Score: {_score:###} Hi Score: {hiScore:###}";
            var textSize = await context.MeasureTextAsync(text).ConfigureAwait(false);

            var x = game.Display.Size.Width - textSize.Width - 50;

            await context.FillTextAsync(text, x, 50).ConfigureAwait(false);
        }
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            _stopwatch.Start();

            if (firstRender)
            {
                _ctx = await _canvas.CreateCanvas2DAsync();

                await _ctx.SetFillStyleAsync("green");

                await _ctx.SetStrokeStyleAsync("#777777");

                await _ctx.SetLineWidthAsync(1);
            }

            await _ctx.ClearRectAsync(0, 0, GridSize *Scale, GridSize *Scale);

            await _ctx.BeginPathAsync();

            for (int i = 0; i <= Scale; ++i)
            {
                await _ctx.MoveToAsync(GridSize *i, 0);

                await _ctx.LineToAsync(GridSize *i, GridSize *Scale);

                await _ctx.MoveToAsync(0, GridSize *i);

                await _ctx.LineToAsync(GridSize *Scale, GridSize *i);
            }

            for (int y = 0; y < Scale; ++y)
            {
                for (int x = 0; x < Scale; ++x)
                {
                    if (_game.GetState(x, y))
                    {
                        await _ctx.RectAsync(x *GridSize + 1, y *GridSize + 1, GridSize - 2, GridSize - 2);
                    }
                }
            }

            await _ctx.StrokeAsync();

            await _ctx.FillAsync();

            _stopwatch.Stop();
            RenderCostInMilliSecond = _stopwatch.ElapsedMilliseconds;
            _stopwatch.Reset();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var fps = 1000f / game.GameTime.ElapsedMilliseconds;

            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(10, 50, 400, 100);

            await context.SetFontAsync("24px verdana");

            await context.StrokeTextAsync($"Total game time (s): {game.GameTime.TotalMilliseconds / 1000}", 20, 80);

            await context.StrokeTextAsync($"Frame time (ms): {game.GameTime.ElapsedMilliseconds}", 20, 110);

            await context.StrokeTextAsync($"FPS: {fps:###}", 20, 140);
        }
Beispiel #19
0
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            var y = (double)game.Display.Size.Height / 2;
            var x = (double)game.Display.Size.Width / 2;

            await context.SetFillStyleAsync("#fff").ConfigureAwait(false);

            await context.SetFontAsync("40px verdana").ConfigureAwait(false);

            await context.SetTextAlignAsync(TextAlign.Center).ConfigureAwait(false);

            await context.FillTextAsync("Blazeroids!", x, y).ConfigureAwait(false);

            await context.SetFontAsync("28px verdana").ConfigureAwait(false);

            await context.FillTextAsync("press Enter to start", x, y + 40)
            .ConfigureAwait(false);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            int maxWidth     = 300;
            int maxHeight    = 30;
            int bottomOffset = 20;
            int rightOffset  = 20;

            float healthRatio = (float)this.PlayerBrain.Stats.Health / this.PlayerBrain.Stats.MaxHealth;
            int   width       = (int)(healthRatio * maxWidth);

            int x = game.Display.Size.Width - width - rightOffset;
            int y = game.Display.Size.Height - maxHeight - bottomOffset;

            var color = healthRatio > .5 ? "green" : "red";

            await context.SetFillStyleAsync(color);

            await context.FillRectAsync(x, y, width, maxHeight);
        }
Beispiel #21
0
 internal async Task SetFillColor(Color color)
 {
     await ctx.SetFillStyleAsync(color.AsHtml);
 }
Beispiel #22
0
 public void Apply(SetColorNode node)
 {
     _canvas2DContext.SetFillStyleAsync(node.Color.Name.ToLower());
 }
Beispiel #23
0
        public override async Task Draw(Canvas2DContext context)
        {
            await context.SetFillStyleAsync("green");

            await context.FillRectAsync(Position.X, Position.Y, Width, Height);
        }