public override void UpdateNPC() { updateMovement(); if (UIMahager.Over == null && UIMahager.Drag == null) { //кординаты курсора мыши относительно окна var mousePos = Mouse.GetPosition(Program.Window); //получаем плитку над которой находится курсор var tile = world.GetTileByWorldPos(mousePos); if (tile != null) { FloatRect tileRect = tile.GetFloatRect(); DebugRender.AddRectangle(tileRect, Color.Green); if (Mouse.IsButtonPressed(Mouse.Button.Left)) { int i = (int)(mousePos.X / Tile.TILE_SIZE); int j = (int)(mousePos.Y / Tile.TILE_SIZE); world.SetTile(TileType.NONE, i, j); } } if (Mouse.IsButtonPressed(Mouse.Button.Right)) { int i = (int)(mousePos.X / Tile.TILE_SIZE); int j = (int)(mousePos.Y / Tile.TILE_SIZE); world.SetTile(TileType.GROUND, i, j); } } }
//проверяет столкновение персонажа со стеной bool updateWallCollision(int i, int j, int iOffset, ref Vector2f stepPos, FloatRect stepRect) { var dirType = iOffset > 0 ? DirectionType.Right : DirectionType.Left; Tile[] walls = new Tile[] { world.GetTile(i + iOffset, j - 1), world.GetTile(i + iOffset, j - 2), world.GetTile(i + iOffset, j - 3), }; bool isWallCollided = false; foreach (Tile t in walls) { if (t == null) { continue; } FloatRect tileRect = new FloatRect(t.Position, new Vector2f(Tile.TILE_SIZE, Tile.TILE_SIZE)); DebugRender.AddRectangle(tileRect, Color.Yellow); if (updateCollision(stepRect, tileRect, dirType, ref stepPos)) { isWallCollided = true; } } return(isWallCollided); }
private void updatePhysics() { //Ускорение velocity.X *= 0.99f; if (!isGhost) { //Гравитация velocity.Y += 0.55f; var offset = velocity + movement; float dist = MathHelper.GetDistance(offset); int countStep = 1; float stepSize = (float)Tile.TILE_SIZE / 2; if (dist > stepSize) { countStep = (int)(dist / stepSize); } Vector2f nextPos = Position + offset; Vector2f stepPos = Position - rect.Origin; FloatRect stepRect = new FloatRect(nextPos, rect.Size); Vector2f stepVec = (nextPos - Position) / countStep; for (int step = 0; step < countStep; step++) { bool isBreakStep = false; stepPos += stepVec; stepRect = new FloatRect(stepPos, rect.Size); //DebugRender.AddRectangle(stepRect, Color.Blue); int i = (int)((stepPos.X + rect.Size.X / 2) / Tile.TILE_SIZE); int j = (int)((stepPos.Y + rect.Size.Y) / Tile.TILE_SIZE); Tile tile = world.GetTile(i, j); if (tile != null) { FloatRect tileRect = new FloatRect(tile.Position, new Vector2f(Tile.TILE_SIZE, Tile.TILE_SIZE)); DebugRender.AddRectangle(tileRect, Color.Red); if (updateCollision(stepRect, tileRect, DirectionType.Down, ref stepPos)) { velocity.Y = 0; isFly = false; isBreakStep = true; } else { isFly = true; } } else { isFly = true; } if (updateWallCollision(i, j, (1), ref stepPos, stepRect) || updateWallCollision(i, j, -1, ref stepPos, stepRect)) { OnWallColided(); isBreakStep = true; } if (isBreakStep) { break; } } Position = stepPos + rect.Origin; } else { Position += velocity + movement; } }