Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ If you run the game right now and move the slime around, you will notice a few i

We can now implement these features using collision detection and response in our game. In the *DungeonSlime* project (your main game project), open the `Game1.cs` file and make the following changes to the `Game1` class:

[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,76–176,181–193,293–294)]
[!code-csharp[](./snippets/game1.cs?highlight=1,5,25-29,40-45,76–177,182–194,294–295)]

The key changes made here are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ protected override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ This tilemap configuration creates a simple dungeon layout with walls around the

With all of the assets now in place and configured, we can update the `Game1` class to load the tilemap and draw it. We will also need to update the collision logic so that the boundary is no longer the edge of the screen, but instead the edges of the wall tiles of the tilemap. Open `Game1.cs` and make the following updates:

[!code-csharp[](./snippets/game1.cs?highlight=31-35,46-61,80-82,109,111,113,125,118,120,122,124,142,145,147,150,153,156,158,161,176–178,300–301)]
[!code-csharp[](./snippets/game1.cs?highlight=31-35,46-61,80-82,109,111,113,125,118,120,122,124,142,145,147,150,153,156,158,161,177–179,301–302)]

The key changes to the `Game1` class include:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ protected override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Add these files to your content project using the MGCB Editor:

Next, open the `Game1.cs` file and update it to the following:

[!code-csharp[](./snippets/game1.cs?highlight=3,6,39-43,92-111,200-201,219-220)]
[!code-csharp[](./snippets/game1.cs?highlight=3,6,39-43,92-111,201-202,220-221)]

The key changes here are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ protected override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);

// Play the bounce sound effect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The key changes made here are:

Next, update the `Game1` class to use the audio controller for audio playback. Open `Game1.cs` and make the following updates:

[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,194–195,213–214,267–285)]
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,104-105,195–196,214–215,268–286)]

> [!NOTE]
> Note there were a lot of replacements in the `LoadContent` method, switching from loading and initializing the background Song and replacing it with a call to the new `AudioController` to do all the work managing the Song reference. Much cleaner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);

// Play the bounce sound effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The key changes here are:

Finally, open the `Game1.cs` file and make the following changes:

[!code-csharp[](./snippets/game1.cs?highlight=48-58,93-99,129-130,240-241,385-396)]
[!code-csharp[](./snippets/game1.cs?highlight=48-58,93-99,129-130,241-242,386-397)]

The key changes made are:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ protected override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);

// Play the bounce sound effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public override void Update(GameTime gameTime)
// normal.
if (normal != Vector2.Zero)
{
normal.Normalize();
_batVelocity = Vector2.Reflect(_batVelocity, normal);

// Play the bounce sound effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public void Bounce(Vector2 normal)
// Apply the new position
Position = newPosition;

// Normalize before reflecting
normal.Normalize();

// Apply reflection based on the normal.
_velocity = Vector2.Reflect(_velocity, normal);

Expand Down