Exemplo n.º 1
0
        public bool IsOnGround(IMoving entity)
        {
            // Get game tile map
            Level level = game.Map;

            // Make it easier to access entity variables
            Vector2 position = entity.Position;
            int     hitbox_x = entity.Bounds.X;
            int     hitbox_y = entity.Bounds.Y;
            int     hitbox_w = entity.Bounds.Width;
            int     hitbox_h = entity.Bounds.Height;

            // Calculate the tile area currently covered by the entity
            int covered_xmin = (int)(position.X + hitbox_x);
            int covered_xmax = (int)Math.Ceiling(position.X + hitbox_x + hitbox_w) - 1;
            int covered_ymax = (int)Math.Ceiling(position.Y + hitbox_y + hitbox_h) - 1;
            int ground_y     = covered_ymax + 1;

            // Entity is not at the exact top of a tile space
            if (covered_ymax != (position.Y + hitbox_y + hitbox_h - 1))
            {
                return(false);
            }

            // Check if a block exists in the tile directly underneath the entity
            return(level.ContainsBlock(covered_xmin, covered_xmax, ground_y, ground_y));
        }
Exemplo n.º 2
0
        public void MoveEntity(IMoving entity, float x, float y)
        {
            var position = entity.Position;

            position.X     += x; position.Y += y;
            entity.Position = position;
        }
Exemplo n.º 3
0
        public new Vector2 Calculate(IMoving entity, float delta)
        {
            Vector2 vector = base.Calculate(entity, delta);

            vector.X *= -entity.Velocity.X;
            vector.Y *= -entity.Velocity.Y;
            return(vector);
        }
Exemplo n.º 4
0
 private void HandleEntityCollisions(IMoving entity, Side side, float xMin, float xMax, float yMin, float yMax)
 {
     foreach (IItem item in game.WorldLoader.Items)
     {
         HandleEntityCollision(entity, item, side, xMin, xMax, yMin, yMax);
     }
     foreach (IEnemy enemy in game.WorldLoader.Enemies)
     {
         HandleEntityCollision(entity, enemy, side, xMin, xMax, yMin, yMax);
     }
 }
    // Use this for initialization
    void Start()
    {
        _moving = GetComponent<Flying>();
        

        if (TargetTransfom == null)
        {
            _levelController = FindObjectOfType<LevelController>();
            TargetTransfom = _levelController.Player.transform;
        }

    }
Exemplo n.º 6
0
        private static void HandleEntityCollision(IMoving entity, IEntity target, Side side, float xMin, float xMax, float yMin, float yMax)
        {
            if (entity == target)
            {
                return;
            }
            float xMinT = target.Position.X;
            float yMinT = target.Position.Y;

            if (BoundsOverlap(xMin, xMax, yMin, yMax, xMinT, xMinT + target.Bounds.Width, yMinT, yMinT + target.Bounds.Height))
            {
                entity.Collision.Handle(entity, target, side);
            }
        }
Exemplo n.º 7
0
        public void MoveEntity(IMoving entity, float x, float y)
        {
            var position = entity.Position;

            position.X     += x; position.Y += y;
            entity.Position = position;
            if (position.Y > game.Map.Height)
            {
                if (entity is IPlayer)
                {
                    game.Reset();
                }
            }
        }
Exemplo n.º 8
0
        public SinusoidalMovement(IMoving baseMovement, AxesDirections axis)
        {
            this.baseMovement = baseMovement;

            switch (axis)
            {
            case AxesDirections.north: this.axis = Vector3.right; break;

            case AxesDirections.south: this.axis = Vector3.left; break;

            case AxesDirections.east: this.axis = Vector3.up; break;

            case AxesDirections.west: this.axis = Vector3.down; break;
            }
        }
Exemplo n.º 9
0
        private void HandleBlockCollision(IMoving entity, Side side, int xMin, int xMax, int yMin, int yMax)
        {
            Level level = game.Map;

            for (int x = xMin; x <= xMax; x++)
            {
                for (int y = yMin; y <= yMax; y++)
                {
                    IBlock block = level.GetBlock(x, y);
                    if (block != null)
                    {
                        // Handle collision between entity and block on given side
                        entity.Collision.Handle(entity, block, side);
                    }
                }
            }
        }
