コード例 #1
0
        protected internal override void Deserialize(IComponentDataReader reader, IAssetStore assetStore)
        {
            base.Deserialize(reader, assetStore);

            Sound     = reader.IsNull("Sound") ? null : assetStore.GetAsset <ISound>(reader.ReadAssetId("Sound"));
            IsPlaying = reader.ReadBool("IsPlaying");
        }
コード例 #2
0
 protected internal override void Deserialize(IComponentDataReader reader, IAssetStore assetStore)
 {
     base.Deserialize(reader, assetStore);
     Sprite = reader.IsNull("Sprite")
         ? null
         : assetStore.GetAsset <Sprite>(reader.ReadAssetId("Sprite"));
 }
コード例 #3
0
        public Entity CreateStaticSprite(Scene scene, double x, double y)
        {
            var entity = new Entity();

            scene.AddEntity(entity);

            entity.AddComponent(new Transform2DComponent
            {
                Translation = new Vector2(x, y),
                Rotation    = 0,
                Scale       = Vector2.One
            });
            entity.AddComponent(new SpriteRendererComponent
            {
                Sprite = _assetStore.GetAsset <Sprite>(AssetsIds.PaintColorPalette)
            });

            return(entity);
        }
コード例 #4
0
        public void ProcessFixedUpdate(Scene scene)
        {
            var box          = scene.AllEntities.Single(e => e.HasComponent <BoxMovementComponent>());
            var mousePointer = scene.RootEntities.Single(e => e.HasComponent <MousePointerComponent>());
            var camera       = scene.AllEntities.Single(e => e.HasComponent <CameraComponent>());

            foreach (var entity in scene.AllEntities.ToList())
            {
                if (entity.HasComponent <DieFromBoxComponent>())
                {
                    var collider = entity.GetComponent <CircleColliderComponent>();
                    if (collider.IsColliding)
                    {
                        var collidedWithBox                  = collider.CollidingEntities.Contains(box);
                        var collidedWithMousePointer         = collider.CollidingEntities.Contains(mousePointer);
                        var mousePointerHasLeftButtonPressed = mousePointer.GetComponent <MousePointerComponent>().LeftButtonPressed;

                        if (collidedWithBox || (collidedWithMousePointer && mousePointerHasLeftButtonPressed))
                        {
                            _audioPlayer.PlayOnce(_assetStore.GetAsset <ISound>(AssetsIds.SfxSound));
                            entity.DestroyAfterFixedTimeStep();
                        }
                    }
                }

                if (entity.HasComponent <CloseGameOnEscapeKeyComponent>())
                {
                    var inputComponent = entity.GetComponent <InputComponent>();
                    if (inputComponent.HardwareInput.KeyboardInput.Escape)
                    {
                        _engineManager.ScheduleEngineShutdown();
                    }
                    if (inputComponent.HardwareInput.KeyboardInput.F5)
                    {
                        _sceneLoader.Save(scene, "quicksave.geisha-scene");
                    }
                    if (inputComponent.HardwareInput.KeyboardInput.F9)
                    {
                        _sceneManager.LoadScene("quicksave.geisha-scene");
                    }

                    var mouseScrollDelta = inputComponent.HardwareInput.MouseInput.ScrollDelta;
                    var scalingFactor    = 0.0001 * mouseScrollDelta;
                    //box.GetComponent<Transform2DComponent>().Scale += new Vector2(scalingFactor, scalingFactor);
                    scalingFactor = mouseScrollDelta == 0 ? 1 : mouseScrollDelta > 0 ? 10d / 11d : 11d / 10d;
                    camera.GetComponent <CameraComponent>().ViewRectangle *= scalingFactor;
                }
            }
        }
コード例 #5
0
        public object LoadAsset(AssetInfo assetInfo, IAssetStore assetStore)
        {
            var fileStream         = _fileSystem.GetFile(assetInfo.AssetFilePath).OpenRead();
            var assetData          = AssetData.Load(fileStream);
            var spriteAssetContent = assetData.ReadJsonContent <SpriteAssetContent>();

            var textureAssetId = new AssetId(spriteAssetContent.TextureAssetId);

            return(new Sprite(
                       sourceTexture: assetStore.GetAsset <ITexture>(textureAssetId),
                       sourceUV: SerializableVector2.ToVector2(spriteAssetContent.SourceUV),
                       sourceDimensions: SerializableVector2.ToVector2(spriteAssetContent.SourceDimensions),
                       pivot: SerializableVector2.ToVector2(spriteAssetContent.Pivot),
                       pixelsPerUnit: spriteAssetContent.PixelsPerUnit));
        }
