Ejemplo n.º 1
0
        public static async ValueTask <CharacterGame> Create(BECanvasComponent canvas, AnimationCollection animationCollection)
        {
            var warrior = new GameObject();

            var animation = animationCollection.GetAnimation("Idle");

            warrior.Components.Add(new Transform(warrior)
            {
                Position  = Vector2.Zero,
                Direction = Vector2.One,
                Size      = animation.FrameSize
            });

            warrior.Components.Add(new AnimatedSpriteRenderComponent(warrior)
            {
                Animation = animation
            });

            InitAnimationController(animationCollection, warrior);

            warrior.Components.Add(new CharacterBrain(animationCollection, warrior));

            var game = new CharacterGame {
                _context = await canvas.CreateCanvas2DAsync(), _warrior = warrior
            };

            return(game);
        }
Ejemplo n.º 2
0
        protected override async ValueTask Init()
        {
            this.AddService(new InputService());

            var collisionService = new CollisionService(this, new Size(64, 64));

            this.AddService(collisionService);

            var sceneGraph = new SceneGraph(this);

            this.AddService(sceneGraph);

            var player = BuildPlayer();

            sceneGraph.Root.AddChild(player);

            for (var i = 0; i != 6; ++i)
            {
                AddAsteroid(sceneGraph);
            }

            var context = await _canvas.CreateCanvas2DAsync();

            var renderService = new RenderService(this, context);

            this.AddService(renderService);
        }
Ejemplo n.º 3
0
        public static async ValueTask <LogoGame> Create(BECanvasComponent canvas, ElementReference spritesheet)
        {
            var blazorLogo = new GameObject();

            blazorLogo.Components.Add(new Transform(blazorLogo)
            {
                Position  = Vector2.Zero,
                Direction = Vector2.One,
            });

            var sprite = new Sprite()
            {
                Size        = new Size(200, 200),
                SpriteSheet = spritesheet
            };

            blazorLogo.Components.Add(new SpriteRenderComponent(sprite, blazorLogo));

            blazorLogo.Components.Add(new LogoBrain(blazorLogo));

            var game = new LogoGame {
                _context = await canvas.CreateCanvas2DAsync(), _blazorLogo = blazorLogo
            };

            return(game);
        }
Ejemplo n.º 4
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                _canvas2DContext = await CanvasReference.CreateCanvas2DAsync();

                _canvasBoundingClientRect = await JsRuntime.InvokeAsync <BoundingClientRect>("TodoIncJsInteropHelper.getBoundingClientRect", _canvasWrapperElement);
            }
        }
Ejemplo n.º 5
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            var ctx = await canvas.CreateCanvas2DAsync();

            await ctx.SetFillStyleAsync("green");

            await ctx.FillRectAsync(10, 100, 100, 100);

            await base.OnAfterRenderAsync(firstRender);
        }
Ejemplo n.º 6
0
        public async ValueTask Init(BECanvasComponent canvas, ElementReference spriteSheet)
        {
            _context = await canvas.CreateCanvas2DAsync();

            _spriteSheet = spriteSheet;
            var result = await _client.GetAsync($"{_apiUrl}/gethighscore");

            var highScore = JsonSerializer.Deserialize <HighScore>(await result.Content.ReadAsStringAsync().ConfigureAwait(false));

            HighScore     = highScore.Score;
            HighScoreName = highScore.Name;
        }
Ejemplo n.º 7
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            _outputCanvasContext = await _canvasReference.CreateCanvas2DAsync();

            await _outputCanvasContext.SetTextBaselineAsync(TextBaseline.Top);

            if (_outputCanvasContext != null)
            {
                if (firstRender)
                {
                    PokaPreview();
                }
            }
        }
        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();
        }
Ejemplo n.º 9
0
        public static async ValueTask <SceneGraphGame> Create(BECanvasComponent canvas, IDictionary <string, AnimationCollection> animations)
        {
            var context = await canvas.CreateCanvas2DAsync();

            var game = new SceneGraphGame(context);

            CreatePlanets(canvas, animations, game._sceneGraph);

            var fpsCounter = new GameObject();

            fpsCounter.Components.Add(new FPSCounterComponent(fpsCounter));
            game._sceneGraph.Root.AddChild(fpsCounter);

            return(game);
        }
