Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions Projects/Tanks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,37 @@ static void Render(string @string, bool renderSpace = false)

if (tank.IsShooting)
{
tank.Bullet = new Bullet()
// Calculate the potential bullet position
int bulletX = tank.Direction switch
{
X = tank.Direction switch
{
Direction.Left => tank.X - 3,
Direction.Right => tank.X + 3,
_ => tank.X,
},
Y = tank.Direction switch
{
Direction.Up => tank.Y - 2,
Direction.Down => tank.Y + 2,
_ => tank.Y,
},
Direction = tank.Direction,
Direction.Left => tank.X - 3,
Direction.Right => tank.X + 3,
_ => tank.X,
};
int bulletY = tank.Direction switch
{
Direction.Up => tank.Y - 2,
Direction.Down => tank.Y + 2,
_ => tank.Y,
};

// Create a temporary bullet to check for collision
var tempBullet = new Bullet
{
X = bulletX,
Y = bulletY,
Direction = tank.Direction
};

// Check if the bullet would spawn in a wall
bool initialCollision = BulletCollisionCheck(tempBullet, out _);

// Only create the bullet if there's no initial collision
if (!initialCollision)
{
tank.Bullet = tempBullet;
}

tank.IsShooting = false;
continue;
}
Expand Down