public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (null == Animation)
            {
                return;
            }

            if (game.GameTime.TotalTime - _lastUpdate > 1000f / Animation.Fps)
            {
                if (_currFrameIndex >= Animation.FramesCount)
                {
                    _currFrameIndex = 0;
                }

                _lastUpdate    = game.GameTime.TotalTime;
                _currFramePosX = _currFrameIndex * Animation.FrameSize.Width;
                ++_currFrameIndex;
            }

            await context.SaveAsync();

            await context.TranslateAsync(_transform.Position.X + (MirrorVertically ? Animation.FrameSize.Width : 0f), _transform.Position.Y);

            await context.ScaleAsync(MirrorVertically? -1f : 1f, 1f);

            await context.DrawImageAsync(Animation.ImageRef,
                                         _currFramePosX, 0,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height,
                                         0, 0,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height);

            await context.RestoreAsync();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (null == Animation)
            {
                return;
            }

            if (game.GameTime.TotalTime - _lastUpdate > 1000f / Animation.Fps)
            {
                if (_currFrameIndex >= Animation.FramesCount)
                {
                    _completed      = true;
                    _currFrameIndex = 0;
                }

                _lastUpdate    = game.GameTime.TotalTime;
                _currFramePosX = _currFrameIndex * Animation.FrameSize.Width;
                ++_currFrameIndex;
            }

            var dx = -(_transform.Direction.X - 1f) * Animation.FrameSize.Width / 2f;
            await context.SetTransformAsync(_transform.Direction.X, 0, 0, 1, dx, 0);

            await context.DrawImageAsync(Animation.ImageRef, _currFramePosX, 0,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height,
                                         _transform.Position.X, _transform.Position.Y,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height);
        }
 public static async Task DrawImageFor(this IDisplayable element, Canvas2DContext context,
                                       ElementReference imageRef)
 {
     await context.DrawImageAsync(imageRef, element.Position.X - element.Size / 2,
                                  element.Position.Y - element.Size / 2, element.Size,
                                  element.Size);
 }
Beispiel #4
0
 private static async Task RenderSprite(Canvas2DContext context, SpriteBase sprite)
 {
     await context.DrawImageAsync(sprite.ElementRef,
                                  sprite.Bounds.X, sprite.Bounds.Y,
                                  sprite.Bounds.Width, sprite.Bounds.Height,
                                  sprite.Origin.X, sprite.Origin.Y,
                                  -sprite.Bounds.Width, -sprite.Bounds.Height);
 }
Beispiel #5
0
        public async ValueTask DrawFromOther(CanvasWrapper other, Point spritePosition, Rectangle sourceRect)
        {
            Point p = spritePosition;

            p.Offset(_origin);

            await _canvas2DContext.DrawImageAsync(
                other._canvas2DContext.Canvas,
                sourceRect.X,
                sourceRect.Y,
                sourceRect.Width,
                sourceRect.Height,
                p.X,
                p.Y,
                sourceRect.Width,
                sourceRect.Height);
        }
        public async Task PlaceImage(IImageController imageController)
        {
            Height = imageController.Height;
            Width  = imageController.Width;
            imageController.SetImageFormat(ImageFormat.Png);
            ImageSource = imageController.GetBase64Image();
            StateHasChanged();
            await Canvas2DContext.DrawImageAsync(ImageToBeLoaded, 0, 0, Width, Height);

            StateHasChanged();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            await context.SaveAsync();

            await context.TranslateAsync(_transform.World.Position.X, _transform.World.Position.Y);

            await context.RotateAsync(_transform.World.Rotation);

            await context.DrawImageAsync(Sprite.Source, -Sprite.Origin.X, -Sprite.Origin.Y,
                                         Sprite.Size.Width, Sprite.Size.Height);

            await context.RestoreAsync();
        }