Exemplo n.º 10
0
        private void spawn()
        {
            var instance_count_to_spawn = instances_per_spawn;

            if (instance_count_to_spawn > count_left_to_spawn)
            {
                instance_count_to_spawn = count_left_to_spawn;
            }

            count_left_to_spawn -= instance_count_to_spawn;

            while (instance_count_to_spawn > 0)
            {
                var random_wall = (AxesDirections)(Random.Range(0, System.Enum.GetValues(typeof(AxesDirections)).Length));
                var orientation = -border_from_screen_center(random_wall);

                GameObject enemyObject = spawn_enemy(random_prefab(), random_position(random_wall), Quaternion.LookRotation(orientation));

                IMoving enemyMovement = enemyObject.GetComponent <Enemy>().Movement;
                if (enemyMovement == null)
                {
                    enemyMovement = new LinearMovement();
                }

                if (instance_count_to_spawn > (instances_per_spawn / 2))
                {
                    enemyMovement = new SinusoidalMovement(enemyMovement, random_wall);
                }
                else
                {
                    enemyMovement = new CircularMovement(enemyMovement, 0.35f);
                }

                enemyObject.GetComponent <Enemy>().Movement = enemyMovement;

                --instance_count_to_spawn;
            }
        }
Exemplo n.º 11
0
    void Awake()
    {
        m_inputGroups = new Dictionary <string, InputGroup>();

        m_movingController = GetComponent <IMoving>();//bind moving controller
        if (m_movingController == null)
        {
            Debug.LogError("Person needs IMoving Component");
        }
        else
        {
            InitializeMovingInputs();
        }

        m_interactor = GetComponent <AccessViaRayCast>();

        InitializeMenuInputs();
        InitializeInteractInputs();
        InitializeToolsActionsInputs();
        InitializePlacementInputs();

        GoToGame();
    }
Exemplo n.º 12
0
 public Vector2 Calculate(IMoving entity, float delta)
 {
     vector.X = force.X / delta;
     vector.Y = force.Y / delta;
     return(vector);
 }
Exemplo n.º 13
0
 public Animal(IMoving _moving)
 {
     Moving = _moving;
 }
Exemplo n.º 14
0
 public CircularMovement(IMoving baseMovement, float radius)
 {
     this.baseMovement = baseMovement;
     this.radius       = radius;
 }
