Esempio n. 1
0
        private void CustomizeTiles(TiledMapComponent mapComponent)
        {
            var tileSize = new Vector2(mapComponent.tiledMap.tileWidth, mapComponent.tiledMap.tileHeight);

            for (float x = 0; x < mapComponent.width; x += tileSize.X)
            {
                for (float y = 0; y < mapComponent.height; y += tileSize.X)
                {
                    var tilePos = new Vector2(x, y);
                    var tile    = mapComponent.getTileAtWorldPosition(tilePos);
                    CustomizeTile(tile, tilePos, tileSize);
                }
            }
        }
        protected void checkForCollision(CollisionResult res)
        {
            //Find tile collision detects
            TiledTile tile = _mapComponent.getTileAtWorldPosition(entity.position + new Vector2(-16) * res.normal);

            if (tile != null && tile.tilesetTile != null && tile.tilesetTile.isDestructable)   //Destroy tile if isDestructable
            {
                _mapComponent.collisionLayer.removeTile(tile.x, tile.y);
                _mapComponent.removeColliders();
                _mapComponent.addColliders();
            }
            else
            {
                entity.destroy();
            }
        }
Esempio n. 3
0
        void IUpdatable.update()
        {
            entity.scene.camera.position = new Vector2(entity.scene.sceneRenderTargetSize.X / 2, entity.scene.sceneRenderTargetSize.Y / 2);
            if (_isGrounded)
            {
                if (Input.isKeyPressed(Keys.Left))
                {
                    _moveDir.X = -1f;
                    if (!canMove())
                    {
                        _moveDir.X = 0;
                        return;
                    }
                    entity.getComponent <RenderableComponent>().flipY = false;
                    entity.transform.rotationDegrees = 90f;
                }
                else if (Input.isKeyPressed(Keys.Right))
                {
                    _moveDir.X = 1f;
                    if (!canMove())
                    {
                        _moveDir.X = 0;
                        return;
                    }
                    entity.getComponent <RenderableComponent>().flipY = false;
                    entity.transform.rotationDegrees = -90f;
                }
                else if (Input.isKeyPressed(Keys.Up))
                {
                    _moveDir.Y = -1f;
                    if (!canMove())
                    {
                        _moveDir.Y = 0;
                        return;
                    }
                    entity.getComponent <RenderableComponent>().flipY = true;
                    entity.transform.rotationDegrees = 0f;
                }
                else if (Input.isKeyPressed(Keys.Down))
                {
                    _moveDir.Y = 1f;
                    if (!canMove())
                    {
                        _moveDir.Y = 0;
                        return;
                    }
                    entity.getComponent <RenderableComponent>().flipY = false;
                    entity.transform.rotationDegrees = 0f;
                }
            }


            if (_moveDir != Vector2.Zero)
            {
                var             movement = _moveDir * _speed * Time.deltaTime;
                CollisionResult res;
                if (_mover.move(movement, out res))
                {
                    var pos  = entity.transform.position + new Vector2(-16) * res.normal;
                    var tile = _tiledMapComponent.getTileAtWorldPosition(pos);

                    if (!_destroyedTile && tile != null && tile.tilesetTile == null)
                    {
                        _destroyedTile = true;
                        _tiledMapComponent.collisionLayer.removeTile(tile.x, tile.y);
                        _tiledMapComponent.removeColliders();
                        _tiledMapComponent.addColliders();
                    }
                    else
                    {
                        _moveDir       = Vector2.Zero;
                        _isGrounded    = true;
                        _destroyedTile = false;
                        entity.getComponent <CameraShake>().shake(8, 0.8f);
                    }
                }
                else
                {
                    _isGrounded = false;
                }
            }
        }