Beispiel #8
0
        public async ValueTask Render(Canvas2DContext context)
        {
            await context.DrawImageAsync(Sprite.SpriteSheet, _transform.Position.X, _transform.Position.Y,
                                         Sprite.Size.Width, Sprite.Size.Height);

            if (DrawBoundingBox)
            {
                await context.BeginPathAsync();

                await context.SetStrokeStyleAsync($"rgb(255,255,0)");

                await context.SetLineWidthAsync(3);

                await context.StrokeRectAsync(_transform.BoundingBox.X, _transform.BoundingBox.Y,
                                              _transform.BoundingBox.Width,
                                              _transform.BoundingBox.Height);
            }
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (null == Animation)
            {
                return;
            }

            if (game.GameTime.TotalMilliseconds - _lastUpdate > 1000f / Animation.Fps)
            {
                _lastUpdate = game.GameTime.TotalMilliseconds;

                _currFramePosX += Animation.FrameSize.Width;
                if (_currFramePosX >= Animation.ImageSize.Width)
                {
                    _currFramePosX  = 0;
                    _currFramePosY += Animation.FrameSize.Height;
                }

                if (_currFramePosY >= Animation.ImageSize.Height)
                {
                    _currFramePosY = 0;
                }

                _currFrameIndex++;
                if (_currFrameIndex >= Animation.FramesCount)
                {
                    _currFrameIndex = _currFramePosX = _currFramePosY = 0;
                }
            }

            await context.SaveAsync();

            await context.TranslateAsync(_transform.World.Position.X + (MirrorVertically ? Animation.FrameSize.Width : 0f), _transform.World.Position.Y);

            await context.ScaleAsync(_transform.World.Scale.X *(MirrorVertically ? -1f:1f), _transform.World.Scale.Y);

            await context.DrawImageAsync(Animation.ImageRef,
                                         _currFramePosX, _currFramePosY,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height,
                                         0, 0,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height);

            await context.RestoreAsync();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (null == Animation)
            {
                return;
            }

            if (game.GameTime.TotalTime - _lastUpdate > 1000f / Animation.Fps)
            {
                ++_currFrameIndex;
                _lastUpdate    = game.GameTime.TotalTime;
                _currFramePosX = (_currFrameIndex % Animation.FramesCount) * Animation.FrameSize.Width;
            }

            await context.DrawImageAsync(Animation.ImageRef, _currFramePosX, 0,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height,
                                         _transform.Position.X, _transform.Position.Y,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height);
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (!this.Owner.Enabled)
            {
                return;
            }

            await context.SaveAsync();

            await context.TranslateAsync(_transform.World.Position.X, _transform.World.Position.Y);

            await context.RotateAsync(_transform.World.Rotation);

            await context.ScaleAsync(_transform.World.Scale.X, _transform.World.Scale.Y);

            await context.DrawImageAsync(Sprite.ElementRef,
                                         Sprite.Bounds.X, Sprite.Bounds.Y,
                                         Sprite.Bounds.Width, Sprite.Bounds.Height,
                                         Sprite.Origin.X, Sprite.Origin.Y,
                                         -Sprite.Bounds.Width, -Sprite.Bounds.Height);

            await context.RestoreAsync();
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            if (null == Animation || !this.Owner.Enabled)
            {
                return;
            }

            await context.SaveAsync();

            await context.TranslateAsync(_transform.World.Position.X + (MirrorVertically ? Animation.FrameSize.Width : 0f), _transform.World.Position.Y);

            await context.RotateAsync(_transform.World.Rotation);

            await context.ScaleAsync(_transform.World.Scale.X *(MirrorVertically ? -1f : 1f), _transform.World.Scale.Y);

            await context.DrawImageAsync(Animation.ImageRef,
                                         _currFramePosX, _currFramePosY,
                                         Animation.FrameSize.Width, Animation.FrameSize.Height,
                                         Animation.HalfFrameSize.Width, Animation.HalfFrameSize.Height,
                                         -Animation.FrameSize.Width, -Animation.FrameSize.Height);

            await context.RestoreAsync();
        }
        private async Task ResetCanvas()
        {
            await Canvas2DContext.DrawImageAsync(ImageToBeLoaded, 0, 0, Width, Height);

            StateHasChanged();
        }
Beispiel #14
0
        private async Task RenderGameFrame()
        {
            if (_lostALife)
            {
                var text   = $"You where hit, lost a life!";
                var length = await _context.MeasureTextAsync(text);

                await _context.SetFillStyleAsync("red");

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

                await _context.SetFillStyleAsync("white");
            }
            foreach (var a in Aliens)
            {
                if (a.Destroyed)
                {
                    continue;
                }
                await _context.DrawImageAsync(_spriteSheet,
                                              a.Sprite.TopLeft.X, a.Sprite.TopLeft.Y, a.Sprite.Size.Width, a.Sprite.Size.Height, a.CurrentPosition.X, a.CurrentPosition.Y, a.Sprite.RenderSize.Width, a.Sprite.RenderSize.Height);
            }
            var ship = Player.Sprite;
            await _context.DrawImageAsync(_spriteSheet,
                                          ship.TopLeft.X, ship.TopLeft.Y, ship.Size.Width, ship.Size.Height, Player.CurrentPosition.X, Player.CurrentPosition.Y, ship.RenderSize.Width, ship.RenderSize.Height);

            foreach (var s in _shots.Where(_ => !_.Remove))
            {
                if (s.CurrentPosition.Y <= 0)
                {
                    s.Remove = true;
                    continue;
                }
                await _context.DrawImageAsync(_spriteSheet,
                                              s.Sprite.TopLeft.X, s.Sprite.TopLeft.Y, s.Sprite.Size.Width, s.Sprite.Size.Height, s.CurrentPosition.X, s.CurrentPosition.Y, s.Sprite.RenderSize.Width, s.Sprite.RenderSize.Height);
            }
            foreach (var b in _bombs.Where(_ => !_.Remove))
            {
                if (b.CurrentPosition.Y >= _height)
                {
                    b.Remove = true;
                    continue;
                }
                var x = b.Sprite.Size;
                var y = b.Sprite.RenderSize;
                await _context.DrawImageAsync(_spriteSheet,
                                              b.Sprite.TopLeft.X, b.Sprite.TopLeft.Y, b.Sprite.Size.Width, b.Sprite.Size.Height, b.CurrentPosition.X, b.CurrentPosition.Y, b.Sprite.RenderSize.Width, b.Sprite.RenderSize.Height);
            }
            if (_motherShip != null)
            {
                if (!_motherShip.HasBeenHit)
                {
                    _motherShip.Move();
                }
                if (_motherShip.CurrentPosition.X < 0)
                {
                    _motherShip = null;
                }
                else
                {
                    await _context.DrawImageAsync(_spriteSheet,
                                                  _motherShip.Sprite.TopLeft.X, _motherShip.Sprite.TopLeft.Y, _motherShip.Sprite.Size.Width, _motherShip.Sprite.Size.Height, _motherShip.CurrentPosition.X, _motherShip.CurrentPosition.Y, _motherShip.Sprite.RenderSize.Width, _motherShip.Sprite.RenderSize.Height); //TODO: Remove hardcoded sizes
                }
            }
        }