コード例 #6
0
            public Entity CreateSprite(Scene scene, AssetId spriteAssetId, Vector2?translation = null, double rotation = 0, Vector2?scale = null)
            {
                var entity = new Entity();

                entity.AddComponent(new Transform2DComponent
                {
                    Translation = translation ?? Vector2.Zero,
                    Rotation    = rotation,
                    Scale       = scale ?? Vector2.One
                });
                entity.AddComponent(new SpriteRendererComponent
                {
                    Sprite = _assetStore.GetAsset <Sprite>(spriteAssetId)
                });
                scene.AddEntity(entity);

                return(entity);
            }
コード例 #7
0
        public object LoadAsset(AssetInfo assetInfo, IAssetStore assetStore)
        {
            using var fileStream = _fileSystem.GetFile(assetInfo.AssetFilePath).OpenRead();
            var assetData = AssetData.Load(fileStream);
            var spriteAnimationAssetContent = assetData.ReadJsonContent <SpriteAnimationAssetContent>();

            if (spriteAnimationAssetContent.Frames == null)
            {
                throw new InvalidOperationException($"{nameof(SpriteAnimationAssetContent)}.{nameof(SpriteAnimationAssetContent.Frames)} cannot be null.");
            }

            var frames = spriteAnimationAssetContent.Frames.Select(f =>
            {
                var sprite = assetStore.GetAsset <Sprite>(new AssetId(f.SpriteAssetId));
                return(new SpriteAnimationFrame(sprite, f.Duration));
            }).ToArray();

            return(new SpriteAnimation(frames, TimeSpan.FromTicks(spriteAnimationAssetContent.DurationTicks)));
        }
コード例 #8
0
            private void CreateBox()
            {
                var box = new Entity();

                box.AddComponent(new Transform2DComponent
                {
                    Translation = new Vector2(300, -600),
                    Rotation    = 0,
                    Scale       = new Vector2(0.5, 0.5)
                });
                box.AddComponent(new SpriteRendererComponent
                {
                    Sprite           = _assetStore.GetAsset <Sprite>(AssetsIds.BoxSprite),
                    SortingLayerName = "Box"
                });
                box.AddComponent(new InputComponent {
                    InputMapping = _assetStore.GetAsset <InputMapping>(AssetsIds.PlayerInput)
                });
                box.AddComponent(new BoxMovementComponent());
                box.AddComponent(new RectangleColliderComponent {
                    Dimension = new Vector2(512, 512)
                });
                box.AddComponent(new CloseGameOnEscapeKeyComponent());

                var boxLabel = new Entity();

                boxLabel.AddComponent(new Transform2DComponent
                {
                    Translation = new Vector2(-80, 350),
                    Rotation    = 0,
                    Scale       = Vector2.One
                });
                boxLabel.AddComponent(new TextRendererComponent
                {
                    Text             = "I am Box!",
                    SortingLayerName = "Box",
                    Color            = Color.FromArgb(255, 255, 0, 0),
                    FontSize         = FontSize.FromDips(40)
                });
                box.AddChild(boxLabel);

                var boxRect = new Entity();

                boxRect.AddComponent(new Transform2DComponent
                {
                    Translation = new Vector2(-350, 0),
                    Rotation    = 0,
                    Scale       = Vector2.One
                });
                boxRect.AddComponent(new RectangleRendererComponent
                {
                    SortingLayerName = "Box",
                    Color            = Color.FromArgb(255, 255, 0, 0),
                    Dimension        = new Vector2(40, 80),
                    FillInterior     = true
                });
                boxRect.AddComponent(new FollowEllipseComponent
                {
                    Height = 100,
                    Width  = 100,
                    X      = -350,
                    Y      = 0
                });
                box.AddChild(boxRect);

                Scene.AddEntity(box);
            }
コード例 #9
0
        public Entity CreateBackgroundDay()
        {
            var entity = new Entity();

            entity.AddComponent(new Transform2DComponent
            {
                Translation = Vector2.Zero,
                Rotation    = 0,
                Scale       = new Vector2(2, 2)
            });
            entity.AddComponent(new SpriteRendererComponent
            {
                Sprite           = _assetStore.GetAsset <Sprite>(new AssetId(new Guid("01497f1f-7d61-46fa-b2a2-57c152eb88f7"))),
                SortingLayerName = "Background"
            });
            return(entity);
        }