Beispiel #1
0
        public static void Run(
            Ecs.Registry ecsRegistry,
            SpriteBatch spriteBatch,
            Camera camera
            )
        {
            HashSet <Ecs.Entity> entities = ecsRegistry.GetEntities(
                typeof(SpriteComp),
                typeof(PositionComp)
                );

            foreach (Ecs.Entity entity in entities)
            {
                SpriteComp spriteComp = ecsRegistry
                                        .GetComponentUnsafe <SpriteComp>(entity);

                PositionComp positionComp = ecsRegistry
                                            .GetComponentUnsafe <PositionComp>(entity);

                if (camera.IsInRange(positionComp.data))
                {
                    Render(spriteComp, positionComp, spriteBatch, camera);
                }
            }
        }
Beispiel #2
0
        // To optimize drawing performance, check that the sprite is in range
        // for the camera before calling this
        public static void Render(
            SpriteComp spriteComp,
            PositionComp positionComp,
            SpriteBatch spriteBatch,
            Camera camera
            )
        {
            var drawingPos =
                camera.GetDrawingPosition(positionComp.data) -
                spriteComp.destOffset;

            DrawingPosRectangle destPosRectangle = GetCenteredDrawingPosRectangle(
                drawingPos,
                camera.Scale(spriteComp.destRectangle)
                );

            // TODO: Figure out how to use origin with rotation

            spriteBatch.Draw(
                spriteComp.texture,
                destPosRectangle,
                spriteComp.texturePosRectangle,
                spriteComp.tint,
                spriteComp.rotation,
                new Vector2(0, 0),
                spriteComp.effects,
                1.0f
                );
        }
Beispiel #3
0
        private static void Render(
            Tiles.Visible tile,
            int tileIndex, // Tile index in its layer
            Map map,
            ContentManager contentManager,
            SpriteBatch spriteBatch,
            Camera camera
            )
        {
            if (tile.gid == 0)
            {
                // Skip empty tiles
                return;
            }

            PhysicalVector2 tilePos  = GetTilePosition(map, tileIndex);
            var             position = new Ecs.Components.Position()
            {
                data = tilePos
            };

            Tileset   tileset             = GetTilesetOfTile(map, tile.gid);
            Texture2D tilesetTexture      = GetTexture(contentManager, tileset);
            var       texturePosRectangle = GetTexturePosRectangle(
                tile.gid,
                tileset,
                map
                );
            var           destRectangle = GetDestRectangle(map);
            SpriteEffects effects       = GetSpriteEffects(tile);

            var sprite = new EcsExt.Components.Visibles.Sprite()
            {
                texture             = tilesetTexture,
                texturePosRectangle = texturePosRectangle,
                destRectangle       = destRectangle,
                tint    = Color.White,
                effects = effects
            };

            EcsExt.Systems.SpriteRender.Render(
                sprite,
                position,
                spriteBatch,
                camera
                );
        }
Beispiel #4
0
        private static void Render(
            GameTile tile,
            int tileIndex, // Tile index in its layer
            Map map,
            ContentManager contentManager,
            SpriteBatch spriteBatch,
            Camera camera
            )
        {
            if (tile.kind == GameTile.Kind.Empty)
            {
                // Skip empty tiles
                return;
            }

            PhysicalVector2 tilePos  = GetTilePosition(map, tileIndex);
            var             position = new Ecs.Components.Position()
            {
                data = tilePos
            };

            Tileset   tileset             = map.GameTileset;
            Texture2D tilesetTexture      = GetTexture(contentManager, tileset);
            int       tileFrame           = tile.GetTileFrame;
            var       texturePosRectangle = GetTexturePosRectangleWithTileFrame(
                tileFrame,
                tileset,
                map
                );
            var destRectangle = GetDestRectangle(map);

            var sprite = new EcsExt.Components.Visibles.Sprite()
            {
                texture             = tilesetTexture,
                texturePosRectangle = texturePosRectangle,
                destRectangle       = destRectangle,
                tint = Color.White
            };

            EcsExt.Systems.SpriteRender.Render(
                sprite,
                position,
                spriteBatch,
                camera
                );
        }
Beispiel #5
0
        public static void Run(Registry registry)
        {
            HashSet <Entity> entities = registry.GetEntities(
                typeof(PositionComp),
                typeof(VelocityComp)
                );

            foreach (Entity entity in entities)
            {
                VelocityComp velocityComp = registry
                                            .GetComponentUnsafe <VelocityComp>(entity);

                PositionComp positionComp = registry
                                            .GetComponentUnsafe <PositionComp>(entity);

                // Decay velocity to simulate friction
                velocityComp.data *= velocityComp.decay;
                velocityComp.data  = RoundIfZero(velocityComp.data);

                positionComp.data += velocityComp.data;
            }
        }