public void Update(Map map) { Random random = new Random(); for (int x = 1; x < map.SizeX; x++) { for (int y = 1; y < map.SizeY; y++) { // If square empty, attempt to place flowers and grass if (map.Blocks[x,y] != null && map.Blocks[x,y - 1] == null && Flora[x, y - 1] == null) { int rand = random.Next(0, 600); if (rand > 0 && rand < 20) { Flora[x, y - 1] = new Flora(Types[1]); } else if (rand == 21) { Flora[x, y - 1] = new Flora(Types[10]); } else if (rand == 22) { Flora[x, y - 1] = new Flora(Types[11]); } else if (rand == 23) { Flora[x, y - 1] = new Flora(Types[12]); } } // If square is empty, attempt to place vines if (map.Blocks[x,y] != null && map.getBlock(x, y + 1) == null) { int rand = random.Next(0, 30); if (rand == 1) { Flora[x, y + 1] = new Flora(Types[30]); } } //if(Flora[x,y] if (Flora[x, y] != null && Flora[x,y].Type.GrowInto != 0) { Flora[x, y].GrowUpto++; if (Flora[x, y].GrowUpto >= Flora[x, y].Type.GrowAt) { Flora[x, y] = new Flora( Types[Flora[x,y].Type.GrowInto] ); } } } } }
/// <summary> /// Solves a collision for the movement object. /// </summary> /// <param name="map">The Map</param> /// <returns>Returns false if the collision was not fully solved</returns> public bool SolveCollision(Map map) { int BlockAtX = Area.Center.X / 24; int BlockAtY = Area.Center.Y / 24; int totalWidth = 0; int totalHeight = 0; int averageWidthW = 0; int averageWidthH = 0; int averageHeightW = 0; int averageHeightH = 0; Vector2 collisionCenterH = Vector2.Zero; Vector2 collisionCenterW = Vector2.Zero; #region Block Collisions for (int x = BlockAtX - 5; x < BlockAtX + 5; x++) { for (int y = BlockAtY - 5; y < BlockAtY + 5; y++) { Block block = map.getBlock(x, y); if (block == null) { continue; } Rectangle blockArea = new Rectangle(x * 24, y * 24, 24, 24); Rectangle intersection = Rectangle.Intersect(Area, blockArea); if (intersection != Rectangle.Empty) // Only handle intersection if it exists { totalHeight += intersection.Height; totalWidth += intersection.Width; if (totalWidth > totalHeight) { if (collisionCenterW == Vector2.Zero) { averageWidthW += intersection.Width; averageHeightW += intersection.Height; collisionCenterW = new Vector2(blockArea.Center.X, blockArea.Center.Y); } else { averageWidthW = (averageWidthW + intersection.Width) / 2; averageHeightW = (averageHeightW + intersection.Height) / 2; collisionCenterW = new Vector2((collisionCenterW.X + blockArea.Center.X) / 2, (collisionCenterW.Y + blockArea.Center.Y) / 2); } } else { if (collisionCenterH == Vector2.Zero) { averageWidthH += intersection.Width; averageHeightH += intersection.Height; collisionCenterH = new Vector2(blockArea.Center.X, blockArea.Center.Y); } else { averageWidthH = (averageWidthH + intersection.Width) / 2; averageHeightH = (averageHeightH + intersection.Height) / 2; collisionCenterH = new Vector2((collisionCenterH.X + blockArea.Center.X) / 2, (collisionCenterH.Y + blockArea.Center.Y) / 2); } } } } } #endregion #region Entity Collisions foreach (Entity entity in map.Entities.Entities) { if (!entity.Solid) { continue; } Rectangle intersection = Rectangle.Intersect(Area, entity.Area); if (intersection != new Rectangle()) { totalHeight += intersection.Height; totalWidth += intersection.Width; if (totalWidth > totalHeight) { if (collisionCenterW == Vector2.Zero) { averageWidthW += intersection.Width; averageHeightW += intersection.Height; collisionCenterW = new Vector2(entity.Area.Center.X, entity.Area.Center.Y); } else { averageWidthW = (averageWidthW + intersection.Width) / 2; averageHeightW = (averageHeightW + intersection.Height) / 2; collisionCenterW = new Vector2((collisionCenterW.X + entity.Area.Center.X) / 2, (collisionCenterW.Y + entity.Area.Center.Y) / 2); } } else { if (collisionCenterH == Vector2.Zero) { averageWidthH += intersection.Width; averageHeightH += intersection.Height; collisionCenterH = new Vector2(entity.Area.Center.X, entity.Area.Center.Y); } else { averageWidthH = (averageWidthH + intersection.Width) / 2; averageHeightH = (averageHeightH + intersection.Height) / 2; collisionCenterH = new Vector2((collisionCenterH.X + entity.Area.Center.X) / 2, (collisionCenterH.Y + entity.Area.Center.Y) / 2); } } } } #endregion if (totalWidth > 0 || totalHeight > 0){ if (totalWidth > totalHeight ) // Horizontal { if (collisionCenterW.Y < Area.Center.Y) // Colliding from the top { Area.Y += averageHeightW; Velocity.Y = 0; } else // Colliding from the bottom { Area.Y -= averageHeightW; JumpPower = MaxJumpPower; Falling = false; if (Velocity.Y > 0) { Velocity.Y = 0; } } } else { if (collisionCenterH.X > Area.Center.X) // Colliding from the right { Area.X -= averageWidthH; if (Velocity.X > 0) { Velocity.X = 0; } } else // Colliding from the left { Area.X += averageWidthH; if (Velocity.X < 0) { Velocity.X = 0; } } } return false; } return true; }
private Point[] PathfindNeighbours(Point loc, Map map) { Point[] points = new Point[4]; if (map.getBlock(loc.X, loc.Y + 1) == null) { points[1] = new Point(loc.X, loc.Y + 1); } else { points[0] = Point.Zero; } if (map.getBlock(loc.X, loc.Y - 1) == null) { points[0] = new Point(loc.X, loc.Y - 1); } else { points[1] = Point.Zero; } if (map.getBlock(loc.X - 1, loc.Y) == null) { points[2] = new Point(loc.X - 1, loc.Y); } else { points[2] = Point.Zero; } if (map.getBlock(loc.X + 1, loc.Y) == null) { points[3] = new Point(loc.X + 1, loc.Y); } else { points[3] = Point.Zero; } return points; }
/// <summary> /// Checks to see if a rectangle has solid footing mover has solid footing /// </summary> /// <param name="area">The area which is being checked</param> /// <param name="map">Checks against all collidables</param> /// <returns>Returns whether the area has solid footing or not</returns> public bool SolidFooting(Rectangle area, Map map) { int BlockAtX = Area.Center.X / 24; int BlockAtY = Area.Center.Y / 24; int totalWidth = 0; int totalHeight = 0; Vector2 collisionCenter = Vector2.Zero; area.Y++; for (int x = BlockAtX - 5; x < BlockAtX + 5; x++) { for (int y = BlockAtY - 5; y < BlockAtY + 5; y++) { Block block = map.getBlock(x, y); if (block == null) { continue; } Rectangle blockArea = new Rectangle(x * 24, y * 24, 24, 24); Rectangle intersection = Rectangle.Intersect(area, blockArea); if (intersection != Rectangle.Empty) { totalHeight += intersection.Height; totalWidth += intersection.Width; if (collisionCenter == Vector2.Zero) { collisionCenter = new Vector2(blockArea.Center.X, blockArea.Center.Y); } else { collisionCenter = new Vector2((collisionCenter.X + blockArea.Center.X) / 2, (collisionCenter.Y + blockArea.Center.Y) / 2); } } } } if (totalWidth > 0 || totalHeight > 0) { if (totalWidth > totalHeight) // Horizontal { if (collisionCenter.Y > area.Center.Y) // Colliding from the bottom { return true; } } } return false; }