Exemplo n.º 15
0
        public void MoveEntity(IMoving entity, float x, float y)
        {
            // Don't waste our time if the entity isn't actually moving
            if (x == 0 && y == 0)
            {
                return;
            }

            // Get game tile map
            Level level = game.Map;

            // Make it easier to access entity bounds
            Vector2 position = entity.Position;
            int     hitbox_x = entity.Bounds.X;
            int     hitbox_y = entity.Bounds.Y;
            int     hitbox_w = entity.Bounds.Width;
            int     hitbox_h = entity.Bounds.Height;

            // Calculate the tile area currently covered by the entity
            int covered_xmin = (int)(position.X + hitbox_x);
            int covered_xmax = (int)Math.Ceiling(position.X + hitbox_x + hitbox_w) - 1;
            int covered_ymin = (int)(position.Y + hitbox_y);
            int covered_ymax = (int)Math.Ceiling(position.Y + hitbox_y + hitbox_h) - 1;

            // Handle x movement
            if (x > 0)
            {
                // Get the farthest tile we want to move to
                int move_x = (int)Math.Ceiling(position.X + hitbox_x + hitbox_w + x) - 1;

                // Step block by block to move as far as possible
                for (int tile_x = covered_xmax; tile_x <= move_x; tile_x++)
                {
                    if (level.ContainsBlock(tile_x, tile_x, covered_ymin, covered_ymax))
                    {
                        // We reached a collision, stop moving and handle collision
                        HandleBlockCollision(entity, Side.LEFT, tile_x, tile_x, covered_ymin, covered_ymax);
                        break;
                    }
                    else
                    {
                        // No block collision, step as far as the next tile
                        float step = MathHelper.Min(x, tile_x + 1 - position.X - hitbox_x - hitbox_w);
                        position.X += step;
                        if (entity is Mario.Mario || entity is MarioStar)
                        {
                            float cameraStep = step * 30f;
                            Game1.Camera.PanCamera(cameraStep, game);
                        }
                        x -= step;

                        // Handle possible entity collisions
                        HandleEntityCollisions(entity, Side.LEFT, position.X, position.X + hitbox_x + hitbox_w, position.Y, position.Y + hitbox_y + hitbox_h);
                    }
                }
            }
            else if (x < 0)
            {
                // Get the farthest tile we want to move to
                int move_x = (int)(entity.Position.X + hitbox_x + x);

                // Step block by block to move as far as possible
                for (int tile_x = covered_xmin; tile_x >= move_x; tile_x--)
                {
                    if (level.ContainsBlock(tile_x, tile_x, covered_ymin, covered_ymax))
                    {
                        // We reached a collision, stop moving and handle collision
                        HandleBlockCollision(entity, Side.RIGHT, tile_x, tile_x, covered_ymin, covered_ymax);
                        break;
                    }
                    else
                    {
                        // No block collision, step as far as the next tile

                        float step = MathHelper.Max(x, tile_x - position.X - hitbox_x);
                        if (entity is Mario.Mario || entity is MarioStar)
                        {
                            float cameraStep = step * 30f;
                            Game1.Camera.PanCamera(cameraStep, game);
                        }
                        position.X += step;
                        x          -= step;

                        // Handle possible entity collisions
                        HandleEntityCollisions(entity, Side.RIGHT, position.X, position.X + hitbox_x + hitbox_w, position.Y, position.Y + hitbox_y + hitbox_h);
                    }
                }
            }

            // Update area covered horizontally as it may have changed
            covered_xmin = (int)(position.X + hitbox_x);
            covered_xmax = (int)Math.Ceiling(position.X + hitbox_x + hitbox_w) - 1;

            // Handle y movement
            if (y > 0)
            {
                // Get the farthest tile we want to move to
                int move_y = (int)Math.Ceiling(position.Y + hitbox_y + hitbox_h + y) - 1;

                // Step block by block to move as far as possible
                for (int tile_y = covered_ymax; tile_y <= move_y; tile_y++)
                {
                    if (level.ContainsBlock(covered_xmin, covered_xmax, tile_y, tile_y))
                    {
                        // We reached a collision, stop moving and handle collision
                        HandleBlockCollision(entity, Side.TOP, covered_xmin, covered_xmax, tile_y, tile_y);
                        break;
                    }
                    else
                    {
                        // No block collision, step as far as the next tile
                        float step = MathHelper.Min(y, tile_y + 1 - position.Y - hitbox_y - hitbox_h);
                        position.Y += step;
                        y          -= step;

                        // Handle possible entity collisions
                        HandleEntityCollisions(entity, Side.TOP, position.X, position.X + hitbox_x + hitbox_w, position.Y, position.Y + hitbox_y + hitbox_h);
                    }
                }
            }
            else if (y < 0)
            {
                // Get the farthest tile we want to move to
                int move_y = (int)(position.Y + hitbox_y + y);

                // Step block by block to move as far as possible
                for (int tile_y = covered_ymin; tile_y >= move_y; tile_y--)
                {
                    if (level.ContainsBlock(covered_xmin, covered_xmax, tile_y, tile_y))
                    {
                        // We reached a collision, stop moving and handle collision
                        HandleBlockCollision(entity, Side.BOTTOM, covered_xmin, covered_xmax, tile_y, tile_y);
                        break;
                    }
                    else
                    {
                        // No block collision, step as far as the next tile
                        float step = MathHelper.Max(y, tile_y - position.Y - hitbox_y);
                        position.Y += step;
                        y          -= step;

                        // Handle possible entity collisions
                        HandleEntityCollisions(entity, Side.BOTTOM, position.X, position.X + hitbox_x + hitbox_w, position.Y, position.Y + hitbox_y + hitbox_h);
                    }
                }
            }

            // Stop entity's velocity in a direction if a collision occured
            if (x != 0 || y != 0)
            {
                entity.Velocity.Scl(new Vector2(x == 0 ? 1 : 0, y == 0 ? 1 : 0));
            }

            // Update the entity's position
            entity.Position = position;
        }
Exemplo n.º 16
0
 public void AddToObserver(IMoving moving)
 {
     Moving += moving.Move;
 }
Exemplo n.º 17
0
 public Vector2 Calculate(IMoving entity, float delta)
 {
     vector.X = entity.Acceleration.X == 0 ? (entity.Velocity.X * force.X * -1) : 0;
     vector.Y = entity.Acceleration.Y == 0 ? (entity.Velocity.Y * force.Y * -1) : 0;
     return(vector);
 }
Exemplo n.º 18
0
 public bool IsOnGround(IMoving entity)
 {
     return(false);
 }
Exemplo n.º 19
0
 public bool IsOnGround(IMoving entity)
 {
     return(true);
 }