Ejemplo n.º 10
0
        public async ValueTask Init(BECanvasComponent canvas, ElementReference spriteSheet)
        {
            _context = await canvas.CreateCanvas2DAsync();

            _gameSpeed    = 0;
            _spriteSheet  = spriteSheet;
            HighScoreGuid = Guid.NewGuid();
            var result = await _client.GetAsync($"{_apiUrl}/gethighscore?id={HighScoreGuid}");

            var x = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (!string.IsNullOrEmpty(x))
            {
                var highScore = JsonSerializer.Deserialize <HighScore>(x);
                HighScore     = highScore.Score;
                HighScoreName = highScore.Name;
            }
        }
Ejemplo n.º 11
0
        public static async ValueTask <AssetsGame> Create(BECanvasComponent canvas, IAssetsResolver assetsResolver)
        {
            var context = await canvas.CreateCanvas2DAsync();

            var game = new AssetsGame(context, assetsResolver);

            var fpsCounter = new GameObject();

            fpsCounter.Components.Add <FPSCounterComponent>();
            game._sceneGraph.Root.AddChild(fpsCounter);

            var player          = new GameObject();
            var playerSprite    = assetsResolver.Get <Sprite>("assets/playerShip2_green.png");
            var playerTransform = player.Components.Add <TransformComponent>();

            playerTransform.Local.Position.X = canvas.Width / 2 - playerSprite.Size.Width;
            playerTransform.Local.Position.Y = canvas.Height - playerSprite.Size.Height * 2;
            var playerSpriteRenderer = player.Components.Add <SpriteRenderComponent>();

            playerSpriteRenderer.Sprite = playerSprite;
            game._sceneGraph.Root.AddChild(player);

            var enemy          = new GameObject();
            var enemySprite    = assetsResolver.Get <Sprite>("assets/enemyRed1.png");
            var enemyTransform = enemy.Components.Add <TransformComponent>();

            enemyTransform.Local.Position.X = canvas.Width / 2 - enemySprite.Size.Width;
            enemyTransform.Local.Position.Y = enemySprite.Size.Height * 2;
            var enemySpriteRenderer = enemy.Components.Add <SpriteRenderComponent>();

            enemySpriteRenderer.Sprite = enemySprite;
            game._sceneGraph.Root.AddChild(enemy);

            var rand = new Random();

            for (var i = 0; i != 6; ++i)
            {
                AddAsteroid(game, canvas, assetsResolver, rand);
            }

            return(game);
        }
Ejemplo n.º 12
0
        internal async Task SetCurrentCanvasContext(BECanvasComponent c)
        {
            if (c is null)
            {
                throw new ArgumentNullException();
            }
            if (currentCanvas != c)
            {
                currentCanvas = c;
            }
            if (currentCanvasContext == null)
            {
                currentCanvasContext = await c.CreateCanvas2DAsync();

                if (currentCanvasContext is null)
                {
                    throw new ArgumentNullException();
                }
            }
        }
Ejemplo n.º 13
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            DynamicCtx1 = await DynamicCanvas1.CreateCanvas2DAsync();

            StaticCtx = await StaticCanvas.CreateCanvas2DAsync();

            foreach (var canvas in BufferCanvases)
            {
                canvas.Context = await canvas.CanvasRef.CreateCanvas2DAsync();
            }

            foreach (var canvas in BigBufferCanvases)
            {
                canvas.Context = await canvas.CanvasRef.CreateCanvas2DAsync();
            }

            foreach (var canvas in BiggerBufferCanvases)
            {
                canvas.Context = await canvas.CanvasRef.CreateCanvas2DAsync();
            }

            await JsRuntime.InvokeAsync <object>("initFromBlazor", DotNetObjectReference.Create(this));
        }
Ejemplo n.º 14
0
 public void Setup()
 {
     new TaskFactory().StartNew(async() => { _canvas2DContext = await _canvasReference.CreateCanvas2DAsync(); })
     .Wait();
 }
Ejemplo n.º 15
0
        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);
            }
        }
Ejemplo n.º 16
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            _context = await _canvasReference.CreateCanvas2DAsync();

            _ball.Draw(_context);
        }
 protected override Task <Canvas2DContext> CreateRenderingContextAsync(BECanvasComponent canvas)
 {
     return(canvas.CreateCanvas2DAsync());
 }
        public static async Task <SpacewarsCanvasContext> CreateAsync(Player player, BECanvasComponent _canvasReference)
        {
            var context = await _canvasReference.CreateCanvas2DAsync();

            return(new SpacewarsCanvasContext(player, context));
        }