Beispiel #15
0
 public async ValueTask Render(Canvas2DContext context)
 {
     await context.DrawImageAsync(Sprite.SpriteSheet, _transform.Position.X, _transform.Position.Y, Sprite.Size.Width, Sprite.Size.Height);
 }
        private async Task BuildCanvas()
        {
            if (context == null)
            {
                context = await canvasReference.CreateCanvas2DAsync();

                await context.SetTextAlignAsync(TextAlign.Center);

                await context.SetTextBaselineAsync(TextBaseline.Middle);
            }

            await context.ClearRectAsync(0, 0, 150, 150);

            if (loading)
            {
                await context.FillTextAsync("Loading...", 75, 75);

                return;
            }

            Point drawPosition;

            if (character.HairBelowBody != null)
            {
                drawPosition = CalculateDrawPosition(character.HairBelowBodyPosition, character.HairBelowBody);
                await context.DrawImageAsync(HairBelowBodyRef, drawPosition.X, drawPosition.Y);
            }

            drawPosition = CalculateDrawPosition(character.BodyPosition, character.Body);
            await context.DrawImageAsync(BodyRef, drawPosition.X, drawPosition.Y);

            if (character.Mail != null)
            {
                drawPosition = CalculateDrawPosition(character.MailPosition, character.Mail);
                await context.DrawImageAsync(MailRef, drawPosition.X, drawPosition.Y);
            }


            drawPosition = CalculateDrawPosition(character.HeadPosition, character.Head);
            await context.DrawImageAsync(HeadRef, drawPosition.X, drawPosition.Y);

            if (character.EarType != EarType.Normal)
            {
                drawPosition = CalculateDrawPosition(character.EarPosition, character.Ear);
                await context.DrawImageAsync(EarRef, drawPosition.X, drawPosition.Y);
            }
            if (character.HasFace)
            {
                drawPosition = CalculateDrawPosition(character.FacePosition, character.Face);
                await context.DrawImageAsync(FaceRef, drawPosition.X, drawPosition.Y);
            }
            if (character.Hair != null)
            {
                drawPosition = CalculateDrawPosition(character.HairPosition, character.Hair);
                await context.DrawImageAsync(HairRef, drawPosition.X, drawPosition.Y);
            }
            if (character.HairOverHead != null)
            {
                drawPosition = CalculateDrawPosition(character.HairOverHeadPosition, character.HairOverHead);
                await context.DrawImageAsync(HairOverHeadRef, drawPosition.X, drawPosition.Y);
            }
            if (character.BackHair != null)
            {
                drawPosition = CalculateDrawPosition(character.BackHairPosition, character.BackHair);
                await context.DrawImageAsync(BackHairRef, drawPosition.X, drawPosition.Y);
            }
            if (character.BackHairBelowCap != null)
            {
                drawPosition = CalculateDrawPosition(character.BackHairBelowCapPosition, character.BackHairBelowCap);
                await context.DrawImageAsync(BackHairBelowCapRef, drawPosition.X, drawPosition.Y);
            }

            if (character.Pants != null)
            {
                drawPosition = CalculateDrawPosition(character.PantsPosition, character.Pants);
                await context.DrawImageAsync(PantsRef, drawPosition.X, drawPosition.Y);
            }
            if (character.Arm != null)
            {
                drawPosition = CalculateDrawPosition(character.ArmPosition, character.Arm);
                await context.DrawImageAsync(ArmRef, drawPosition.X, drawPosition.Y);
            }
            if (character.Hand != null)
            {
                drawPosition = CalculateDrawPosition(character.HandPosition, character.Hand);
                await context.DrawImageAsync(HandRef, drawPosition.X, drawPosition.Y);
            }
            if (character.MailArm != null)
            {
                drawPosition = CalculateDrawPosition(character.MailArmPosition, character.MailArm);
                await context.DrawImageAsync(MailArmRef, drawPosition.X, drawPosition.Y);
            }
        }
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            await this.OffscreenRenderer.Render();

            await context.DrawImageAsync(this.OffscreenRenderer.Image, 0, 0).ConfigureAwait(false);
        }