Beispiel #1
0
        /// <summary>
        /// Detects and resolves all collisions between the player and his neighboring
        /// tiles. When a collision is detected, the player is pushed away along one
        /// axis to prevent overlapping. There is some special logic for the Y axis to
        /// handle platforms which behave differently depending on direction of movement.
        /// </summary>
        protected void HandleCollisions(CollisionDirection direction, GameTime gameTime)
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = Bounds;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            //Reset flag to search for ground collision.
            IsOnGround = false;


            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    Rectangle      tileBounds = ((Map)Map).GetTileBounds(x, y);
                    BlockCollision collision  = ((Map)Map).GetCollision(x, y);
                    Vector2        depth;
                    bool           intersects = TileIntersectsPlayer(Bounds, tileBounds, direction, out depth);
                    HandleCollisions(gameTime, direction, collision, tileBounds, depth, intersects, x, y);
                }
            }
            // Save the new bounds bottom.
            PreviousState.Bounds = bounds;
        }
Beispiel #2
0
    IEnumerator NextBlock(BlockCollision _nextBlock)
    {
        yield return(new WaitForSeconds(_removeAfterCollision));

        EventDestroyBlock?.Invoke(_nextBlock, this);
        _nextBlock.CollisionDetect = true;
    }
Beispiel #3
0
    private void OnNextBlock(BlockCollision block)
    {
        GameManager.Instance.ScoreManager.ModifyScore(+1, block.transform.localScale.x * block.transform.localScale.z);

        if (GameManager.Instance.ScoreManager.Score == GameManager.Instance.ScoreManager.LimitBlocksInRound)
        {
            _fxWin.Play();
            _audioWin.Play();

            if (GameManager.Instance.ScoreManager.Stage == GameManager.Instance.ScoreManager.MaxStages)
            {
                State = StateGame.WIN_STATE;
                GameManager.Instance.ScoreManager.LimitBlocksInRound++;
            }

            OnExitRound();
            GameManager.Instance.ScoreManager.Stage += 1;
            return;
        }

        //if (GameManager.Instance.ScoreManager.Score != 0 && GameManager.Instance.ScoreManager.Score % 2 == 0)
        //    GameManager.Instance.FogColor.FogColor = GameManager.Instance.Gradient.RandomColor();

        _baseMovement.Target =
            (GameManager.Instance.Base.transform.position + Vector3.down * block.transform.localScale.y);

        _block.Collision.EventNextBlock -= OnNextBlock;
        _block.Movement.EventExit       -= OnExitRound;

        SpawnBlock(block.transform);
    }
Beispiel #4
0
        private void on_block_stay(BlockCollision collision)
        {
            if (planetaria_rigidbody.colliding) // FIXME: GitHub issue #67
            {
                if (collision.magnetism != 0)
                {
                    magnet_floor = true;
                }
                float velocity = planetaria_rigidbody.relative_velocity.x;

                velocity += horizontal * -planetaria_rigidbody.relative_velocity.y * transform.scale * acceleration * 20f; // Time.deltaTime omitted intentionally (included in relative_velocity.y)

                if (Mathf.Abs(velocity) > 3f * transform.scale)
                {
                    velocity = Mathf.Sign(velocity) * 3f * transform.scale;
                }
                planetaria_rigidbody.relative_velocity = new Vector2(velocity, 0);
                transform.direction = collision.geometry_visitor.normal();
                if (Time.time - last_jump_attempt < .2f)
                {
                    planetaria_rigidbody.derail(0, 3 * transform.scale);
                }
            }
            else
            {
                Debug.LogError("And yet this is happening?");
                planetaria_rigidbody.derail(0, 1);
            }
        }
Beispiel #5
0
 public PiranhaEntity(Vector2 loc) : base(loc)
 {
     _initalPos      = loc;
     EntityCollision = new BlockCollision(this);//not collidable
     Sprite          = new Piranha(loc);
     State           = new HiddenState(this);
 }
