/// <summary> /// gets all the non-empty tiles that intersect the passed in bounds for the collision layer. The returned List can be put back in the /// pool via ListPool.free. /// </summary> /// <returns>The tiles intersecting bounds.</returns> /// <param name="bounds">Bounds.</param> public List <TiledTile> getTilesIntersectingBounds(Rectangle bounds) { Insist.isNotNull(collisionLayer, "collisionLayer must not be null!"); // offset the passed in world position to compensate for the entity position bounds.Location -= (entity.transform.position + _localOffset).ToPoint(); return(collisionLayer.getTilesIntersectingBounds(bounds)); }
/// <summary> /// gets all the non-empty tiles that intersect the passed in bounds /// </summary> /// <returns>The tiles intersecting bounds.</returns> /// <param name="bounds">Bounds.</param> public List <TiledTile> getTilesIntersectingBounds(Rectangle bounds) { // offset the passed in world position to compensate for the entity position bounds.Location -= localPosition.ToPoint(); return(_collisionLayer.getTilesIntersectingBounds(bounds)); }
void IUpdatable.update() { if (Input.isKeyDown(Keys.Left) && !Input.isKeyDown(Keys.Right)) { velocity.X = -(float)Constants.PLAYER_SPEED; } if (Input.isKeyDown(Keys.Right) && !Input.isKeyDown(Keys.Left)) { velocity.X = (float)Constants.PLAYER_SPEED; } if (!Input.isKeyDown(Keys.Left) && !Input.isKeyDown(Keys.Right)) { velocity.X = 0; } if (Input.isKeyDown(Keys.Up) && (grounded || (DateTime.Now - coyoteTime).TotalMilliseconds < Constants.PLAYER_COYOTE_TIME)) { velocity.Y = -(float)Math.Sqrt(2 * Constants.GRAVITY * Constants.PLAYER_JUMP_HEIGHT); } grounded = false; velocity.Y += (float)Constants.GRAVITY * Time.deltaTime; Console.WriteLine(coyoteTime + "\n" + DateTime.Now + "\n"); // water collision TiledTileLayer waterLayer = (TiledTileLayer)entity.scene.entities.findEntity("map_tiles") .getComponent <TiledMapComponent>() .tiledMap.getLayer((int)Constants.Layer.Water); List <TiledTile> waterTiles = waterLayer.getTilesIntersectingBounds(new Rectangle((int)entity.position.X - Constants.BUFFER_ZONE, (int)entity.position.Y - Constants.BUFFER_ZONE, Constants.PLAYER_WIDTH, Constants.PLAYER_HEIGHT)); for (int i = 0; i < waterTiles.Count; i++) { TiledTile tile = waterTiles[i]; // Console.WriteLine(underWater + " " + i + " " + waterTiles[i].id); // Console.WriteLine(waterTiles.Count); if (tile.id == (int)Constants.Id.Water) { velocity.Y -= Constants.BOUYANT_FORCE * Time.deltaTime; if (underWater == false) { if (Math.Abs(velocity.Y) < Constants.DAMPENING_FORCE_UPON_ENTRY) { velocity.Y = 0; } else { velocity.Y -= Constants.DAMPENING_FORCE_UPON_ENTRY; } underWater = true; } break; } } // If none of the tiles were water if (waterTiles.Count == 0) { underWater = false; } var motion = velocity * Time.deltaTime; // wall collision collisions.Clear(); if (collider.collidesWithAnyMultiple(motion, collisions)) { for (int i = 0; i < collisions.Count; i++) { motion -= collisions[i].minimumTranslationVector; // Console.WriteLine(collisions[i].minimumTranslationVector); // Console.WriteLine(entity.position); if (collisions[i].normal.Y == 1) { velocity.Y = 0; } if (collisions[i].normal.Y == -1) { velocity.Y = 0; Console.WriteLine("Hit floor!"); grounded = true; coyoteTime = DateTime.Now; } } } entity.position += motion; }