Beispiel #6
0
 public Pipe(string part, Game1 game)
 {
     Part      = part;
     this.game = game;
     if (part.Equals("UGTop") || part.Equals("Top"))
     {
         Collision  = new BlockCollision();
         pipeSprite = WorldElementSpriteFactory.Instance.CreatePipeTopSprite();
     }
     else if (part.Equals("Body"))
     {
         Collision  = new BlockCollision();
         pipeSprite = WorldElementSpriteFactory.Instance.CreatePipeTopBodySprite();
     }
     else if (part.Equals("SWEntrance"))
     {
         Bounds     = new Rectangle(0, 0, 1, 2);
         Collision  = new BlockCollision();
         pipeSprite = WorldElementSpriteFactory.Instance.CreateSWPipeEntranceSprite();
     }
     else if (part.Equals("SWBody"))
     {
         Collision  = new BlockCollision();
         pipeSprite = WorldElementSpriteFactory.Instance.CreateSWPipeBodySprite();
     }
 }
Beispiel #7
0
        /// <summary>
        /// Detects and resolves all collisions between the player and his neighboring
        /// tiles. When a collision is detected, the player is pushed away along one
        /// axis to prevent overlapping. There is some special logic for the Y axis to
        /// handle platforms which behave differently depending on direction of movement.
        /// </summary>
        private void HandleCollisions(CollisionDirection direction)
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = BoundingRectangle;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            // Reset flag to search for ground collision.
            isOnGround = false;
            // For each potentially colliding tile,
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    Rectangle tileBounds = level.GetBounds(x, y);
                    // If this tile is collidable,
                    BlockCollision collision = level.GetCollision(x, y);
                    Vector2        depth;
                    if (collision != BlockCollision.Passable && PlayerCharacter.TileIntersectsPlayer(BoundingRectangle, tileBounds, direction, out depth))
                    {
                        if (collision == BlockCollision.Impassable || isOnGround || collision == BlockCollision.Platform || collision == BlockCollision.Falling)
                        {
                            if (direction == CollisionDirection.Horizontal)
                            {
                                position.X += depth.X;
                            }
                            else
                            {
                                if (Type == ParticleType.Rain)
                                {
                                    level.DefaultParticleEngine.SpawnParticle(ParticleType.RainSplash, (int)Position.X, (int)Position.Y + bounds.Height - 3);
                                    //Remove particle
                                    TTL = 0;
                                    //Make rain put out fire
                                    if (level.InLevelBounds(Position) && (level.tiles[(int)(Position.X / Tile.Width), (int)(Position.Y / Tile.Width)].ForegroundFireMeta > 0 || level.tiles[(int)(Position.X / Tile.Width), (int)(Position.Y / Tile.Width)].BackgroundFireMeta > 0))
                                    {
                                        level.tiles[(int)(Position.X / Tile.Width), (int)(Position.Y / Tile.Width)].ForegroundFireMeta = level.tiles[(int)(Position.X / Tile.Width), (int)(Position.Y / Tile.Width)].BackgroundFireMeta = 0;
                                    }
                                }
                                else if (Type == ParticleType.Snow)
                                {
                                    if (!AlphaFade)
                                    {
                                        TTL       = TTLoriginal = 20;
                                        AlphaFade = true;
                                    }
                                }
                                isOnGround  = true;
                                position.Y += depth.Y;
                            }
                        }
                    }
                }
            }
            // Save the new bounds bottom.
            previousBounds = bounds;
        }
Beispiel #8
0
        public EnemyPipeEntity(Vector2 loc, List <Tile> tiles, List <Entity> movingEntities)
        {
            Sprite          = new EnemyPipeBlock(loc);
            EntityCollision = new BlockCollision(this);

            CollideList    = tiles;
            MovingEntities = movingEntities;
        }
Beispiel #9
0
 //Constructor
 public Block(PyramidPanic game, string blockName,
              Vector2 position, BlockCollision blockCollision, Char charItem)
 {
     this.game      = game;
     this.texture   = game.Content.Load <Texture2D>(@"PlaySceneAssets\Blocks\" + blockName);
     this.rectangle = new Rectangle((int)position.X, (int)position.Y, this.texture.Width, this.texture.Height);
     this.position  = position;
     this.charItem  = charItem;
 }
Beispiel #10
0
 //Constuctor
 public Block(PyramidPanic game, string blockName,
         Vector2 position, BlockCollision blockCollision, Char charItem)
 {
     this.game = game;
     this.texture = game.Content.Load<Texture2D>(@"PlaySceneAssets\Block\" + blockName);
     this.rectangle = new Rectangle((int)position.X, (int)position.Y, this.texture.Width, this.texture.Height);
     this.position = position;
     this.charItem = charItem;
 }
Beispiel #11
0
 public UsedBlockEntity(Vector2 loc, EventSoundEffects sounds) : base(loc, sounds)//there are no sounds for used blocks
 {
     Factory = new BlockFactory();
     BState  = new UsedBlockState(this);
     Sprite  = Factory.BuildSprite(Factory.FindType(BState), loc);
     BState.Enter(null);
     SpritePosition  = loc;
     InitialPos      = loc;
     EntityCollision = new BlockCollision(this);
 }
Beispiel #12
0
 //Constructor
 public MovingBlock(PyramidPanic game, string blockName, Vector2 location,
                 BlockCollision blockCollision, char charItem )
 {
     this.game = game;
     this.texture = this.game.Content.Load<Texture2D>(@"PlaySceneAssets\Blocks\" + blockName);
     this.collisionTexture = this.game.Content.Load<Texture2D>(@"PlaySceneAssets\Explorer\CollisionText");
     this.startLocation = location;
     this.Location = location;
     this.state = new MovingBlockIdle(this);
 }
Beispiel #13
0
 //Constructor
 public Block(PyramidPanic game, string blockName, Vector2 location,
                 BlockCollision blockCollision, char charItem )
 {
     this.game = game;
     this.blockName = blockName;
     this.texture = this.game.Content.Load<Texture2D>(@"PlaySceneAssets\Blocks\" + blockName);
     this.location = new Vector2(location.X, location.Y);
     this.blockCollision = blockCollision;
     this.charItem = charItem;
     this.rectangle = new Rectangle((int)this.location.X, (int)this.location.Y, this.texture.Width, this.texture.Height);
 }
Beispiel #14
0
 /// <summary>
 /// Creates a new instance of a <c>BlockType</c>
 /// </summary>
 /// <param name="name">Name of the block</param>
 public BlockType(string name, Layer layer, Rectangle source, BlockCollision collision = BlockCollision.Passable, TileType type = TileType.Default)
 {
     Name        = name;
     Source      = source;
     Layer       = layer;
     Collision   = collision;
     Type        = type;
     TotalFrames = 1;
     ID          = (byte)BlockList.Count();
     BlockList.Add(this);
 }
Beispiel #15
0
 public HiddenBlockEntity(Vector2 loc, EventSoundEffects sounds) : base(loc, sounds)
 {
     Sounds  = sounds;
     Factory = new BlockFactory();
     BState  = new HiddenBlockState(this);
     Sprite  = Factory.BuildSprite(Factory.FindType(BState), loc);
     BState.Enter(null);
     SpritePosition  = loc;
     InitialPos      = loc;
     EntityCollision = new BlockCollision(this);
 }
Beispiel #16
0
 /// <summary>
 /// Creates a new instance of a <c>BlockType</c>
 /// </summary>
 /// <param name="name">Name of the block</param>
 public BlockType(string name, Layer layer, Rectangle source, BlockCollision collision = BlockCollision.Passable, TileType type = TileType.Default)
 {
     Name = name;
     Source = source;
     Layer = layer;
     Collision = collision;
     Type = type;
     TotalFrames = 1;
     ID = (byte)BlockList.Count();
     BlockList.Add(this);
 }
        public BlockCollision Collides(Vector2 position, IBlock block)
        {
            var            ballCenter      = new Vector2(position.X + _ballRadius, position.Y + _ballRadius);
            var            collisionPoints = _collisionPoints[block];
            BlockCollision blockCollision  = new BlockCollision()
            {
                Collides = true
            };

            foreach (Vector2 currentCollisionPoints in collisionPoints.LeftCollisionPoints)
            {
                float distance = Vector2.Distance(ballCenter, currentCollisionPoints);
                if (distance <= _ballRadius)
                {
                    blockCollision.Type = BlockCollisionType.Left;
                    return(blockCollision);
                }
            }

            foreach (Vector2 currentCollisionPoints in collisionPoints.RightCollisionPoints)
            {
                float distance = Vector2.Distance(ballCenter, currentCollisionPoints);
                if (distance <= _ballRadius)
                {
                    blockCollision.Type = BlockCollisionType.Right;
                    return(blockCollision);
                }
            }

            foreach (Vector2 currentCollisionPoints in collisionPoints.TopCollisionPoints)
            {
                float distance = Vector2.Distance(ballCenter, currentCollisionPoints);
                if (distance <= _ballRadius)
                {
                    blockCollision.Type = BlockCollisionType.Top;
                    return(blockCollision);
                }
            }

            foreach (Vector2 currentCollisionPoints in collisionPoints.BottomCollisionPoints)
            {
                float distance = Vector2.Distance(ballCenter, currentCollisionPoints);
                if (distance <= _ballRadius)
                {
                    blockCollision.Type = BlockCollisionType.Bottom;
                    return(blockCollision);
                }
            }

            blockCollision.Collides = false;
            return(blockCollision);
        }
Beispiel #18
0
 public PipeBlockEntity(Vector2 loc, EventSoundEffects eventSound) : base(loc, eventSound)
 {
     Sounds  = eventSound;
     Factory = new BlockFactory();
     BState  = new PipeBlockState(this);
     Sprite  = Factory.BuildSprite(Factory.FindType(BState), loc);
     BState.Enter(null);
     SpritePosition  = loc;
     InitialPos      = loc;
     EntityCollision = new BlockCollision(this);
     Warpable        = false;
     WarpVelocity    = new Vector2(0, -1);//normally an outpipe
 }
Beispiel #19
0
    private void OnDestroyBlock(BlockCollision _nextBlock, BlockCollision _oldBlock)
    {
        _oldBlock.EventDestroyBlock -= OnDestroyBlock;
        _oldBlock.EventExitRaund    -= OnExitRaund;

        Destroy(_oldBlock.gameObject);

        _targetBase = _targetBase + (Vector3.down * (_nextBlock.transform.localScale.y));
        Vector3 b = Base.transform.position;

        CreateBlock(_nextBlock.transform);
        EventChangeScore?.Invoke();
    }
Beispiel #20
0
    // Start is called before the first frame update
    private void Start()
    {
        bc             = GetComponent <BlockCollision>();
        bm             = GetComponent <BlockMovement>();
        spriteRenderer = GetComponent <SpriteRenderer>();
        spriteColor    = spriteRenderer.color;

        dissolve      = GetComponent <Dissolve>();
        arrowDissolve = arrowTransform.gameObject.GetComponent <Dissolve>();

        obstacleChildRenderer = spike.GetComponent <SpriteRenderer>();
        obstacleChildCollider = spike.GetComponent <Collider2D>();
        collider = GetComponent <Collider2D>();
    }
Beispiel #21
0
        /// <summary>
        /// Checks if the character is aligned to a block with a certain collision
        /// </summary>
        public virtual bool IsAligned(BlockCollision col)
        {
            int playerOffset = ((int)position.X % Tile.Width) - Tile.Center;

            if (Math.Abs(playerOffset) <= LadderAlignment && level.GetTileOnPlayer(position).Foreground.Collision == col)
            {
                // Align the player with the middle of the tile
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #22
0
        /// <summary>
        /// Method to handle collisions for the player
        /// </summary>
        private void HandleCollision()
        {
            Rectangle bounds      = BoundingRectangle;
            int       leftBlock   = (int)Math.Floor((float)bounds.Left / Block.Width);
            int       rightBlock  = (int)Math.Ceiling(((float)bounds.Right / Block.Width)) - 1;
            int       topBlock    = (int)Math.Floor((float)bounds.Top / Block.Height);
            int       bottomBlock = (int)Math.Ceiling(((float)bounds.Bottom / Block.Height)) - 1;

            onGround = false;

            for (int y = topBlock; y <= bottomBlock; ++y)
            {
                for (int x = leftBlock; x <= rightBlock; ++x)
                {
                    BlockCollision collision = Level.GetCollision(x, y);
                    if (collision != BlockCollision.Passable)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle blockBounds = Level.GetBounds(x, y);
                        Vector2   depth       = RectangleExtensionHandler.GetIntersectionDepth(bounds, blockBounds);
                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);
                            if (absDepthY < absDepthX || collision == BlockCollision.Platform)
                            {
                                if (oldBottom <= blockBounds.Top)
                                {
                                    onGround = true;
                                }

                                if (collision == BlockCollision.Impassable || OnGround)
                                {
                                    position = new Vector2(Position.X, Position.Y + depth.Y);
                                    bounds   = BoundingRectangle;
                                }
                            }
                            else if (collision == BlockCollision.Impassable)
                            {
                                // Resolve the collision along the X axis.
                                position = new Vector2(Position.X + depth.X, Position.Y);
                                bounds   = BoundingRectangle;
                            }
                        }
                    }
                }
            }
            oldBottom = bounds.Bottom;
        }
Beispiel #23
0
 public BridgeBlockEntity(Vector2 loc, EventSoundEffects eventSound) : base(loc, eventSound)
 {
     Items = new Collection <Entity>
     {
         this//first is this one
     };
     Sounds  = eventSound;
     Factory = new BlockFactory();
     BState  = new BridgeBlockState(this);
     Sprite  = Factory.BuildSprite(Factory.FindType(BState), loc);
     BState.Enter(null);
     SpritePosition  = loc;
     InitialPos      = loc;
     EntityCollision = new BlockCollision(this);
 }
Beispiel #24
0
        /// <summary>
        /// Creates a new instance a block type and adds it to the block list.
        /// </summary>
        /// <param name="name">Name of the block</param>
        /// <param name="layer">The layer(s) the tile can be placed on.</param>
        /// <param name="collision">The physics that the tile will interact with entities with.</param>
        /// <param name="category">Block category used for sorting in the inventory.</param>
        public BlockType(string name, Layer layer, BlockCollision collision = BlockCollision.Passable, BlockCategory category = null)
        {
            // TODO: ID will be calulcated by server, and the list will be rearranged after plugins are loaded
            // This way we can use an array or list instead of LINQ/for loop to find an id in `FromID`
            Name         = name;
            Layer        = layer;
            Collision    = collision;
            Category     = category;
            ID           = (ushort)Blocks.Count();
            IsRenderable = true;

            Draw = (batch, tile, x, y) =>
            {
                batch.Draw(Texture, new Vector2(x * Tile.Width, y * Tile.Height));
            };

            Blocks.Add(this);
        }
 void Awake()
 {
     // Debug.Log("LoggableObject Awake "+this.gameObject.name);
     if (string.IsNullOrEmpty(prefabName))
     {
         if (this.gameObject.name.Contains("(Clone)"))
         {
             prefabName = this.gameObject.name.Substring(0, this.gameObject.name.LastIndexOf("(Clone)"));
         }
         else
         {
             prefabName = this.gameObject.name;
         }
     }
     this.collider   = GetComponent <Collider>();
     this.checkpoint = GetComponent <CheckPointTrigger>();
     this.block      = GetComponent <BlockCollision>();
 }
Beispiel #26
0
    private void OnExitRaund(BlockCollision _oldBlock)
    {
        _game = false;
        _oldBlock.EventDestroyBlock -= OnDestroyBlock;
        _oldBlock.EventExitRaund    -= OnExitRaund;
        Destroy(_oldBlock.gameObject);
        Debug.Log("Exit Rount");

        var stack = Base.gameObject.GetComponentsInChildren <BlockCollision>();

        Saving.Instance.Clear();
        foreach (var block in stack)
        {
            Saving.Instance.Append(block.transform, block.BlockColor.Color);
        }

        Saving.Instance.Append(_backGround.FromColor, _backGround.ToColor);

        Saving.Instance.Write();
        EventChangeRecord?.Invoke();
    }
Beispiel #27
0
 public Block(Texture2D t, BlockCollision collision)
 {
     texture   = t;
     Collision = collision;
 }
Beispiel #28
0
        /// <summary>
        /// Handles collisions for a given block
        /// </summary>
        private bool HandleCollisions(GameTime gameTime, CollisionDirection direction, BlockCollision collision, Rectangle tileBounds, Vector2 depth, bool intersects, int x, int y)
        {
            if (collision != BlockCollision.Passable && intersects)
            {
                // If we crossed the top of a tile, we are on the ground.
                if (PreviousState.Bounds.Bottom <= tileBounds.Top)
                {
                    if (collision == BlockCollision.Platform)
                    {
                        IsOnGround = true;
                    }
                }
                if (collision == BlockCollision.Gravity)
                {
                    Tile tile = Map.Tiles[x, y, 1];
                    if (tile.Block == BlockType.UpArrow)
                        GravityDirection = GravityDirection.Up;
                    else if (tile.Block == BlockType.DownArrow)
                        GravityDirection = GravityDirection.Down;
                    else if (tile.Block == BlockType.RightArrow)
                        GravityDirection = GravityDirection.Right;
                    else if (tile.Block == BlockType.LeftArrow)
                        GravityDirection = GravityDirection.Left;
                    return true;
                }
                if (collision == BlockCollision.Impassable || IsOnGround)
                {
                    //Now that we know we hit something, resolve the collison
                    if (direction == CollisionDirection.Horizontal)
                    {
                        SimulationState.Position.X += depth.X;
                        IsOnGround = true;
                    }
                    if (direction == CollisionDirection.Vertical)
                    {
                        //Cancel jump if hit something (Ie, when you jump and hit the roof)
                        IsJumping = false;
                        JumpTime = 0;
                        //Obviously hit ground or roof
                        IsOnGround = true;

                        SimulationState.Position.Y += depth.Y;
                    }
                }
            }
            return false;
        }
Beispiel #29
0
 public static bool CanFlowThrough(this BlockCollision collision)
 {
     return(collision != BlockCollision.Impassable && collision != BlockCollision.Falling);
 }
Beispiel #30
0
 private Block LoadMapBlock(string spriteName, BlockCollision collision)
 {
     return(LoadBlock(spriteName, collision));
 }
Beispiel #31
0
 private Block LoadBlock(string spriteName, BlockCollision collision)
 {
     return(new Block(Content.Load <Texture2D>(spriteName), collision));
 }
Beispiel #32
0
 public FloorBlockEntity(Vector2 loc)
 {
     Sprite          = new FloorBlock(loc);
     EntityCollision = new BlockCollision(this);
 }
Beispiel #33
0
 private void on_block_enter(BlockCollision collision)
 {
 }
Beispiel #34
0
 private void on_block_exit(BlockCollision collision)
 {
     last_jump_attempt   = -1;
     transform.direction = Vector3.up;
     magnet_floor        = false;
 }
Beispiel #35
0
 /// <summary>
 /// Constructs a new tile.
 /// </summary>
 public Block(Texture2D texture, BlockCollision collision)
 {
     Texture   = texture;
     Collision = collision;
 }
Beispiel #36
0
 public Block(Texture2D block, Vector2 where, BlockCollision collision)
 {
     this.blockTexture = block;
        this.position = where;
        blkCollision = collision;
 }